Note
Go to the end to download the full example code
Analytical test case # 3¶
In this example, we consider a simple optimization problem to illustrate algorithms interfaces and DOE libraries integration. Integer variables are used
Imports¶
from __future__ import annotations
from gemseo import configure_logger
from gemseo import execute_post
from gemseo.algos.design_space import DesignSpace
from gemseo.algos.doe.doe_factory import DOEFactory
from gemseo.algos.opt_problem import OptimizationProblem
from gemseo.core.mdofunctions.mdo_function import MDOFunction
from numpy import sum as np_sum
LOGGER = configure_logger()
Define the objective function¶
We define the objective function \(f(x)=\sum_{i=1}^dx_i\)
using a MDOFunction
.
objective = MDOFunction(np_sum, name="f", expr="sum(x)")
Define the design space¶
Then, we define the DesignSpace
with GEMSEO.
design_space = DesignSpace()
design_space.add_variable("x", 2, l_b=-5, u_b=5, var_type="integer")
Define the optimization problem¶
Then, we define the OptimizationProblem
with GEMSEO.
problem = OptimizationProblem(design_space)
problem.objective = objective
Solve the optimization problem using a DOE algorithm¶
We can see this optimization problem as a trade-off and solve it by means of a design of experiments (DOE), e.g. full factorial design
DOEFactory().execute(problem, "fullfact", n_samples=11**2)
INFO - 16:27:29: Optimization problem:
INFO - 16:27:29: minimize f = sum(x)
INFO - 16:27:29: with respect to x
INFO - 16:27:29: over the design space:
INFO - 16:27:29: +------+-------------+-------+-------------+---------+
INFO - 16:27:29: | name | lower_bound | value | upper_bound | type |
INFO - 16:27:29: +------+-------------+-------+-------------+---------+
INFO - 16:27:29: | x[0] | -5 | None | 5 | integer |
INFO - 16:27:29: | x[1] | -5 | None | 5 | integer |
INFO - 16:27:29: +------+-------------+-------+-------------+---------+
INFO - 16:27:29: Solving optimization problem with algorithm fullfact:
INFO - 16:27:29: ... 0%| | 0/121 [00:00<?, ?it]
INFO - 16:27:29: ... 1%| | 1/121 [00:00<00:00, 4249.55 it/sec, obj=-10]
INFO - 16:27:29: ... 2%|▏ | 2/121 [00:00<00:00, 3320.91 it/sec, obj=-9]
INFO - 16:27:29: ... 2%|▏ | 3/121 [00:00<00:00, 3171.10 it/sec, obj=-8]
INFO - 16:27:29: ... 3%|▎ | 4/121 [00:00<00:00, 3137.10 it/sec, obj=-7]
INFO - 16:27:29: ... 4%|▍ | 5/121 [00:00<00:00, 3118.91 it/sec, obj=-6]
INFO - 16:27:29: ... 5%|▍ | 6/121 [00:00<00:00, 2911.70 it/sec, obj=-5]
INFO - 16:27:29: ... 6%|▌ | 7/121 [00:00<00:00, 2917.34 it/sec, obj=-4]
INFO - 16:27:29: ... 7%|▋ | 8/121 [00:00<00:00, 2931.80 it/sec, obj=-3]
INFO - 16:27:29: ... 7%|▋ | 9/121 [00:00<00:00, 2946.82 it/sec, obj=-2]
INFO - 16:27:29: ... 8%|▊ | 10/121 [00:00<00:00, 2960.62 it/sec, obj=-1]
INFO - 16:27:29: ... 9%|▉ | 11/121 [00:00<00:00, 2941.31 it/sec, obj=0]
INFO - 16:27:29: ... 10%|▉ | 12/121 [00:00<00:00, 2952.35 it/sec, obj=-9]
INFO - 16:27:29: ... 11%|█ | 13/121 [00:00<00:00, 2963.04 it/sec, obj=-8]
INFO - 16:27:29: ... 12%|█▏ | 14/121 [00:00<00:00, 2959.99 it/sec, obj=-7]
INFO - 16:27:29: ... 12%|█▏ | 15/121 [00:00<00:00, 2965.01 it/sec, obj=-6]
INFO - 16:27:29: ... 13%|█▎ | 16/121 [00:00<00:00, 2973.37 it/sec, obj=-5]
INFO - 16:27:29: ... 14%|█▍ | 17/121 [00:00<00:00, 2979.41 it/sec, obj=-4]
INFO - 16:27:29: ... 15%|█▍ | 18/121 [00:00<00:00, 2970.47 it/sec, obj=-3]
INFO - 16:27:29: ... 16%|█▌ | 19/121 [00:00<00:00, 2974.24 it/sec, obj=-2]
INFO - 16:27:29: ... 17%|█▋ | 20/121 [00:00<00:00, 2977.75 it/sec, obj=-1]
INFO - 16:27:29: ... 17%|█▋ | 21/121 [00:00<00:00, 2983.65 it/sec, obj=0]
INFO - 16:27:29: ... 18%|█▊ | 22/121 [00:00<00:00, 2988.94 it/sec, obj=1]
INFO - 16:27:29: ... 19%|█▉ | 23/121 [00:00<00:00, 2972.39 it/sec, obj=-8]
INFO - 16:27:29: ... 20%|█▉ | 24/121 [00:00<00:00, 2975.39 it/sec, obj=-7]
INFO - 16:27:29: ... 21%|██ | 25/121 [00:00<00:00, 2978.57 it/sec, obj=-6]
INFO - 16:27:29: ... 21%|██▏ | 26/121 [00:00<00:00, 2976.31 it/sec, obj=-5]
INFO - 16:27:29: ... 22%|██▏ | 27/121 [00:00<00:00, 2979.46 it/sec, obj=-4]
INFO - 16:27:29: ... 23%|██▎ | 28/121 [00:00<00:00, 2983.68 it/sec, obj=-3]
INFO - 16:27:29: ... 24%|██▍ | 29/121 [00:00<00:00, 2988.13 it/sec, obj=-2]
INFO - 16:27:29: ... 25%|██▍ | 30/121 [00:00<00:00, 2991.44 it/sec, obj=-1]
INFO - 16:27:29: ... 26%|██▌ | 31/121 [00:00<00:00, 2995.10 it/sec, obj=0]
INFO - 16:27:29: ... 26%|██▋ | 32/121 [00:00<00:00, 2998.07 it/sec, obj=1]
INFO - 16:27:29: ... 27%|██▋ | 33/121 [00:00<00:00, 3000.80 it/sec, obj=2]
INFO - 16:27:29: ... 28%|██▊ | 34/121 [00:00<00:00, 3003.38 it/sec, obj=-7]
INFO - 16:27:29: ... 29%|██▉ | 35/121 [00:00<00:00, 2994.83 it/sec, obj=-6]
INFO - 16:27:29: ... 30%|██▉ | 36/121 [00:00<00:00, 2996.11 it/sec, obj=-5]
INFO - 16:27:29: ... 31%|███ | 37/121 [00:00<00:00, 2998.19 it/sec, obj=-4]
INFO - 16:27:29: ... 31%|███▏ | 38/121 [00:00<00:00, 2996.10 it/sec, obj=-3]
INFO - 16:27:29: ... 32%|███▏ | 39/121 [00:00<00:00, 2997.19 it/sec, obj=-2]
INFO - 16:27:29: ... 33%|███▎ | 40/121 [00:00<00:00, 2998.66 it/sec, obj=-1]
INFO - 16:27:29: ... 34%|███▍ | 41/121 [00:00<00:00, 3001.58 it/sec, obj=0]
INFO - 16:27:29: ... 35%|███▍ | 42/121 [00:00<00:00, 3004.62 it/sec, obj=1]
INFO - 16:27:29: ... 36%|███▌ | 43/121 [00:00<00:00, 3006.47 it/sec, obj=2]
INFO - 16:27:29: ... 36%|███▋ | 44/121 [00:00<00:00, 3008.38 it/sec, obj=3]
INFO - 16:27:29: ... 37%|███▋ | 45/121 [00:00<00:00, 3010.70 it/sec, obj=-6]
INFO - 16:27:29: ... 38%|███▊ | 46/121 [00:00<00:00, 3013.20 it/sec, obj=-5]
INFO - 16:27:29: ... 39%|███▉ | 47/121 [00:00<00:00, 3009.10 it/sec, obj=-4]
INFO - 16:27:29: ... 40%|███▉ | 48/121 [00:00<00:00, 3010.13 it/sec, obj=-3]
INFO - 16:27:29: ... 40%|████ | 49/121 [00:00<00:00, 3012.22 it/sec, obj=-2]
INFO - 16:27:29: ... 41%|████▏ | 50/121 [00:00<00:00, 3011.20 it/sec, obj=-1]
INFO - 16:27:29: ... 42%|████▏ | 51/121 [00:00<00:00, 3012.09 it/sec, obj=0]
INFO - 16:27:29: ... 43%|████▎ | 52/121 [00:00<00:00, 3013.82 it/sec, obj=1]
INFO - 16:27:29: ... 44%|████▍ | 53/121 [00:00<00:00, 3015.15 it/sec, obj=2]
INFO - 16:27:29: ... 45%|████▍ | 54/121 [00:00<00:00, 3016.76 it/sec, obj=3]
INFO - 16:27:29: ... 45%|████▌ | 55/121 [00:00<00:00, 3018.47 it/sec, obj=4]
INFO - 16:27:29: ... 46%|████▋ | 56/121 [00:00<00:00, 3020.20 it/sec, obj=-5]
INFO - 16:27:29: ... 47%|████▋ | 57/121 [00:00<00:00, 3021.76 it/sec, obj=-4]
INFO - 16:27:29: ... 48%|████▊ | 58/121 [00:00<00:00, 3023.49 it/sec, obj=-3]
INFO - 16:27:29: ... 49%|████▉ | 59/121 [00:00<00:00, 3021.24 it/sec, obj=-2]
INFO - 16:27:30: ... 50%|████▉ | 60/121 [00:00<00:00, 3021.94 it/sec, obj=-1]
INFO - 16:27:30: ... 50%|█████ | 61/121 [00:00<00:00, 3023.37 it/sec, obj=0]
INFO - 16:27:30: ... 51%|█████ | 62/121 [00:00<00:00, 3024.86 it/sec, obj=1]
INFO - 16:27:30: ... 52%|█████▏ | 63/121 [00:00<00:00, 3023.32 it/sec, obj=2]
INFO - 16:27:30: ... 53%|█████▎ | 64/121 [00:00<00:00, 3024.62 it/sec, obj=3]
INFO - 16:27:30: ... 54%|█████▎ | 65/121 [00:00<00:00, 3026.16 it/sec, obj=4]
INFO - 16:27:30: ... 55%|█████▍ | 66/121 [00:00<00:00, 3026.72 it/sec, obj=5]
INFO - 16:27:30: ... 55%|█████▌ | 67/121 [00:00<00:00, 3027.40 it/sec, obj=-4]
INFO - 16:27:30: ... 56%|█████▌ | 68/121 [00:00<00:00, 3028.38 it/sec, obj=-3]
INFO - 16:27:30: ... 57%|█████▋ | 69/121 [00:00<00:00, 3029.42 it/sec, obj=-2]
INFO - 16:27:30: ... 58%|█████▊ | 70/121 [00:00<00:00, 3030.25 it/sec, obj=-1]
INFO - 16:27:30: ... 59%|█████▊ | 71/121 [00:00<00:00, 3028.56 it/sec, obj=0]
INFO - 16:27:30: ... 60%|█████▉ | 72/121 [00:00<00:00, 3028.17 it/sec, obj=1]
INFO - 16:27:30: ... 60%|██████ | 73/121 [00:00<00:00, 3029.22 it/sec, obj=2]
INFO - 16:27:30: ... 61%|██████ | 74/121 [00:00<00:00, 3030.51 it/sec, obj=3]
INFO - 16:27:30: ... 62%|██████▏ | 75/121 [00:00<00:00, 3029.02 it/sec, obj=4]
INFO - 16:27:30: ... 63%|██████▎ | 76/121 [00:00<00:00, 3029.64 it/sec, obj=5]
INFO - 16:27:30: ... 64%|██████▎ | 77/121 [00:00<00:00, 3030.65 it/sec, obj=6]
INFO - 16:27:30: ... 64%|██████▍ | 78/121 [00:00<00:00, 3031.89 it/sec, obj=-3]
INFO - 16:27:30: ... 65%|██████▌ | 79/121 [00:00<00:00, 3033.12 it/sec, obj=-2]
INFO - 16:27:30: ... 66%|██████▌ | 80/121 [00:00<00:00, 3034.32 it/sec, obj=-1]
INFO - 16:27:30: ... 67%|██████▋ | 81/121 [00:00<00:00, 3035.41 it/sec, obj=0]
INFO - 16:27:30: ... 68%|██████▊ | 82/121 [00:00<00:00, 3036.48 it/sec, obj=1]
INFO - 16:27:30: ... 69%|██████▊ | 83/121 [00:00<00:00, 3037.55 it/sec, obj=2]
INFO - 16:27:30: ... 69%|██████▉ | 84/121 [00:00<00:00, 3035.14 it/sec, obj=3]
INFO - 16:27:30: ... 70%|███████ | 85/121 [00:00<00:00, 3035.65 it/sec, obj=4]
INFO - 16:27:30: ... 71%|███████ | 86/121 [00:00<00:00, 3036.15 it/sec, obj=5]
INFO - 16:27:30: ... 72%|███████▏ | 87/121 [00:00<00:00, 3035.23 it/sec, obj=6]
INFO - 16:27:30: ... 73%|███████▎ | 88/121 [00:00<00:00, 3035.70 it/sec, obj=7]
INFO - 16:27:30: ... 74%|███████▎ | 89/121 [00:00<00:00, 3036.38 it/sec, obj=-2]
INFO - 16:27:30: ... 74%|███████▍ | 90/121 [00:00<00:00, 3037.27 it/sec, obj=-1]
INFO - 16:27:30: ... 75%|███████▌ | 91/121 [00:00<00:00, 3038.26 it/sec, obj=0]
INFO - 16:27:30: ... 76%|███████▌ | 92/121 [00:00<00:00, 3039.40 it/sec, obj=1]
INFO - 16:27:30: ... 77%|███████▋ | 93/121 [00:00<00:00, 3040.20 it/sec, obj=2]
INFO - 16:27:30: ... 78%|███████▊ | 94/121 [00:00<00:00, 3041.20 it/sec, obj=3]
INFO - 16:27:30: ... 79%|███████▊ | 95/121 [00:00<00:00, 3041.93 it/sec, obj=4]
INFO - 16:27:30: ... 79%|███████▉ | 96/121 [00:00<00:00, 3039.86 it/sec, obj=5]
INFO - 16:27:30: ... 80%|████████ | 97/121 [00:00<00:00, 3039.99 it/sec, obj=6]
INFO - 16:27:30: ... 81%|████████ | 98/121 [00:00<00:00, 3040.47 it/sec, obj=7]
INFO - 16:27:30: ... 82%|████████▏ | 99/121 [00:00<00:00, 3039.71 it/sec, obj=8]
INFO - 16:27:30: ... 83%|████████▎ | 100/121 [00:00<00:00, 3039.35 it/sec, obj=-1]
INFO - 16:27:30: ... 83%|████████▎ | 101/121 [00:00<00:00, 3039.57 it/sec, obj=0]
INFO - 16:27:30: ... 84%|████████▍ | 102/121 [00:00<00:00, 3040.24 it/sec, obj=1]
INFO - 16:27:30: ... 85%|████████▌ | 103/121 [00:00<00:00, 3040.98 it/sec, obj=2]
INFO - 16:27:30: ... 86%|████████▌ | 104/121 [00:00<00:00, 3041.49 it/sec, obj=3]
INFO - 16:27:30: ... 87%|████████▋ | 105/121 [00:00<00:00, 3042.27 it/sec, obj=4]
INFO - 16:27:30: ... 88%|████████▊ | 106/121 [00:00<00:00, 3043.05 it/sec, obj=5]
INFO - 16:27:30: ... 88%|████████▊ | 107/121 [00:00<00:00, 3044.01 it/sec, obj=6]
INFO - 16:27:30: ... 89%|████████▉ | 108/121 [00:00<00:00, 3042.68 it/sec, obj=7]
INFO - 16:27:30: ... 90%|█████████ | 109/121 [00:00<00:00, 3042.45 it/sec, obj=8]
INFO - 16:27:30: ... 91%|█████████ | 110/121 [00:00<00:00, 3042.86 it/sec, obj=9]
INFO - 16:27:30: ... 92%|█████████▏| 111/121 [00:00<00:00, 3043.68 it/sec, obj=0]
INFO - 16:27:30: ... 93%|█████████▎| 112/121 [00:00<00:00, 3041.99 it/sec, obj=1]
INFO - 16:27:30: ... 93%|█████████▎| 113/121 [00:00<00:00, 3042.36 it/sec, obj=2]
INFO - 16:27:30: ... 94%|█████████▍| 114/121 [00:00<00:00, 3042.97 it/sec, obj=3]
INFO - 16:27:30: ... 95%|█████████▌| 115/121 [00:00<00:00, 3043.49 it/sec, obj=4]
INFO - 16:27:30: ... 96%|█████████▌| 116/121 [00:00<00:00, 3044.10 it/sec, obj=5]
INFO - 16:27:30: ... 97%|█████████▋| 117/121 [00:00<00:00, 3044.93 it/sec, obj=6]
INFO - 16:27:30: ... 98%|█████████▊| 118/121 [00:00<00:00, 3045.62 it/sec, obj=7]
INFO - 16:27:30: ... 98%|█████████▊| 119/121 [00:00<00:00, 3046.07 it/sec, obj=8]
INFO - 16:27:30: ... 99%|█████████▉| 120/121 [00:00<00:00, 3046.88 it/sec, obj=9]
INFO - 16:27:30: ... 100%|██████████| 121/121 [00:00<00:00, 3044.05 it/sec, obj=10]
INFO - 16:27:30: Optimization result:
INFO - 16:27:30: Optimizer info:
INFO - 16:27:30: Status: None
INFO - 16:27:30: Message: None
INFO - 16:27:30: Number of calls to the objective function by the optimizer: 121
INFO - 16:27:30: Solution:
INFO - 16:27:30: Objective: -10.0
INFO - 16:27:30: Design space:
INFO - 16:27:30: +------+-------------+-------+-------------+---------+
INFO - 16:27:30: | name | lower_bound | value | upper_bound | type |
INFO - 16:27:30: +------+-------------+-------+-------------+---------+
INFO - 16:27:30: | x[0] | -5 | -5 | 5 | integer |
INFO - 16:27:30: | x[1] | -5 | -5 | 5 | integer |
INFO - 16:27:30: +------+-------------+-------+-------------+---------+
Optimization result:
Design variables: [-5. -5.]
Objective function: -10.0
Feasible solution: True
Post-process the results¶
execute_post(
problem,
"ScatterPlotMatrix",
variable_names=["x", "f"],
save=False,
show=True,
)
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/envs/5.0.0/lib/python3.9/site-packages/genson/schema/strategies/base.py:42: UserWarning: Schema incompatible. Keyword 'description' has conflicting values ('The width and height of the figure in inches, e.g. ``(w, h)``.\nIf ``None``, use the :attr:`.OptPostProcessor.DEFAULT_FIG_SIZE`\nof the post-processor.' vs. 'The width and height of the figure in inches, e.g. `(w, h)`.\nIf ``None``, use the :attr:`.OptPostProcessor.DEFAULT_FIG_SIZE`\nof the post-processor.'). Using 'The width and height of the figure in inches, e.g. ``(w, h)``.\nIf ``None``, use the :attr:`.OptPostProcessor.DEFAULT_FIG_SIZE`\nof the post-processor.'
warn(('Schema incompatible. Keyword {0!r} has conflicting '
<gemseo.post.scatter_mat.ScatterPlotMatrix object at 0x7fea3d4956a0>
Note that you can get all the optimization algorithms names:
algo_list = DOEFactory().algorithms
print("Available algorithms ", algo_list)
Available algorithms ['CustomDOE', 'DiagonalDOE', 'OT_SOBOL', 'OT_RANDOM', 'OT_HASELGROVE', 'OT_REVERSE_HALTON', 'OT_HALTON', 'OT_FAURE', 'OT_MONTE_CARLO', 'OT_FACTORIAL', 'OT_COMPOSITE', 'OT_AXIAL', 'OT_OPT_LHS', 'OT_LHS', 'OT_LHSC', 'OT_FULLFACT', 'OT_SOBOL_INDICES', 'fullfact', 'ff2n', 'pbdesign', 'bbdesign', 'ccdesign', 'lhs']
Total running time of the script: ( 0 minutes 0.806 seconds)