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_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
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, 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:58:34: Optimization problem:
INFO - 08:58:34: minimize f = sum(x)
INFO - 08:58:34: with respect to x
INFO - 08:58:34: over the design space:
INFO - 08:58:34: +------+-------------+-------+-------------+---------+
INFO - 08:58:34: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:58:34: +------+-------------+-------+-------------+---------+
INFO - 08:58:34: | x[0] | -5 | None | 5 | integer |
INFO - 08:58:34: | x[1] | -5 | None | 5 | integer |
INFO - 08:58:34: +------+-------------+-------+-------------+---------+
INFO - 08:58:34: Solving optimization problem with algorithm fullfact:
INFO - 08:58:34: 1%| | 1/121 [00:00<00:00, 4173.44 it/sec, obj=-10]
INFO - 08:58:34: 2%|▏ | 2/121 [00:00<00:00, 3279.36 it/sec, obj=-9]
INFO - 08:58:34: 2%|▏ | 3/121 [00:00<00:00, 3264.90 it/sec, obj=-8]
INFO - 08:58:34: 3%|▎ | 4/121 [00:00<00:00, 3281.93 it/sec, obj=-7]
INFO - 08:58:34: 4%|▍ | 5/121 [00:00<00:00, 3248.38 it/sec, obj=-6]
INFO - 08:58:34: 5%|▍ | 6/121 [00:00<00:00, 3255.18 it/sec, obj=-5]
INFO - 08:58:34: 6%|▌ | 7/121 [00:00<00:00, 3279.00 it/sec, obj=-4]
INFO - 08:58:34: 7%|▋ | 8/121 [00:00<00:00, 3298.71 it/sec, obj=-3]
INFO - 08:58:34: 7%|▋ | 9/121 [00:00<00:00, 3314.78 it/sec, obj=-2]
INFO - 08:58:34: 8%|▊ | 10/121 [00:00<00:00, 3250.89 it/sec, obj=-1]
INFO - 08:58:34: 9%|▉ | 11/121 [00:00<00:00, 3266.82 it/sec, obj=0]
INFO - 08:58:34: 10%|▉ | 12/121 [00:00<00:00, 3280.43 it/sec, obj=-9]
INFO - 08:58:34: 11%|█ | 13/121 [00:00<00:00, 3295.22 it/sec, obj=-8]
INFO - 08:58:34: 12%|█▏ | 14/121 [00:00<00:00, 3308.00 it/sec, obj=-7]
INFO - 08:58:34: 12%|█▏ | 15/121 [00:00<00:00, 3317.93 it/sec, obj=-6]
INFO - 08:58:34: 13%|█▎ | 16/121 [00:00<00:00, 3327.66 it/sec, obj=-5]
INFO - 08:58:34: 14%|█▍ | 17/121 [00:00<00:00, 3335.82 it/sec, obj=-4]
INFO - 08:58:34: 15%|█▍ | 18/121 [00:00<00:00, 3342.07 it/sec, obj=-3]
INFO - 08:58:34: 16%|█▌ | 19/121 [00:00<00:00, 3333.97 it/sec, obj=-2]
INFO - 08:58:34: 17%|█▋ | 20/121 [00:00<00:00, 3338.48 it/sec, obj=-1]
INFO - 08:58:34: 17%|█▋ | 21/121 [00:00<00:00, 3344.11 it/sec, obj=0]
INFO - 08:58:34: 18%|█▊ | 22/121 [00:00<00:00, 3349.35 it/sec, obj=1]
INFO - 08:58:34: 19%|█▉ | 23/121 [00:00<00:00, 3327.55 it/sec, obj=-8]
INFO - 08:58:34: 20%|█▉ | 24/121 [00:00<00:00, 3322.66 it/sec, obj=-7]
INFO - 08:58:34: 21%|██ | 25/121 [00:00<00:00, 3326.17 it/sec, obj=-6]
INFO - 08:58:34: 21%|██▏ | 26/121 [00:00<00:00, 3331.15 it/sec, obj=-5]
INFO - 08:58:34: 22%|██▏ | 27/121 [00:00<00:00, 3335.58 it/sec, obj=-4]
INFO - 08:58:34: 23%|██▎ | 28/121 [00:00<00:00, 3339.70 it/sec, obj=-3]
INFO - 08:58:34: 24%|██▍ | 29/121 [00:00<00:00, 3344.28 it/sec, obj=-2]
INFO - 08:58:34: 25%|██▍ | 30/121 [00:00<00:00, 3348.03 it/sec, obj=-1]
INFO - 08:58:34: 26%|██▌ | 31/121 [00:00<00:00, 3351.21 it/sec, obj=0]
INFO - 08:58:34: 26%|██▋ | 32/121 [00:00<00:00, 3345.74 it/sec, obj=1]
INFO - 08:58:34: 27%|██▋ | 33/121 [00:00<00:00, 3345.71 it/sec, obj=2]
INFO - 08:58:34: 28%|██▊ | 34/121 [00:00<00:00, 3348.12 it/sec, obj=-7]
INFO - 08:58:34: 29%|██▉ | 35/121 [00:00<00:00, 3350.47 it/sec, obj=-6]
INFO - 08:58:34: 30%|██▉ | 36/121 [00:00<00:00, 3353.13 it/sec, obj=-5]
INFO - 08:58:34: 31%|███ | 37/121 [00:00<00:00, 3341.64 it/sec, obj=-4]
INFO - 08:58:34: 31%|███▏ | 38/121 [00:00<00:00, 3343.13 it/sec, obj=-3]
INFO - 08:58:34: 32%|███▏ | 39/121 [00:00<00:00, 3345.83 it/sec, obj=-2]
INFO - 08:58:34: 33%|███▎ | 40/121 [00:00<00:00, 3348.41 it/sec, obj=-1]
INFO - 08:58:34: 34%|███▍ | 41/121 [00:00<00:00, 3350.93 it/sec, obj=0]
INFO - 08:58:34: 35%|███▍ | 42/121 [00:00<00:00, 3353.85 it/sec, obj=1]
INFO - 08:58:34: 36%|███▌ | 43/121 [00:00<00:00, 3356.01 it/sec, obj=2]
INFO - 08:58:34: 36%|███▋ | 44/121 [00:00<00:00, 3358.31 it/sec, obj=3]
INFO - 08:58:34: 37%|███▋ | 45/121 [00:00<00:00, 3361.06 it/sec, obj=-6]
INFO - 08:58:34: 38%|███▊ | 46/121 [00:00<00:00, 3356.90 it/sec, obj=-5]
INFO - 08:58:34: 39%|███▉ | 47/121 [00:00<00:00, 3358.53 it/sec, obj=-4]
INFO - 08:58:34: 40%|███▉ | 48/121 [00:00<00:00, 3361.21 it/sec, obj=-3]
INFO - 08:58:34: 40%|████ | 49/121 [00:00<00:00, 3364.01 it/sec, obj=-2]
INFO - 08:58:34: 41%|████▏ | 50/121 [00:00<00:00, 3359.42 it/sec, obj=-1]
INFO - 08:58:34: 42%|████▏ | 51/121 [00:00<00:00, 3360.13 it/sec, obj=0]
INFO - 08:58:34: 43%|████▎ | 52/121 [00:00<00:00, 3362.17 it/sec, obj=1]
INFO - 08:58:34: 44%|████▍ | 53/121 [00:00<00:00, 3363.41 it/sec, obj=2]
INFO - 08:58:34: 45%|████▍ | 54/121 [00:00<00:00, 3365.41 it/sec, obj=3]
INFO - 08:58:34: 45%|████▌ | 55/121 [00:00<00:00, 3366.95 it/sec, obj=4]
INFO - 08:58:34: 46%|████▋ | 56/121 [00:00<00:00, 3368.77 it/sec, obj=-5]
INFO - 08:58:34: 47%|████▋ | 57/121 [00:00<00:00, 3370.63 it/sec, obj=-4]
INFO - 08:58:34: 48%|████▊ | 58/121 [00:00<00:00, 3372.94 it/sec, obj=-3]
INFO - 08:58:34: 49%|████▉ | 59/121 [00:00<00:00, 3371.63 it/sec, obj=-2]
INFO - 08:58:34: 50%|████▉ | 60/121 [00:00<00:00, 3372.85 it/sec, obj=-1]
INFO - 08:58:34: 50%|█████ | 61/121 [00:00<00:00, 3374.52 it/sec, obj=0]
INFO - 08:58:34: 51%|█████ | 62/121 [00:00<00:00, 3348.92 it/sec, obj=1]
INFO - 08:58:34: 52%|█████▏ | 63/121 [00:00<00:00, 3310.13 it/sec, obj=2]
INFO - 08:58:34: 53%|█████▎ | 64/121 [00:00<00:00, 3296.23 it/sec, obj=3]
INFO - 08:58:34: 54%|█████▎ | 65/121 [00:00<00:00, 3295.30 it/sec, obj=4]
INFO - 08:58:34: 55%|█████▍ | 66/121 [00:00<00:00, 3295.92 it/sec, obj=5]
INFO - 08:58:34: 55%|█████▌ | 67/121 [00:00<00:00, 3296.91 it/sec, obj=-4]
INFO - 08:58:34: 56%|█████▌ | 68/121 [00:00<00:00, 3298.82 it/sec, obj=-3]
INFO - 08:58:34: 57%|█████▋ | 69/121 [00:00<00:00, 3301.06 it/sec, obj=-2]
INFO - 08:58:34: 58%|█████▊ | 70/121 [00:00<00:00, 3303.23 it/sec, obj=-1]
INFO - 08:58:34: 59%|█████▊ | 71/121 [00:00<00:00, 3302.20 it/sec, obj=0]
INFO - 08:58:34: 60%|█████▉ | 72/121 [00:00<00:00, 3303.00 it/sec, obj=1]
INFO - 08:58:34: 60%|██████ | 73/121 [00:00<00:00, 3305.06 it/sec, obj=2]
INFO - 08:58:34: 61%|██████ | 74/121 [00:00<00:00, 3307.56 it/sec, obj=3]
INFO - 08:58:34: 62%|██████▏ | 75/121 [00:00<00:00, 3309.27 it/sec, obj=4]
INFO - 08:58:34: 63%|██████▎ | 76/121 [00:00<00:00, 3304.14 it/sec, obj=5]
INFO - 08:58:34: 64%|██████▎ | 77/121 [00:00<00:00, 3304.63 it/sec, obj=6]
INFO - 08:58:34: 64%|██████▍ | 78/121 [00:00<00:00, 3306.27 it/sec, obj=-3]
INFO - 08:58:34: 65%|██████▌ | 79/121 [00:00<00:00, 3307.91 it/sec, obj=-2]
INFO - 08:58:34: 66%|██████▌ | 80/121 [00:00<00:00, 3309.93 it/sec, obj=-1]
INFO - 08:58:34: 67%|██████▋ | 81/121 [00:00<00:00, 3311.84 it/sec, obj=0]
INFO - 08:58:34: 68%|██████▊ | 82/121 [00:00<00:00, 3313.64 it/sec, obj=1]
INFO - 08:58:34: 69%|██████▊ | 83/121 [00:00<00:00, 3315.40 it/sec, obj=2]
INFO - 08:58:34: 69%|██████▉ | 84/121 [00:00<00:00, 3317.43 it/sec, obj=3]
INFO - 08:58:34: 70%|███████ | 85/121 [00:00<00:00, 3292.78 it/sec, obj=4]
INFO - 08:58:34: 71%|███████ | 86/121 [00:00<00:00, 3284.20 it/sec, obj=5]
INFO - 08:58:34: 72%|███████▏ | 87/121 [00:00<00:00, 3262.77 it/sec, obj=6]
INFO - 08:58:34: 73%|███████▎ | 88/121 [00:00<00:00, 3247.36 it/sec, obj=7]
INFO - 08:58:34: 74%|███████▎ | 89/121 [00:00<00:00, 3246.99 it/sec, obj=-2]
INFO - 08:58:34: 74%|███████▍ | 90/121 [00:00<00:00, 3248.40 it/sec, obj=-1]
INFO - 08:58:34: 75%|███████▌ | 91/121 [00:00<00:00, 3250.26 it/sec, obj=0]
INFO - 08:58:34: 76%|███████▌ | 92/121 [00:00<00:00, 3252.14 it/sec, obj=1]
INFO - 08:58:34: 77%|███████▋ | 93/121 [00:00<00:00, 3253.84 it/sec, obj=2]
INFO - 08:58:34: 78%|███████▊ | 94/121 [00:00<00:00, 3255.88 it/sec, obj=3]
INFO - 08:58:34: 79%|███████▊ | 95/121 [00:00<00:00, 3257.83 it/sec, obj=4]
INFO - 08:58:34: 79%|███████▉ | 96/121 [00:00<00:00, 3258.27 it/sec, obj=5]
INFO - 08:58:34: 80%|████████ | 97/121 [00:00<00:00, 3258.93 it/sec, obj=6]
INFO - 08:58:34: 81%|████████ | 98/121 [00:00<00:00, 3260.74 it/sec, obj=7]
INFO - 08:58:34: 82%|████████▏ | 99/121 [00:00<00:00, 3262.59 it/sec, obj=8]
INFO - 08:58:34: 83%|████████▎ | 100/121 [00:00<00:00, 3264.56 it/sec, obj=-1]
INFO - 08:58:34: 83%|████████▎ | 101/121 [00:00<00:00, 3261.39 it/sec, obj=0]
INFO - 08:58:34: 84%|████████▍ | 102/121 [00:00<00:00, 3262.53 it/sec, obj=1]
INFO - 08:58:34: 85%|████████▌ | 103/121 [00:00<00:00, 3264.07 it/sec, obj=2]
INFO - 08:58:34: 86%|████████▌ | 104/121 [00:00<00:00, 3265.86 it/sec, obj=3]
INFO - 08:58:34: 87%|████████▋ | 105/121 [00:00<00:00, 3267.71 it/sec, obj=4]
INFO - 08:58:34: 88%|████████▊ | 106/121 [00:00<00:00, 3269.43 it/sec, obj=5]
INFO - 08:58:34: 88%|████████▊ | 107/121 [00:00<00:00, 3271.26 it/sec, obj=6]
INFO - 08:58:34: 89%|████████▉ | 108/121 [00:00<00:00, 3273.04 it/sec, obj=7]
INFO - 08:58:34: 90%|█████████ | 109/121 [00:00<00:00, 3274.71 it/sec, obj=8]
INFO - 08:58:34: 91%|█████████ | 110/121 [00:00<00:00, 3274.20 it/sec, obj=9]
INFO - 08:58:34: 92%|█████████▏| 111/121 [00:00<00:00, 3275.28 it/sec, obj=0]
INFO - 08:58:34: 93%|█████████▎| 112/121 [00:00<00:00, 3259.00 it/sec, obj=1]
INFO - 08:58:34: 93%|█████████▎| 113/121 [00:00<00:00, 3241.44 it/sec, obj=2]
INFO - 08:58:34: 94%|█████████▍| 114/121 [00:00<00:00, 3239.24 it/sec, obj=3]
INFO - 08:58:34: 95%|█████████▌| 115/121 [00:00<00:00, 3239.80 it/sec, obj=4]
INFO - 08:58:34: 96%|█████████▌| 116/121 [00:00<00:00, 3240.70 it/sec, obj=5]
INFO - 08:58:34: 97%|█████████▋| 117/121 [00:00<00:00, 3241.80 it/sec, obj=6]
INFO - 08:58:34: 98%|█████████▊| 118/121 [00:00<00:00, 3243.05 it/sec, obj=7]
INFO - 08:58:34: 98%|█████████▊| 119/121 [00:00<00:00, 3243.98 it/sec, obj=8]
INFO - 08:58:34: 99%|█████████▉| 120/121 [00:00<00:00, 3245.13 it/sec, obj=9]
INFO - 08:58:34: 100%|██████████| 121/121 [00:00<00:00, 3240.17 it/sec, obj=10]
INFO - 08:58:34: Optimization result:
INFO - 08:58:34: Optimizer info:
INFO - 08:58:34: Status: None
INFO - 08:58:34: Message: None
INFO - 08:58:34: Number of calls to the objective function by the optimizer: 121
INFO - 08:58:34: Solution:
INFO - 08:58:34: Objective: -10.0
INFO - 08:58:34: Design space:
INFO - 08:58:34: +------+-------------+-------+-------------+---------+
INFO - 08:58:34: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:58:34: +------+-------------+-------+-------------+---------+
INFO - 08:58:34: | x[0] | -5 | -5 | 5 | integer |
INFO - 08:58:34: | x[1] | -5 | -5 | 5 | integer |
INFO - 08:58:34: +------+-------------+-------+-------------+---------+
Post-process the results¶
execute_post(
problem,
"ScatterPlotMatrix",
variable_names=["x", "f"],
save=False,
show=True,
)
<gemseo.post.scatter_mat.ScatterPlotMatrix object at 0x7f8b0abd1ca0>
Note that you can get all the optimization algorithms names:
DOEFactory().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', 'Halton', 'LHS', 'MC', 'PoissonDisk', 'Sobol']
Total running time of the script: (0 minutes 0.789 seconds)