# 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 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()


## 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:23:05: Optimization problem:
INFO - 08:23:05:    minimize f = sum(x)
INFO - 08:23:05:    with respect to x
INFO - 08:23:05:    over the design space:
INFO - 08:23:05:    +------+-------------+-------+-------------+---------+
INFO - 08:23:05:    | name | lower_bound | value | upper_bound | type    |
INFO - 08:23:05:    +------+-------------+-------+-------------+---------+
INFO - 08:23:05:    | x[0] |      -5     |  None |      5      | integer |
INFO - 08:23:05:    | x[1] |      -5     |  None |      5      | integer |
INFO - 08:23:05:    +------+-------------+-------+-------------+---------+
INFO - 08:23:05: Solving optimization problem with algorithm fullfact:
INFO - 08:23:05: ...   0%|          | 0/121 [00:00<?, ?it]
INFO - 08:23:05: ...   1%|          | 1/121 [00:00<00:00, 4036.87 it/sec, obj=-10]
INFO - 08:23:05: ...   2%|▏         | 2/121 [00:00<00:00, 3316.97 it/sec, obj=-9]
INFO - 08:23:05: ...   2%|▏         | 3/121 [00:00<00:00, 3184.74 it/sec, obj=-8]
INFO - 08:23:05: ...   3%|▎         | 4/121 [00:00<00:00, 3151.83 it/sec, obj=-7]
INFO - 08:23:05: ...   4%|▍         | 5/121 [00:00<00:00, 3037.59 it/sec, obj=-6]
INFO - 08:23:05: ...   5%|▍         | 6/121 [00:00<00:00, 3015.32 it/sec, obj=-5]
INFO - 08:23:05: ...   6%|▌         | 7/121 [00:00<00:00, 3026.51 it/sec, obj=-4]
INFO - 08:23:05: ...   7%|▋         | 8/121 [00:00<00:00, 3021.29 it/sec, obj=-3]
INFO - 08:23:05: ...   7%|▋         | 9/121 [00:00<00:00, 3023.53 it/sec, obj=-2]
INFO - 08:23:05: ...   8%|▊         | 10/121 [00:00<00:00, 2927.76 it/sec, obj=-1]
INFO - 08:23:05: ...   9%|▉         | 11/121 [00:00<00:00, 2933.83 it/sec, obj=0]
INFO - 08:23:05: ...  10%|▉         | 12/121 [00:00<00:00, 2946.30 it/sec, obj=-9]
INFO - 08:23:05: ...  11%|█         | 13/121 [00:00<00:00, 2959.02 it/sec, obj=-8]
INFO - 08:23:05: ...  12%|█▏        | 14/121 [00:00<00:00, 2971.22 it/sec, obj=-7]
INFO - 08:23:05: ...  12%|█▏        | 15/121 [00:00<00:00, 2980.74 it/sec, obj=-6]
INFO - 08:23:05: ...  13%|█▎        | 16/121 [00:00<00:00, 2989.79 it/sec, obj=-5]
INFO - 08:23:05: ...  14%|█▍        | 17/121 [00:00<00:00, 2978.41 it/sec, obj=-4]
INFO - 08:23:05: ...  15%|█▍        | 18/121 [00:00<00:00, 2982.44 it/sec, obj=-3]
INFO - 08:23:05: ...  16%|█▌        | 19/121 [00:00<00:00, 2989.08 it/sec, obj=-2]
INFO - 08:23:05: ...  17%|█▋        | 20/121 [00:00<00:00, 2986.44 it/sec, obj=-1]
INFO - 08:23:05: ...  17%|█▋        | 21/121 [00:00<00:00, 2990.13 it/sec, obj=0]
INFO - 08:23:05: ...  18%|█▊        | 22/121 [00:00<00:00, 2994.57 it/sec, obj=1]
INFO - 08:23:05: ...  19%|█▉        | 23/121 [00:00<00:00, 2998.26 it/sec, obj=-8]
INFO - 08:23:05: ...  20%|█▉        | 24/121 [00:00<00:00, 3002.90 it/sec, obj=-7]
INFO - 08:23:05: ...  21%|██        | 25/121 [00:00<00:00, 3007.96 it/sec, obj=-6]
INFO - 08:23:05: ...  21%|██▏       | 26/121 [00:00<00:00, 3013.07 it/sec, obj=-5]
INFO - 08:23:05: ...  22%|██▏       | 27/121 [00:00<00:00, 3017.40 it/sec, obj=-4]
INFO - 08:23:05: ...  23%|██▎       | 28/121 [00:00<00:00, 3020.90 it/sec, obj=-3]
INFO - 08:23:05: ...  24%|██▍       | 29/121 [00:00<00:00, 3008.98 it/sec, obj=-2]
INFO - 08:23:05: ...  25%|██▍       | 30/121 [00:00<00:00, 3006.74 it/sec, obj=-1]
INFO - 08:23:05: ...  26%|██▌       | 31/121 [00:00<00:00, 3008.97 it/sec, obj=0]
INFO - 08:23:05: ...  26%|██▋       | 32/121 [00:00<00:00, 3007.14 it/sec, obj=1]
INFO - 08:23:05: ...  27%|██▋       | 33/121 [00:00<00:00, 3008.04 it/sec, obj=2]
INFO - 08:23:05: ...  28%|██▊       | 34/121 [00:00<00:00, 3010.03 it/sec, obj=-7]
INFO - 08:23:05: ...  29%|██▉       | 35/121 [00:00<00:00, 3012.65 it/sec, obj=-6]
INFO - 08:23:05: ...  30%|██▉       | 36/121 [00:00<00:00, 3015.20 it/sec, obj=-5]
INFO - 08:23:05: ...  31%|███       | 37/121 [00:00<00:00, 3018.13 it/sec, obj=-4]
INFO - 08:23:05: ...  31%|███▏      | 38/121 [00:00<00:00, 3020.40 it/sec, obj=-3]
INFO - 08:23:05: ...  32%|███▏      | 39/121 [00:00<00:00, 3023.01 it/sec, obj=-2]
INFO - 08:23:05: ...  33%|███▎      | 40/121 [00:00<00:00, 3025.32 it/sec, obj=-1]
INFO - 08:23:05: ...  34%|███▍      | 41/121 [00:00<00:00, 3019.60 it/sec, obj=0]
INFO - 08:23:05: ...  35%|███▍      | 42/121 [00:00<00:00, 3018.62 it/sec, obj=1]
INFO - 08:23:05: ...  36%|███▌      | 43/121 [00:00<00:00, 3019.81 it/sec, obj=2]
INFO - 08:23:05: ...  36%|███▋      | 44/121 [00:00<00:00, 3021.98 it/sec, obj=3]
INFO - 08:23:05: ...  37%|███▋      | 45/121 [00:00<00:00, 3019.90 it/sec, obj=-6]
INFO - 08:23:05: ...  38%|███▊      | 46/121 [00:00<00:00, 3022.59 it/sec, obj=-5]
INFO - 08:23:05: ...  39%|███▉      | 47/121 [00:00<00:00, 3024.85 it/sec, obj=-4]
INFO - 08:23:05: ...  40%|███▉      | 48/121 [00:00<00:00, 3027.19 it/sec, obj=-3]
INFO - 08:23:05: ...  40%|████      | 49/121 [00:00<00:00, 3029.49 it/sec, obj=-2]
INFO - 08:23:05: ...  41%|████▏     | 50/121 [00:00<00:00, 3031.09 it/sec, obj=-1]
INFO - 08:23:05: ...  42%|████▏     | 51/121 [00:00<00:00, 3032.84 it/sec, obj=0]
INFO - 08:23:05: ...  43%|████▎     | 52/121 [00:00<00:00, 3034.87 it/sec, obj=1]
INFO - 08:23:05: ...  44%|████▍     | 53/121 [00:00<00:00, 3036.53 it/sec, obj=2]
INFO - 08:23:05: ...  45%|████▍     | 54/121 [00:00<00:00, 3032.15 it/sec, obj=3]
INFO - 08:23:05: ...  45%|████▌     | 55/121 [00:00<00:00, 3033.08 it/sec, obj=4]
INFO - 08:23:05: ...  46%|████▋     | 56/121 [00:00<00:00, 3026.66 it/sec, obj=-5]
INFO - 08:23:05: ...  47%|████▋     | 57/121 [00:00<00:00, 3023.32 it/sec, obj=-4]
INFO - 08:23:05: ...  48%|████▊     | 58/121 [00:00<00:00, 3024.39 it/sec, obj=-3]
INFO - 08:23:05: ...  49%|████▉     | 59/121 [00:00<00:00, 3026.01 it/sec, obj=-2]
INFO - 08:23:05: ...  50%|████▉     | 60/121 [00:00<00:00, 3027.87 it/sec, obj=-1]
INFO - 08:23:05: ...  50%|█████     | 61/121 [00:00<00:00, 3029.38 it/sec, obj=0]
INFO - 08:23:05: ...  51%|█████     | 62/121 [00:00<00:00, 3030.92 it/sec, obj=1]
INFO - 08:23:05: ...  52%|█████▏    | 63/121 [00:00<00:00, 3032.51 it/sec, obj=2]
INFO - 08:23:05: ...  53%|█████▎    | 64/121 [00:00<00:00, 3034.09 it/sec, obj=3]
INFO - 08:23:05: ...  54%|█████▎    | 65/121 [00:00<00:00, 3035.73 it/sec, obj=4]
INFO - 08:23:05: ...  55%|█████▍    | 66/121 [00:00<00:00, 3029.07 it/sec, obj=5]
INFO - 08:23:05: ...  55%|█████▌    | 67/121 [00:00<00:00, 3029.16 it/sec, obj=-4]
INFO - 08:23:05: ...  56%|█████▌    | 68/121 [00:00<00:00, 3030.21 it/sec, obj=-3]
INFO - 08:23:05: ...  57%|█████▋    | 69/121 [00:00<00:00, 3029.04 it/sec, obj=-2]
INFO - 08:23:05: ...  58%|█████▊    | 70/121 [00:00<00:00, 3029.82 it/sec, obj=-1]
INFO - 08:23:05: ...  59%|█████▊    | 71/121 [00:00<00:00, 3031.03 it/sec, obj=0]
INFO - 08:23:05: ...  60%|█████▉    | 72/121 [00:00<00:00, 3032.30 it/sec, obj=1]
INFO - 08:23:05: ...  60%|██████    | 73/121 [00:00<00:00, 3033.66 it/sec, obj=2]
INFO - 08:23:05: ...  61%|██████    | 74/121 [00:00<00:00, 3035.04 it/sec, obj=3]
INFO - 08:23:05: ...  62%|██████▏   | 75/121 [00:00<00:00, 3036.45 it/sec, obj=4]
INFO - 08:23:05: ...  63%|██████▎   | 76/121 [00:00<00:00, 3037.61 it/sec, obj=5]
INFO - 08:23:05: ...  64%|██████▎   | 77/121 [00:00<00:00, 3038.92 it/sec, obj=6]
INFO - 08:23:05: ...  64%|██████▍   | 78/121 [00:00<00:00, 3036.73 it/sec, obj=-3]
INFO - 08:23:05: ...  65%|██████▌   | 79/121 [00:00<00:00, 3037.23 it/sec, obj=-2]
INFO - 08:23:05: ...  66%|██████▌   | 80/121 [00:00<00:00, 3038.30 it/sec, obj=-1]
INFO - 08:23:05: ...  67%|██████▋   | 81/121 [00:00<00:00, 3037.48 it/sec, obj=0]
INFO - 08:23:05: ...  68%|██████▊   | 82/121 [00:00<00:00, 3037.82 it/sec, obj=1]
INFO - 08:23:05: ...  69%|██████▊   | 83/121 [00:00<00:00, 3038.95 it/sec, obj=2]
INFO - 08:23:05: ...  69%|██████▉   | 84/121 [00:00<00:00, 3040.01 it/sec, obj=3]
INFO - 08:23:05: ...  70%|███████   | 85/121 [00:00<00:00, 3041.06 it/sec, obj=4]
INFO - 08:23:05: ...  71%|███████   | 86/121 [00:00<00:00, 3042.12 it/sec, obj=5]
INFO - 08:23:05: ...  72%|███████▏  | 87/121 [00:00<00:00, 3043.23 it/sec, obj=6]
INFO - 08:23:05: ...  73%|███████▎  | 88/121 [00:00<00:00, 3044.14 it/sec, obj=7]
INFO - 08:23:05: ...  74%|███████▎  | 89/121 [00:00<00:00, 3044.98 it/sec, obj=-2]
INFO - 08:23:05: ...  74%|███████▍  | 90/121 [00:00<00:00, 3043.49 it/sec, obj=-1]
INFO - 08:23:05: ...  75%|███████▌  | 91/121 [00:00<00:00, 3043.47 it/sec, obj=0]
INFO - 08:23:05: ...  76%|███████▌  | 92/121 [00:00<00:00, 3044.41 it/sec, obj=1]
INFO - 08:23:05: ...  77%|███████▋  | 93/121 [00:00<00:00, 3045.21 it/sec, obj=2]
INFO - 08:23:05: ...  78%|███████▊  | 94/121 [00:00<00:00, 3043.88 it/sec, obj=3]
INFO - 08:23:05: ...  79%|███████▊  | 95/121 [00:00<00:00, 3044.53 it/sec, obj=4]
INFO - 08:23:05: ...  79%|███████▉  | 96/121 [00:00<00:00, 3045.47 it/sec, obj=5]
INFO - 08:23:05: ...  80%|████████  | 97/121 [00:00<00:00, 3046.15 it/sec, obj=6]
INFO - 08:23:05: ...  81%|████████  | 98/121 [00:00<00:00, 3046.60 it/sec, obj=7]
INFO - 08:23:05: ...  82%|████████▏ | 99/121 [00:00<00:00, 3047.27 it/sec, obj=8]
INFO - 08:23:05: ...  83%|████████▎ | 100/121 [00:00<00:00, 3048.03 it/sec, obj=-1]
INFO - 08:23:05: ...  83%|████████▎ | 101/121 [00:00<00:00, 3048.80 it/sec, obj=0]
INFO - 08:23:05: ...  84%|████████▍ | 102/121 [00:00<00:00, 3049.69 it/sec, obj=1]
INFO - 08:23:05: ...  85%|████████▌ | 103/121 [00:00<00:00, 3047.15 it/sec, obj=2]
INFO - 08:23:05: ...  86%|████████▌ | 104/121 [00:00<00:00, 3047.57 it/sec, obj=3]
INFO - 08:23:05: ...  87%|████████▋ | 105/121 [00:00<00:00, 3042.02 it/sec, obj=4]
INFO - 08:23:05: ...  88%|████████▊ | 106/121 [00:00<00:00, 3024.42 it/sec, obj=5]
INFO - 08:23:05: ...  88%|████████▊ | 107/121 [00:00<00:00, 3015.52 it/sec, obj=6]
INFO - 08:23:05: ...  89%|████████▉ | 108/121 [00:00<00:00, 3014.97 it/sec, obj=7]
INFO - 08:23:05: ...  90%|█████████ | 109/121 [00:00<00:00, 3015.14 it/sec, obj=8]
INFO - 08:23:05: ...  91%|█████████ | 110/121 [00:00<00:00, 3015.53 it/sec, obj=9]
INFO - 08:23:05: ...  92%|█████████▏| 111/121 [00:00<00:00, 3016.27 it/sec, obj=0]
INFO - 08:23:05: ...  93%|█████████▎| 112/121 [00:00<00:00, 3016.88 it/sec, obj=1]
INFO - 08:23:05: ...  93%|█████████▎| 113/121 [00:00<00:00, 3017.45 it/sec, obj=2]
INFO - 08:23:05: ...  94%|█████████▍| 114/121 [00:00<00:00, 3015.07 it/sec, obj=3]
INFO - 08:23:05: ...  95%|█████████▌| 115/121 [00:00<00:00, 3015.56 it/sec, obj=4]
INFO - 08:23:05: ...  96%|█████████▌| 116/121 [00:00<00:00, 3016.19 it/sec, obj=5]
INFO - 08:23:05: ...  97%|█████████▋| 117/121 [00:00<00:00, 3015.39 it/sec, obj=6]
INFO - 08:23:05: ...  98%|█████████▊| 118/121 [00:00<00:00, 3016.14 it/sec, obj=7]
INFO - 08:23:05: ...  98%|█████████▊| 119/121 [00:00<00:00, 3016.81 it/sec, obj=8]
INFO - 08:23:05: ...  99%|█████████▉| 120/121 [00:00<00:00, 3017.63 it/sec, obj=9]
INFO - 08:23:05: ... 100%|██████████| 121/121 [00:00<00:00, 3018.42 it/sec, obj=10]
INFO - 08:23:05: Optimization result:
INFO - 08:23:05:    Optimizer info:
INFO - 08:23:05:       Status: None
INFO - 08:23:05:       Message: None
INFO - 08:23:05:       Number of calls to the objective function by the optimizer: 121
INFO - 08:23:05:    Solution:
INFO - 08:23:05:       Objective: -10.0
INFO - 08:23:05:       Design space:
INFO - 08:23:05:       +------+-------------+-------+-------------+---------+
INFO - 08:23:05:       | name | lower_bound | value | upper_bound | type    |
INFO - 08:23:05:       +------+-------------+-------+-------------+---------+
INFO - 08:23:05:       | x[0] |      -5     |   -5  |      5      | integer |
INFO - 08:23:05:       | x[1] |      -5     |   -5  |      5      | integer |
INFO - 08:23:05:       +------+-------------+-------+-------------+---------+

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,
)

<gemseo.post.scatter_mat.ScatterPlotMatrix object at 0x7f0cfc70e670>


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']


Total running time of the script: (0 minutes 0.721 seconds)

Gallery generated by Sphinx-Gallery