.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/optimization_problem/plot_simple_opt_3.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_optimization_problem_plot_simple_opt_3.py: Analytical test case # 3 ======================== .. GENERATED FROM PYTHON SOURCE LINES 26-32 In this example, we consider a simple optimization problem to illustrate algorithms interfaces and DOE libraries integration. Integer variables are used Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 32-46 .. code-block:: Python 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() .. GENERATED FROM PYTHON SOURCE LINES 47-51 Define the objective function ----------------------------- We define the objective function :math:`f(x)=\sum_{i=1}^dx_i` using an :class:`.MDOFunction`. .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python objective = MDOFunction(np_sum, name="f", expr="sum(x)") .. GENERATED FROM PYTHON SOURCE LINES 54-57 Define the design space ----------------------- Then, we define the :class:`.DesignSpace` with |g|. .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python design_space = DesignSpace() design_space.add_variable("x", 2, l_b=-5, u_b=5, var_type="integer") .. GENERATED FROM PYTHON SOURCE LINES 61-64 Define the optimization problem ------------------------------- Then, we define the :class:`.OptimizationProblem` with |g|. .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python problem = OptimizationProblem(design_space) problem.objective = objective .. GENERATED FROM PYTHON SOURCE LINES 68-73 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 .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python DOEFactory().execute(problem, "fullfact", n_samples=11**2) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 10:53:02: Optimization problem: INFO - 10:53:02: minimize f = sum(x) INFO - 10:53:02: with respect to x INFO - 10:53:02: over the design space: INFO - 10:53:02: +------+-------------+-------+-------------+---------+ INFO - 10:53:02: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:53:02: +------+-------------+-------+-------------+---------+ INFO - 10:53:02: | x[0] | -5 | None | 5 | integer | INFO - 10:53:02: | x[1] | -5 | None | 5 | integer | INFO - 10:53:02: +------+-------------+-------+-------------+---------+ INFO - 10:53:02: Solving optimization problem with algorithm fullfact: INFO - 10:53:02: 1%| | 1/121 [00:00<00:00, 4056.39 it/sec, obj=-10] INFO - 10:53:02: 2%|▏ | 2/121 [00:00<00:00, 3188.37 it/sec, obj=-9] INFO - 10:53:02: 2%|▏ | 3/121 [00:00<00:00, 3173.50 it/sec, obj=-8] INFO - 10:53:02: 3%|▎ | 4/121 [00:00<00:00, 3183.53 it/sec, obj=-7] INFO - 10:53:02: 4%|▍ | 5/121 [00:00<00:00, 3204.20 it/sec, obj=-6] INFO - 10:53:02: 5%|▍ | 6/121 [00:00<00:00, 3220.20 it/sec, obj=-5] INFO - 10:53:02: 6%|▌ | 7/121 [00:00<00:00, 3235.28 it/sec, obj=-4] INFO - 10:53:02: 7%|▋ | 8/121 [00:00<00:00, 3246.37 it/sec, obj=-3] INFO - 10:53:02: 7%|▋ | 9/121 [00:00<00:00, 3239.68 it/sec, obj=-2] INFO - 10:53:02: 8%|▊ | 10/121 [00:00<00:00, 3235.85 it/sec, obj=-1] INFO - 10:53:02: 9%|▉ | 11/121 [00:00<00:00, 3242.94 it/sec, obj=0] INFO - 10:53:02: 10%|▉ | 12/121 [00:00<00:00, 3213.00 it/sec, obj=-9] INFO - 10:53:02: 11%|█ | 13/121 [00:00<00:00, 3212.51 it/sec, obj=-8] INFO - 10:53:02: 12%|█▏ | 14/121 [00:00<00:00, 3173.90 it/sec, obj=-7] INFO - 10:53:02: 12%|█▏ | 15/121 [00:00<00:00, 3158.68 it/sec, obj=-6] INFO - 10:53:02: 13%|█▎ | 16/121 [00:00<00:00, 3165.36 it/sec, obj=-5] INFO - 10:53:02: 14%|█▍ | 17/121 [00:00<00:00, 3173.68 it/sec, obj=-4] INFO - 10:53:02: 15%|█▍ | 18/121 [00:00<00:00, 3182.59 it/sec, obj=-3] INFO - 10:53:02: 16%|█▌ | 19/121 [00:00<00:00, 3190.73 it/sec, obj=-2] INFO - 10:53:02: 17%|█▋ | 20/121 [00:00<00:00, 3196.63 it/sec, obj=-1] INFO - 10:53:02: 17%|█▋ | 21/121 [00:00<00:00, 3203.16 it/sec, obj=0] INFO - 10:53:02: 18%|█▊ | 22/121 [00:00<00:00, 3199.09 it/sec, obj=1] INFO - 10:53:02: 19%|█▉ | 23/121 [00:00<00:00, 3200.48 it/sec, obj=-8] INFO - 10:53:02: 20%|█▉ | 24/121 [00:00<00:00, 3204.31 it/sec, obj=-7] INFO - 10:53:02: 21%|██ | 25/121 [00:00<00:00, 3184.93 it/sec, obj=-6] INFO - 10:53:02: 21%|██▏ | 26/121 [00:00<00:00, 3186.60 it/sec, obj=-5] INFO - 10:53:02: 22%|██▏ | 27/121 [00:00<00:00, 3191.20 it/sec, obj=-4] INFO - 10:53:02: 23%|██▎ | 28/121 [00:00<00:00, 3195.40 it/sec, obj=-3] INFO - 10:53:02: 24%|██▍ | 29/121 [00:00<00:00, 3199.40 it/sec, obj=-2] INFO - 10:53:02: 25%|██▍ | 30/121 [00:00<00:00, 3202.66 it/sec, obj=-1] INFO - 10:53:02: 26%|██▌ | 31/121 [00:00<00:00, 3206.10 it/sec, obj=0] INFO - 10:53:02: 26%|██▋ | 32/121 [00:00<00:00, 3208.57 it/sec, obj=1] INFO - 10:53:02: 27%|██▋ | 33/121 [00:00<00:00, 3212.24 it/sec, obj=2] INFO - 10:53:02: 28%|██▊ | 34/121 [00:00<00:00, 3215.19 it/sec, obj=-7] INFO - 10:53:02: 29%|██▉ | 35/121 [00:00<00:00, 3212.13 it/sec, obj=-6] INFO - 10:53:02: 30%|██▉ | 36/121 [00:00<00:00, 3213.48 it/sec, obj=-5] INFO - 10:53:02: 31%|███ | 37/121 [00:00<00:00, 3216.22 it/sec, obj=-4] INFO - 10:53:02: 31%|███▏ | 38/121 [00:00<00:00, 3206.72 it/sec, obj=-3] INFO - 10:53:02: 32%|███▏ | 39/121 [00:00<00:00, 3208.73 it/sec, obj=-2] INFO - 10:53:02: 33%|███▎ | 40/121 [00:00<00:00, 3210.83 it/sec, obj=-1] INFO - 10:53:02: 34%|███▍ | 41/121 [00:00<00:00, 3213.73 it/sec, obj=0] INFO - 10:53:02: 35%|███▍ | 42/121 [00:00<00:00, 3216.20 it/sec, obj=1] INFO - 10:53:02: 36%|███▌ | 43/121 [00:00<00:00, 3217.93 it/sec, obj=2] INFO - 10:53:02: 36%|███▋ | 44/121 [00:00<00:00, 3220.42 it/sec, obj=3] INFO - 10:53:02: 37%|███▋ | 45/121 [00:00<00:00, 3223.03 it/sec, obj=-6] INFO - 10:53:02: 38%|███▊ | 46/121 [00:00<00:00, 3226.12 it/sec, obj=-5] INFO - 10:53:02: 39%|███▉ | 47/121 [00:00<00:00, 3228.29 it/sec, obj=-4] INFO - 10:53:02: 40%|███▉ | 48/121 [00:00<00:00, 3227.01 it/sec, obj=-3] INFO - 10:53:02: 40%|████ | 49/121 [00:00<00:00, 3228.31 it/sec, obj=-2] INFO - 10:53:02: 41%|████▏ | 50/121 [00:00<00:00, 3230.66 it/sec, obj=-1] INFO - 10:53:02: 42%|████▏ | 51/121 [00:00<00:00, 3226.24 it/sec, obj=0] INFO - 10:53:02: 43%|████▎ | 52/121 [00:00<00:00, 3227.34 it/sec, obj=1] INFO - 10:53:02: 44%|████▍ | 53/121 [00:00<00:00, 3229.06 it/sec, obj=2] INFO - 10:53:02: 45%|████▍ | 54/121 [00:00<00:00, 3231.27 it/sec, obj=3] INFO - 10:53:02: 45%|████▌ | 55/121 [00:00<00:00, 3233.03 it/sec, obj=4] INFO - 10:53:02: 46%|████▋ | 56/121 [00:00<00:00, 3235.10 it/sec, obj=-5] INFO - 10:53:02: 47%|████▋ | 57/121 [00:00<00:00, 3237.09 it/sec, obj=-4] INFO - 10:53:02: 48%|████▊ | 58/121 [00:00<00:00, 3239.19 it/sec, obj=-3] INFO - 10:53:02: 49%|████▉ | 59/121 [00:00<00:00, 3241.05 it/sec, obj=-2] INFO - 10:53:02: 50%|████▉ | 60/121 [00:00<00:00, 3242.81 it/sec, obj=-1] INFO - 10:53:02: 50%|█████ | 61/121 [00:00<00:00, 3241.27 it/sec, obj=0] INFO - 10:53:02: 51%|█████ | 62/121 [00:00<00:00, 3241.67 it/sec, obj=1] INFO - 10:53:02: 52%|█████▏ | 63/121 [00:00<00:00, 3243.34 it/sec, obj=2] INFO - 10:53:02: 53%|█████▎ | 64/121 [00:00<00:00, 3239.70 it/sec, obj=3] INFO - 10:53:02: 54%|█████▎ | 65/121 [00:00<00:00, 3240.04 it/sec, obj=4] INFO - 10:53:02: 55%|█████▍ | 66/121 [00:00<00:00, 3240.89 it/sec, obj=5] INFO - 10:53:02: 55%|█████▌ | 67/121 [00:00<00:00, 3241.61 it/sec, obj=-4] INFO - 10:53:02: 56%|█████▌ | 68/121 [00:00<00:00, 3242.67 it/sec, obj=-3] INFO - 10:53:02: 57%|█████▋ | 69/121 [00:00<00:00, 3243.82 it/sec, obj=-2] INFO - 10:53:02: 58%|█████▊ | 70/121 [00:00<00:00, 3245.00 it/sec, obj=-1] INFO - 10:53:02: 59%|█████▊ | 71/121 [00:00<00:00, 3246.51 it/sec, obj=0] INFO - 10:53:02: 60%|█████▉ | 72/121 [00:00<00:00, 3247.62 it/sec, obj=1] INFO - 10:53:02: 60%|██████ | 73/121 [00:00<00:00, 3248.60 it/sec, obj=2] INFO - 10:53:02: 61%|██████ | 74/121 [00:00<00:00, 3247.93 it/sec, obj=3] INFO - 10:53:02: 62%|██████▏ | 75/121 [00:00<00:00, 3247.27 it/sec, obj=4] INFO - 10:53:02: 63%|██████▎ | 76/121 [00:00<00:00, 3247.85 it/sec, obj=5] INFO - 10:53:02: 64%|██████▎ | 77/121 [00:00<00:00, 3244.41 it/sec, obj=6] INFO - 10:53:02: 64%|██████▍ | 78/121 [00:00<00:00, 3244.50 it/sec, obj=-3] INFO - 10:53:02: 65%|██████▌ | 79/121 [00:00<00:00, 3245.25 it/sec, obj=-2] INFO - 10:53:02: 66%|██████▌ | 80/121 [00:00<00:00, 3246.33 it/sec, obj=-1] INFO - 10:53:02: 67%|██████▋ | 81/121 [00:00<00:00, 3247.08 it/sec, obj=0] INFO - 10:53:02: 68%|██████▊ | 82/121 [00:00<00:00, 3248.05 it/sec, obj=1] INFO - 10:53:02: 69%|██████▊ | 83/121 [00:00<00:00, 3248.76 it/sec, obj=2] INFO - 10:53:02: 69%|██████▉ | 84/121 [00:00<00:00, 3227.90 it/sec, obj=3] INFO - 10:53:02: 70%|███████ | 85/121 [00:00<00:00, 3227.29 it/sec, obj=4] INFO - 10:53:02: 71%|███████ | 86/121 [00:00<00:00, 3228.03 it/sec, obj=5] INFO - 10:53:02: 72%|███████▏ | 87/121 [00:00<00:00, 3226.50 it/sec, obj=6] INFO - 10:53:02: 73%|███████▎ | 88/121 [00:00<00:00, 3227.80 it/sec, obj=7] INFO - 10:53:02: 74%|███████▎ | 89/121 [00:00<00:00, 3229.51 it/sec, obj=-2] INFO - 10:53:02: 74%|███████▍ | 90/121 [00:00<00:00, 3223.69 it/sec, obj=-1] INFO - 10:53:02: 75%|███████▌ | 91/121 [00:00<00:00, 3224.56 it/sec, obj=0] INFO - 10:53:02: 76%|███████▌ | 92/121 [00:00<00:00, 3225.71 it/sec, obj=1] INFO - 10:53:02: 77%|███████▋ | 93/121 [00:00<00:00, 3219.15 it/sec, obj=2] INFO - 10:53:02: 78%|███████▊ | 94/121 [00:00<00:00, 3218.93 it/sec, obj=3] INFO - 10:53:02: 79%|███████▊ | 95/121 [00:00<00:00, 3219.90 it/sec, obj=4] INFO - 10:53:02: 79%|███████▉ | 96/121 [00:00<00:00, 3220.86 it/sec, obj=5] INFO - 10:53:02: 80%|████████ | 97/121 [00:00<00:00, 3221.94 it/sec, obj=6] INFO - 10:53:02: 81%|████████ | 98/121 [00:00<00:00, 3223.02 it/sec, obj=7] INFO - 10:53:02: 82%|████████▏ | 99/121 [00:00<00:00, 3223.76 it/sec, obj=8] INFO - 10:53:02: 83%|████████▎ | 100/121 [00:00<00:00, 3222.17 it/sec, obj=-1] INFO - 10:53:02: 83%|████████▎ | 101/121 [00:00<00:00, 3222.95 it/sec, obj=0] INFO - 10:53:02: 84%|████████▍ | 102/121 [00:00<00:00, 3221.24 it/sec, obj=1] INFO - 10:53:02: 85%|████████▌ | 103/121 [00:00<00:00, 3221.17 it/sec, obj=2] INFO - 10:53:02: 86%|████████▌ | 104/121 [00:00<00:00, 3221.88 it/sec, obj=3] INFO - 10:53:02: 87%|████████▋ | 105/121 [00:00<00:00, 3222.96 it/sec, obj=4] INFO - 10:53:02: 88%|████████▊ | 106/121 [00:00<00:00, 3224.07 it/sec, obj=5] INFO - 10:53:02: 88%|████████▊ | 107/121 [00:00<00:00, 3225.02 it/sec, obj=6] INFO - 10:53:02: 89%|████████▉ | 108/121 [00:00<00:00, 3226.11 it/sec, obj=7] INFO - 10:53:02: 90%|█████████ | 109/121 [00:00<00:00, 3227.05 it/sec, obj=8] INFO - 10:53:02: 91%|█████████ | 110/121 [00:00<00:00, 3228.01 it/sec, obj=9] INFO - 10:53:02: 92%|█████████▏| 111/121 [00:00<00:00, 3229.16 it/sec, obj=0] INFO - 10:53:02: 93%|█████████▎| 112/121 [00:00<00:00, 3230.16 it/sec, obj=1] INFO - 10:53:02: 93%|█████████▎| 113/121 [00:00<00:00, 3229.09 it/sec, obj=2] INFO - 10:53:02: 94%|█████████▍| 114/121 [00:00<00:00, 3229.90 it/sec, obj=3] INFO - 10:53:02: 95%|█████████▌| 115/121 [00:00<00:00, 3228.07 it/sec, obj=4] INFO - 10:53:02: 96%|█████████▌| 116/121 [00:00<00:00, 3227.05 it/sec, obj=5] INFO - 10:53:02: 97%|█████████▋| 117/121 [00:00<00:00, 3227.81 it/sec, obj=6] INFO - 10:53:02: 98%|█████████▊| 118/121 [00:00<00:00, 3228.75 it/sec, obj=7] INFO - 10:53:02: 98%|█████████▊| 119/121 [00:00<00:00, 3228.93 it/sec, obj=8] INFO - 10:53:02: 99%|█████████▉| 120/121 [00:00<00:00, 3229.70 it/sec, obj=9] INFO - 10:53:02: 100%|██████████| 121/121 [00:00<00:00, 3230.33 it/sec, obj=10] INFO - 10:53:02: Optimization result: INFO - 10:53:02: Optimizer info: INFO - 10:53:02: Status: None INFO - 10:53:02: Message: None INFO - 10:53:02: Number of calls to the objective function by the optimizer: 121 INFO - 10:53:02: Solution: INFO - 10:53:02: Objective: -10.0 INFO - 10:53:02: Design space: INFO - 10:53:02: +------+-------------+-------+-------------+---------+ INFO - 10:53:02: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:53:02: +------+-------------+-------+-------------+---------+ INFO - 10:53:02: | x[0] | -5 | -5 | 5 | integer | INFO - 10:53:02: | x[1] | -5 | -5 | 5 | integer | INFO - 10:53:02: +------+-------------+-------+-------------+---------+ .. raw:: html
Optimization result:
  • Design variables: [-5. -5.]
  • Objective function: -10.0
  • Feasible solution: True


.. GENERATED FROM PYTHON SOURCE LINES 76-78 Post-process the results ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 78-86 .. code-block:: Python execute_post( problem, "ScatterPlotMatrix", variable_names=["x", "f"], save=False, show=True, ) .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_3_001.png :alt: plot simple opt 3 :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_3_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 87-88 Note that you can get all the optimization algorithms names: .. GENERATED FROM PYTHON SOURCE LINES 88-89 .. code-block:: Python DOEFactory().algorithms .. rst-class:: sphx-glr-script-out .. code-block:: none ['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'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.748 seconds) .. _sphx_glr_download_examples_optimization_problem_plot_simple_opt_3.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_simple_opt_3.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simple_opt_3.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_