.. 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 - 13:54:05: Optimization problem: INFO - 13:54:05: minimize f = sum(x) INFO - 13:54:05: with respect to x INFO - 13:54:05: over the design space: INFO - 13:54:05: +------+-------------+-------+-------------+---------+ INFO - 13:54:05: | Name | Lower bound | Value | Upper bound | Type | INFO - 13:54:05: +------+-------------+-------+-------------+---------+ INFO - 13:54:05: | x[0] | -5 | None | 5 | integer | INFO - 13:54:05: | x[1] | -5 | None | 5 | integer | INFO - 13:54:05: +------+-------------+-------+-------------+---------+ INFO - 13:54:05: Solving optimization problem with algorithm fullfact: INFO - 13:54:05: 1%| | 1/121 [00:00<00:00, 4524.60 it/sec, obj=-10] INFO - 13:54:05: 2%|▏ | 2/121 [00:00<00:00, 3515.76 it/sec, obj=-9] INFO - 13:54:05: 2%|▏ | 3/121 [00:00<00:00, 3458.74 it/sec, obj=-8] INFO - 13:54:05: 3%|▎ | 4/121 [00:00<00:00, 3452.81 it/sec, obj=-7] INFO - 13:54:05: 4%|▍ | 5/121 [00:00<00:00, 3459.51 it/sec, obj=-6] INFO - 13:54:05: 5%|▍ | 6/121 [00:00<00:00, 3466.84 it/sec, obj=-5] INFO - 13:54:05: 6%|▌ | 7/121 [00:00<00:00, 3478.28 it/sec, obj=-4] INFO - 13:54:05: 7%|▋ | 8/121 [00:00<00:00, 3480.39 it/sec, obj=-3] INFO - 13:54:05: 7%|▋ | 9/121 [00:00<00:00, 3483.32 it/sec, obj=-2] INFO - 13:54:05: 8%|▊ | 10/121 [00:00<00:00, 3421.13 it/sec, obj=-1] INFO - 13:54:05: 9%|▉ | 11/121 [00:00<00:00, 3413.79 it/sec, obj=0] INFO - 13:54:05: 10%|▉ | 12/121 [00:00<00:00, 3418.34 it/sec, obj=-9] INFO - 13:54:05: 11%|█ | 13/121 [00:00<00:00, 3424.78 it/sec, obj=-8] INFO - 13:54:05: 12%|█▏ | 14/121 [00:00<00:00, 3431.73 it/sec, obj=-7] INFO - 13:54:05: 12%|█▏ | 15/121 [00:00<00:00, 3441.72 it/sec, obj=-6] INFO - 13:54:05: 13%|█▎ | 16/121 [00:00<00:00, 3448.55 it/sec, obj=-5] INFO - 13:54:05: 14%|█▍ | 17/121 [00:00<00:00, 3454.44 it/sec, obj=-4] INFO - 13:54:05: 15%|█▍ | 18/121 [00:00<00:00, 3459.54 it/sec, obj=-3] INFO - 13:54:05: 16%|█▌ | 19/121 [00:00<00:00, 3464.56 it/sec, obj=-2] INFO - 13:54:05: 17%|█▋ | 20/121 [00:00<00:00, 3468.52 it/sec, obj=-1] INFO - 13:54:05: 17%|█▋ | 21/121 [00:00<00:00, 3473.48 it/sec, obj=0] INFO - 13:54:05: 18%|█▊ | 22/121 [00:00<00:00, 3477.08 it/sec, obj=1] INFO - 13:54:05: 19%|█▉ | 23/121 [00:00<00:00, 3479.37 it/sec, obj=-8] INFO - 13:54:05: 20%|█▉ | 24/121 [00:00<00:00, 3444.90 it/sec, obj=-7] INFO - 13:54:05: 21%|██ | 25/121 [00:00<00:00, 3440.66 it/sec, obj=-6] INFO - 13:54:05: 21%|██▏ | 26/121 [00:00<00:00, 3443.06 it/sec, obj=-5] INFO - 13:54:05: 22%|██▏ | 27/121 [00:00<00:00, 3446.11 it/sec, obj=-4] INFO - 13:54:05: 23%|██▎ | 28/121 [00:00<00:00, 3449.26 it/sec, obj=-3] INFO - 13:54:05: 24%|██▍ | 29/121 [00:00<00:00, 3452.59 it/sec, obj=-2] INFO - 13:54:05: 25%|██▍ | 30/121 [00:00<00:00, 3455.99 it/sec, obj=-1] INFO - 13:54:05: 26%|██▌ | 31/121 [00:00<00:00, 3458.71 it/sec, obj=0] INFO - 13:54:05: 26%|██▋ | 32/121 [00:00<00:00, 3461.45 it/sec, obj=1] INFO - 13:54:05: 27%|██▋ | 33/121 [00:00<00:00, 3463.68 it/sec, obj=2] INFO - 13:54:05: 28%|██▊ | 34/121 [00:00<00:00, 3465.02 it/sec, obj=-7] INFO - 13:54:05: 29%|██▉ | 35/121 [00:00<00:00, 3466.37 it/sec, obj=-6] INFO - 13:54:05: 30%|██▉ | 36/121 [00:00<00:00, 3468.04 it/sec, obj=-5] INFO - 13:54:05: 31%|███ | 37/121 [00:00<00:00, 3469.54 it/sec, obj=-4] INFO - 13:54:05: 31%|███▏ | 38/121 [00:00<00:00, 3452.18 it/sec, obj=-3] INFO - 13:54:05: 32%|███▏ | 39/121 [00:00<00:00, 3451.81 it/sec, obj=-2] INFO - 13:54:05: 33%|███▎ | 40/121 [00:00<00:00, 3453.03 it/sec, obj=-1] INFO - 13:54:05: 34%|███▍ | 41/121 [00:00<00:00, 3454.95 it/sec, obj=0] INFO - 13:54:05: 35%|███▍ | 42/121 [00:00<00:00, 3456.91 it/sec, obj=1] INFO - 13:54:05: 36%|███▌ | 43/121 [00:00<00:00, 3458.59 it/sec, obj=2] INFO - 13:54:05: 36%|███▋ | 44/121 [00:00<00:00, 3460.78 it/sec, obj=3] INFO - 13:54:05: 37%|███▋ | 45/121 [00:00<00:00, 3463.25 it/sec, obj=-6] INFO - 13:54:05: 38%|███▊ | 46/121 [00:00<00:00, 3465.50 it/sec, obj=-5] INFO - 13:54:05: 39%|███▉ | 47/121 [00:00<00:00, 3466.55 it/sec, obj=-4] INFO - 13:54:05: 40%|███▉ | 48/121 [00:00<00:00, 3468.16 it/sec, obj=-3] INFO - 13:54:05: 40%|████ | 49/121 [00:00<00:00, 3470.11 it/sec, obj=-2] INFO - 13:54:05: 41%|████▏ | 50/121 [00:00<00:00, 3471.59 it/sec, obj=-1] INFO - 13:54:05: 42%|████▏ | 51/121 [00:00<00:00, 3473.18 it/sec, obj=0] INFO - 13:54:05: 43%|████▎ | 52/121 [00:00<00:00, 3461.75 it/sec, obj=1] INFO - 13:54:05: 44%|████▍ | 53/121 [00:00<00:00, 3461.72 it/sec, obj=2] INFO - 13:54:05: 45%|████▍ | 54/121 [00:00<00:00, 3462.87 it/sec, obj=3] INFO - 13:54:05: 45%|████▌ | 55/121 [00:00<00:00, 3464.08 it/sec, obj=4] INFO - 13:54:05: 46%|████▋ | 56/121 [00:00<00:00, 3465.80 it/sec, obj=-5] INFO - 13:54:05: 47%|████▋ | 57/121 [00:00<00:00, 3467.22 it/sec, obj=-4] INFO - 13:54:05: 48%|████▊ | 58/121 [00:00<00:00, 3468.54 it/sec, obj=-3] INFO - 13:54:05: 49%|████▉ | 59/121 [00:00<00:00, 3470.01 it/sec, obj=-2] INFO - 13:54:05: 50%|████▉ | 60/121 [00:00<00:00, 3471.67 it/sec, obj=-1] INFO - 13:54:05: 50%|█████ | 61/121 [00:00<00:00, 3473.00 it/sec, obj=0] INFO - 13:54:05: 51%|█████ | 62/121 [00:00<00:00, 3474.43 it/sec, obj=1] INFO - 13:54:05: 52%|█████▏ | 63/121 [00:00<00:00, 3475.90 it/sec, obj=2] INFO - 13:54:05: 53%|█████▎ | 64/121 [00:00<00:00, 3477.55 it/sec, obj=3] INFO - 13:54:05: 54%|█████▎ | 65/121 [00:00<00:00, 3479.20 it/sec, obj=4] INFO - 13:54:05: 55%|█████▍ | 66/121 [00:00<00:00, 3470.67 it/sec, obj=5] INFO - 13:54:05: 55%|█████▌ | 67/121 [00:00<00:00, 3470.05 it/sec, obj=-4] INFO - 13:54:05: 56%|█████▌ | 68/121 [00:00<00:00, 3471.22 it/sec, obj=-3] INFO - 13:54:05: 57%|█████▋ | 69/121 [00:00<00:00, 3472.52 it/sec, obj=-2] INFO - 13:54:05: 58%|█████▊ | 70/121 [00:00<00:00, 3473.67 it/sec, obj=-1] INFO - 13:54:05: 59%|█████▊ | 71/121 [00:00<00:00, 3474.82 it/sec, obj=0] INFO - 13:54:05: 60%|█████▉ | 72/121 [00:00<00:00, 3475.78 it/sec, obj=1] INFO - 13:54:05: 60%|██████ | 73/121 [00:00<00:00, 3477.19 it/sec, obj=2] INFO - 13:54:05: 61%|██████ | 74/121 [00:00<00:00, 3478.76 it/sec, obj=3] INFO - 13:54:05: 62%|██████▏ | 75/121 [00:00<00:00, 3480.10 it/sec, obj=4] INFO - 13:54:05: 63%|██████▎ | 76/121 [00:00<00:00, 3481.09 it/sec, obj=5] INFO - 13:54:05: 64%|██████▎ | 77/121 [00:00<00:00, 3482.33 it/sec, obj=6] INFO - 13:54:05: 64%|██████▍ | 78/121 [00:00<00:00, 3483.08 it/sec, obj=-3] INFO - 13:54:05: 65%|██████▌ | 79/121 [00:00<00:00, 3484.26 it/sec, obj=-2] INFO - 13:54:05: 66%|██████▌ | 80/121 [00:00<00:00, 3477.29 it/sec, obj=-1] INFO - 13:54:05: 67%|██████▋ | 81/121 [00:00<00:00, 3476.83 it/sec, obj=0] INFO - 13:54:05: 68%|██████▊ | 82/121 [00:00<00:00, 3476.99 it/sec, obj=1] INFO - 13:54:05: 69%|██████▊ | 83/121 [00:00<00:00, 3477.62 it/sec, obj=2] INFO - 13:54:05: 69%|██████▉ | 84/121 [00:00<00:00, 3478.59 it/sec, obj=3] INFO - 13:54:05: 70%|███████ | 85/121 [00:00<00:00, 3479.46 it/sec, obj=4] INFO - 13:54:05: 71%|███████ | 86/121 [00:00<00:00, 3457.83 it/sec, obj=5] INFO - 13:54:05: 72%|███████▏ | 87/121 [00:00<00:00, 3456.52 it/sec, obj=6] INFO - 13:54:05: 73%|███████▎ | 88/121 [00:00<00:00, 3457.41 it/sec, obj=7] INFO - 13:54:05: 74%|███████▎ | 89/121 [00:00<00:00, 3458.72 it/sec, obj=-2] INFO - 13:54:05: 74%|███████▍ | 90/121 [00:00<00:00, 3419.86 it/sec, obj=-1] INFO - 13:54:05: 75%|███████▌ | 91/121 [00:00<00:00, 3392.21 it/sec, obj=0] INFO - 13:54:05: 76%|███████▌ | 92/121 [00:00<00:00, 3383.72 it/sec, obj=1] INFO - 13:54:05: 77%|███████▋ | 93/121 [00:00<00:00, 3383.32 it/sec, obj=2] INFO - 13:54:05: 78%|███████▊ | 94/121 [00:00<00:00, 3384.10 it/sec, obj=3] INFO - 13:54:05: 79%|███████▊ | 95/121 [00:00<00:00, 3385.32 it/sec, obj=4] INFO - 13:54:05: 79%|███████▉ | 96/121 [00:00<00:00, 3386.77 it/sec, obj=5] INFO - 13:54:05: 80%|████████ | 97/121 [00:00<00:00, 3388.11 it/sec, obj=6] INFO - 13:54:05: 81%|████████ | 98/121 [00:00<00:00, 3389.76 it/sec, obj=7] INFO - 13:54:05: 82%|████████▏ | 99/121 [00:00<00:00, 3391.34 it/sec, obj=8] INFO - 13:54:05: 83%|████████▎ | 100/121 [00:00<00:00, 3392.82 it/sec, obj=-1] INFO - 13:54:05: 83%|████████▎ | 101/121 [00:00<00:00, 3394.43 it/sec, obj=0] INFO - 13:54:05: 84%|████████▍ | 102/121 [00:00<00:00, 3396.52 it/sec, obj=1] INFO - 13:54:05: 85%|████████▌ | 103/121 [00:00<00:00, 3397.99 it/sec, obj=2] INFO - 13:54:05: 86%|████████▌ | 104/121 [00:00<00:00, 3396.67 it/sec, obj=3] INFO - 13:54:05: 87%|████████▋ | 105/121 [00:00<00:00, 3390.13 it/sec, obj=4] INFO - 13:54:05: 88%|████████▊ | 106/121 [00:00<00:00, 3389.98 it/sec, obj=5] INFO - 13:54:05: 88%|████████▊ | 107/121 [00:00<00:00, 3391.07 it/sec, obj=6] INFO - 13:54:05: 89%|████████▉ | 108/121 [00:00<00:00, 3392.18 it/sec, obj=7] INFO - 13:54:05: 90%|█████████ | 109/121 [00:00<00:00, 3393.48 it/sec, obj=8] INFO - 13:54:05: 91%|█████████ | 110/121 [00:00<00:00, 3394.75 it/sec, obj=9] INFO - 13:54:05: 92%|█████████▏| 111/121 [00:00<00:00, 3396.49 it/sec, obj=0] INFO - 13:54:05: 93%|█████████▎| 112/121 [00:00<00:00, 3397.87 it/sec, obj=1] INFO - 13:54:05: 93%|█████████▎| 113/121 [00:00<00:00, 3399.17 it/sec, obj=2] INFO - 13:54:05: 94%|█████████▍| 114/121 [00:00<00:00, 3400.69 it/sec, obj=3] INFO - 13:54:05: 95%|█████████▌| 115/121 [00:00<00:00, 3402.21 it/sec, obj=4] INFO - 13:54:05: 96%|█████████▌| 116/121 [00:00<00:00, 3403.63 it/sec, obj=5] INFO - 13:54:05: 97%|█████████▋| 117/121 [00:00<00:00, 3389.86 it/sec, obj=6] INFO - 13:54:05: 98%|█████████▊| 118/121 [00:00<00:00, 3370.13 it/sec, obj=7] INFO - 13:54:05: 98%|█████████▊| 119/121 [00:00<00:00, 3367.62 it/sec, obj=8] INFO - 13:54:05: 99%|█████████▉| 120/121 [00:00<00:00, 3367.99 it/sec, obj=9] INFO - 13:54:05: 100%|██████████| 121/121 [00:00<00:00, 3368.83 it/sec, obj=10] INFO - 13:54:05: Optimization result: INFO - 13:54:05: Optimizer info: INFO - 13:54:05: Status: None INFO - 13:54:05: Message: None INFO - 13:54:05: Number of calls to the objective function by the optimizer: 121 INFO - 13:54:05: Solution: INFO - 13:54:05: Objective: -10.0 INFO - 13:54:05: Design space: INFO - 13:54:05: +------+-------------+-------+-------------+---------+ INFO - 13:54:05: | Name | Lower bound | Value | Upper bound | Type | INFO - 13:54:05: +------+-------------+-------+-------------+---------+ INFO - 13:54:05: | x[0] | -5 | -5 | 5 | integer | INFO - 13:54:05: | x[1] | -5 | -5 | 5 | integer | INFO - 13:54:05: +------+-------------+-------+-------------+---------+ .. 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.721 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 `_