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


## 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:58:58: Optimization problem:
INFO - 16:58:58:    minimize f = sum(x)
INFO - 16:58:58:    with respect to x
INFO - 16:58:58:    over the design space:
INFO - 16:58:58:    +------+-------------+-------+-------------+---------+
INFO - 16:58:58:    | name | lower_bound | value | upper_bound | type    |
INFO - 16:58:58:    +------+-------------+-------+-------------+---------+
INFO - 16:58:58:    | x[0] |      -5     |  None |      5      | integer |
INFO - 16:58:58:    | x[1] |      -5     |  None |      5      | integer |
INFO - 16:58:58:    +------+-------------+-------+-------------+---------+
INFO - 16:58:58: Solving optimization problem with algorithm fullfact:
INFO - 16:58:58: ...   0%|          | 0/121 [00:00<?, ?it]
INFO - 16:58:58: ...   1%|          | 1/121 [00:00<00:00, 5302.53 it/sec, obj=-10]
INFO - 16:58:58: ...   2%|▏         | 2/121 [00:00<00:00, 3562.04 it/sec, obj=-9]
INFO - 16:58:58: ...   2%|▏         | 3/121 [00:00<00:00, 3557.51 it/sec, obj=-8]
INFO - 16:58:58: ...   3%|▎         | 4/121 [00:00<00:00, 3628.29 it/sec, obj=-7]
INFO - 16:58:58: ...   4%|▍         | 5/121 [00:00<00:00, 3686.33 it/sec, obj=-6]
INFO - 16:58:58: ...   5%|▍         | 6/121 [00:00<00:00, 3724.96 it/sec, obj=-5]
INFO - 16:58:58: ...   6%|▌         | 7/121 [00:00<00:00, 3756.89 it/sec, obj=-4]
INFO - 16:58:58: ...   7%|▋         | 8/121 [00:00<00:00, 3781.21 it/sec, obj=-3]
INFO - 16:58:58: ...   7%|▋         | 9/121 [00:00<00:00, 3804.93 it/sec, obj=-2]
INFO - 16:58:58: ...   8%|▊         | 10/121 [00:00<00:00, 3825.87 it/sec, obj=-1]
INFO - 16:58:58: ...   9%|▉         | 11/121 [00:00<00:00, 3843.82 it/sec, obj=0]
INFO - 16:58:58: ...  10%|▉         | 12/121 [00:00<00:00, 3858.01 it/sec, obj=-9]
INFO - 16:58:58: ...  11%|█         | 13/121 [00:00<00:00, 3814.60 it/sec, obj=-8]
INFO - 16:58:58: ...  12%|█▏        | 14/121 [00:00<00:00, 3780.84 it/sec, obj=-7]
INFO - 16:58:58: ...  12%|█▏        | 15/121 [00:00<00:00, 3757.44 it/sec, obj=-6]
INFO - 16:58:58: ...  13%|█▎        | 16/121 [00:00<00:00, 3738.03 it/sec, obj=-5]
INFO - 16:58:58: ...  14%|█▍        | 17/121 [00:00<00:00, 3685.11 it/sec, obj=-4]
INFO - 16:58:58: ...  15%|█▍        | 18/121 [00:00<00:00, 3682.80 it/sec, obj=-3]
INFO - 16:58:58: ...  16%|█▌        | 19/121 [00:00<00:00, 3621.53 it/sec, obj=-2]
INFO - 16:58:58: ...  17%|█▋        | 20/121 [00:00<00:00, 3626.88 it/sec, obj=-1]
INFO - 16:58:58: ...  17%|█▋        | 21/121 [00:00<00:00, 3640.74 it/sec, obj=0]
INFO - 16:58:58: ...  18%|█▊        | 22/121 [00:00<00:00, 3654.73 it/sec, obj=1]
INFO - 16:58:58: ...  19%|█▉        | 23/121 [00:00<00:00, 3668.72 it/sec, obj=-8]
INFO - 16:58:58: ...  20%|█▉        | 24/121 [00:00<00:00, 3682.17 it/sec, obj=-7]
INFO - 16:58:58: ...  21%|██        | 25/121 [00:00<00:00, 3693.08 it/sec, obj=-6]
INFO - 16:58:58: ...  21%|██▏       | 26/121 [00:00<00:00, 3703.96 it/sec, obj=-5]
INFO - 16:58:58: ...  22%|██▏       | 27/121 [00:00<00:00, 3698.68 it/sec, obj=-4]
INFO - 16:58:58: ...  23%|██▎       | 28/121 [00:00<00:00, 3684.99 it/sec, obj=-3]
INFO - 16:58:58: ...  24%|██▍       | 29/121 [00:00<00:00, 3675.32 it/sec, obj=-2]
INFO - 16:58:58: ...  25%|██▍       | 30/121 [00:00<00:00, 3666.46 it/sec, obj=-1]
INFO - 16:58:58: ...  26%|██▌       | 31/121 [00:00<00:00, 3654.09 it/sec, obj=0]
INFO - 16:58:58: ...  26%|██▋       | 32/121 [00:00<00:00, 3648.21 it/sec, obj=1]
INFO - 16:58:58: ...  27%|██▋       | 33/121 [00:00<00:00, 3656.66 it/sec, obj=2]
INFO - 16:58:58: ...  28%|██▊       | 34/121 [00:00<00:00, 3665.50 it/sec, obj=-7]
INFO - 16:58:58: ...  29%|██▉       | 35/121 [00:00<00:00, 3674.61 it/sec, obj=-6]
INFO - 16:58:58: ...  30%|██▉       | 36/121 [00:00<00:00, 3683.70 it/sec, obj=-5]
INFO - 16:58:58: ...  31%|███       | 37/121 [00:00<00:00, 3692.87 it/sec, obj=-4]
INFO - 16:58:58: ...  31%|███▏      | 38/121 [00:00<00:00, 3700.83 it/sec, obj=-3]
INFO - 16:58:58: ...  32%|███▏      | 39/121 [00:00<00:00, 3709.25 it/sec, obj=-2]
INFO - 16:58:58: ...  33%|███▎      | 40/121 [00:00<00:00, 3716.87 it/sec, obj=-1]
INFO - 16:58:58: ...  34%|███▍      | 41/121 [00:00<00:00, 3724.07 it/sec, obj=0]
INFO - 16:58:58: ...  35%|███▍      | 42/121 [00:00<00:00, 3726.54 it/sec, obj=1]
INFO - 16:58:58: ...  36%|███▌      | 43/121 [00:00<00:00, 3710.70 it/sec, obj=2]
INFO - 16:58:58: ...  36%|███▋      | 44/121 [00:00<00:00, 3704.84 it/sec, obj=3]
INFO - 16:58:58: ...  37%|███▋      | 45/121 [00:00<00:00, 3700.06 it/sec, obj=-6]
INFO - 16:58:58: ...  38%|███▊      | 46/121 [00:00<00:00, 3695.28 it/sec, obj=-5]
INFO - 16:58:58: ...  39%|███▉      | 47/121 [00:00<00:00, 3683.48 it/sec, obj=-4]
INFO - 16:58:58: ...  40%|███▉      | 48/121 [00:00<00:00, 3688.31 it/sec, obj=-3]
INFO - 16:58:58: ...  40%|████      | 49/121 [00:00<00:00, 3694.62 it/sec, obj=-2]
INFO - 16:58:58: ...  41%|████▏     | 50/121 [00:00<00:00, 3700.18 it/sec, obj=-1]
INFO - 16:58:58: ...  42%|████▏     | 51/121 [00:00<00:00, 3706.50 it/sec, obj=0]
INFO - 16:58:58: ...  43%|████▎     | 52/121 [00:00<00:00, 3712.91 it/sec, obj=1]
INFO - 16:58:58: ...  44%|████▍     | 53/121 [00:00<00:00, 3713.45 it/sec, obj=2]
INFO - 16:58:58: ...  45%|████▍     | 54/121 [00:00<00:00, 3714.82 it/sec, obj=3]
INFO - 16:58:58: ...  45%|████▌     | 55/121 [00:00<00:00, 3718.89 it/sec, obj=4]
INFO - 16:58:58: ...  46%|████▋     | 56/121 [00:00<00:00, 3723.90 it/sec, obj=-5]
INFO - 16:58:58: ...  47%|████▋     | 57/121 [00:00<00:00, 3708.26 it/sec, obj=-4]
INFO - 16:58:58: ...  48%|████▊     | 58/121 [00:00<00:00, 3697.11 it/sec, obj=-3]
INFO - 16:58:58: ...  49%|████▉     | 59/121 [00:00<00:00, 3691.56 it/sec, obj=-2]
INFO - 16:58:58: ...  50%|████▉     | 60/121 [00:00<00:00, 3687.46 it/sec, obj=-1]
INFO - 16:58:58: ...  50%|█████     | 61/121 [00:00<00:00, 3677.79 it/sec, obj=0]
INFO - 16:58:58: ...  51%|█████     | 62/121 [00:00<00:00, 3680.88 it/sec, obj=1]
INFO - 16:58:58: ...  52%|█████▏    | 63/121 [00:00<00:00, 3686.40 it/sec, obj=2]
INFO - 16:58:58: ...  53%|█████▎    | 64/121 [00:00<00:00, 3691.81 it/sec, obj=3]
INFO - 16:58:58: ...  54%|█████▎    | 65/121 [00:00<00:00, 3697.58 it/sec, obj=4]
INFO - 16:58:58: ...  55%|█████▍    | 66/121 [00:00<00:00, 3701.99 it/sec, obj=5]
INFO - 16:58:58: ...  55%|█████▌    | 67/121 [00:00<00:00, 3706.44 it/sec, obj=-4]
INFO - 16:58:58: ...  56%|█████▌    | 68/121 [00:00<00:00, 3711.29 it/sec, obj=-3]
INFO - 16:58:58: ...  57%|█████▋    | 69/121 [00:00<00:00, 3715.68 it/sec, obj=-2]
INFO - 16:58:58: ...  58%|█████▊    | 70/121 [00:00<00:00, 3719.96 it/sec, obj=-1]
INFO - 16:58:58: ...  59%|█████▊    | 71/121 [00:00<00:00, 3724.45 it/sec, obj=0]
INFO - 16:58:58: ...  60%|█████▉    | 72/121 [00:00<00:00, 3721.24 it/sec, obj=1]
INFO - 16:58:58: ...  60%|██████    | 73/121 [00:00<00:00, 3716.32 it/sec, obj=2]
INFO - 16:58:58: ...  61%|██████    | 74/121 [00:00<00:00, 3713.37 it/sec, obj=3]
INFO - 16:58:58: ...  62%|██████▏   | 75/121 [00:00<00:00, 3709.85 it/sec, obj=4]
INFO - 16:58:58: ...  63%|██████▎   | 76/121 [00:00<00:00, 3701.90 it/sec, obj=5]
INFO - 16:58:58: ...  64%|██████▎   | 77/121 [00:00<00:00, 3704.07 it/sec, obj=6]
INFO - 16:58:58: ...  64%|██████▍   | 78/121 [00:00<00:00, 3707.78 it/sec, obj=-3]
INFO - 16:58:58: ...  65%|██████▌   | 79/121 [00:00<00:00, 3711.77 it/sec, obj=-2]
INFO - 16:58:58: ...  66%|██████▌   | 80/121 [00:00<00:00, 3715.84 it/sec, obj=-1]
INFO - 16:58:58: ...  67%|██████▋   | 81/121 [00:00<00:00, 3719.54 it/sec, obj=0]
INFO - 16:58:58: ...  68%|██████▊   | 82/121 [00:00<00:00, 3722.90 it/sec, obj=1]
INFO - 16:58:58: ...  69%|██████▊   | 83/121 [00:00<00:00, 3726.79 it/sec, obj=2]
INFO - 16:58:58: ...  69%|██████▉   | 84/121 [00:00<00:00, 3730.68 it/sec, obj=3]
INFO - 16:58:58: ...  70%|███████   | 85/121 [00:00<00:00, 3734.48 it/sec, obj=4]
INFO - 16:58:58: ...  71%|███████   | 86/121 [00:00<00:00, 3737.62 it/sec, obj=5]
INFO - 16:58:58: ...  72%|███████▏  | 87/121 [00:00<00:00, 3739.23 it/sec, obj=6]
INFO - 16:58:58: ...  73%|███████▎  | 88/121 [00:00<00:00, 3732.38 it/sec, obj=7]
INFO - 16:58:58: ...  74%|███████▎  | 89/121 [00:00<00:00, 3729.28 it/sec, obj=-2]
INFO - 16:58:58: ...  74%|███████▍  | 90/121 [00:00<00:00, 3726.14 it/sec, obj=-1]
INFO - 16:58:58: ...  75%|███████▌  | 91/121 [00:00<00:00, 3723.54 it/sec, obj=0]
INFO - 16:58:58: ...  76%|███████▌  | 92/121 [00:00<00:00, 3720.22 it/sec, obj=1]
INFO - 16:58:58: ...  77%|███████▋  | 93/121 [00:00<00:00, 3723.11 it/sec, obj=2]
INFO - 16:58:58: ...  78%|███████▊  | 94/121 [00:00<00:00, 3724.08 it/sec, obj=3]
INFO - 16:58:58: ...  79%|███████▊  | 95/121 [00:00<00:00, 3724.37 it/sec, obj=4]
INFO - 16:58:58: ...  79%|███████▉  | 96/121 [00:00<00:00, 3727.17 it/sec, obj=5]
INFO - 16:58:58: ...  80%|████████  | 97/121 [00:00<00:00, 3730.15 it/sec, obj=6]
INFO - 16:58:58: ...  81%|████████  | 98/121 [00:00<00:00, 3732.81 it/sec, obj=7]
INFO - 16:58:58: ...  82%|████████▏ | 99/121 [00:00<00:00, 3735.82 it/sec, obj=8]
INFO - 16:58:58: ...  83%|████████▎ | 100/121 [00:00<00:00, 3738.51 it/sec, obj=-1]
INFO - 16:58:58: ...  83%|████████▎ | 101/121 [00:00<00:00, 3741.28 it/sec, obj=0]
INFO - 16:58:58: ...  84%|████████▍ | 102/121 [00:00<00:00, 3742.49 it/sec, obj=1]
INFO - 16:58:58: ...  85%|████████▌ | 103/121 [00:00<00:00, 3736.11 it/sec, obj=2]
INFO - 16:58:58: ...  86%|████████▌ | 104/121 [00:00<00:00, 3733.28 it/sec, obj=3]
INFO - 16:58:58: ...  87%|████████▋ | 105/121 [00:00<00:00, 3730.95 it/sec, obj=4]
INFO - 16:58:58: ...  88%|████████▊ | 106/121 [00:00<00:00, 3728.52 it/sec, obj=5]
INFO - 16:58:58: ...  88%|████████▊ | 107/121 [00:00<00:00, 3725.21 it/sec, obj=6]
INFO - 16:58:58: ...  89%|████████▉ | 108/121 [00:00<00:00, 3727.96 it/sec, obj=7]
INFO - 16:58:58: ...  90%|█████████ | 109/121 [00:00<00:00, 3730.25 it/sec, obj=8]
INFO - 16:58:58: ...  91%|█████████ | 110/121 [00:00<00:00, 3732.79 it/sec, obj=9]
INFO - 16:58:58: ...  92%|█████████▏| 111/121 [00:00<00:00, 3735.75 it/sec, obj=0]
INFO - 16:58:58: ...  93%|█████████▎| 112/121 [00:00<00:00, 3738.33 it/sec, obj=1]
INFO - 16:58:58: ...  93%|█████████▎| 113/121 [00:00<00:00, 3740.81 it/sec, obj=2]
INFO - 16:58:58: ...  94%|█████████▍| 114/121 [00:00<00:00, 3743.30 it/sec, obj=3]
INFO - 16:58:58: ...  95%|█████████▌| 115/121 [00:00<00:00, 3745.76 it/sec, obj=4]
INFO - 16:58:58: ...  96%|█████████▌| 116/121 [00:00<00:00, 3748.26 it/sec, obj=5]
INFO - 16:58:58: ...  97%|█████████▋| 117/121 [00:00<00:00, 3750.84 it/sec, obj=6]
INFO - 16:58:58: ...  98%|█████████▊| 118/121 [00:00<00:00, 3745.20 it/sec, obj=7]
INFO - 16:58:58: ...  98%|█████████▊| 119/121 [00:00<00:00, 3742.33 it/sec, obj=8]
INFO - 16:58:58: ...  99%|█████████▉| 120/121 [00:00<00:00, 3739.66 it/sec, obj=9]
INFO - 16:58:58: ... 100%|██████████| 121/121 [00:00<00:00, 3737.14 it/sec, obj=10]
INFO - 16:58:58: Optimization result:
INFO - 16:58:58:    Optimizer info:
INFO - 16:58:58:       Status: None
INFO - 16:58:58:       Message: None
INFO - 16:58:58:       Number of calls to the objective function by the optimizer: 121
INFO - 16:58:58:    Solution:
INFO - 16:58:58:       Objective: -10.0
INFO - 16:58:58:       Design space:
INFO - 16:58:58:       +------+-------------+-------+-------------+---------+
INFO - 16:58:58:       | name | lower_bound | value | upper_bound | type    |
INFO - 16:58:58:       +------+-------------+-------+-------------+---------+
INFO - 16:58:58:       | x[0] |      -5     |   -5  |      5      | integer |
INFO - 16:58:58:       | x[1] |      -5     |   -5  |      5      | integer |
INFO - 16:58:58:       +------+-------------+-------+-------------+---------+

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


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

Gallery generated by Sphinx-Gallery