.. 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 - 08:55:42: Optimization problem: INFO - 08:55:42: minimize f = sum(x) INFO - 08:55:42: with respect to x INFO - 08:55:42: over the design space: INFO - 08:55:42: +------+-------------+-------+-------------+---------+ INFO - 08:55:42: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:55:42: +------+-------------+-------+-------------+---------+ INFO - 08:55:42: | x[0] | -5 | None | 5 | integer | INFO - 08:55:42: | x[1] | -5 | None | 5 | integer | INFO - 08:55:42: +------+-------------+-------+-------------+---------+ INFO - 08:55:42: Solving optimization problem with algorithm fullfact: INFO - 08:55:42: 1%| | 1/121 [00:00<00:00, 4899.89 it/sec, obj=-10] INFO - 08:55:42: 2%|▏ | 2/121 [00:00<00:00, 3743.24 it/sec, obj=-9] INFO - 08:55:42: 2%|▏ | 3/121 [00:00<00:00, 3730.48 it/sec, obj=-8] INFO - 08:55:42: 3%|▎ | 4/121 [00:00<00:00, 3746.59 it/sec, obj=-7] INFO - 08:55:42: 4%|▍ | 5/121 [00:00<00:00, 3769.82 it/sec, obj=-6] INFO - 08:55:42: 5%|▍ | 6/121 [00:00<00:00, 3788.89 it/sec, obj=-5] INFO - 08:55:42: 6%|▌ | 7/121 [00:00<00:00, 3812.51 it/sec, obj=-4] INFO - 08:55:42: 7%|▋ | 8/121 [00:00<00:00, 3826.92 it/sec, obj=-3] INFO - 08:55:42: 7%|▋ | 9/121 [00:00<00:00, 3837.42 it/sec, obj=-2] INFO - 08:55:42: 8%|▊ | 10/121 [00:00<00:00, 3847.99 it/sec, obj=-1] INFO - 08:55:42: 9%|▉ | 11/121 [00:00<00:00, 3857.64 it/sec, obj=0] INFO - 08:55:42: 10%|▉ | 12/121 [00:00<00:00, 3863.64 it/sec, obj=-9] INFO - 08:55:42: 11%|█ | 13/121 [00:00<00:00, 3868.19 it/sec, obj=-8] INFO - 08:55:42: 12%|█▏ | 14/121 [00:00<00:00, 3875.16 it/sec, obj=-7] INFO - 08:55:42: 12%|█▏ | 15/121 [00:00<00:00, 3856.48 it/sec, obj=-6] INFO - 08:55:42: 13%|█▎ | 16/121 [00:00<00:00, 3847.32 it/sec, obj=-5] INFO - 08:55:42: 14%|█▍ | 17/121 [00:00<00:00, 3833.09 it/sec, obj=-4] INFO - 08:55:42: 15%|█▍ | 18/121 [00:00<00:00, 3836.45 it/sec, obj=-3] INFO - 08:55:42: 16%|█▌ | 19/121 [00:00<00:00, 3841.12 it/sec, obj=-2] INFO - 08:55:42: 17%|█▋ | 20/121 [00:00<00:00, 3846.04 it/sec, obj=-1] INFO - 08:55:42: 17%|█▋ | 21/121 [00:00<00:00, 3852.36 it/sec, obj=0] INFO - 08:55:42: 18%|█▊ | 22/121 [00:00<00:00, 3856.19 it/sec, obj=1] INFO - 08:55:42: 19%|█▉ | 23/121 [00:00<00:00, 3859.69 it/sec, obj=-8] INFO - 08:55:42: 20%|█▉ | 24/121 [00:00<00:00, 3863.64 it/sec, obj=-7] INFO - 08:55:42: 21%|██ | 25/121 [00:00<00:00, 3867.14 it/sec, obj=-6] INFO - 08:55:42: 21%|██▏ | 26/121 [00:00<00:00, 3870.79 it/sec, obj=-5] INFO - 08:55:42: 22%|██▏ | 27/121 [00:00<00:00, 3874.31 it/sec, obj=-4] INFO - 08:55:42: 23%|██▎ | 28/121 [00:00<00:00, 3877.20 it/sec, obj=-3] INFO - 08:55:42: 24%|██▍ | 29/121 [00:00<00:00, 3878.91 it/sec, obj=-2] INFO - 08:55:42: 25%|██▍ | 30/121 [00:00<00:00, 3880.50 it/sec, obj=-1] INFO - 08:55:42: 26%|██▌ | 31/121 [00:00<00:00, 3857.00 it/sec, obj=0] INFO - 08:55:42: 26%|██▋ | 32/121 [00:00<00:00, 3847.43 it/sec, obj=1] INFO - 08:55:42: 27%|██▋ | 33/121 [00:00<00:00, 3847.66 it/sec, obj=2] INFO - 08:55:42: 28%|██▊ | 34/121 [00:00<00:00, 3849.75 it/sec, obj=-7] INFO - 08:55:42: 29%|██▉ | 35/121 [00:00<00:00, 3851.11 it/sec, obj=-6] INFO - 08:55:42: 30%|██▉ | 36/121 [00:00<00:00, 3851.81 it/sec, obj=-5] INFO - 08:55:42: 31%|███ | 37/121 [00:00<00:00, 3853.43 it/sec, obj=-4] INFO - 08:55:42: 31%|███▏ | 38/121 [00:00<00:00, 3855.06 it/sec, obj=-3] INFO - 08:55:42: 32%|███▏ | 39/121 [00:00<00:00, 3857.42 it/sec, obj=-2] INFO - 08:55:42: 33%|███▎ | 40/121 [00:00<00:00, 3859.32 it/sec, obj=-1] INFO - 08:55:42: 34%|███▍ | 41/121 [00:00<00:00, 3861.12 it/sec, obj=0] INFO - 08:55:42: 35%|███▍ | 42/121 [00:00<00:00, 3862.58 it/sec, obj=1] INFO - 08:55:42: 36%|███▌ | 43/121 [00:00<00:00, 3864.56 it/sec, obj=2] INFO - 08:55:42: 36%|███▋ | 44/121 [00:00<00:00, 3866.61 it/sec, obj=3] INFO - 08:55:42: 37%|███▋ | 45/121 [00:00<00:00, 3851.68 it/sec, obj=-6] INFO - 08:55:42: 38%|███▊ | 46/121 [00:00<00:00, 3823.96 it/sec, obj=-5] INFO - 08:55:42: 39%|███▉ | 47/121 [00:00<00:00, 3813.22 it/sec, obj=-4] INFO - 08:55:42: 40%|███▉ | 48/121 [00:00<00:00, 3812.43 it/sec, obj=-3] INFO - 08:55:42: 40%|████ | 49/121 [00:00<00:00, 3813.78 it/sec, obj=-2] INFO - 08:55:42: 41%|████▏ | 50/121 [00:00<00:00, 3815.15 it/sec, obj=-1] INFO - 08:55:42: 42%|████▏ | 51/121 [00:00<00:00, 3817.63 it/sec, obj=0] INFO - 08:55:42: 43%|████▎ | 52/121 [00:00<00:00, 3819.61 it/sec, obj=1] INFO - 08:55:42: 44%|████▍ | 53/121 [00:00<00:00, 3820.80 it/sec, obj=2] INFO - 08:55:42: 45%|████▍ | 54/121 [00:00<00:00, 3823.04 it/sec, obj=3] INFO - 08:55:42: 45%|████▌ | 55/121 [00:00<00:00, 3825.08 it/sec, obj=4] INFO - 08:55:42: 46%|████▋ | 56/121 [00:00<00:00, 3826.98 it/sec, obj=-5] INFO - 08:55:42: 47%|████▋ | 57/121 [00:00<00:00, 3828.76 it/sec, obj=-4] INFO - 08:55:42: 48%|████▊ | 58/121 [00:00<00:00, 3830.84 it/sec, obj=-3] INFO - 08:55:42: 49%|████▉ | 59/121 [00:00<00:00, 3833.14 it/sec, obj=-2] INFO - 08:55:42: 50%|████▉ | 60/121 [00:00<00:00, 3835.08 it/sec, obj=-1] INFO - 08:55:42: 50%|█████ | 61/121 [00:00<00:00, 3830.82 it/sec, obj=0] INFO - 08:55:42: 51%|█████ | 62/121 [00:00<00:00, 3830.30 it/sec, obj=1] INFO - 08:55:42: 52%|█████▏ | 63/121 [00:00<00:00, 3827.64 it/sec, obj=2] INFO - 08:55:42: 53%|█████▎ | 64/121 [00:00<00:00, 3829.05 it/sec, obj=3] INFO - 08:55:42: 54%|█████▎ | 65/121 [00:00<00:00, 3831.17 it/sec, obj=4] INFO - 08:55:42: 55%|█████▍ | 66/121 [00:00<00:00, 3832.85 it/sec, obj=5] INFO - 08:55:42: 55%|█████▌ | 67/121 [00:00<00:00, 3833.92 it/sec, obj=-4] INFO - 08:55:42: 56%|█████▌ | 68/121 [00:00<00:00, 3835.36 it/sec, obj=-3] INFO - 08:55:42: 57%|█████▋ | 69/121 [00:00<00:00, 3837.07 it/sec, obj=-2] INFO - 08:55:42: 58%|█████▊ | 70/121 [00:00<00:00, 3838.58 it/sec, obj=-1] INFO - 08:55:42: 59%|█████▊ | 71/121 [00:00<00:00, 3840.10 it/sec, obj=0] INFO - 08:55:42: 60%|█████▉ | 72/121 [00:00<00:00, 3841.48 it/sec, obj=1] INFO - 08:55:42: 60%|██████ | 73/121 [00:00<00:00, 3843.01 it/sec, obj=2] INFO - 08:55:42: 61%|██████ | 74/121 [00:00<00:00, 3844.93 it/sec, obj=3] INFO - 08:55:42: 62%|██████▏ | 75/121 [00:00<00:00, 3846.71 it/sec, obj=4] INFO - 08:55:42: 63%|██████▎ | 76/121 [00:00<00:00, 3848.36 it/sec, obj=5] INFO - 08:55:42: 64%|██████▎ | 77/121 [00:00<00:00, 3843.77 it/sec, obj=6] INFO - 08:55:42: 64%|██████▍ | 78/121 [00:00<00:00, 3842.07 it/sec, obj=-3] INFO - 08:55:42: 65%|██████▌ | 79/121 [00:00<00:00, 3842.05 it/sec, obj=-2] INFO - 08:55:42: 66%|██████▌ | 80/121 [00:00<00:00, 3843.36 it/sec, obj=-1] INFO - 08:55:42: 67%|██████▋ | 81/121 [00:00<00:00, 3844.89 it/sec, obj=0] INFO - 08:55:42: 68%|██████▊ | 82/121 [00:00<00:00, 3846.22 it/sec, obj=1] INFO - 08:55:42: 69%|██████▊ | 83/121 [00:00<00:00, 3847.39 it/sec, obj=2] INFO - 08:55:42: 69%|██████▉ | 84/121 [00:00<00:00, 3848.78 it/sec, obj=3] INFO - 08:55:42: 70%|███████ | 85/121 [00:00<00:00, 3850.10 it/sec, obj=4] INFO - 08:55:42: 71%|███████ | 86/121 [00:00<00:00, 3831.15 it/sec, obj=5] INFO - 08:55:42: 72%|███████▏ | 87/121 [00:00<00:00, 3830.01 it/sec, obj=6] INFO - 08:55:42: 73%|███████▎ | 88/121 [00:00<00:00, 3830.93 it/sec, obj=7] INFO - 08:55:42: 74%|███████▎ | 89/121 [00:00<00:00, 3832.22 it/sec, obj=-2] INFO - 08:55:42: 74%|███████▍ | 90/121 [00:00<00:00, 3833.14 it/sec, obj=-1] INFO - 08:55:42: 75%|███████▌ | 91/121 [00:00<00:00, 3834.61 it/sec, obj=0] INFO - 08:55:42: 76%|███████▌ | 92/121 [00:00<00:00, 3831.33 it/sec, obj=1] INFO - 08:55:42: 77%|███████▋ | 93/121 [00:00<00:00, 3829.70 it/sec, obj=2] INFO - 08:55:42: 78%|███████▊ | 94/121 [00:00<00:00, 3829.74 it/sec, obj=3] INFO - 08:55:42: 79%|███████▊ | 95/121 [00:00<00:00, 3830.53 it/sec, obj=4] INFO - 08:55:42: 79%|███████▉ | 96/121 [00:00<00:00, 3831.80 it/sec, obj=5] INFO - 08:55:42: 80%|████████ | 97/121 [00:00<00:00, 3832.98 it/sec, obj=6] INFO - 08:55:42: 81%|████████ | 98/121 [00:00<00:00, 3834.35 it/sec, obj=7] INFO - 08:55:42: 82%|████████▏ | 99/121 [00:00<00:00, 3835.54 it/sec, obj=8] INFO - 08:55:42: 83%|████████▎ | 100/121 [00:00<00:00, 3836.58 it/sec, obj=-1] INFO - 08:55:42: 83%|████████▎ | 101/121 [00:00<00:00, 3837.81 it/sec, obj=0] INFO - 08:55:42: 84%|████████▍ | 102/121 [00:00<00:00, 3839.01 it/sec, obj=1] INFO - 08:55:42: 85%|████████▌ | 103/121 [00:00<00:00, 3840.12 it/sec, obj=2] INFO - 08:55:42: 86%|████████▌ | 104/121 [00:00<00:00, 3841.24 it/sec, obj=3] INFO - 08:55:42: 87%|████████▋ | 105/121 [00:00<00:00, 3842.75 it/sec, obj=4] INFO - 08:55:42: 88%|████████▊ | 106/121 [00:00<00:00, 3844.09 it/sec, obj=5] INFO - 08:55:42: 88%|████████▊ | 107/121 [00:00<00:00, 3845.48 it/sec, obj=6] INFO - 08:55:42: 89%|████████▉ | 108/121 [00:00<00:00, 3842.21 it/sec, obj=7] INFO - 08:55:42: 90%|█████████ | 109/121 [00:00<00:00, 3840.74 it/sec, obj=8] INFO - 08:55:42: 91%|█████████ | 110/121 [00:00<00:00, 3841.16 it/sec, obj=9] INFO - 08:55:42: 92%|█████████▏| 111/121 [00:00<00:00, 3842.33 it/sec, obj=0] INFO - 08:55:42: 93%|█████████▎| 112/121 [00:00<00:00, 3843.29 it/sec, obj=1] INFO - 08:55:42: 93%|█████████▎| 113/121 [00:00<00:00, 3844.49 it/sec, obj=2] INFO - 08:55:42: 94%|█████████▍| 114/121 [00:00<00:00, 3845.79 it/sec, obj=3] INFO - 08:55:42: 95%|█████████▌| 115/121 [00:00<00:00, 3846.91 it/sec, obj=4] INFO - 08:55:42: 96%|█████████▌| 116/121 [00:00<00:00, 3847.99 it/sec, obj=5] INFO - 08:55:42: 97%|█████████▋| 117/121 [00:00<00:00, 3849.19 it/sec, obj=6] INFO - 08:55:42: 98%|█████████▊| 118/121 [00:00<00:00, 3850.29 it/sec, obj=7] INFO - 08:55:42: 98%|█████████▊| 119/121 [00:00<00:00, 3850.78 it/sec, obj=8] INFO - 08:55:42: 99%|█████████▉| 120/121 [00:00<00:00, 3851.75 it/sec, obj=9] INFO - 08:55:42: 100%|██████████| 121/121 [00:00<00:00, 3852.57 it/sec, obj=10] INFO - 08:55:42: Optimization result: INFO - 08:55:42: Optimizer info: INFO - 08:55:42: Status: None INFO - 08:55:42: Message: None INFO - 08:55:42: Number of calls to the objective function by the optimizer: 121 INFO - 08:55:42: Solution: INFO - 08:55:42: Objective: -10.0 INFO - 08:55:42: Design space: INFO - 08:55:42: +------+-------------+-------+-------------+---------+ INFO - 08:55:42: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:55:42: +------+-------------+-------+-------------+---------+ INFO - 08:55:42: | x[0] | -5 | -5 | 5 | integer | INFO - 08:55:42: | x[1] | -5 | -5 | 5 | integer | INFO - 08:55:42: +------+-------------+-------+-------------+---------+ .. 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.658 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 `_