.. 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-48 .. code-block:: Python 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() .. GENERATED FROM PYTHON SOURCE LINES 49-53 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 53-55 .. code-block:: Python objective = MDOFunction(np_sum, name="f", expr="sum(x)") .. GENERATED FROM PYTHON SOURCE LINES 56-59 Define the design space ----------------------- Then, we define the :class:`.DesignSpace` with |g|. .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python design_space = DesignSpace() design_space.add_variable("x", 2, lower_bound=-5, upper_bound=5, type_="integer") .. GENERATED FROM PYTHON SOURCE LINES 63-66 Define the optimization problem ------------------------------- Then, we define the :class:`.OptimizationProblem` with |g|. .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python problem = OptimizationProblem(design_space) problem.objective = objective .. GENERATED FROM PYTHON SOURCE LINES 70-75 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 75-77 .. code-block:: Python execute_algo(problem, algo_name="PYDOE_FULLFACT", n_samples=11**2, algo_type="doe") .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 11:43:12: Optimization problem: INFO - 11:43:12: minimize f = sum(x) INFO - 11:43:12: with respect to x INFO - 11:43:12: over the design space: INFO - 11:43:12: +------+-------------+-------+-------------+---------+ INFO - 11:43:12: | Name | Lower bound | Value | Upper bound | Type | INFO - 11:43:12: +------+-------------+-------+-------------+---------+ INFO - 11:43:12: | x[0] | -5 | None | 5 | integer | INFO - 11:43:12: | x[1] | -5 | None | 5 | integer | INFO - 11:43:12: +------+-------------+-------+-------------+---------+ INFO - 11:43:12: Solving optimization problem with algorithm PYDOE_FULLFACT: INFO - 11:43:12: 1%| | 1/121 [00:00<00:00, 5924.16 it/sec, obj=-10] INFO - 11:43:12: 2%|▏ | 2/121 [00:00<00:00, 3666.35 it/sec, obj=-9] INFO - 11:43:12: 2%|▏ | 3/121 [00:00<00:00, 3596.15 it/sec, obj=-8] INFO - 11:43:12: 3%|▎ | 4/121 [00:00<00:00, 3936.47 it/sec, obj=-7] INFO - 11:43:12: 4%|▍ | 5/121 [00:00<00:00, 4241.81 it/sec, obj=-6] INFO - 11:43:12: 5%|▍ | 6/121 [00:00<00:00, 4503.55 it/sec, obj=-5] INFO - 11:43:12: 6%|▌ | 7/121 [00:00<00:00, 4617.82 it/sec, obj=-4] INFO - 11:43:12: 7%|▋ | 8/121 [00:00<00:00, 4773.03 it/sec, obj=-3] INFO - 11:43:12: 7%|▋ | 9/121 [00:00<00:00, 4928.03 it/sec, obj=-2] INFO - 11:43:12: 8%|▊ | 10/121 [00:00<00:00, 5030.35 it/sec, obj=-1] INFO - 11:43:12: 9%|▉ | 11/121 [00:00<00:00, 5164.24 it/sec, obj=0] INFO - 11:43:12: 10%|▉ | 12/121 [00:00<00:00, 5283.61 it/sec, obj=-9] INFO - 11:43:12: 11%|█ | 13/121 [00:00<00:00, 5392.74 it/sec, obj=-8] INFO - 11:43:12: 12%|█▏ | 14/121 [00:00<00:00, 5433.04 it/sec, obj=-7] INFO - 11:43:12: 12%|█▏ | 15/121 [00:00<00:00, 5516.40 it/sec, obj=-6] INFO - 11:43:12: 13%|█▎ | 16/121 [00:00<00:00, 5595.67 it/sec, obj=-5] INFO - 11:43:12: 14%|█▍ | 17/121 [00:00<00:00, 5665.28 it/sec, obj=-4] INFO - 11:43:12: 15%|█▍ | 18/121 [00:00<00:00, 5726.88 it/sec, obj=-3] INFO - 11:43:12: 16%|█▌ | 19/121 [00:00<00:00, 5789.87 it/sec, obj=-2] INFO - 11:43:12: 17%|█▋ | 20/121 [00:00<00:00, 5845.31 it/sec, obj=-1] INFO - 11:43:12: 17%|█▋ | 21/121 [00:00<00:00, 5855.63 it/sec, obj=0] INFO - 11:43:12: 18%|█▊ | 22/121 [00:00<00:00, 5898.78 it/sec, obj=1] INFO - 11:43:12: 19%|█▉ | 23/121 [00:00<00:00, 5939.48 it/sec, obj=-8] INFO - 11:43:12: 20%|█▉ | 24/121 [00:00<00:00, 5979.76 it/sec, obj=-7] INFO - 11:43:12: 21%|██ | 25/121 [00:00<00:00, 6015.58 it/sec, obj=-6] INFO - 11:43:12: 21%|██▏ | 26/121 [00:00<00:00, 6049.37 it/sec, obj=-5] INFO - 11:43:12: 22%|██▏ | 27/121 [00:00<00:00, 6053.03 it/sec, obj=-4] INFO - 11:43:12: 23%|██▎ | 28/121 [00:00<00:00, 6058.94 it/sec, obj=-3] INFO - 11:43:12: 24%|██▍ | 29/121 [00:00<00:00, 6085.70 it/sec, obj=-2] INFO - 11:43:12: 25%|██▍ | 30/121 [00:00<00:00, 6109.99 it/sec, obj=-1] INFO - 11:43:12: 26%|██▌ | 31/121 [00:00<00:00, 6136.65 it/sec, obj=0] INFO - 11:43:12: 26%|██▋ | 32/121 [00:00<00:00, 6163.28 it/sec, obj=1] INFO - 11:43:12: 27%|██▋ | 33/121 [00:00<00:00, 6190.16 it/sec, obj=2] INFO - 11:43:12: 28%|██▊ | 34/121 [00:00<00:00, 6187.63 it/sec, obj=-7] INFO - 11:43:12: 29%|██▉ | 35/121 [00:00<00:00, 6202.49 it/sec, obj=-6] INFO - 11:43:12: 30%|██▉ | 36/121 [00:00<00:00, 6223.52 it/sec, obj=-5] INFO - 11:43:12: 31%|███ | 37/121 [00:00<00:00, 6229.75 it/sec, obj=-4] INFO - 11:43:12: 31%|███▏ | 38/121 [00:00<00:00, 6246.17 it/sec, obj=-3] INFO - 11:43:12: 32%|███▏ | 39/121 [00:00<00:00, 6264.23 it/sec, obj=-2] INFO - 11:43:12: 33%|███▎ | 40/121 [00:00<00:00, 6281.25 it/sec, obj=-1] INFO - 11:43:12: 34%|███▍ | 41/121 [00:00<00:00, 6277.75 it/sec, obj=0] INFO - 11:43:12: 35%|███▍ | 42/121 [00:00<00:00, 6292.35 it/sec, obj=1] INFO - 11:43:12: 36%|███▌ | 43/121 [00:00<00:00, 6305.90 it/sec, obj=2] INFO - 11:43:12: 36%|███▋ | 44/121 [00:00<00:00, 6321.92 it/sec, obj=3] INFO - 11:43:12: 37%|███▋ | 45/121 [00:00<00:00, 6339.21 it/sec, obj=-6] INFO - 11:43:12: 38%|███▊ | 46/121 [00:00<00:00, 6356.26 it/sec, obj=-5] INFO - 11:43:12: 39%|███▉ | 47/121 [00:00<00:00, 6371.03 it/sec, obj=-4] INFO - 11:43:12: 40%|███▉ | 48/121 [00:00<00:00, 6369.08 it/sec, obj=-3] INFO - 11:43:12: 40%|████ | 49/121 [00:00<00:00, 6382.64 it/sec, obj=-2] INFO - 11:43:12: 41%|████▏ | 50/121 [00:00<00:00, 6395.51 it/sec, obj=-1] INFO - 11:43:12: 42%|████▏ | 51/121 [00:00<00:00, 6407.55 it/sec, obj=0] INFO - 11:43:12: 43%|████▎ | 52/121 [00:00<00:00, 6421.43 it/sec, obj=1] INFO - 11:43:12: 44%|████▍ | 53/121 [00:00<00:00, 6432.80 it/sec, obj=2] INFO - 11:43:12: 45%|████▍ | 54/121 [00:00<00:00, 6443.96 it/sec, obj=3] INFO - 11:43:12: 45%|████▌ | 55/121 [00:00<00:00, 6436.21 it/sec, obj=4] INFO - 11:43:12: 46%|████▋ | 56/121 [00:00<00:00, 6438.45 it/sec, obj=-5] INFO - 11:43:12: 47%|████▋ | 57/121 [00:00<00:00, 6426.07 it/sec, obj=-4] INFO - 11:43:12: 48%|████▊ | 58/121 [00:00<00:00, 6431.96 it/sec, obj=-3] INFO - 11:43:12: 49%|████▉ | 59/121 [00:00<00:00, 6442.86 it/sec, obj=-2] INFO - 11:43:12: 50%|████▉ | 60/121 [00:00<00:00, 6454.60 it/sec, obj=-1] INFO - 11:43:12: 50%|█████ | 61/121 [00:00<00:00, 6450.34 it/sec, obj=0] INFO - 11:43:12: 51%|█████ | 62/121 [00:00<00:00, 6455.50 it/sec, obj=1] INFO - 11:43:12: 52%|█████▏ | 63/121 [00:00<00:00, 6466.20 it/sec, obj=2] INFO - 11:43:12: 53%|█████▎ | 64/121 [00:00<00:00, 6383.27 it/sec, obj=3] INFO - 11:43:12: 54%|█████▎ | 65/121 [00:00<00:00, 6371.94 it/sec, obj=4] INFO - 11:43:12: 55%|█████▍ | 66/121 [00:00<00:00, 6370.51 it/sec, obj=5] INFO - 11:43:12: 55%|█████▌ | 67/121 [00:00<00:00, 6359.90 it/sec, obj=-4] INFO - 11:43:12: 56%|█████▌ | 68/121 [00:00<00:00, 6364.79 it/sec, obj=-3] INFO - 11:43:12: 57%|█████▋ | 69/121 [00:00<00:00, 6369.83 it/sec, obj=-2] INFO - 11:43:12: 58%|█████▊ | 70/121 [00:00<00:00, 6375.57 it/sec, obj=-1] INFO - 11:43:12: 59%|█████▊ | 71/121 [00:00<00:00, 6384.02 it/sec, obj=0] INFO - 11:43:12: 60%|█████▉ | 72/121 [00:00<00:00, 6391.05 it/sec, obj=1] INFO - 11:43:12: 60%|██████ | 73/121 [00:00<00:00, 6398.30 it/sec, obj=2] INFO - 11:43:12: 61%|██████ | 74/121 [00:00<00:00, 6392.83 it/sec, obj=3] INFO - 11:43:12: 62%|██████▏ | 75/121 [00:00<00:00, 6398.44 it/sec, obj=4] INFO - 11:43:12: 63%|██████▎ | 76/121 [00:00<00:00, 6401.85 it/sec, obj=5] INFO - 11:43:12: 64%|██████▎ | 77/121 [00:00<00:00, 6405.80 it/sec, obj=6] INFO - 11:43:12: 64%|██████▍ | 78/121 [00:00<00:00, 6409.67 it/sec, obj=-3] INFO - 11:43:12: 65%|██████▌ | 79/121 [00:00<00:00, 6414.92 it/sec, obj=-2] INFO - 11:43:12: 66%|██████▌ | 80/121 [00:00<00:00, 6413.92 it/sec, obj=-1] INFO - 11:43:12: 67%|██████▋ | 81/121 [00:00<00:00, 6419.13 it/sec, obj=0] INFO - 11:43:12: 68%|██████▊ | 82/121 [00:00<00:00, 6426.37 it/sec, obj=1] INFO - 11:43:12: 69%|██████▊ | 83/121 [00:00<00:00, 6434.88 it/sec, obj=2] INFO - 11:43:12: 69%|██████▉ | 84/121 [00:00<00:00, 6443.33 it/sec, obj=3] INFO - 11:43:12: 70%|███████ | 85/121 [00:00<00:00, 6450.32 it/sec, obj=4] INFO - 11:43:12: 71%|███████ | 86/121 [00:00<00:00, 6456.82 it/sec, obj=5] INFO - 11:43:12: 72%|███████▏ | 87/121 [00:00<00:00, 6456.20 it/sec, obj=6] INFO - 11:43:12: 73%|███████▎ | 88/121 [00:00<00:00, 6461.93 it/sec, obj=7] INFO - 11:43:12: 74%|███████▎ | 89/121 [00:00<00:00, 6468.99 it/sec, obj=-2] INFO - 11:43:12: 74%|███████▍ | 90/121 [00:00<00:00, 6470.69 it/sec, obj=-1] INFO - 11:43:12: 75%|███████▌ | 91/121 [00:00<00:00, 6476.76 it/sec, obj=0] INFO - 11:43:12: 76%|███████▌ | 92/121 [00:00<00:00, 6484.22 it/sec, obj=1] INFO - 11:43:12: 77%|███████▋ | 93/121 [00:00<00:00, 6491.65 it/sec, obj=2] INFO - 11:43:12: 78%|███████▊ | 94/121 [00:00<00:00, 6490.81 it/sec, obj=3] INFO - 11:43:12: 79%|███████▊ | 95/121 [00:00<00:00, 6494.85 it/sec, obj=4] INFO - 11:43:12: 79%|███████▉ | 96/121 [00:00<00:00, 6501.22 it/sec, obj=5] INFO - 11:43:12: 80%|████████ | 97/121 [00:00<00:00, 6506.64 it/sec, obj=6] INFO - 11:43:12: 81%|████████ | 98/121 [00:00<00:00, 6512.58 it/sec, obj=7] INFO - 11:43:12: 82%|████████▏ | 99/121 [00:00<00:00, 6518.62 it/sec, obj=8] INFO - 11:43:12: 83%|████████▎ | 100/121 [00:00<00:00, 6523.94 it/sec, obj=-1] INFO - 11:43:12: 83%|████████▎ | 101/121 [00:00<00:00, 6521.32 it/sec, obj=0] INFO - 11:43:12: 84%|████████▍ | 102/121 [00:00<00:00, 6526.90 it/sec, obj=1] INFO - 11:43:12: 85%|████████▌ | 103/121 [00:00<00:00, 6532.20 it/sec, obj=2] INFO - 11:43:12: 86%|████████▌ | 104/121 [00:00<00:00, 6537.30 it/sec, obj=3] INFO - 11:43:12: 87%|████████▋ | 105/121 [00:00<00:00, 6543.67 it/sec, obj=4] INFO - 11:43:12: 88%|████████▊ | 106/121 [00:00<00:00, 6550.41 it/sec, obj=5] INFO - 11:43:12: 88%|████████▊ | 107/121 [00:00<00:00, 6556.38 it/sec, obj=6] INFO - 11:43:12: 89%|████████▉ | 108/121 [00:00<00:00, 6555.50 it/sec, obj=7] INFO - 11:43:12: 90%|█████████ | 109/121 [00:00<00:00, 6556.70 it/sec, obj=8] INFO - 11:43:12: 91%|█████████ | 110/121 [00:00<00:00, 6561.15 it/sec, obj=9] INFO - 11:43:12: 92%|█████████▏| 111/121 [00:00<00:00, 6566.26 it/sec, obj=0] INFO - 11:43:12: 93%|█████████▎| 112/121 [00:00<00:00, 6570.65 it/sec, obj=1] INFO - 11:43:12: 93%|█████████▎| 113/121 [00:00<00:00, 6575.42 it/sec, obj=2] INFO - 11:43:12: 94%|█████████▍| 114/121 [00:00<00:00, 6580.66 it/sec, obj=3] INFO - 11:43:12: 95%|█████████▌| 115/121 [00:00<00:00, 6577.19 it/sec, obj=4] INFO - 11:43:12: 96%|█████████▌| 116/121 [00:00<00:00, 6578.50 it/sec, obj=5] INFO - 11:43:12: 97%|█████████▋| 117/121 [00:00<00:00, 6581.90 it/sec, obj=6] INFO - 11:43:12: 98%|█████████▊| 118/121 [00:00<00:00, 6579.56 it/sec, obj=7] INFO - 11:43:12: 98%|█████████▊| 119/121 [00:00<00:00, 6579.78 it/sec, obj=8] INFO - 11:43:12: 99%|█████████▉| 120/121 [00:00<00:00, 6582.14 it/sec, obj=9] INFO - 11:43:12: 100%|██████████| 121/121 [00:00<00:00, 6586.26 it/sec, obj=10] INFO - 11:43:12: Optimization result: INFO - 11:43:12: Optimizer info: INFO - 11:43:12: Status: None INFO - 11:43:12: Message: None INFO - 11:43:12: Number of calls to the objective function by the optimizer: 121 INFO - 11:43:12: Solution: INFO - 11:43:12: Objective: -10.0 INFO - 11:43:12: Design space: INFO - 11:43:12: +------+-------------+-------+-------------+---------+ INFO - 11:43:12: | Name | Lower bound | Value | Upper bound | Type | INFO - 11:43:12: +------+-------------+-------+-------------+---------+ INFO - 11:43:12: | x[0] | -5 | -5 | 5 | integer | INFO - 11:43:12: | x[1] | -5 | -5 | 5 | integer | INFO - 11:43:12: +------+-------------+-------+-------------+---------+ .. raw:: html
Optimization result:
  • Design variables: [-5. -5.]
  • Objective function: -10.0
  • Feasible solution: True


.. GENERATED FROM PYTHON SOURCE LINES 78-80 Post-process the results ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 80-88 .. code-block:: Python execute_post( problem, post_name="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 89-90 Note that you can get all the optimization algorithms names: .. GENERATED FROM PYTHON SOURCE LINES 90-91 .. code-block:: Python get_available_opt_algorithms() .. rst-class:: sphx-glr-script-out .. code-block:: none ['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'] .. GENERATED FROM PYTHON SOURCE LINES 92-93 and all the DOE algorithms names: .. GENERATED FROM PYTHON SOURCE LINES 93-94 .. code-block:: Python get_available_doe_algorithms() .. rst-class:: sphx-glr-script-out .. code-block:: none ['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'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.465 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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_simple_opt_3.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_