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


## 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 - 14:16:15: Optimization problem:
INFO - 14:16:15:    minimize f = sum(x)
INFO - 14:16:15:    with respect to x
INFO - 14:16:15:    over the design space:
INFO - 14:16:15:       +------+-------------+-------+-------------+---------+
INFO - 14:16:15:       | Name | Lower bound | Value | Upper bound | Type    |
INFO - 14:16:15:       +------+-------------+-------+-------------+---------+
INFO - 14:16:15:       | x[0] |      -5     |  None |      5      | integer |
INFO - 14:16:15:       | x[1] |      -5     |  None |      5      | integer |
INFO - 14:16:15:       +------+-------------+-------+-------------+---------+
INFO - 14:16:15: Solving optimization problem with algorithm fullfact:
INFO - 14:16:15:      1%|          | 1/121 [00:00<00:00, 4253.86 it/sec, obj=-10]
INFO - 14:16:15:      2%|▏         | 2/121 [00:00<00:00, 3262.78 it/sec, obj=-9]
INFO - 14:16:15:      2%|▏         | 3/121 [00:00<00:00, 3221.43 it/sec, obj=-8]
INFO - 14:16:15:      3%|▎         | 4/121 [00:00<00:00, 3228.25 it/sec, obj=-7]
INFO - 14:16:15:      4%|▍         | 5/121 [00:00<00:00, 3241.35 it/sec, obj=-6]
INFO - 14:16:15:      5%|▍         | 6/121 [00:00<00:00, 3246.78 it/sec, obj=-5]
INFO - 14:16:15:      6%|▌         | 7/121 [00:00<00:00, 3252.84 it/sec, obj=-4]
INFO - 14:16:15:      7%|▋         | 8/121 [00:00<00:00, 3264.68 it/sec, obj=-3]
INFO - 14:16:15:      7%|▋         | 9/121 [00:00<00:00, 3293.67 it/sec, obj=-2]
INFO - 14:16:15:      8%|▊         | 10/121 [00:00<00:00, 3335.96 it/sec, obj=-1]
INFO - 14:16:15:      9%|▉         | 11/121 [00:00<00:00, 3324.73 it/sec, obj=0]
INFO - 14:16:15:     10%|▉         | 12/121 [00:00<00:00, 3292.88 it/sec, obj=-9]
INFO - 14:16:15:     11%|█         | 13/121 [00:00<00:00, 3284.70 it/sec, obj=-8]
INFO - 14:16:15:     12%|█▏        | 14/121 [00:00<00:00, 3286.70 it/sec, obj=-7]
INFO - 14:16:15:     12%|█▏        | 15/121 [00:00<00:00, 3274.41 it/sec, obj=-6]
INFO - 14:16:15:     13%|█▎        | 16/121 [00:00<00:00, 3268.98 it/sec, obj=-5]
INFO - 14:16:15:     14%|█▍        | 17/121 [00:00<00:00, 3253.77 it/sec, obj=-4]
INFO - 14:16:15:     15%|█▍        | 18/121 [00:00<00:00, 3233.02 it/sec, obj=-3]
INFO - 14:16:15:     16%|█▌        | 19/121 [00:00<00:00, 3228.87 it/sec, obj=-2]
INFO - 14:16:15:     17%|█▋        | 20/121 [00:00<00:00, 3230.24 it/sec, obj=-1]
INFO - 14:16:15:     17%|█▋        | 21/121 [00:00<00:00, 3227.10 it/sec, obj=0]
INFO - 14:16:15:     18%|█▊        | 22/121 [00:00<00:00, 3228.53 it/sec, obj=1]
INFO - 14:16:15:     19%|█▉        | 23/121 [00:00<00:00, 3230.28 it/sec, obj=-8]
INFO - 14:16:15:     20%|█▉        | 24/121 [00:00<00:00, 3233.75 it/sec, obj=-7]
INFO - 14:16:15:     21%|██        | 25/121 [00:00<00:00, 3214.03 it/sec, obj=-6]
INFO - 14:16:15:     21%|██▏       | 26/121 [00:00<00:00, 3215.16 it/sec, obj=-5]
INFO - 14:16:15:     22%|██▏       | 27/121 [00:00<00:00, 3228.50 it/sec, obj=-4]
INFO - 14:16:15:     23%|██▎       | 28/121 [00:00<00:00, 3228.87 it/sec, obj=-3]
INFO - 14:16:15:     24%|██▍       | 29/121 [00:00<00:00, 3232.82 it/sec, obj=-2]
INFO - 14:16:15:     25%|██▍       | 30/121 [00:00<00:00, 3232.94 it/sec, obj=-1]
INFO - 14:16:15:     26%|██▌       | 31/121 [00:00<00:00, 3231.04 it/sec, obj=0]
INFO - 14:16:15:     26%|██▋       | 32/121 [00:00<00:00, 3233.77 it/sec, obj=1]
INFO - 14:16:15:     27%|██▋       | 33/121 [00:00<00:00, 3242.87 it/sec, obj=2]
INFO - 14:16:15:     28%|██▊       | 34/121 [00:00<00:00, 3237.23 it/sec, obj=-7]
INFO - 14:16:15:     29%|██▉       | 35/121 [00:00<00:00, 3237.34 it/sec, obj=-6]
INFO - 14:16:15:     30%|██▉       | 36/121 [00:00<00:00, 3239.82 it/sec, obj=-5]
INFO - 14:16:15:     31%|███       | 37/121 [00:00<00:00, 3247.72 it/sec, obj=-4]
INFO - 14:16:15:     31%|███▏      | 38/121 [00:00<00:00, 3249.01 it/sec, obj=-3]
INFO - 14:16:15:     32%|███▏      | 39/121 [00:00<00:00, 3259.17 it/sec, obj=-2]
INFO - 14:16:15:     33%|███▎      | 40/121 [00:00<00:00, 3269.84 it/sec, obj=-1]
INFO - 14:16:15:     34%|███▍      | 41/121 [00:00<00:00, 3280.93 it/sec, obj=0]
INFO - 14:16:15:     35%|███▍      | 42/121 [00:00<00:00, 3290.88 it/sec, obj=1]
INFO - 14:16:15:     36%|███▌      | 43/121 [00:00<00:00, 3300.79 it/sec, obj=2]
INFO - 14:16:15:     36%|███▋      | 44/121 [00:00<00:00, 3310.30 it/sec, obj=3]
INFO - 14:16:15:     37%|███▋      | 45/121 [00:00<00:00, 3320.03 it/sec, obj=-6]
INFO - 14:16:15:     38%|███▊      | 46/121 [00:00<00:00, 3329.39 it/sec, obj=-5]
INFO - 14:16:15:     39%|███▉      | 47/121 [00:00<00:00, 3338.06 it/sec, obj=-4]
INFO - 14:16:15:     40%|███▉      | 48/121 [00:00<00:00, 3347.08 it/sec, obj=-3]
INFO - 14:16:15:     40%|████      | 49/121 [00:00<00:00, 3340.23 it/sec, obj=-2]
INFO - 14:16:15:     41%|████▏     | 50/121 [00:00<00:00, 3339.41 it/sec, obj=-1]
INFO - 14:16:15:     42%|████▏     | 51/121 [00:00<00:00, 3333.01 it/sec, obj=0]
INFO - 14:16:15:     43%|████▎     | 52/121 [00:00<00:00, 3325.11 it/sec, obj=1]
INFO - 14:16:15:     44%|████▍     | 53/121 [00:00<00:00, 3323.64 it/sec, obj=2]
INFO - 14:16:15:     45%|████▍     | 54/121 [00:00<00:00, 3329.06 it/sec, obj=3]
INFO - 14:16:15:     45%|████▌     | 55/121 [00:00<00:00, 3327.76 it/sec, obj=4]
INFO - 14:16:15:     46%|████▋     | 56/121 [00:00<00:00, 3328.77 it/sec, obj=-5]
INFO - 14:16:15:     47%|████▋     | 57/121 [00:00<00:00, 3329.42 it/sec, obj=-4]
INFO - 14:16:15:     48%|████▊     | 58/121 [00:00<00:00, 3330.41 it/sec, obj=-3]
INFO - 14:16:15:     49%|████▉     | 59/121 [00:00<00:00, 3331.01 it/sec, obj=-2]
INFO - 14:16:15:     50%|████▉     | 60/121 [00:00<00:00, 3334.68 it/sec, obj=-1]
INFO - 14:16:15:     50%|█████     | 61/121 [00:00<00:00, 3341.46 it/sec, obj=0]
INFO - 14:16:15:     51%|█████     | 62/121 [00:00<00:00, 3337.66 it/sec, obj=1]
INFO - 14:16:15:     52%|█████▏    | 63/121 [00:00<00:00, 3334.61 it/sec, obj=2]
INFO - 14:16:15:     53%|█████▎    | 64/121 [00:00<00:00, 3334.23 it/sec, obj=3]
INFO - 14:16:15:     54%|█████▎    | 65/121 [00:00<00:00, 3334.23 it/sec, obj=4]
INFO - 14:16:15:     55%|█████▍    | 66/121 [00:00<00:00, 3331.18 it/sec, obj=5]
INFO - 14:16:15:     55%|█████▌    | 67/121 [00:00<00:00, 3336.44 it/sec, obj=-4]
INFO - 14:16:15:     56%|█████▌    | 68/121 [00:00<00:00, 3334.49 it/sec, obj=-3]
INFO - 14:16:15:     57%|█████▋    | 69/121 [00:00<00:00, 3334.53 it/sec, obj=-2]
INFO - 14:16:15:     58%|█████▊    | 70/121 [00:00<00:00, 3334.33 it/sec, obj=-1]
INFO - 14:16:15:     59%|█████▊    | 71/121 [00:00<00:00, 3334.59 it/sec, obj=0]
INFO - 14:16:15:     60%|█████▉    | 72/121 [00:00<00:00, 3334.91 it/sec, obj=1]
INFO - 14:16:15:     60%|██████    | 73/121 [00:00<00:00, 3335.45 it/sec, obj=2]
INFO - 14:16:15:     61%|██████    | 74/121 [00:00<00:00, 3336.15 it/sec, obj=3]
INFO - 14:16:15:     62%|██████▏   | 75/121 [00:00<00:00, 3334.39 it/sec, obj=4]
INFO - 14:16:15:     63%|██████▎   | 76/121 [00:00<00:00, 3333.83 it/sec, obj=5]
INFO - 14:16:15:     64%|██████▎   | 77/121 [00:00<00:00, 3334.21 it/sec, obj=6]
INFO - 14:16:15:     64%|██████▍   | 78/121 [00:00<00:00, 3334.85 it/sec, obj=-3]
INFO - 14:16:15:     65%|██████▌   | 79/121 [00:00<00:00, 3330.39 it/sec, obj=-2]
INFO - 14:16:15:     66%|██████▌   | 80/121 [00:00<00:00, 3330.10 it/sec, obj=-1]
INFO - 14:16:15:     67%|██████▋   | 81/121 [00:00<00:00, 3330.22 it/sec, obj=0]
INFO - 14:16:15:     68%|██████▊   | 82/121 [00:00<00:00, 3330.65 it/sec, obj=1]
INFO - 14:16:15:     69%|██████▊   | 83/121 [00:00<00:00, 3332.76 it/sec, obj=2]
INFO - 14:16:15:     69%|██████▉   | 84/121 [00:00<00:00, 3337.48 it/sec, obj=3]
INFO - 14:16:15:     70%|███████   | 85/121 [00:00<00:00, 3318.37 it/sec, obj=4]
INFO - 14:16:15:     71%|███████   | 86/121 [00:00<00:00, 3321.06 it/sec, obj=5]
INFO - 14:16:15:     72%|███████▏  | 87/121 [00:00<00:00, 3319.79 it/sec, obj=6]
INFO - 14:16:15:     73%|███████▎  | 88/121 [00:00<00:00, 3318.10 it/sec, obj=7]
INFO - 14:16:15:     74%|███████▎  | 89/121 [00:00<00:00, 3317.69 it/sec, obj=-2]
INFO - 14:16:15:     74%|███████▍  | 90/121 [00:00<00:00, 3318.22 it/sec, obj=-1]
INFO - 14:16:15:     75%|███████▌  | 91/121 [00:00<00:00, 3318.94 it/sec, obj=0]
INFO - 14:16:15:     76%|███████▌  | 92/121 [00:00<00:00, 3315.09 it/sec, obj=1]
INFO - 14:16:15:     77%|███████▋  | 93/121 [00:00<00:00, 3314.73 it/sec, obj=2]
INFO - 14:16:15:     78%|███████▊  | 94/121 [00:00<00:00, 3317.69 it/sec, obj=3]
INFO - 14:16:15:     79%|███████▊  | 95/121 [00:00<00:00, 3322.12 it/sec, obj=4]
INFO - 14:16:15:     79%|███████▉  | 96/121 [00:00<00:00, 3321.86 it/sec, obj=5]
INFO - 14:16:15:     80%|████████  | 97/121 [00:00<00:00, 3322.21 it/sec, obj=6]
INFO - 14:16:15:     81%|████████  | 98/121 [00:00<00:00, 3322.65 it/sec, obj=7]
INFO - 14:16:15:     82%|████████▏ | 99/121 [00:00<00:00, 3323.19 it/sec, obj=8]
INFO - 14:16:15:     83%|████████▎ | 100/121 [00:00<00:00, 3323.51 it/sec, obj=-1]
INFO - 14:16:15:     83%|████████▎ | 101/121 [00:00<00:00, 3323.95 it/sec, obj=0]
INFO - 14:16:15:     84%|████████▍ | 102/121 [00:00<00:00, 3321.89 it/sec, obj=1]
INFO - 14:16:15:     85%|████████▌ | 103/121 [00:00<00:00, 3322.03 it/sec, obj=2]
INFO - 14:16:15:     86%|████████▌ | 104/121 [00:00<00:00, 3322.20 it/sec, obj=3]
INFO - 14:16:15:     87%|████████▋ | 105/121 [00:00<00:00, 3321.61 it/sec, obj=4]
INFO - 14:16:15:     88%|████████▊ | 106/121 [00:00<00:00, 3324.71 it/sec, obj=5]
INFO - 14:16:15:     88%|████████▊ | 107/121 [00:00<00:00, 3328.66 it/sec, obj=6]
INFO - 14:16:15:     89%|████████▉ | 108/121 [00:00<00:00, 3332.44 it/sec, obj=7]
INFO - 14:16:15:     90%|█████████ | 109/121 [00:00<00:00, 3335.93 it/sec, obj=8]
INFO - 14:16:15:     91%|█████████ | 110/121 [00:00<00:00, 3339.68 it/sec, obj=9]
INFO - 14:16:15:     92%|█████████▏| 111/121 [00:00<00:00, 3343.54 it/sec, obj=0]
INFO - 14:16:15:     93%|█████████▎| 112/121 [00:00<00:00, 3342.84 it/sec, obj=1]
INFO - 14:16:15:     93%|█████████▎| 113/121 [00:00<00:00, 3342.92 it/sec, obj=2]
INFO - 14:16:15:     94%|█████████▍| 114/121 [00:00<00:00, 3343.29 it/sec, obj=3]
INFO - 14:16:15:     95%|█████████▌| 115/121 [00:00<00:00, 3343.53 it/sec, obj=4]
INFO - 14:16:15:     96%|█████████▌| 116/121 [00:00<00:00, 3341.29 it/sec, obj=5]
INFO - 14:16:15:     97%|█████████▋| 117/121 [00:00<00:00, 3341.23 it/sec, obj=6]
INFO - 14:16:15:     98%|█████████▊| 118/121 [00:00<00:00, 3343.27 it/sec, obj=7]
INFO - 14:16:15:     98%|█████████▊| 119/121 [00:00<00:00, 3343.98 it/sec, obj=8]
INFO - 14:16:15:     99%|█████████▉| 120/121 [00:00<00:00, 3346.23 it/sec, obj=9]
INFO - 14:16:15:    100%|██████████| 121/121 [00:00<00:00, 3349.18 it/sec, obj=10]
INFO - 14:16:15: Optimization result:
INFO - 14:16:15:    Optimizer info:
INFO - 14:16:15:       Status: None
INFO - 14:16:15:       Message: None
INFO - 14:16:15:       Number of calls to the objective function by the optimizer: 121
INFO - 14:16:15:    Solution:
INFO - 14:16:15:       Objective: -10.0
INFO - 14:16:15:       Design space:
INFO - 14:16:15:          +------+-------------+-------+-------------+---------+
INFO - 14:16:15:          | Name | Lower bound | Value | Upper bound | Type    |
INFO - 14:16:15:          +------+-------------+-------+-------------+---------+
INFO - 14:16:15:          | x[0] |      -5     |   -5  |      5      | integer |
INFO - 14:16:15:          | x[1] |      -5     |   -5  |      5      | integer |
INFO - 14:16:15:          +------+-------------+-------+-------------+---------+

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 0x7f00d44703d0>


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

Gallery generated by Sphinx-Gallery