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.algos.design_space import DesignSpace
from gemseo.algos.doe.doe_factory import DOEFactory
from gemseo.algos.opt_problem import OptimizationProblem
from gemseo.api import configure_logger
from gemseo.api import execute_post
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:00:06: Optimization problem:
    INFO - 16:00:06:    minimize f = sum(x)
    INFO - 16:00:06:    with respect to x
    INFO - 16:00:06:    over the design space:
    INFO - 16:00:06:    +------+-------------+-------+-------------+---------+
    INFO - 16:00:06:    | name | lower_bound | value | upper_bound | type    |
    INFO - 16:00:06:    +------+-------------+-------+-------------+---------+
    INFO - 16:00:06:    | x[0] |      -5     |  None |      5      | integer |
    INFO - 16:00:06:    | x[1] |      -5     |  None |      5      | integer |
    INFO - 16:00:06:    +------+-------------+-------+-------------+---------+
    INFO - 16:00:06: Solving optimization problem with algorithm fullfact:
    INFO - 16:00:06: ...   0%|          | 0/121 [00:00<?, ?it]
    INFO - 16:00:06: ...   1%|          | 1/121 [00:00<00:00, 4559.03 it/sec, obj=-10]
    INFO - 16:00:06: ...   2%|▏         | 2/121 [00:00<00:00, 3577.23 it/sec, obj=-9]
    INFO - 16:00:06: ...   2%|▏         | 3/121 [00:00<00:00, 3368.92 it/sec, obj=-8]
    INFO - 16:00:06: ...   3%|▎         | 4/121 [00:00<00:00, 3337.42 it/sec, obj=-7]
    INFO - 16:00:06: ...   4%|▍         | 5/121 [00:00<00:00, 3337.82 it/sec, obj=-6]
    INFO - 16:00:06: ...   5%|▍         | 6/121 [00:00<00:00, 3340.30 it/sec, obj=-5]
    INFO - 16:00:06: ...   6%|▌         | 7/121 [00:00<00:00, 3346.65 it/sec, obj=-4]
    INFO - 16:00:06: ...   7%|▋         | 8/121 [00:00<00:00, 3353.10 it/sec, obj=-3]
    INFO - 16:00:06: ...   7%|▋         | 9/121 [00:00<00:00, 3356.94 it/sec, obj=-2]
    INFO - 16:00:06: ...   8%|▊         | 10/121 [00:00<00:00, 3364.06 it/sec, obj=-1]
    INFO - 16:00:06: ...   9%|▉         | 11/121 [00:00<00:00, 3370.89 it/sec, obj=0]
    INFO - 16:00:06: ...  10%|▉         | 12/121 [00:00<00:00, 3376.15 it/sec, obj=-9]
    INFO - 16:00:06: ...  11%|█         | 13/121 [00:00<00:00, 3333.70 it/sec, obj=-8]
    INFO - 16:00:06: ...  12%|█▏        | 14/121 [00:00<00:00, 3330.32 it/sec, obj=-7]
    INFO - 16:00:06: ...  12%|█▏        | 15/121 [00:00<00:00, 3334.81 it/sec, obj=-6]
    INFO - 16:00:06: ...  13%|█▎        | 16/121 [00:00<00:00, 3328.48 it/sec, obj=-5]
    INFO - 16:00:06: ...  14%|█▍        | 17/121 [00:00<00:00, 3329.75 it/sec, obj=-4]
    INFO - 16:00:06: ...  15%|█▍        | 18/121 [00:00<00:00, 3331.16 it/sec, obj=-3]
    INFO - 16:00:06: ...  16%|█▌        | 19/121 [00:00<00:00, 3335.36 it/sec, obj=-2]
    INFO - 16:00:06: ...  17%|█▋        | 20/121 [00:00<00:00, 3267.74 it/sec, obj=-1]
    INFO - 16:00:06: ...  17%|█▋        | 21/121 [00:00<00:00, 3266.23 it/sec, obj=0]
    INFO - 16:00:06: ...  18%|█▊        | 22/121 [00:00<00:00, 3268.79 it/sec, obj=1]
    INFO - 16:00:06: ...  19%|█▉        | 23/121 [00:00<00:00, 3273.69 it/sec, obj=-8]
    INFO - 16:00:06: ...  20%|█▉        | 24/121 [00:00<00:00, 3278.72 it/sec, obj=-7]
    INFO - 16:00:06: ...  21%|██        | 25/121 [00:00<00:00, 3282.54 it/sec, obj=-6]
    INFO - 16:00:06: ...  21%|██▏       | 26/121 [00:00<00:00, 3270.12 it/sec, obj=-5]
    INFO - 16:00:06: ...  22%|██▏       | 27/121 [00:00<00:00, 3274.43 it/sec, obj=-4]
    INFO - 16:00:06: ...  23%|██▎       | 28/121 [00:00<00:00, 3280.28 it/sec, obj=-3]
    INFO - 16:00:06: ...  24%|██▍       | 29/121 [00:00<00:00, 3279.36 it/sec, obj=-2]
    INFO - 16:00:06: ...  25%|██▍       | 30/121 [00:00<00:00, 3281.59 it/sec, obj=-1]
    INFO - 16:00:06: ...  26%|██▌       | 31/121 [00:00<00:00, 3285.58 it/sec, obj=0]
    INFO - 16:00:06: ...  26%|██▋       | 32/121 [00:00<00:00, 3289.89 it/sec, obj=1]
    INFO - 16:00:06: ...  27%|██▋       | 33/121 [00:00<00:00, 3294.50 it/sec, obj=2]
    INFO - 16:00:06: ...  28%|██▊       | 34/121 [00:00<00:00, 3298.17 it/sec, obj=-7]
    INFO - 16:00:06: ...  29%|██▉       | 35/121 [00:00<00:00, 3302.16 it/sec, obj=-6]
    INFO - 16:00:06: ...  30%|██▉       | 36/121 [00:00<00:00, 3305.49 it/sec, obj=-5]
    INFO - 16:00:06: ...  31%|███       | 37/121 [00:00<00:00, 3309.08 it/sec, obj=-4]
    INFO - 16:00:06: ...  31%|███▏      | 38/121 [00:00<00:00, 3312.00 it/sec, obj=-3]
    INFO - 16:00:06: ...  32%|███▏      | 39/121 [00:00<00:00, 3304.34 it/sec, obj=-2]
    INFO - 16:00:06: ...  33%|███▎      | 40/121 [00:00<00:00, 3302.21 it/sec, obj=-1]
    INFO - 16:00:06: ...  34%|███▍      | 41/121 [00:00<00:00, 3304.57 it/sec, obj=0]
    INFO - 16:00:06: ...  35%|███▍      | 42/121 [00:00<00:00, 3304.65 it/sec, obj=1]
    INFO - 16:00:06: ...  36%|███▌      | 43/121 [00:00<00:00, 3303.27 it/sec, obj=2]
    INFO - 16:00:06: ...  36%|███▋      | 44/121 [00:00<00:00, 3306.57 it/sec, obj=3]
    INFO - 16:00:06: ...  37%|███▋      | 45/121 [00:00<00:00, 3309.55 it/sec, obj=-6]
    INFO - 16:00:06: ...  38%|███▊      | 46/121 [00:00<00:00, 3311.73 it/sec, obj=-5]
    INFO - 16:00:06: ...  39%|███▉      | 47/121 [00:00<00:00, 3314.09 it/sec, obj=-4]
    INFO - 16:00:06: ...  40%|███▉      | 48/121 [00:00<00:00, 3316.80 it/sec, obj=-3]
    INFO - 16:00:06: ...  40%|████      | 49/121 [00:00<00:00, 3319.62 it/sec, obj=-2]
    INFO - 16:00:06: ...  41%|████▏     | 50/121 [00:00<00:00, 3321.80 it/sec, obj=-1]
    INFO - 16:00:06: ...  42%|████▏     | 51/121 [00:00<00:00, 3324.26 it/sec, obj=0]
    INFO - 16:00:06: ...  43%|████▎     | 52/121 [00:00<00:00, 3326.88 it/sec, obj=1]
    INFO - 16:00:06: ...  44%|████▍     | 53/121 [00:00<00:00, 3320.76 it/sec, obj=2]
    INFO - 16:00:06: ...  45%|████▍     | 54/121 [00:00<00:00, 3321.49 it/sec, obj=3]
    INFO - 16:00:06: ...  45%|████▌     | 55/121 [00:00<00:00, 3323.54 it/sec, obj=4]
    INFO - 16:00:06: ...  46%|████▋     | 56/121 [00:00<00:00, 3320.86 it/sec, obj=-5]
    INFO - 16:00:06: ...  47%|████▋     | 57/121 [00:00<00:00, 3322.43 it/sec, obj=-4]
    INFO - 16:00:06: ...  48%|████▊     | 58/121 [00:00<00:00, 3324.81 it/sec, obj=-3]
    INFO - 16:00:06: ...  49%|████▉     | 59/121 [00:00<00:00, 3326.98 it/sec, obj=-2]
    INFO - 16:00:06: ...  50%|████▉     | 60/121 [00:00<00:00, 3329.25 it/sec, obj=-1]
    INFO - 16:00:06: ...  50%|█████     | 61/121 [00:00<00:00, 3331.41 it/sec, obj=0]
    INFO - 16:00:06: ...  51%|█████     | 62/121 [00:00<00:00, 3333.51 it/sec, obj=1]
    INFO - 16:00:06: ...  52%|█████▏    | 63/121 [00:00<00:00, 3335.54 it/sec, obj=2]
    INFO - 16:00:06: ...  53%|█████▎    | 64/121 [00:00<00:00, 3337.50 it/sec, obj=3]
    INFO - 16:00:06: ...  54%|█████▎    | 65/121 [00:00<00:00, 3339.29 it/sec, obj=4]
    INFO - 16:00:06: ...  55%|█████▍    | 66/121 [00:00<00:00, 3336.03 it/sec, obj=5]
    INFO - 16:00:06: ...  55%|█████▌    | 67/121 [00:00<00:00, 3335.93 it/sec, obj=-4]
    INFO - 16:00:06: ...  56%|█████▌    | 68/121 [00:00<00:00, 3337.46 it/sec, obj=-3]
    INFO - 16:00:06: ...  57%|█████▋    | 69/121 [00:00<00:00, 3339.07 it/sec, obj=-2]
    INFO - 16:00:06: ...  58%|█████▊    | 70/121 [00:00<00:00, 3336.34 it/sec, obj=-1]
    INFO - 16:00:06: ...  59%|█████▊    | 71/121 [00:00<00:00, 3337.69 it/sec, obj=0]
    INFO - 16:00:06: ...  60%|█████▉    | 72/121 [00:00<00:00, 3339.23 it/sec, obj=1]
    INFO - 16:00:06: ...  60%|██████    | 73/121 [00:00<00:00, 3341.02 it/sec, obj=2]
    INFO - 16:00:06: ...  61%|██████    | 74/121 [00:00<00:00, 3342.58 it/sec, obj=3]
    INFO - 16:00:06: ...  62%|██████▏   | 75/121 [00:00<00:00, 3344.03 it/sec, obj=4]
    INFO - 16:00:06: ...  63%|██████▎   | 76/121 [00:00<00:00, 3345.16 it/sec, obj=5]
    INFO - 16:00:06: ...  64%|██████▎   | 77/121 [00:00<00:00, 3346.44 it/sec, obj=6]
    INFO - 16:00:06: ...  64%|██████▍   | 78/121 [00:00<00:00, 3347.65 it/sec, obj=-3]
    INFO - 16:00:06: ...  65%|██████▌   | 79/121 [00:00<00:00, 3349.07 it/sec, obj=-2]
    INFO - 16:00:06: ...  66%|██████▌   | 80/121 [00:00<00:00, 3345.97 it/sec, obj=-1]
    INFO - 16:00:06: ...  67%|██████▋   | 81/121 [00:00<00:00, 3346.65 it/sec, obj=0]
    INFO - 16:00:06: ...  68%|██████▊   | 82/121 [00:00<00:00, 3347.15 it/sec, obj=1]
    INFO - 16:00:06: ...  69%|██████▊   | 83/121 [00:00<00:00, 3346.32 it/sec, obj=2]
    INFO - 16:00:06: ...  69%|██████▉   | 84/121 [00:00<00:00, 3346.52 it/sec, obj=3]
    INFO - 16:00:06: ...  70%|███████   | 85/121 [00:00<00:00, 3347.63 it/sec, obj=4]
    INFO - 16:00:06: ...  71%|███████   | 86/121 [00:00<00:00, 3348.00 it/sec, obj=5]
    INFO - 16:00:06: ...  72%|███████▏  | 87/121 [00:00<00:00, 3349.31 it/sec, obj=6]
    INFO - 16:00:06: ...  73%|███████▎  | 88/121 [00:00<00:00, 3350.57 it/sec, obj=7]
    INFO - 16:00:06: ...  74%|███████▎  | 89/121 [00:00<00:00, 3352.16 it/sec, obj=-2]
    INFO - 16:00:06: ...  74%|███████▍  | 90/121 [00:00<00:00, 3353.03 it/sec, obj=-1]
    INFO - 16:00:06: ...  75%|███████▌  | 91/121 [00:00<00:00, 3354.26 it/sec, obj=0]
    INFO - 16:00:06: ...  76%|███████▌  | 92/121 [00:00<00:00, 3355.30 it/sec, obj=1]
    INFO - 16:00:06: ...  77%|███████▋  | 93/121 [00:00<00:00, 3353.34 it/sec, obj=2]
    INFO - 16:00:06: ...  78%|███████▊  | 94/121 [00:00<00:00, 3352.76 it/sec, obj=3]
    INFO - 16:00:06: ...  79%|███████▊  | 95/121 [00:00<00:00, 3353.47 it/sec, obj=4]
    INFO - 16:00:06: ...  79%|███████▉  | 96/121 [00:00<00:00, 3354.55 it/sec, obj=5]
    INFO - 16:00:06: ...  80%|████████  | 97/121 [00:00<00:00, 3353.04 it/sec, obj=6]
    INFO - 16:00:06: ...  81%|████████  | 98/121 [00:00<00:00, 3352.90 it/sec, obj=7]
    INFO - 16:00:06: ...  82%|████████▏ | 99/121 [00:00<00:00, 3353.76 it/sec, obj=8]
    INFO - 16:00:06: ...  83%|████████▎ | 100/121 [00:00<00:00, 3354.58 it/sec, obj=-1]
    INFO - 16:00:06: ...  83%|████████▎ | 101/121 [00:00<00:00, 3355.50 it/sec, obj=0]
    INFO - 16:00:06: ...  84%|████████▍ | 102/121 [00:00<00:00, 3356.47 it/sec, obj=1]
    INFO - 16:00:06: ...  85%|████████▌ | 103/121 [00:00<00:00, 3357.45 it/sec, obj=2]
    INFO - 16:00:06: ...  86%|████████▌ | 104/121 [00:00<00:00, 3358.21 it/sec, obj=3]
    INFO - 16:00:06: ...  87%|████████▋ | 105/121 [00:00<00:00, 3359.38 it/sec, obj=4]
    INFO - 16:00:06: ...  88%|████████▊ | 106/121 [00:00<00:00, 3360.13 it/sec, obj=5]
    INFO - 16:00:06: ...  88%|████████▊ | 107/121 [00:00<00:00, 3356.57 it/sec, obj=6]
    INFO - 16:00:06: ...  89%|████████▉ | 108/121 [00:00<00:00, 3357.03 it/sec, obj=7]
    INFO - 16:00:06: ...  90%|█████████ | 109/121 [00:00<00:00, 3357.56 it/sec, obj=8]
    INFO - 16:00:06: ...  91%|█████████ | 110/121 [00:00<00:00, 3357.01 it/sec, obj=9]
    INFO - 16:00:06: ...  92%|█████████▏| 111/121 [00:00<00:00, 3357.14 it/sec, obj=0]
    INFO - 16:00:06: ...  93%|█████████▎| 112/121 [00:00<00:00, 3357.67 it/sec, obj=1]
    INFO - 16:00:06: ...  93%|█████████▎| 113/121 [00:00<00:00, 3358.39 it/sec, obj=2]
    INFO - 16:00:06: ...  94%|█████████▍| 114/121 [00:00<00:00, 3359.17 it/sec, obj=3]
    INFO - 16:00:06: ...  95%|█████████▌| 115/121 [00:00<00:00, 3359.93 it/sec, obj=4]
    INFO - 16:00:06: ...  96%|█████████▌| 116/121 [00:00<00:00, 3360.77 it/sec, obj=5]
    INFO - 16:00:06: ...  97%|█████████▋| 117/121 [00:00<00:00, 3361.70 it/sec, obj=6]
    INFO - 16:00:06: ...  98%|█████████▊| 118/121 [00:00<00:00, 3362.26 it/sec, obj=7]
    INFO - 16:00:06: ...  98%|█████████▊| 119/121 [00:00<00:00, 3362.99 it/sec, obj=8]
    INFO - 16:00:06: ...  99%|█████████▉| 120/121 [00:00<00:00, 3354.33 it/sec, obj=9]
    INFO - 16:00:06: ... 100%|██████████| 121/121 [00:00<00:00, 3337.13 it/sec, obj=10]
    INFO - 16:00:06: Optimization result:
    INFO - 16:00:06:    Optimizer info:
    INFO - 16:00:06:       Status: None
    INFO - 16:00:06:       Message: None
    INFO - 16:00:06:       Number of calls to the objective function by the optimizer: 121
    INFO - 16:00:06:    Solution:
    INFO - 16:00:06:       Objective: -10.0
    INFO - 16:00:06:       Design space:
    INFO - 16:00:06:       +------+-------------+-------+-------------+---------+
    INFO - 16:00:06:       | name | lower_bound | value | upper_bound | type    |
    INFO - 16:00:06:       +------+-------------+-------+-------------+---------+
    INFO - 16:00:06:       | x[0] |      -5     |   -5  |      5      | integer |
    INFO - 16:00:06:       | x[1] |      -5     |   -5  |      5      | integer |
    INFO - 16:00: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,
)
plot simple opt 3
<gemseo.post.scatter_mat.ScatterPlotMatrix object at 0x7ff1aefc68e0>

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.681 seconds)

Gallery generated by Sphinx-Gallery