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 numpy import sum as np_sum
from gemseo import configure_logger
from gemseo import execute_algo
from gemseo import execute_post
from gemseo import get_available_doe_algorithms
from gemseo import get_available_opt_algorithms
from gemseo.algos.design_space import DesignSpace
from gemseo.algos.optimization_problem import OptimizationProblem
from gemseo.core.mdo_functions.mdo_function import MDOFunction
LOGGER = configure_logger()
Define the objective function#
We define the objective function \(f(x)=\sum_{i=1}^dx_i\)
using an 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, lower_bound=-5, upper_bound=5, 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
execute_algo(problem, algo_name="PYDOE_FULLFACT", n_samples=11**2, algo_type="doe")
INFO - 08:35:28: Optimization problem:
INFO - 08:35:28: minimize f = sum(x)
INFO - 08:35:28: with respect to x
INFO - 08:35:28: over the design space:
INFO - 08:35:28: +------+-------------+-------+-------------+---------+
INFO - 08:35:28: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:35:28: +------+-------------+-------+-------------+---------+
INFO - 08:35:28: | x[0] | -5 | None | 5 | integer |
INFO - 08:35:28: | x[1] | -5 | None | 5 | integer |
INFO - 08:35:28: +------+-------------+-------+-------------+---------+
INFO - 08:35:28: Solving optimization problem with algorithm PYDOE_FULLFACT:
INFO - 08:35:28: 1%| | 1/121 [00:00<00:00, 5866.16 it/sec, obj=-10]
INFO - 08:35:28: 2%|▏ | 2/121 [00:00<00:00, 4431.38 it/sec, obj=-9]
INFO - 08:35:28: 2%|▏ | 3/121 [00:00<00:00, 4307.74 it/sec, obj=-8]
INFO - 08:35:28: 3%|▎ | 4/121 [00:00<00:00, 4254.94 it/sec, obj=-7]
INFO - 08:35:28: 4%|▍ | 5/121 [00:00<00:00, 4299.20 it/sec, obj=-6]
INFO - 08:35:28: 5%|▍ | 6/121 [00:00<00:00, 4334.45 it/sec, obj=-5]
INFO - 08:35:28: 6%|▌ | 7/121 [00:00<00:00, 4365.82 it/sec, obj=-4]
INFO - 08:35:28: 7%|▋ | 8/121 [00:00<00:00, 4392.52 it/sec, obj=-3]
INFO - 08:35:28: 7%|▋ | 9/121 [00:00<00:00, 4414.02 it/sec, obj=-2]
INFO - 08:35:28: 8%|▊ | 10/121 [00:00<00:00, 4319.57 it/sec, obj=-1]
INFO - 08:35:28: 9%|▉ | 11/121 [00:00<00:00, 4323.62 it/sec, obj=0]
INFO - 08:35:28: 10%|▉ | 12/121 [00:00<00:00, 4142.18 it/sec, obj=-9]
INFO - 08:35:28: 11%|█ | 13/121 [00:00<00:00, 3965.52 it/sec, obj=-8]
INFO - 08:35:28: 12%|█▏ | 14/121 [00:00<00:00, 3934.62 it/sec, obj=-7]
INFO - 08:35:28: 12%|█▏ | 15/121 [00:00<00:00, 3919.18 it/sec, obj=-6]
INFO - 08:35:28: 13%|█▎ | 16/121 [00:00<00:00, 3941.78 it/sec, obj=-5]
INFO - 08:35:28: 14%|█▍ | 17/121 [00:00<00:00, 3964.59 it/sec, obj=-4]
INFO - 08:35:28: 15%|█▍ | 18/121 [00:00<00:00, 3984.88 it/sec, obj=-3]
INFO - 08:35:28: 16%|█▌ | 19/121 [00:00<00:00, 3993.17 it/sec, obj=-2]
INFO - 08:35:28: 17%|█▋ | 20/121 [00:00<00:00, 4015.99 it/sec, obj=-1]
INFO - 08:35:28: 17%|█▋ | 21/121 [00:00<00:00, 4038.72 it/sec, obj=0]
INFO - 08:35:28: 18%|█▊ | 22/121 [00:00<00:00, 4059.42 it/sec, obj=1]
INFO - 08:35:28: 19%|█▉ | 23/121 [00:00<00:00, 4077.30 it/sec, obj=-8]
INFO - 08:35:28: 20%|█▉ | 24/121 [00:00<00:00, 4096.33 it/sec, obj=-7]
INFO - 08:35:28: 21%|██ | 25/121 [00:00<00:00, 4113.19 it/sec, obj=-6]
INFO - 08:35:28: 21%|██▏ | 26/121 [00:00<00:00, 4099.23 it/sec, obj=-5]
INFO - 08:35:28: 22%|██▏ | 27/121 [00:00<00:00, 4112.51 it/sec, obj=-4]
INFO - 08:35:28: 23%|██▎ | 28/121 [00:00<00:00, 4127.24 it/sec, obj=-3]
INFO - 08:35:28: 24%|██▍ | 29/121 [00:00<00:00, 4141.46 it/sec, obj=-2]
INFO - 08:35:28: 25%|██▍ | 30/121 [00:00<00:00, 4154.70 it/sec, obj=-1]
INFO - 08:35:28: 26%|██▌ | 31/121 [00:00<00:00, 4165.95 it/sec, obj=0]
INFO - 08:35:28: 26%|██▋ | 32/121 [00:00<00:00, 4158.18 it/sec, obj=1]
INFO - 08:35:28: 27%|██▋ | 33/121 [00:00<00:00, 4164.15 it/sec, obj=2]
INFO - 08:35:28: 28%|██▊ | 34/121 [00:00<00:00, 4171.73 it/sec, obj=-7]
INFO - 08:35:28: 29%|██▉ | 35/121 [00:00<00:00, 4178.78 it/sec, obj=-6]
INFO - 08:35:28: 30%|██▉ | 36/121 [00:00<00:00, 4185.35 it/sec, obj=-5]
INFO - 08:35:28: 31%|███ | 37/121 [00:00<00:00, 4188.64 it/sec, obj=-4]
INFO - 08:35:28: 31%|███▏ | 38/121 [00:00<00:00, 4198.17 it/sec, obj=-3]
INFO - 08:35:28: 32%|███▏ | 39/121 [00:00<00:00, 4208.12 it/sec, obj=-2]
INFO - 08:35:28: 33%|███▎ | 40/121 [00:00<00:00, 4216.76 it/sec, obj=-1]
INFO - 08:35:28: 34%|███▍ | 41/121 [00:00<00:00, 4226.15 it/sec, obj=0]
INFO - 08:35:28: 35%|███▍ | 42/121 [00:00<00:00, 4233.92 it/sec, obj=1]
INFO - 08:35:28: 36%|███▌ | 43/121 [00:00<00:00, 4226.35 it/sec, obj=2]
INFO - 08:35:28: 36%|███▋ | 44/121 [00:00<00:00, 4223.19 it/sec, obj=3]
INFO - 08:35:28: 37%|███▋ | 45/121 [00:00<00:00, 4229.55 it/sec, obj=-6]
INFO - 08:35:28: 38%|███▊ | 46/121 [00:00<00:00, 4231.65 it/sec, obj=-5]
INFO - 08:35:28: 39%|███▉ | 47/121 [00:00<00:00, 4233.67 it/sec, obj=-4]
INFO - 08:35:28: 40%|███▉ | 48/121 [00:00<00:00, 4240.06 it/sec, obj=-3]
INFO - 08:35:28: 40%|████ | 49/121 [00:00<00:00, 4199.53 it/sec, obj=-2]
INFO - 08:35:28: 41%|████▏ | 50/121 [00:00<00:00, 4196.99 it/sec, obj=-1]
INFO - 08:35:28: 42%|████▏ | 51/121 [00:00<00:00, 4201.14 it/sec, obj=0]
INFO - 08:35:28: 43%|████▎ | 52/121 [00:00<00:00, 4201.66 it/sec, obj=1]
INFO - 08:35:28: 44%|████▍ | 53/121 [00:00<00:00, 4205.81 it/sec, obj=2]
INFO - 08:35:28: 45%|████▍ | 54/121 [00:00<00:00, 4207.16 it/sec, obj=3]
INFO - 08:35:28: 45%|████▌ | 55/121 [00:00<00:00, 4213.69 it/sec, obj=4]
INFO - 08:35:28: 46%|████▋ | 56/121 [00:00<00:00, 4220.53 it/sec, obj=-5]
INFO - 08:35:28: 47%|████▋ | 57/121 [00:00<00:00, 4226.78 it/sec, obj=-4]
INFO - 08:35:28: 48%|████▊ | 58/121 [00:00<00:00, 4232.62 it/sec, obj=-3]
INFO - 08:35:28: 49%|████▉ | 59/121 [00:00<00:00, 4238.56 it/sec, obj=-2]
INFO - 08:35:28: 50%|████▉ | 60/121 [00:00<00:00, 4237.24 it/sec, obj=-1]
INFO - 08:35:28: 50%|█████ | 61/121 [00:00<00:00, 4237.65 it/sec, obj=0]
INFO - 08:35:28: 51%|█████ | 62/121 [00:00<00:00, 4242.48 it/sec, obj=1]
INFO - 08:35:28: 52%|█████▏ | 63/121 [00:00<00:00, 4247.43 it/sec, obj=2]
INFO - 08:35:28: 53%|█████▎ | 64/121 [00:00<00:00, 4252.91 it/sec, obj=3]
INFO - 08:35:28: 54%|█████▎ | 65/121 [00:00<00:00, 4258.44 it/sec, obj=4]
INFO - 08:35:28: 55%|█████▍ | 66/121 [00:00<00:00, 4263.55 it/sec, obj=5]
INFO - 08:35:28: 55%|█████▌ | 67/121 [00:00<00:00, 4257.08 it/sec, obj=-4]
INFO - 08:35:28: 56%|█████▌ | 68/121 [00:00<00:00, 4259.32 it/sec, obj=-3]
INFO - 08:35:28: 57%|█████▋ | 69/121 [00:00<00:00, 4262.13 it/sec, obj=-2]
INFO - 08:35:28: 58%|█████▊ | 70/121 [00:00<00:00, 4264.67 it/sec, obj=-1]
INFO - 08:35:28: 59%|█████▊ | 71/121 [00:00<00:00, 4267.27 it/sec, obj=0]
INFO - 08:35:28: 60%|█████▉ | 72/121 [00:00<00:00, 4267.93 it/sec, obj=1]
INFO - 08:35:28: 60%|██████ | 73/121 [00:00<00:00, 4271.78 it/sec, obj=2]
INFO - 08:35:28: 61%|██████ | 74/121 [00:00<00:00, 4275.42 it/sec, obj=3]
INFO - 08:35:28: 62%|██████▏ | 75/121 [00:00<00:00, 4278.27 it/sec, obj=4]
INFO - 08:35:28: 63%|██████▎ | 76/121 [00:00<00:00, 4281.86 it/sec, obj=5]
INFO - 08:35:28: 64%|██████▎ | 77/121 [00:00<00:00, 4285.52 it/sec, obj=6]
INFO - 08:35:28: 64%|██████▍ | 78/121 [00:00<00:00, 4283.15 it/sec, obj=-3]
INFO - 08:35:28: 65%|██████▌ | 79/121 [00:00<00:00, 4284.33 it/sec, obj=-2]
INFO - 08:35:28: 66%|██████▌ | 80/121 [00:00<00:00, 4287.45 it/sec, obj=-1]
INFO - 08:35:28: 67%|██████▋ | 81/121 [00:00<00:00, 4290.82 it/sec, obj=0]
INFO - 08:35:28: 68%|██████▊ | 82/121 [00:00<00:00, 4294.06 it/sec, obj=1]
INFO - 08:35:28: 69%|██████▊ | 83/121 [00:00<00:00, 4296.91 it/sec, obj=2]
INFO - 08:35:28: 69%|██████▉ | 84/121 [00:00<00:00, 4300.28 it/sec, obj=3]
INFO - 08:35:28: 70%|███████ | 85/121 [00:00<00:00, 4294.39 it/sec, obj=4]
INFO - 08:35:28: 71%|███████ | 86/121 [00:00<00:00, 4294.68 it/sec, obj=5]
INFO - 08:35:28: 72%|███████▏ | 87/121 [00:00<00:00, 4296.23 it/sec, obj=6]
INFO - 08:35:28: 73%|███████▎ | 88/121 [00:00<00:00, 4297.94 it/sec, obj=7]
INFO - 08:35:28: 74%|███████▎ | 89/121 [00:00<00:00, 4299.72 it/sec, obj=-2]
INFO - 08:35:28: 74%|███████▍ | 90/121 [00:00<00:00, 4299.89 it/sec, obj=-1]
INFO - 08:35:28: 75%|███████▌ | 91/121 [00:00<00:00, 4303.06 it/sec, obj=0]
INFO - 08:35:28: 76%|███████▌ | 92/121 [00:00<00:00, 4306.31 it/sec, obj=1]
INFO - 08:35:28: 77%|███████▋ | 93/121 [00:00<00:00, 4309.45 it/sec, obj=2]
INFO - 08:35:28: 78%|███████▊ | 94/121 [00:00<00:00, 4312.39 it/sec, obj=3]
INFO - 08:35:28: 79%|███████▊ | 95/121 [00:00<00:00, 4315.08 it/sec, obj=4]
INFO - 08:35:28: 79%|███████▉ | 96/121 [00:00<00:00, 4310.32 it/sec, obj=5]
INFO - 08:35:28: 80%|████████ | 97/121 [00:00<00:00, 4311.20 it/sec, obj=6]
INFO - 08:35:28: 81%|████████ | 98/121 [00:00<00:00, 4314.00 it/sec, obj=7]
INFO - 08:35:28: 82%|████████▏ | 99/121 [00:00<00:00, 4316.70 it/sec, obj=8]
INFO - 08:35:28: 83%|████████▎ | 100/121 [00:00<00:00, 4319.17 it/sec, obj=-1]
INFO - 08:35:28: 83%|████████▎ | 101/121 [00:00<00:00, 4321.51 it/sec, obj=0]
INFO - 08:35:28: 84%|████████▍ | 102/121 [00:00<00:00, 4318.92 it/sec, obj=1]
INFO - 08:35:28: 85%|████████▌ | 103/121 [00:00<00:00, 4318.75 it/sec, obj=2]
INFO - 08:35:28: 86%|████████▌ | 104/121 [00:00<00:00, 4319.74 it/sec, obj=3]
INFO - 08:35:28: 87%|████████▋ | 105/121 [00:00<00:00, 4321.22 it/sec, obj=4]
INFO - 08:35:28: 88%|████████▊ | 106/121 [00:00<00:00, 4322.51 it/sec, obj=5]
INFO - 08:35:28: 88%|████████▊ | 107/121 [00:00<00:00, 4321.94 it/sec, obj=6]
INFO - 08:35:28: 89%|████████▉ | 108/121 [00:00<00:00, 4324.35 it/sec, obj=7]
INFO - 08:35:28: 90%|█████████ | 109/121 [00:00<00:00, 4326.56 it/sec, obj=8]
INFO - 08:35:28: 91%|█████████ | 110/121 [00:00<00:00, 4329.06 it/sec, obj=9]
INFO - 08:35:28: 92%|█████████▏| 111/121 [00:00<00:00, 4331.87 it/sec, obj=0]
INFO - 08:35:28: 93%|█████████▎| 112/121 [00:00<00:00, 4334.28 it/sec, obj=1]
INFO - 08:35:28: 93%|█████████▎| 113/121 [00:00<00:00, 4336.69 it/sec, obj=2]
INFO - 08:35:28: 94%|█████████▍| 114/121 [00:00<00:00, 4333.51 it/sec, obj=3]
INFO - 08:35:28: 95%|█████████▌| 115/121 [00:00<00:00, 4334.83 it/sec, obj=4]
INFO - 08:35:28: 96%|█████████▌| 116/121 [00:00<00:00, 4336.40 it/sec, obj=5]
INFO - 08:35:28: 97%|█████████▋| 117/121 [00:00<00:00, 4338.67 it/sec, obj=6]
INFO - 08:35:28: 98%|█████████▊| 118/121 [00:00<00:00, 4340.79 it/sec, obj=7]
INFO - 08:35:28: 98%|█████████▊| 119/121 [00:00<00:00, 4342.46 it/sec, obj=8]
INFO - 08:35:28: 99%|█████████▉| 120/121 [00:00<00:00, 4339.46 it/sec, obj=9]
INFO - 08:35:28: 100%|██████████| 121/121 [00:00<00:00, 4339.44 it/sec, obj=10]
INFO - 08:35:28: Optimization result:
INFO - 08:35:28: Optimizer info:
INFO - 08:35:28: Status: None
INFO - 08:35:28: Message: None
INFO - 08:35:28: Number of calls to the objective function by the optimizer: 121
INFO - 08:35:28: Solution:
INFO - 08:35:28: Objective: -10.0
INFO - 08:35:28: Design space:
INFO - 08:35:28: +------+-------------+-------+-------------+---------+
INFO - 08:35:28: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:35:28: +------+-------------+-------+-------------+---------+
INFO - 08:35:28: | x[0] | -5 | -5 | 5 | integer |
INFO - 08:35:28: | x[1] | -5 | -5 | 5 | integer |
INFO - 08:35:28: +------+-------------+-------+-------------+---------+
Post-process the results#
execute_post(
problem,
post_name="ScatterPlotMatrix",
variable_names=["x", "f"],
save=False,
show=True,
)

<gemseo.post.scatter_plot_matrix.ScatterPlotMatrix object at 0x7f6df7f7a070>
Note that you can get all the optimization algorithms names:
get_available_opt_algorithms()
['Augmented_Lagrangian_order_0', 'Augmented_Lagrangian_order_1', 'MNBI', 'MultiStart', 'NLOPT_MMA', 'NLOPT_COBYLA', 'NLOPT_SLSQP', 'NLOPT_BOBYQA', 'NLOPT_BFGS', 'NLOPT_NEWUOA', 'DUAL_ANNEALING', 'SHGO', 'DIFFERENTIAL_EVOLUTION', 'INTERIOR_POINT', 'DUAL_SIMPLEX', 'Scipy_MILP', 'SLSQP', 'L-BFGS-B', 'TNC', 'NELDER-MEAD']
and all the DOE algorithms names:
get_available_doe_algorithms()
['CustomDOE', 'DiagonalDOE', 'MorrisDOE', 'OATDOE', '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', 'PYDOE_BBDESIGN', 'PYDOE_CCDESIGN', 'PYDOE_FF2N', 'PYDOE_FULLFACT', 'PYDOE_LHS', 'PYDOE_PBDESIGN', 'Halton', 'LHS', 'MC', 'PoissonDisk', 'Sobol']
Total running time of the script: (0 minutes 0.658 seconds)