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:47: Optimization problem:
INFO - 08:35:47: minimize f = sum(x)
INFO - 08:35:47: with respect to x
INFO - 08:35:47: over the design space:
INFO - 08:35:47: +------+-------------+-------+-------------+---------+
INFO - 08:35:47: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:35:47: +------+-------------+-------+-------------+---------+
INFO - 08:35:47: | x[0] | -5 | None | 5 | integer |
INFO - 08:35:47: | x[1] | -5 | None | 5 | integer |
INFO - 08:35:47: +------+-------------+-------+-------------+---------+
INFO - 08:35:47: Solving optimization problem with algorithm PYDOE_FULLFACT:
INFO - 08:35:47: 1%| | 1/121 [00:00<00:00, 4957.81 it/sec, obj=-10]
INFO - 08:35:47: 2%|▏ | 2/121 [00:00<00:00, 3826.92 it/sec, obj=-9]
INFO - 08:35:47: 2%|▏ | 3/121 [00:00<00:00, 3744.91 it/sec, obj=-8]
INFO - 08:35:47: 3%|▎ | 4/121 [00:00<00:00, 3733.25 it/sec, obj=-7]
INFO - 08:35:47: 4%|▍ | 5/121 [00:00<00:00, 3744.91 it/sec, obj=-6]
INFO - 08:35:47: 5%|▍ | 6/121 [00:00<00:00, 3756.09 it/sec, obj=-5]
INFO - 08:35:47: 6%|▌ | 7/121 [00:00<00:00, 3773.80 it/sec, obj=-4]
INFO - 08:35:47: 7%|▋ | 8/121 [00:00<00:00, 3777.80 it/sec, obj=-3]
INFO - 08:35:47: 7%|▋ | 9/121 [00:00<00:00, 3784.71 it/sec, obj=-2]
INFO - 08:35:47: 8%|▊ | 10/121 [00:00<00:00, 3790.60 it/sec, obj=-1]
INFO - 08:35:47: 9%|▉ | 11/121 [00:00<00:00, 3798.25 it/sec, obj=0]
INFO - 08:35:47: 10%|▉ | 12/121 [00:00<00:00, 3776.38 it/sec, obj=-9]
INFO - 08:35:47: 11%|█ | 13/121 [00:00<00:00, 3778.91 it/sec, obj=-8]
INFO - 08:35:47: 12%|█▏ | 14/121 [00:00<00:00, 3728.27 it/sec, obj=-7]
INFO - 08:35:47: 12%|█▏ | 15/121 [00:00<00:00, 3726.95 it/sec, obj=-6]
INFO - 08:35:47: 13%|█▎ | 16/121 [00:00<00:00, 3734.70 it/sec, obj=-5]
INFO - 08:35:47: 14%|█▍ | 17/121 [00:00<00:00, 3742.36 it/sec, obj=-4]
INFO - 08:35:47: 15%|█▍ | 18/121 [00:00<00:00, 3748.63 it/sec, obj=-3]
INFO - 08:35:47: 16%|█▌ | 19/121 [00:00<00:00, 3755.15 it/sec, obj=-2]
INFO - 08:35:47: 17%|█▋ | 20/121 [00:00<00:00, 3761.71 it/sec, obj=-1]
INFO - 08:35:47: 17%|█▋ | 21/121 [00:00<00:00, 3770.08 it/sec, obj=0]
INFO - 08:35:47: 18%|█▊ | 22/121 [00:00<00:00, 3794.19 it/sec, obj=1]
INFO - 08:35:47: 19%|█▉ | 23/121 [00:00<00:00, 3816.02 it/sec, obj=-8]
INFO - 08:35:47: 20%|█▉ | 24/121 [00:00<00:00, 3837.86 it/sec, obj=-7]
INFO - 08:35:47: 21%|██ | 25/121 [00:00<00:00, 3857.33 it/sec, obj=-6]
INFO - 08:35:47: 21%|██▏ | 26/121 [00:00<00:00, 3849.75 it/sec, obj=-5]
INFO - 08:35:47: 22%|██▏ | 27/121 [00:00<00:00, 3843.28 it/sec, obj=-4]
INFO - 08:35:47: 23%|██▎ | 28/121 [00:00<00:00, 3838.05 it/sec, obj=-3]
INFO - 08:35:47: 24%|██▍ | 29/121 [00:00<00:00, 3822.71 it/sec, obj=-2]
INFO - 08:35:47: 25%|██▍ | 30/121 [00:00<00:00, 3814.97 it/sec, obj=-1]
INFO - 08:35:47: 26%|██▌ | 31/121 [00:00<00:00, 3814.57 it/sec, obj=0]
INFO - 08:35:47: 26%|██▋ | 32/121 [00:00<00:00, 3815.82 it/sec, obj=1]
INFO - 08:35:47: 27%|██▋ | 33/121 [00:00<00:00, 3817.95 it/sec, obj=2]
INFO - 08:35:47: 28%|██▊ | 34/121 [00:00<00:00, 3819.44 it/sec, obj=-7]
INFO - 08:35:47: 29%|██▉ | 35/121 [00:00<00:00, 3820.45 it/sec, obj=-6]
INFO - 08:35:47: 30%|██▉ | 36/121 [00:00<00:00, 3822.37 it/sec, obj=-5]
INFO - 08:35:47: 31%|███ | 37/121 [00:00<00:00, 3824.47 it/sec, obj=-4]
INFO - 08:35:47: 31%|███▏ | 38/121 [00:00<00:00, 3825.73 it/sec, obj=-3]
INFO - 08:35:47: 32%|███▏ | 39/121 [00:00<00:00, 3827.37 it/sec, obj=-2]
INFO - 08:35:47: 33%|███▎ | 40/121 [00:00<00:00, 3829.02 it/sec, obj=-1]
INFO - 08:35:47: 34%|███▍ | 41/121 [00:00<00:00, 3830.76 it/sec, obj=0]
INFO - 08:35:47: 35%|███▍ | 42/121 [00:00<00:00, 3833.00 it/sec, obj=1]
INFO - 08:35:47: 36%|███▌ | 43/121 [00:00<00:00, 3820.84 it/sec, obj=2]
INFO - 08:35:47: 36%|███▋ | 44/121 [00:00<00:00, 3820.98 it/sec, obj=3]
INFO - 08:35:47: 37%|███▋ | 45/121 [00:00<00:00, 3803.17 it/sec, obj=-6]
INFO - 08:35:47: 38%|███▊ | 46/121 [00:00<00:00, 3802.56 it/sec, obj=-5]
INFO - 08:35:47: 39%|███▉ | 47/121 [00:00<00:00, 3803.22 it/sec, obj=-4]
INFO - 08:35:47: 40%|███▉ | 48/121 [00:00<00:00, 3804.50 it/sec, obj=-3]
INFO - 08:35:47: 40%|████ | 49/121 [00:00<00:00, 3770.82 it/sec, obj=-2]
INFO - 08:35:47: 41%|████▏ | 50/121 [00:00<00:00, 3768.47 it/sec, obj=-1]
INFO - 08:35:47: 42%|████▏ | 51/121 [00:00<00:00, 3770.06 it/sec, obj=0]
INFO - 08:35:47: 43%|████▎ | 52/121 [00:00<00:00, 3771.99 it/sec, obj=1]
INFO - 08:35:47: 44%|████▍ | 53/121 [00:00<00:00, 3773.65 it/sec, obj=2]
INFO - 08:35:47: 45%|████▍ | 54/121 [00:00<00:00, 3776.76 it/sec, obj=3]
INFO - 08:35:47: 45%|████▌ | 55/121 [00:00<00:00, 3775.81 it/sec, obj=4]
INFO - 08:35:47: 46%|████▋ | 56/121 [00:00<00:00, 3777.80 it/sec, obj=-5]
INFO - 08:35:47: 47%|████▋ | 57/121 [00:00<00:00, 3773.64 it/sec, obj=-4]
INFO - 08:35:47: 48%|████▊ | 58/121 [00:00<00:00, 3773.55 it/sec, obj=-3]
INFO - 08:35:47: 49%|████▉ | 59/121 [00:00<00:00, 3767.03 it/sec, obj=-2]
INFO - 08:35:47: 50%|████▉ | 60/121 [00:00<00:00, 3766.94 it/sec, obj=-1]
INFO - 08:35:47: 50%|█████ | 61/121 [00:00<00:00, 3768.41 it/sec, obj=0]
INFO - 08:35:47: 51%|█████ | 62/121 [00:00<00:00, 3770.00 it/sec, obj=1]
INFO - 08:35:47: 52%|█████▏ | 63/121 [00:00<00:00, 3771.75 it/sec, obj=2]
INFO - 08:35:47: 53%|█████▎ | 64/121 [00:00<00:00, 3773.55 it/sec, obj=3]
INFO - 08:35:47: 54%|█████▎ | 65/121 [00:00<00:00, 3775.15 it/sec, obj=4]
INFO - 08:35:47: 55%|█████▍ | 66/121 [00:00<00:00, 3776.49 it/sec, obj=5]
INFO - 08:35:47: 55%|█████▌ | 67/121 [00:00<00:00, 3777.64 it/sec, obj=-4]
INFO - 08:35:47: 56%|█████▌ | 68/121 [00:00<00:00, 3779.05 it/sec, obj=-3]
INFO - 08:35:47: 57%|█████▋ | 69/121 [00:00<00:00, 3781.61 it/sec, obj=-2]
INFO - 08:35:47: 58%|█████▊ | 70/121 [00:00<00:00, 3789.04 it/sec, obj=-1]
INFO - 08:35:47: 59%|█████▊ | 71/121 [00:00<00:00, 3796.67 it/sec, obj=0]
INFO - 08:35:47: 60%|█████▉ | 72/121 [00:00<00:00, 3803.73 it/sec, obj=1]
INFO - 08:35:47: 60%|██████ | 73/121 [00:00<00:00, 3797.96 it/sec, obj=2]
INFO - 08:35:47: 61%|██████ | 74/121 [00:00<00:00, 3798.44 it/sec, obj=3]
INFO - 08:35:47: 62%|██████▏ | 75/121 [00:00<00:00, 3792.64 it/sec, obj=4]
INFO - 08:35:47: 63%|██████▎ | 76/121 [00:00<00:00, 3793.09 it/sec, obj=5]
INFO - 08:35:47: 64%|██████▎ | 77/121 [00:00<00:00, 3794.41 it/sec, obj=6]
INFO - 08:35:47: 64%|██████▍ | 78/121 [00:00<00:00, 3796.28 it/sec, obj=-3]
INFO - 08:35:47: 65%|██████▌ | 79/121 [00:00<00:00, 3795.71 it/sec, obj=-2]
INFO - 08:35:47: 66%|██████▌ | 80/121 [00:00<00:00, 3797.00 it/sec, obj=-1]
INFO - 08:35:47: 67%|██████▋ | 81/121 [00:00<00:00, 3797.91 it/sec, obj=0]
INFO - 08:35:47: 68%|██████▊ | 82/121 [00:00<00:00, 3798.73 it/sec, obj=1]
INFO - 08:35:47: 69%|██████▊ | 83/121 [00:00<00:00, 3799.27 it/sec, obj=2]
INFO - 08:35:47: 69%|██████▉ | 84/121 [00:00<00:00, 3800.17 it/sec, obj=3]
INFO - 08:35:47: 70%|███████ | 85/121 [00:00<00:00, 3801.21 it/sec, obj=4]
INFO - 08:35:47: 71%|███████ | 86/121 [00:00<00:00, 3801.75 it/sec, obj=5]
INFO - 08:35:47: 72%|███████▏ | 87/121 [00:00<00:00, 3802.75 it/sec, obj=6]
INFO - 08:35:47: 73%|███████▎ | 88/121 [00:00<00:00, 3800.71 it/sec, obj=7]
INFO - 08:35:47: 74%|███████▎ | 89/121 [00:00<00:00, 3800.89 it/sec, obj=-2]
INFO - 08:35:47: 74%|███████▍ | 90/121 [00:00<00:00, 3796.70 it/sec, obj=-1]
INFO - 08:35:47: 75%|███████▌ | 91/121 [00:00<00:00, 3796.92 it/sec, obj=0]
INFO - 08:35:47: 76%|███████▌ | 92/121 [00:00<00:00, 3797.77 it/sec, obj=1]
INFO - 08:35:47: 77%|███████▋ | 93/121 [00:00<00:00, 3798.74 it/sec, obj=2]
INFO - 08:35:47: 78%|███████▊ | 94/121 [00:00<00:00, 3800.25 it/sec, obj=3]
INFO - 08:35:47: 79%|███████▊ | 95/121 [00:00<00:00, 3805.83 it/sec, obj=4]
INFO - 08:35:47: 79%|███████▉ | 96/121 [00:00<00:00, 3811.42 it/sec, obj=5]
INFO - 08:35:47: 80%|████████ | 97/121 [00:00<00:00, 3816.83 it/sec, obj=6]
INFO - 08:35:47: 81%|████████ | 98/121 [00:00<00:00, 3822.19 it/sec, obj=7]
INFO - 08:35:47: 82%|████████▏ | 99/121 [00:00<00:00, 3827.55 it/sec, obj=8]
INFO - 08:35:47: 83%|████████▎ | 100/121 [00:00<00:00, 3826.57 it/sec, obj=-1]
INFO - 08:35:47: 83%|████████▎ | 101/121 [00:00<00:00, 3827.27 it/sec, obj=0]
INFO - 08:35:47: 84%|████████▍ | 102/121 [00:00<00:00, 3828.26 it/sec, obj=1]
INFO - 08:35:47: 85%|████████▌ | 103/121 [00:00<00:00, 3828.85 it/sec, obj=2]
INFO - 08:35:47: 86%|████████▌ | 104/121 [00:00<00:00, 3826.32 it/sec, obj=3]
INFO - 08:35:47: 87%|████████▋ | 105/121 [00:00<00:00, 3826.32 it/sec, obj=4]
INFO - 08:35:47: 88%|████████▊ | 106/121 [00:00<00:00, 3821.85 it/sec, obj=5]
INFO - 08:35:47: 88%|████████▊ | 107/121 [00:00<00:00, 3825.91 it/sec, obj=6]
INFO - 08:35:47: 89%|████████▉ | 108/121 [00:00<00:00, 3830.64 it/sec, obj=7]
INFO - 08:35:47: 90%|█████████ | 109/121 [00:00<00:00, 3829.29 it/sec, obj=8]
INFO - 08:35:47: 91%|█████████ | 110/121 [00:00<00:00, 3829.84 it/sec, obj=9]
INFO - 08:35:47: 92%|█████████▏| 111/121 [00:00<00:00, 3830.48 it/sec, obj=0]
INFO - 08:35:47: 93%|█████████▎| 112/121 [00:00<00:00, 3830.95 it/sec, obj=1]
INFO - 08:35:47: 93%|█████████▎| 113/121 [00:00<00:00, 3831.44 it/sec, obj=2]
INFO - 08:35:47: 94%|█████████▍| 114/121 [00:00<00:00, 3832.26 it/sec, obj=3]
INFO - 08:35:47: 95%|█████████▌| 115/121 [00:00<00:00, 3833.31 it/sec, obj=4]
INFO - 08:35:47: 96%|█████████▌| 116/121 [00:00<00:00, 3833.80 it/sec, obj=5]
INFO - 08:35:47: 97%|█████████▋| 117/121 [00:00<00:00, 3834.22 it/sec, obj=6]
INFO - 08:35:47: 98%|█████████▊| 118/121 [00:00<00:00, 3834.69 it/sec, obj=7]
INFO - 08:35:47: 98%|█████████▊| 119/121 [00:00<00:00, 3833.36 it/sec, obj=8]
INFO - 08:35:47: 99%|█████████▉| 120/121 [00:00<00:00, 3836.55 it/sec, obj=9]
INFO - 08:35:47: 100%|██████████| 121/121 [00:00<00:00, 3840.24 it/sec, obj=10]
INFO - 08:35:47: Optimization result:
INFO - 08:35:47: Optimizer info:
INFO - 08:35:47: Status: None
INFO - 08:35:47: Message: None
INFO - 08:35:47: Number of calls to the objective function by the optimizer: 121
INFO - 08:35:47: Solution:
INFO - 08:35:47: Objective: -10.0
INFO - 08:35:47: Design space:
INFO - 08:35:47: +------+-------------+-------+-------------+---------+
INFO - 08:35:47: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:35:47: +------+-------------+-------+-------------+---------+
INFO - 08:35:47: | x[0] | -5 | -5 | 5 | integer |
INFO - 08:35:47: | x[1] | -5 | -5 | 5 | integer |
INFO - 08:35:47: +------+-------------+-------+-------------+---------+
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 0x7f2514ea8310>
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.715 seconds)