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_algo
from gemseo import execute_post
from gemseo import get_available_doe_algorithms
from gemseo import get_available_opt_algorithms
from gemseo.algos.design_space import DesignSpace
from gemseo.algos.optimization_problem import OptimizationProblem
from gemseo.core.mdo_functions.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, lower_bound=-5, upper_bound=5, 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

execute_algo(problem, algo_name="PYDOE_FULLFACT", n_samples=11**2, algo_type="doe")
INFO - 20:38:18: Optimization problem:
INFO - 20:38:18:    minimize f = sum(x)
INFO - 20:38:18:    with respect to x
INFO - 20:38:18:    over the design space:
INFO - 20:38:18:       +------+-------------+-------+-------------+---------+
INFO - 20:38:18:       | Name | Lower bound | Value | Upper bound | Type    |
INFO - 20:38:18:       +------+-------------+-------+-------------+---------+
INFO - 20:38:18:       | x[0] |      -5     |  None |      5      | integer |
INFO - 20:38:18:       | x[1] |      -5     |  None |      5      | integer |
INFO - 20:38:18:       +------+-------------+-------+-------------+---------+
INFO - 20:38:18: Solving optimization problem with algorithm PYDOE_FULLFACT:
INFO - 20:38:18:      1%|          | 1/121 [00:00<00:00, 6574.14 it/sec, obj=-10]
INFO - 20:38:18:      2%|▏         | 2/121 [00:00<00:00, 5761.41 it/sec, obj=-9]
INFO - 20:38:18:      2%|▏         | 3/121 [00:00<00:00, 6055.30 it/sec, obj=-8]
INFO - 20:38:18:      3%|▎         | 4/121 [00:00<00:00, 6435.45 it/sec, obj=-7]
INFO - 20:38:18:      4%|▍         | 5/121 [00:00<00:00, 6710.89 it/sec, obj=-6]
INFO - 20:38:18:      5%|▍         | 6/121 [00:00<00:00, 6915.59 it/sec, obj=-5]
INFO - 20:38:18:      6%|▌         | 7/121 [00:00<00:00, 7131.44 it/sec, obj=-4]
INFO - 20:38:18:      7%|▋         | 8/121 [00:00<00:00, 7305.56 it/sec, obj=-3]
INFO - 20:38:18:      7%|▋         | 9/121 [00:00<00:00, 7457.28 it/sec, obj=-2]
INFO - 20:38:18:      8%|▊         | 10/121 [00:00<00:00, 7449.92 it/sec, obj=-1]
INFO - 20:38:18:      9%|▉         | 11/121 [00:00<00:00, 7514.23 it/sec, obj=0]
INFO - 20:38:18:     10%|▉         | 12/121 [00:00<00:00, 7592.65 it/sec, obj=-9]
INFO - 20:38:18:     11%|█         | 13/121 [00:00<00:00, 7687.29 it/sec, obj=-8]
INFO - 20:38:18:     12%|█▏        | 14/121 [00:00<00:00, 7767.23 it/sec, obj=-7]
INFO - 20:38:18:     12%|█▏        | 15/121 [00:00<00:00, 7770.11 it/sec, obj=-6]
INFO - 20:38:18:     13%|█▎        | 16/121 [00:00<00:00, 7783.45 it/sec, obj=-5]
INFO - 20:38:18:     14%|█▍        | 17/121 [00:00<00:00, 7831.21 it/sec, obj=-4]
INFO - 20:38:18:     15%|█▍        | 18/121 [00:00<00:00, 7869.24 it/sec, obj=-3]
INFO - 20:38:18:     16%|█▌        | 19/121 [00:00<00:00, 7829.81 it/sec, obj=-2]
INFO - 20:38:18:     17%|█▋        | 20/121 [00:00<00:00, 7878.85 it/sec, obj=-1]
INFO - 20:38:18:     17%|█▋        | 21/121 [00:00<00:00, 7928.74 it/sec, obj=0]
INFO - 20:38:18:     18%|█▊        | 22/121 [00:00<00:00, 7971.21 it/sec, obj=1]
INFO - 20:38:18:     19%|█▉        | 23/121 [00:00<00:00, 8013.04 it/sec, obj=-8]
INFO - 20:38:18:     20%|█▉        | 24/121 [00:00<00:00, 8056.29 it/sec, obj=-7]
INFO - 20:38:18:     21%|██        | 25/121 [00:00<00:00, 8090.86 it/sec, obj=-6]
INFO - 20:38:18:     21%|██▏       | 26/121 [00:00<00:00, 8126.07 it/sec, obj=-5]
INFO - 20:38:18:     22%|██▏       | 27/121 [00:00<00:00, 8119.17 it/sec, obj=-4]
INFO - 20:38:18:     23%|██▎       | 28/121 [00:00<00:00, 8143.15 it/sec, obj=-3]
INFO - 20:38:18:     24%|██▍       | 29/121 [00:00<00:00, 8174.93 it/sec, obj=-2]
INFO - 20:38:18:     25%|██▍       | 30/121 [00:00<00:00, 8205.36 it/sec, obj=-1]
INFO - 20:38:18:     26%|██▌       | 31/121 [00:00<00:00, 8239.76 it/sec, obj=0]
INFO - 20:38:18:     26%|██▋       | 32/121 [00:00<00:00, 8273.81 it/sec, obj=1]
INFO - 20:38:18:     27%|██▋       | 33/121 [00:00<00:00, 8304.56 it/sec, obj=2]
INFO - 20:38:18:     28%|██▊       | 34/121 [00:00<00:00, 8318.15 it/sec, obj=-7]
INFO - 20:38:18:     29%|██▉       | 35/121 [00:00<00:00, 8340.00 it/sec, obj=-6]
INFO - 20:38:18:     30%|██▉       | 36/121 [00:00<00:00, 8331.67 it/sec, obj=-5]
INFO - 20:38:18:     31%|███       | 37/121 [00:00<00:00, 8317.57 it/sec, obj=-4]
INFO - 20:38:18:     31%|███▏      | 38/121 [00:00<00:00, 8330.30 it/sec, obj=-3]
INFO - 20:38:18:     32%|███▏      | 39/121 [00:00<00:00, 8347.09 it/sec, obj=-2]
INFO - 20:38:18:     33%|███▎      | 40/121 [00:00<00:00, 8370.19 it/sec, obj=-1]
INFO - 20:38:18:     34%|███▍      | 41/121 [00:00<00:00, 8389.43 it/sec, obj=0]
INFO - 20:38:18:     35%|███▍      | 42/121 [00:00<00:00, 8407.42 it/sec, obj=1]
INFO - 20:38:18:     36%|███▌      | 43/121 [00:00<00:00, 8422.30 it/sec, obj=2]
INFO - 20:38:18:     36%|███▋      | 44/121 [00:00<00:00, 8439.24 it/sec, obj=3]
INFO - 20:38:18:     37%|███▋      | 45/121 [00:00<00:00, 8424.18 it/sec, obj=-6]
INFO - 20:38:18:     38%|███▊      | 46/121 [00:00<00:00, 8429.66 it/sec, obj=-5]
INFO - 20:38:18:     39%|███▉      | 47/121 [00:00<00:00, 8447.56 it/sec, obj=-4]
INFO - 20:38:18:     40%|███▉      | 48/121 [00:00<00:00, 8469.06 it/sec, obj=-3]
INFO - 20:38:18:     40%|████      | 49/121 [00:00<00:00, 8494.00 it/sec, obj=-2]
INFO - 20:38:18:     41%|████▏     | 50/121 [00:00<00:00, 8511.17 it/sec, obj=-1]
INFO - 20:38:18:     42%|████▏     | 51/121 [00:00<00:00, 8505.35 it/sec, obj=0]
INFO - 20:38:18:     43%|████▎     | 52/121 [00:00<00:00, 8519.35 it/sec, obj=1]
INFO - 20:38:18:     44%|████▍     | 53/121 [00:00<00:00, 8534.17 it/sec, obj=2]
INFO - 20:38:18:     45%|████▍     | 54/121 [00:00<00:00, 8528.86 it/sec, obj=3]
INFO - 20:38:18:     45%|████▌     | 55/121 [00:00<00:00, 8538.58 it/sec, obj=4]
INFO - 20:38:18:     46%|████▋     | 56/121 [00:00<00:00, 8557.00 it/sec, obj=-5]
INFO - 20:38:18:     47%|████▋     | 57/121 [00:00<00:00, 8576.08 it/sec, obj=-4]
INFO - 20:38:18:     48%|████▊     | 58/121 [00:00<00:00, 8594.58 it/sec, obj=-3]
INFO - 20:38:18:     49%|████▉     | 59/121 [00:00<00:00, 8613.43 it/sec, obj=-2]
INFO - 20:38:18:     50%|████▉     | 60/121 [00:00<00:00, 8632.62 it/sec, obj=-1]
INFO - 20:38:18:     50%|█████     | 61/121 [00:00<00:00, 8649.80 it/sec, obj=0]
INFO - 20:38:18:     51%|█████     | 62/121 [00:00<00:00, 8665.92 it/sec, obj=1]
INFO - 20:38:18:     52%|█████▏    | 63/121 [00:00<00:00, 8529.96 it/sec, obj=2]
INFO - 20:38:18:     53%|█████▎    | 64/121 [00:00<00:00, 8525.01 it/sec, obj=3]
INFO - 20:38:18:     54%|█████▎    | 65/121 [00:00<00:00, 8535.68 it/sec, obj=4]
INFO - 20:38:18:     55%|█████▍    | 66/121 [00:00<00:00, 8536.84 it/sec, obj=5]
INFO - 20:38:18:     55%|█████▌    | 67/121 [00:00<00:00, 8547.31 it/sec, obj=-4]
INFO - 20:38:18:     56%|█████▌    | 68/121 [00:00<00:00, 8560.32 it/sec, obj=-3]
INFO - 20:38:18:     57%|█████▋    | 69/121 [00:00<00:00, 8572.74 it/sec, obj=-2]
INFO - 20:38:18:     58%|█████▊    | 70/121 [00:00<00:00, 8583.08 it/sec, obj=-1]
INFO - 20:38:18:     59%|█████▊    | 71/121 [00:00<00:00, 8594.39 it/sec, obj=0]
INFO - 20:38:18:     60%|█████▉    | 72/121 [00:00<00:00, 8574.63 it/sec, obj=1]
INFO - 20:38:18:     60%|██████    | 73/121 [00:00<00:00, 8583.32 it/sec, obj=2]
INFO - 20:38:18:     61%|██████    | 74/121 [00:00<00:00, 8594.17 it/sec, obj=3]
INFO - 20:38:18:     62%|██████▏   | 75/121 [00:00<00:00, 8604.29 it/sec, obj=4]
INFO - 20:38:18:     63%|██████▎   | 76/121 [00:00<00:00, 8615.79 it/sec, obj=5]
INFO - 20:38:18:     64%|██████▎   | 77/121 [00:00<00:00, 8629.10 it/sec, obj=6]
INFO - 20:38:18:     64%|██████▍   | 78/121 [00:00<00:00, 8639.60 it/sec, obj=-3]
INFO - 20:38:18:     65%|██████▌   | 79/121 [00:00<00:00, 8652.34 it/sec, obj=-2]
INFO - 20:38:18:     66%|██████▌   | 80/121 [00:00<00:00, 8666.59 it/sec, obj=-1]
INFO - 20:38:18:     67%|██████▋   | 81/121 [00:00<00:00, 8658.85 it/sec, obj=0]
INFO - 20:38:18:     68%|██████▊   | 82/121 [00:00<00:00, 8663.30 it/sec, obj=1]
INFO - 20:38:18:     69%|██████▊   | 83/121 [00:00<00:00, 8675.64 it/sec, obj=2]
INFO - 20:38:18:     69%|██████▉   | 84/121 [00:00<00:00, 8687.50 it/sec, obj=3]
INFO - 20:38:18:     70%|███████   | 85/121 [00:00<00:00, 8692.96 it/sec, obj=4]
INFO - 20:38:18:     71%|███████   | 86/121 [00:00<00:00, 8700.62 it/sec, obj=5]
INFO - 20:38:18:     72%|███████▏  | 87/121 [00:00<00:00, 8710.60 it/sec, obj=6]
INFO - 20:38:18:     73%|███████▎  | 88/121 [00:00<00:00, 8722.03 it/sec, obj=7]
INFO - 20:38:18:     74%|███████▎  | 89/121 [00:00<00:00, 8732.41 it/sec, obj=-2]
INFO - 20:38:18:     74%|███████▍  | 90/121 [00:00<00:00, 8727.02 it/sec, obj=-1]
INFO - 20:38:18:     75%|███████▌  | 91/121 [00:00<00:00, 8726.94 it/sec, obj=0]
INFO - 20:38:18:     76%|███████▌  | 92/121 [00:00<00:00, 8733.98 it/sec, obj=1]
INFO - 20:38:18:     77%|███████▋  | 93/121 [00:00<00:00, 8743.03 it/sec, obj=2]
INFO - 20:38:18:     78%|███████▊  | 94/121 [00:00<00:00, 8751.52 it/sec, obj=3]
INFO - 20:38:18:     79%|███████▊  | 95/121 [00:00<00:00, 8759.65 it/sec, obj=4]
INFO - 20:38:18:     79%|███████▉  | 96/121 [00:00<00:00, 8767.05 it/sec, obj=5]
INFO - 20:38:18:     80%|████████  | 97/121 [00:00<00:00, 8774.88 it/sec, obj=6]
INFO - 20:38:18:     81%|████████  | 98/121 [00:00<00:00, 8782.57 it/sec, obj=7]
INFO - 20:38:18:     82%|████████▏ | 99/121 [00:00<00:00, 8792.16 it/sec, obj=8]
INFO - 20:38:18:     83%|████████▎ | 100/121 [00:00<00:00, 8782.78 it/sec, obj=-1]
INFO - 20:38:18:     83%|████████▎ | 101/121 [00:00<00:00, 8788.16 it/sec, obj=0]
INFO - 20:38:18:     84%|████████▍ | 102/121 [00:00<00:00, 8796.71 it/sec, obj=1]
INFO - 20:38:18:     85%|████████▌ | 103/121 [00:00<00:00, 8803.30 it/sec, obj=2]
INFO - 20:38:18:     86%|████████▌ | 104/121 [00:00<00:00, 8811.03 it/sec, obj=3]
INFO - 20:38:18:     87%|████████▋ | 105/121 [00:00<00:00, 8820.74 it/sec, obj=4]
INFO - 20:38:18:     88%|████████▊ | 106/121 [00:00<00:00, 8829.24 it/sec, obj=5]
INFO - 20:38:18:     88%|████████▊ | 107/121 [00:00<00:00, 8836.37 it/sec, obj=6]
INFO - 20:38:18:     89%|████████▉ | 108/121 [00:00<00:00, 8842.70 it/sec, obj=7]
INFO - 20:38:18:     90%|█████████ | 109/121 [00:00<00:00, 8824.49 it/sec, obj=8]
INFO - 20:38:18:     91%|█████████ | 110/121 [00:00<00:00, 8825.55 it/sec, obj=9]
INFO - 20:38:18:     92%|█████████▏| 111/121 [00:00<00:00, 8833.63 it/sec, obj=0]
INFO - 20:38:18:     93%|█████████▎| 112/121 [00:00<00:00, 8840.42 it/sec, obj=1]
INFO - 20:38:18:     93%|█████████▎| 113/121 [00:00<00:00, 8845.94 it/sec, obj=2]
INFO - 20:38:18:     94%|█████████▍| 114/121 [00:00<00:00, 8851.36 it/sec, obj=3]
INFO - 20:38:18:     95%|█████████▌| 115/121 [00:00<00:00, 8857.52 it/sec, obj=4]
INFO - 20:38:18:     96%|█████████▌| 116/121 [00:00<00:00, 8862.93 it/sec, obj=5]
INFO - 20:38:18:     97%|█████████▋| 117/121 [00:00<00:00, 8868.73 it/sec, obj=6]
INFO - 20:38:18:     98%|█████████▊| 118/121 [00:00<00:00, 8863.48 it/sec, obj=7]
INFO - 20:38:18:     98%|█████████▊| 119/121 [00:00<00:00, 8861.00 it/sec, obj=8]
INFO - 20:38:18:     99%|█████████▉| 120/121 [00:00<00:00, 8865.89 it/sec, obj=9]
INFO - 20:38:18:    100%|██████████| 121/121 [00:00<00:00, 8870.24 it/sec, obj=10]
INFO - 20:38:18: Optimization result:
INFO - 20:38:18:    Optimizer info:
INFO - 20:38:18:       Status: None
INFO - 20:38:18:       Message: None
INFO - 20:38:18:       Number of calls to the objective function by the optimizer: 0
INFO - 20:38:18:    Solution:
INFO - 20:38:18:       Objective: -10.0
INFO - 20:38:18:       Design space:
INFO - 20:38:18:          +------+-------------+-------+-------------+---------+
INFO - 20:38:18:          | Name | Lower bound | Value | Upper bound | Type    |
INFO - 20:38:18:          +------+-------------+-------+-------------+---------+
INFO - 20:38:18:          | x[0] |      -5     |   -5  |      5      | integer |
INFO - 20:38:18:          | x[1] |      -5     |   -5  |      5      | integer |
INFO - 20:38:18:          +------+-------------+-------+-------------+---------+
Optimization result:
  • Design variables: [-5. -5.]
  • Objective function: -10.0
  • Feasible solution: True


Post-process the results#

execute_post(
    problem,
    post_name="ScatterPlotMatrix",
    variable_names=["x", "f"],
    save=False,
    show=True,
)
plot simple opt 3
<gemseo.post.scatter_plot_matrix.ScatterPlotMatrix object at 0x78590cde8680>

Note that you can get all the optimization algorithms names:

get_available_opt_algorithms()
['Augmented_Lagrangian_order_0', 'Augmented_Lagrangian_order_1', 'HEXALY', 'MMA', 'MNBI', 'MultiStart', 'NLOPT_MMA', 'NLOPT_COBYLA', 'NLOPT_SLSQP', 'NLOPT_BOBYQA', 'NLOPT_BFGS', 'NLOPT_NEWUOA', 'PDFO_COBYLA', 'PDFO_BOBYQA', 'PDFO_NEWUOA', 'PYOPTSPARSE_SLSQP', 'PYOPTSPARSE_SNOPT', 'PYMOO_GA', 'PYMOO_NSGA2', 'PYMOO_NSGA3', 'PYMOO_UNSGA3', 'PYMOO_RNSGA3', 'SMT_EGO', 'DUAL_ANNEALING', 'SHGO', 'DIFFERENTIAL_EVOLUTION', 'INTERIOR_POINT', 'DUAL_SIMPLEX', 'Scipy_MILP', 'SLSQP', 'L-BFGS-B', 'TNC', 'NELDER-MEAD', 'COBYQA', 'SBO']

and all the DOE algorithms names:

get_available_doe_algorithms()
['CustomDOE', 'DiagonalDOE', 'MorrisDOE', 'OATDOE', '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', 'PYDOE_BBDESIGN', 'PYDOE_CCDESIGN', 'PYDOE_FF2N', 'PYDOE_FULLFACT', 'PYDOE_LHS', 'PYDOE_PBDESIGN', 'Halton', 'LHS', 'MC', 'PoissonDisk', 'Sobol']

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

Gallery generated by Sphinx-Gallery