.. 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:07:45: Optimization problem: INFO - 13:07:45: minimize f = sum(x) INFO - 13:07:45: with respect to x INFO - 13:07:45: over the design space: INFO - 13:07:45: +------+-------------+-------+-------------+---------+ INFO - 13:07:45: | Name | Lower bound | Value | Upper bound | Type | INFO - 13:07:45: +------+-------------+-------+-------------+---------+ INFO - 13:07:45: | x[0] | -5 | None | 5 | integer | INFO - 13:07:45: | x[1] | -5 | None | 5 | integer | INFO - 13:07:45: +------+-------------+-------+-------------+---------+ INFO - 13:07:45: Solving optimization problem with algorithm fullfact: INFO - 13:07:45: 1%| | 1/121 [00:00<00:00, 3855.06 it/sec, obj=-10] INFO - 13:07:45: 2%|▏ | 2/121 [00:00<00:00, 2833.99 it/sec, obj=-9] INFO - 13:07:45: 2%|▏ | 3/121 [00:00<00:00, 2870.84 it/sec, obj=-8] INFO - 13:07:45: 3%|▎ | 4/121 [00:00<00:00, 2938.22 it/sec, obj=-7] INFO - 13:07:45: 4%|▍ | 5/121 [00:00<00:00, 3000.22 it/sec, obj=-6] INFO - 13:07:45: 5%|▍ | 6/121 [00:00<00:00, 3002.72 it/sec, obj=-5] INFO - 13:07:45: 6%|▌ | 7/121 [00:00<00:00, 3029.32 it/sec, obj=-4] INFO - 13:07:45: 7%|▋ | 8/121 [00:00<00:00, 3062.93 it/sec, obj=-3] INFO - 13:07:45: 7%|▋ | 9/121 [00:00<00:00, 3093.91 it/sec, obj=-2] INFO - 13:07:45: 8%|▊ | 10/121 [00:00<00:00, 3116.36 it/sec, obj=-1] INFO - 13:07:45: 9%|▉ | 11/121 [00:00<00:00, 3139.88 it/sec, obj=0] INFO - 13:07:45: 10%|▉ | 12/121 [00:00<00:00, 3157.97 it/sec, obj=-9] INFO - 13:07:45: 11%|█ | 13/121 [00:00<00:00, 3174.54 it/sec, obj=-8] INFO - 13:07:45: 12%|█▏ | 14/121 [00:00<00:00, 3190.45 it/sec, obj=-7] INFO - 13:07:45: 12%|█▏ | 15/121 [00:00<00:00, 3158.04 it/sec, obj=-6] INFO - 13:07:45: 13%|█▎ | 16/121 [00:00<00:00, 3159.55 it/sec, obj=-5] INFO - 13:07:45: 14%|█▍ | 17/121 [00:00<00:00, 3169.59 it/sec, obj=-4] INFO - 13:07:45: 15%|█▍ | 18/121 [00:00<00:00, 3180.58 it/sec, obj=-3] INFO - 13:07:45: 16%|█▌ | 19/121 [00:00<00:00, 3180.55 it/sec, obj=-2] INFO - 13:07:45: 17%|█▋ | 20/121 [00:00<00:00, 3187.04 it/sec, obj=-1] INFO - 13:07:45: 17%|█▋ | 21/121 [00:00<00:00, 3195.49 it/sec, obj=0] INFO - 13:07:45: 18%|█▊ | 22/121 [00:00<00:00, 3203.43 it/sec, obj=1] INFO - 13:07:45: 19%|█▉ | 23/121 [00:00<00:00, 3210.50 it/sec, obj=-8] INFO - 13:07:45: 20%|█▉ | 24/121 [00:00<00:00, 3218.14 it/sec, obj=-7] INFO - 13:07:45: 21%|██ | 25/121 [00:00<00:00, 3224.60 it/sec, obj=-6] INFO - 13:07:45: 21%|██▏ | 26/121 [00:00<00:00, 3231.07 it/sec, obj=-5] INFO - 13:07:45: 22%|██▏ | 27/121 [00:00<00:00, 3236.81 it/sec, obj=-4] INFO - 13:07:45: 23%|██▎ | 28/121 [00:00<00:00, 3221.87 it/sec, obj=-3] INFO - 13:07:45: 24%|██▍ | 29/121 [00:00<00:00, 3221.94 it/sec, obj=-2] INFO - 13:07:45: 25%|██▍ | 30/121 [00:00<00:00, 3226.06 it/sec, obj=-1] INFO - 13:07:45: 26%|██▌ | 31/121 [00:00<00:00, 3229.91 it/sec, obj=0] INFO - 13:07:45: 26%|██▋ | 32/121 [00:00<00:00, 3228.41 it/sec, obj=1] INFO - 13:07:45: 27%|██▋ | 33/121 [00:00<00:00, 3228.57 it/sec, obj=2] INFO - 13:07:45: 28%|██▊ | 34/121 [00:00<00:00, 3231.80 it/sec, obj=-7] INFO - 13:07:45: 29%|██▉ | 35/121 [00:00<00:00, 3235.85 it/sec, obj=-6] INFO - 13:07:45: 30%|██▉ | 36/121 [00:00<00:00, 3239.47 it/sec, obj=-5] INFO - 13:07:45: 31%|███ | 37/121 [00:00<00:00, 3243.38 it/sec, obj=-4] INFO - 13:07:45: 31%|███▏ | 38/121 [00:00<00:00, 3247.29 it/sec, obj=-3] INFO - 13:07:45: 32%|███▏ | 39/121 [00:00<00:00, 3251.20 it/sec, obj=-2] INFO - 13:07:45: 33%|███▎ | 40/121 [00:00<00:00, 3254.74 it/sec, obj=-1] INFO - 13:07:45: 34%|███▍ | 41/121 [00:00<00:00, 3243.85 it/sec, obj=0] INFO - 13:07:45: 35%|███▍ | 42/121 [00:00<00:00, 3239.26 it/sec, obj=1] INFO - 13:07:45: 36%|███▌ | 43/121 [00:00<00:00, 3226.85 it/sec, obj=2] INFO - 13:07:45: 36%|███▋ | 44/121 [00:00<00:00, 3224.13 it/sec, obj=3] INFO - 13:07:45: 37%|███▋ | 45/121 [00:00<00:00, 3221.21 it/sec, obj=-6] INFO - 13:07:45: 38%|███▊ | 46/121 [00:00<00:00, 3222.78 it/sec, obj=-5] INFO - 13:07:45: 39%|███▉ | 47/121 [00:00<00:00, 3225.75 it/sec, obj=-4] INFO - 13:07:45: 40%|███▉ | 48/121 [00:00<00:00, 3228.82 it/sec, obj=-3] INFO - 13:07:45: 40%|████ | 49/121 [00:00<00:00, 3231.66 it/sec, obj=-2] INFO - 13:07:45: 41%|████▏ | 50/121 [00:00<00:00, 3234.05 it/sec, obj=-1] INFO - 13:07:45: 42%|████▏ | 51/121 [00:00<00:00, 3237.03 it/sec, obj=0] INFO - 13:07:45: 43%|████▎ | 52/121 [00:00<00:00, 3239.86 it/sec, obj=1] INFO - 13:07:45: 44%|████▍ | 53/121 [00:00<00:00, 3242.20 it/sec, obj=2] INFO - 13:07:45: 45%|████▍ | 54/121 [00:00<00:00, 3235.93 it/sec, obj=3] INFO - 13:07:45: 45%|████▌ | 55/121 [00:00<00:00, 3236.07 it/sec, obj=4] INFO - 13:07:45: 46%|████▋ | 56/121 [00:00<00:00, 3238.13 it/sec, obj=-5] INFO - 13:07:45: 47%|████▋ | 57/121 [00:00<00:00, 3240.38 it/sec, obj=-4] INFO - 13:07:45: 48%|████▊ | 58/121 [00:00<00:00, 3239.53 it/sec, obj=-3] INFO - 13:07:45: 49%|████▉ | 59/121 [00:00<00:00, 3239.82 it/sec, obj=-2] INFO - 13:07:45: 50%|████▉ | 60/121 [00:00<00:00, 3241.93 it/sec, obj=-1] INFO - 13:07:45: 50%|█████ | 61/121 [00:00<00:00, 3244.06 it/sec, obj=0] INFO - 13:07:45: 51%|█████ | 62/121 [00:00<00:00, 3246.41 it/sec, obj=1] INFO - 13:07:45: 52%|█████▏ | 63/121 [00:00<00:00, 3248.60 it/sec, obj=2] INFO - 13:07:45: 53%|█████▎ | 64/121 [00:00<00:00, 3251.20 it/sec, obj=3] INFO - 13:07:45: 54%|█████▎ | 65/121 [00:00<00:00, 3253.30 it/sec, obj=4] INFO - 13:07:45: 55%|█████▍ | 66/121 [00:00<00:00, 3255.45 it/sec, obj=5] INFO - 13:07:45: 55%|█████▌ | 67/121 [00:00<00:00, 3250.83 it/sec, obj=-4] INFO - 13:07:45: 56%|█████▌ | 68/121 [00:00<00:00, 3250.10 it/sec, obj=-3] INFO - 13:07:45: 57%|█████▋ | 69/121 [00:00<00:00, 3252.20 it/sec, obj=-2] INFO - 13:07:45: 58%|█████▊ | 70/121 [00:00<00:00, 3254.28 it/sec, obj=-1] INFO - 13:07:45: 59%|█████▊ | 71/121 [00:00<00:00, 3256.30 it/sec, obj=0] INFO - 13:07:45: 60%|█████▉ | 72/121 [00:00<00:00, 3253.71 it/sec, obj=1] INFO - 13:07:45: 60%|██████ | 73/121 [00:00<00:00, 3255.34 it/sec, obj=2] INFO - 13:07:45: 61%|██████ | 74/121 [00:00<00:00, 3257.61 it/sec, obj=3] INFO - 13:07:45: 62%|██████▏ | 75/121 [00:00<00:00, 3259.55 it/sec, obj=4] INFO - 13:07:45: 63%|██████▎ | 76/121 [00:00<00:00, 3261.18 it/sec, obj=5] INFO - 13:07:45: 64%|██████▎ | 77/121 [00:00<00:00, 3262.93 it/sec, obj=6] INFO - 13:07:45: 64%|██████▍ | 78/121 [00:00<00:00, 3264.93 it/sec, obj=-3] INFO - 13:07:45: 65%|██████▌ | 79/121 [00:00<00:00, 3266.72 it/sec, obj=-2] INFO - 13:07:45: 66%|██████▌ | 80/121 [00:00<00:00, 3268.53 it/sec, obj=-1] INFO - 13:07:45: 67%|██████▋ | 81/121 [00:00<00:00, 3263.77 it/sec, obj=0] INFO - 13:07:45: 68%|██████▊ | 82/121 [00:00<00:00, 3264.76 it/sec, obj=1] INFO - 13:07:45: 69%|██████▊ | 83/121 [00:00<00:00, 3265.83 it/sec, obj=2] INFO - 13:07:45: 69%|██████▉ | 84/121 [00:00<00:00, 3267.44 it/sec, obj=3] INFO - 13:07:45: 70%|███████ | 85/121 [00:00<00:00, 3240.29 it/sec, obj=4] INFO - 13:07:45: 71%|███████ | 86/121 [00:00<00:00, 3239.31 it/sec, obj=5] INFO - 13:07:45: 72%|███████▏ | 87/121 [00:00<00:00, 3240.48 it/sec, obj=6] INFO - 13:07:45: 73%|███████▎ | 88/121 [00:00<00:00, 3242.09 it/sec, obj=7] INFO - 13:07:45: 74%|███████▎ | 89/121 [00:00<00:00, 3243.71 it/sec, obj=-2] INFO - 13:07:45: 74%|███████▍ | 90/121 [00:00<00:00, 3245.14 it/sec, obj=-1] INFO - 13:07:45: 75%|███████▌ | 91/121 [00:00<00:00, 3246.92 it/sec, obj=0] INFO - 13:07:45: 76%|███████▌ | 92/121 [00:00<00:00, 3248.58 it/sec, obj=1] INFO - 13:07:45: 77%|███████▋ | 93/121 [00:00<00:00, 3245.37 it/sec, obj=2] INFO - 13:07:45: 78%|███████▊ | 94/121 [00:00<00:00, 3245.08 it/sec, obj=3] INFO - 13:07:45: 79%|███████▊ | 95/121 [00:00<00:00, 3246.07 it/sec, obj=4] INFO - 13:07:45: 79%|███████▉ | 96/121 [00:00<00:00, 3247.62 it/sec, obj=5] INFO - 13:07:45: 80%|████████ | 97/121 [00:00<00:00, 3247.48 it/sec, obj=6] INFO - 13:07:45: 81%|████████ | 98/121 [00:00<00:00, 3246.60 it/sec, obj=7] INFO - 13:07:45: 82%|████████▏ | 99/121 [00:00<00:00, 3247.51 it/sec, obj=8] INFO - 13:07:45: 83%|████████▎ | 100/121 [00:00<00:00, 3248.65 it/sec, obj=-1] INFO - 13:07:45: 83%|████████▎ | 101/121 [00:00<00:00, 3249.93 it/sec, obj=0] INFO - 13:07:45: 84%|████████▍ | 102/121 [00:00<00:00, 3251.20 it/sec, obj=1] INFO - 13:07:45: 85%|████████▌ | 103/121 [00:00<00:00, 3252.48 it/sec, obj=2] INFO - 13:07:45: 86%|████████▌ | 104/121 [00:00<00:00, 3253.56 it/sec, obj=3] INFO - 13:07:45: 87%|████████▋ | 105/121 [00:00<00:00, 3255.00 it/sec, obj=4] INFO - 13:07:45: 88%|████████▊ | 106/121 [00:00<00:00, 3256.49 it/sec, obj=5] INFO - 13:07:45: 88%|████████▊ | 107/121 [00:00<00:00, 3252.69 it/sec, obj=6] INFO - 13:07:45: 89%|████████▉ | 108/121 [00:00<00:00, 3253.64 it/sec, obj=7] INFO - 13:07:45: 90%|█████████ | 109/121 [00:00<00:00, 3254.48 it/sec, obj=8] INFO - 13:07:45: 91%|█████████ | 110/121 [00:00<00:00, 3255.57 it/sec, obj=9] INFO - 13:07:45: 92%|█████████▏| 111/121 [00:00<00:00, 3254.10 it/sec, obj=0] INFO - 13:07:45: 93%|█████████▎| 112/121 [00:00<00:00, 3254.64 it/sec, obj=1] INFO - 13:07:45: 93%|█████████▎| 113/121 [00:00<00:00, 3255.49 it/sec, obj=2] INFO - 13:07:45: 94%|█████████▍| 114/121 [00:00<00:00, 3256.54 it/sec, obj=3] INFO - 13:07:45: 95%|█████████▌| 115/121 [00:00<00:00, 3257.55 it/sec, obj=4] INFO - 13:07:45: 96%|█████████▌| 116/121 [00:00<00:00, 3258.63 it/sec, obj=5] INFO - 13:07:45: 97%|█████████▋| 117/121 [00:00<00:00, 3259.76 it/sec, obj=6] INFO - 13:07:45: 98%|█████████▊| 118/121 [00:00<00:00, 3260.76 it/sec, obj=7] INFO - 13:07:45: 98%|█████████▊| 119/121 [00:00<00:00, 3261.47 it/sec, obj=8] INFO - 13:07:45: 99%|█████████▉| 120/121 [00:00<00:00, 3254.03 it/sec, obj=9] INFO - 13:07:45: 100%|██████████| 121/121 [00:00<00:00, 3253.63 it/sec, obj=10] INFO - 13:07:45: Optimization result: INFO - 13:07:45: Optimizer info: INFO - 13:07:45: Status: None INFO - 13:07:45: Message: None INFO - 13:07:45: Number of calls to the objective function by the optimizer: 121 INFO - 13:07:45: Solution: INFO - 13:07:45: Objective: -10.0 INFO - 13:07:45: Design space: INFO - 13:07:45: +------+-------------+-------+-------------+---------+ INFO - 13:07:45: | Name | Lower bound | Value | Upper bound | Type | INFO - 13:07:45: +------+-------------+-------+-------------+---------+ INFO - 13:07:45: | x[0] | -5 | -5 | 5 | integer | INFO - 13:07:45: | x[1] | -5 | -5 | 5 | integer | INFO - 13:07:45: +------+-------------+-------+-------------+---------+ .. 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.794 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 `_