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 - 08:37:06: Optimization problem:
INFO - 08:37:06: minimize f = sum(x)
INFO - 08:37:06: with respect to x
INFO - 08:37:06: over the design space:
INFO - 08:37:06: +------+-------------+-------+-------------+---------+
INFO - 08:37:06: | name | lower_bound | value | upper_bound | type |
INFO - 08:37:06: +------+-------------+-------+-------------+---------+
INFO - 08:37:06: | x[0] | -5 | None | 5 | integer |
INFO - 08:37:06: | x[1] | -5 | None | 5 | integer |
INFO - 08:37:06: +------+-------------+-------+-------------+---------+
INFO - 08:37:06: Solving optimization problem with algorithm fullfact:
INFO - 08:37:06: ... 0%| | 0/121 [00:00<?, ?it]
INFO - 08:37:06: ... 1%| | 1/121 [00:00<00:00, 3701.95 it/sec, obj=-10]
INFO - 08:37:06: ... 2%|▏ | 2/121 [00:00<00:00, 3247.62 it/sec, obj=-9]
INFO - 08:37:06: ... 2%|▏ | 3/121 [00:00<00:00, 3141.02 it/sec, obj=-8]
INFO - 08:37:06: ... 3%|▎ | 4/121 [00:00<00:00, 3107.47 it/sec, obj=-7]
INFO - 08:37:06: ... 4%|▍ | 5/121 [00:00<00:00, 2870.06 it/sec, obj=-6]
INFO - 08:37:06: ... 5%|▍ | 6/121 [00:00<00:00, 2873.47 it/sec, obj=-5]
INFO - 08:37:06: ... 6%|▌ | 7/121 [00:00<00:00, 2868.88 it/sec, obj=-4]
INFO - 08:37:06: ... 7%|▋ | 8/121 [00:00<00:00, 2889.88 it/sec, obj=-3]
INFO - 08:37:06: ... 7%|▋ | 9/121 [00:00<00:00, 2906.66 it/sec, obj=-2]
INFO - 08:37:06: ... 8%|▊ | 10/121 [00:00<00:00, 2923.27 it/sec, obj=-1]
INFO - 08:37:06: ... 9%|▉ | 11/121 [00:00<00:00, 2909.59 it/sec, obj=0]
INFO - 08:37:06: ... 10%|▉ | 12/121 [00:00<00:00, 2912.54 it/sec, obj=-9]
INFO - 08:37:06: ... 11%|█ | 13/121 [00:00<00:00, 2923.17 it/sec, obj=-8]
INFO - 08:37:06: ... 12%|█▏ | 14/121 [00:00<00:00, 2934.11 it/sec, obj=-7]
INFO - 08:37:06: ... 12%|█▏ | 15/121 [00:00<00:00, 2943.92 it/sec, obj=-6]
INFO - 08:37:06: ... 13%|█▎ | 16/121 [00:00<00:00, 2953.48 it/sec, obj=-5]
INFO - 08:37:06: ... 14%|█▍ | 17/121 [00:00<00:00, 2962.45 it/sec, obj=-4]
INFO - 08:37:06: ... 15%|█▍ | 18/121 [00:00<00:00, 2969.19 it/sec, obj=-3]
INFO - 08:37:06: ... 16%|█▌ | 19/121 [00:00<00:00, 2966.49 it/sec, obj=-2]
INFO - 08:37:06: ... 17%|█▋ | 20/121 [00:00<00:00, 2969.63 it/sec, obj=-1]
INFO - 08:37:06: ... 17%|█▋ | 21/121 [00:00<00:00, 2974.18 it/sec, obj=0]
INFO - 08:37:06: ... 18%|█▊ | 22/121 [00:00<00:00, 2979.20 it/sec, obj=1]
INFO - 08:37:06: ... 19%|█▉ | 23/121 [00:00<00:00, 2970.01 it/sec, obj=-8]
INFO - 08:37:06: ... 20%|█▉ | 24/121 [00:00<00:00, 2961.99 it/sec, obj=-7]
INFO - 08:37:06: ... 21%|██ | 25/121 [00:00<00:00, 2963.84 it/sec, obj=-6]
INFO - 08:37:06: ... 21%|██▏ | 26/121 [00:00<00:00, 2967.96 it/sec, obj=-5]
INFO - 08:37:06: ... 22%|██▏ | 27/121 [00:00<00:00, 2962.70 it/sec, obj=-4]
INFO - 08:37:06: ... 23%|██▎ | 28/121 [00:00<00:00, 2964.55 it/sec, obj=-3]
INFO - 08:37:06: ... 24%|██▍ | 29/121 [00:00<00:00, 2968.22 it/sec, obj=-2]
INFO - 08:37:06: ... 25%|██▍ | 30/121 [00:00<00:00, 2971.03 it/sec, obj=-1]
INFO - 08:37:06: ... 26%|██▌ | 31/121 [00:00<00:00, 2968.03 it/sec, obj=0]
INFO - 08:37:06: ... 26%|██▋ | 32/121 [00:00<00:00, 2971.13 it/sec, obj=1]
INFO - 08:37:06: ... 27%|██▋ | 33/121 [00:00<00:00, 2974.17 it/sec, obj=2]
INFO - 08:37:06: ... 28%|██▊ | 34/121 [00:00<00:00, 2976.92 it/sec, obj=-7]
INFO - 08:37:06: ... 29%|██▉ | 35/121 [00:00<00:00, 2971.55 it/sec, obj=-6]
INFO - 08:37:06: ... 30%|██▉ | 36/121 [00:00<00:00, 2970.12 it/sec, obj=-5]
INFO - 08:37:06: ... 31%|███ | 37/121 [00:00<00:00, 2972.23 it/sec, obj=-4]
INFO - 08:37:06: ... 31%|███▏ | 38/121 [00:00<00:00, 2973.85 it/sec, obj=-3]
INFO - 08:37:06: ... 32%|███▏ | 39/121 [00:00<00:00, 2975.66 it/sec, obj=-2]
INFO - 08:37:06: ... 33%|███▎ | 40/121 [00:00<00:00, 2976.95 it/sec, obj=-1]
INFO - 08:37:06: ... 34%|███▍ | 41/121 [00:00<00:00, 2978.86 it/sec, obj=0]
INFO - 08:37:06: ... 35%|███▍ | 42/121 [00:00<00:00, 2980.82 it/sec, obj=1]
INFO - 08:37:06: ... 36%|███▌ | 43/121 [00:00<00:00, 2977.73 it/sec, obj=2]
INFO - 08:37:06: ... 36%|███▋ | 44/121 [00:00<00:00, 2979.53 it/sec, obj=3]
INFO - 08:37:06: ... 37%|███▋ | 45/121 [00:00<00:00, 2981.31 it/sec, obj=-6]
INFO - 08:37:06: ... 38%|███▊ | 46/121 [00:00<00:00, 2983.84 it/sec, obj=-5]
INFO - 08:37:06: ... 39%|███▉ | 47/121 [00:00<00:00, 2981.03 it/sec, obj=-4]
INFO - 08:37:06: ... 40%|███▉ | 48/121 [00:00<00:00, 2980.67 it/sec, obj=-3]
INFO - 08:37:06: ... 40%|████ | 49/121 [00:00<00:00, 2982.37 it/sec, obj=-2]
INFO - 08:37:06: ... 41%|████▏ | 50/121 [00:00<00:00, 2984.21 it/sec, obj=-1]
INFO - 08:37:06: ... 42%|████▏ | 51/121 [00:00<00:00, 2986.35 it/sec, obj=0]
INFO - 08:37:06: ... 43%|████▎ | 52/121 [00:00<00:00, 2988.42 it/sec, obj=1]
INFO - 08:37:06: ... 44%|████▍ | 53/121 [00:00<00:00, 2990.13 it/sec, obj=2]
INFO - 08:37:06: ... 45%|████▍ | 54/121 [00:00<00:00, 2991.97 it/sec, obj=3]
INFO - 08:37:06: ... 45%|████▌ | 55/121 [00:00<00:00, 2990.38 it/sec, obj=4]
INFO - 08:37:06: ... 46%|████▋ | 56/121 [00:00<00:00, 2992.27 it/sec, obj=-5]
INFO - 08:37:06: ... 47%|████▋ | 57/121 [00:00<00:00, 2994.21 it/sec, obj=-4]
INFO - 08:37:06: ... 48%|████▊ | 58/121 [00:00<00:00, 2996.08 it/sec, obj=-3]
INFO - 08:37:06: ... 49%|████▉ | 59/121 [00:00<00:00, 2998.00 it/sec, obj=-2]
INFO - 08:37:06: ... 50%|████▉ | 60/121 [00:00<00:00, 2994.36 it/sec, obj=-1]
INFO - 08:37:06: ... 50%|█████ | 61/121 [00:00<00:00, 2995.90 it/sec, obj=0]
INFO - 08:37:06: ... 51%|█████ | 62/121 [00:00<00:00, 2997.83 it/sec, obj=1]
INFO - 08:37:06: ... 52%|█████▏ | 63/121 [00:00<00:00, 2999.50 it/sec, obj=2]
INFO - 08:37:06: ... 53%|█████▎ | 64/121 [00:00<00:00, 3001.19 it/sec, obj=3]
INFO - 08:37:06: ... 54%|█████▎ | 65/121 [00:00<00:00, 3002.93 it/sec, obj=4]
INFO - 08:37:06: ... 55%|█████▍ | 66/121 [00:00<00:00, 3004.42 it/sec, obj=5]
INFO - 08:37:06: ... 55%|█████▌ | 67/121 [00:00<00:00, 3003.23 it/sec, obj=-4]
INFO - 08:37:06: ... 56%|█████▌ | 68/121 [00:00<00:00, 3003.91 it/sec, obj=-3]
INFO - 08:37:06: ... 57%|█████▋ | 69/121 [00:00<00:00, 3005.20 it/sec, obj=-2]
INFO - 08:37:06: ... 58%|█████▊ | 70/121 [00:00<00:00, 3006.42 it/sec, obj=-1]
INFO - 08:37:06: ... 59%|█████▊ | 71/121 [00:00<00:00, 3007.70 it/sec, obj=0]
INFO - 08:37:06: ... 60%|█████▉ | 72/121 [00:00<00:00, 3004.28 it/sec, obj=1]
INFO - 08:37:06: ... 60%|██████ | 73/121 [00:00<00:00, 3005.14 it/sec, obj=2]
INFO - 08:37:06: ... 61%|██████ | 74/121 [00:00<00:00, 3006.38 it/sec, obj=3]
INFO - 08:37:06: ... 62%|██████▏ | 75/121 [00:00<00:00, 3007.65 it/sec, obj=4]
INFO - 08:37:06: ... 63%|██████▎ | 76/121 [00:00<00:00, 3008.94 it/sec, obj=5]
INFO - 08:37:06: ... 64%|██████▎ | 77/121 [00:00<00:00, 3010.26 it/sec, obj=6]
INFO - 08:37:06: ... 64%|██████▍ | 78/121 [00:00<00:00, 3011.60 it/sec, obj=-3]
INFO - 08:37:06: ... 65%|██████▌ | 79/121 [00:00<00:00, 3011.18 it/sec, obj=-2]
INFO - 08:37:06: ... 66%|██████▌ | 80/121 [00:00<00:00, 3011.47 it/sec, obj=-1]
INFO - 08:37:06: ... 67%|██████▋ | 81/121 [00:00<00:00, 3012.32 it/sec, obj=0]
INFO - 08:37:06: ... 68%|██████▊ | 82/121 [00:00<00:00, 3013.23 it/sec, obj=1]
INFO - 08:37:06: ... 69%|██████▊ | 83/121 [00:00<00:00, 3014.40 it/sec, obj=2]
INFO - 08:37:06: ... 69%|██████▉ | 84/121 [00:00<00:00, 3012.20 it/sec, obj=3]
INFO - 08:37:06: ... 70%|███████ | 85/121 [00:00<00:00, 3012.28 it/sec, obj=4]
INFO - 08:37:06: ... 71%|███████ | 86/121 [00:00<00:00, 3013.02 it/sec, obj=5]
INFO - 08:37:06: ... 72%|███████▏ | 87/121 [00:00<00:00, 3014.27 it/sec, obj=6]
INFO - 08:37:06: ... 73%|███████▎ | 88/121 [00:00<00:00, 3015.39 it/sec, obj=7]
INFO - 08:37:06: ... 74%|███████▎ | 89/121 [00:00<00:00, 3016.41 it/sec, obj=-2]
INFO - 08:37:06: ... 74%|███████▍ | 90/121 [00:00<00:00, 3017.32 it/sec, obj=-1]
INFO - 08:37:06: ... 75%|███████▌ | 91/121 [00:00<00:00, 3017.13 it/sec, obj=0]
INFO - 08:37:06: ... 76%|███████▌ | 92/121 [00:00<00:00, 3016.99 it/sec, obj=1]
INFO - 08:37:06: ... 77%|███████▋ | 93/121 [00:00<00:00, 3017.72 it/sec, obj=2]
INFO - 08:37:06: ... 78%|███████▊ | 94/121 [00:00<00:00, 3018.27 it/sec, obj=3]
INFO - 08:37:06: ... 79%|███████▊ | 95/121 [00:00<00:00, 3018.95 it/sec, obj=4]
INFO - 08:37:06: ... 79%|███████▉ | 96/121 [00:00<00:00, 3017.28 it/sec, obj=5]
INFO - 08:37:06: ... 80%|████████ | 97/121 [00:00<00:00, 3017.10 it/sec, obj=6]
INFO - 08:37:06: ... 81%|████████ | 98/121 [00:00<00:00, 3017.80 it/sec, obj=7]
INFO - 08:37:06: ... 82%|████████▏ | 99/121 [00:00<00:00, 3006.89 it/sec, obj=8]
INFO - 08:37:06: ... 83%|████████▎ | 100/121 [00:00<00:00, 2991.27 it/sec, obj=-1]
INFO - 08:37:06: ... 83%|████████▎ | 101/121 [00:00<00:00, 2989.08 it/sec, obj=0]
INFO - 08:37:06: ... 84%|████████▍ | 102/121 [00:00<00:00, 2989.07 it/sec, obj=1]
INFO - 08:37:06: ... 85%|████████▌ | 103/121 [00:00<00:00, 2986.98 it/sec, obj=2]
INFO - 08:37:06: ... 86%|████████▌ | 104/121 [00:00<00:00, 2987.15 it/sec, obj=3]
INFO - 08:37:06: ... 87%|████████▋ | 105/121 [00:00<00:00, 2987.70 it/sec, obj=4]
INFO - 08:37:06: ... 88%|████████▊ | 106/121 [00:00<00:00, 2988.48 it/sec, obj=5]
INFO - 08:37:06: ... 88%|████████▊ | 107/121 [00:00<00:00, 2987.06 it/sec, obj=6]
INFO - 08:37:06: ... 89%|████████▉ | 108/121 [00:00<00:00, 2987.18 it/sec, obj=7]
INFO - 08:37:06: ... 90%|█████████ | 109/121 [00:00<00:00, 2987.71 it/sec, obj=8]
INFO - 08:37:06: ... 91%|█████████ | 110/121 [00:00<00:00, 2988.32 it/sec, obj=9]
INFO - 08:37:06: ... 92%|█████████▏| 111/121 [00:00<00:00, 2989.12 it/sec, obj=0]
INFO - 08:37:06: ... 93%|█████████▎| 112/121 [00:00<00:00, 2989.91 it/sec, obj=1]
INFO - 08:37:06: ... 93%|█████████▎| 113/121 [00:00<00:00, 2990.60 it/sec, obj=2]
INFO - 08:37:06: ... 94%|█████████▍| 114/121 [00:00<00:00, 2991.47 it/sec, obj=3]
INFO - 08:37:06: ... 95%|█████████▌| 115/121 [00:00<00:00, 2990.51 it/sec, obj=4]
INFO - 08:37:06: ... 96%|█████████▌| 116/121 [00:00<00:00, 2991.12 it/sec, obj=5]
INFO - 08:37:06: ... 97%|█████████▋| 117/121 [00:00<00:00, 2991.95 it/sec, obj=6]
INFO - 08:37:06: ... 98%|█████████▊| 118/121 [00:00<00:00, 2992.83 it/sec, obj=7]
INFO - 08:37:06: ... 98%|█████████▊| 119/121 [00:00<00:00, 2991.73 it/sec, obj=8]
INFO - 08:37:06: ... 99%|█████████▉| 120/121 [00:00<00:00, 2991.75 it/sec, obj=9]
INFO - 08:37:06: ... 100%|██████████| 121/121 [00:00<00:00, 2992.40 it/sec, obj=10]
INFO - 08:37:06: Optimization result:
INFO - 08:37:06: Optimizer info:
INFO - 08:37:06: Status: None
INFO - 08:37:06: Message: None
INFO - 08:37:06: Number of calls to the objective function by the optimizer: 121
INFO - 08:37:06: Solution:
INFO - 08:37:06: Objective: -10.0
INFO - 08:37:06: Design space:
INFO - 08:37:06: +------+-------------+-------+-------------+---------+
INFO - 08:37:06: | name | lower_bound | value | upper_bound | type |
INFO - 08:37:06: +------+-------------+-------+-------------+---------+
INFO - 08:37:06: | x[0] | -5 | -5 | 5 | integer |
INFO - 08:37:06: | x[1] | -5 | -5 | 5 | integer |
INFO - 08:37:06: +------+-------------+-------+-------------+---------+
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.1/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 0x7f87770812b0>
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.761 seconds)