.. 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 - 20:38:18: Optimization problem: INFO - 20:38:18: minimize f = sum(x) INFO - 20:38:18: with respect to x INFO - 20:38:18: over the design space: INFO - 20:38:18: +------+-------------+-------+-------------+---------+ INFO - 20:38:18: | Name | Lower bound | Value | Upper bound | Type | INFO - 20:38:18: +------+-------------+-------+-------------+---------+ INFO - 20:38:18: | x[0] | -5 | None | 5 | integer | INFO - 20:38:18: | x[1] | -5 | None | 5 | integer | INFO - 20:38:18: +------+-------------+-------+-------------+---------+ INFO - 20:38:18: Solving optimization problem with algorithm PYDOE_FULLFACT: INFO - 20:38:18: 1%| | 1/121 [00:00<00:00, 6574.14 it/sec, obj=-10] INFO - 20:38:18: 2%|▏ | 2/121 [00:00<00:00, 5761.41 it/sec, obj=-9] INFO - 20:38:18: 2%|▏ | 3/121 [00:00<00:00, 6055.30 it/sec, obj=-8] INFO - 20:38:18: 3%|▎ | 4/121 [00:00<00:00, 6435.45 it/sec, obj=-7] INFO - 20:38:18: 4%|▍ | 5/121 [00:00<00:00, 6710.89 it/sec, obj=-6] INFO - 20:38:18: 5%|▍ | 6/121 [00:00<00:00, 6915.59 it/sec, obj=-5] INFO - 20:38:18: 6%|▌ | 7/121 [00:00<00:00, 7131.44 it/sec, obj=-4] INFO - 20:38:18: 7%|▋ | 8/121 [00:00<00:00, 7305.56 it/sec, obj=-3] INFO - 20:38:18: 7%|▋ | 9/121 [00:00<00:00, 7457.28 it/sec, obj=-2] INFO - 20:38:18: 8%|▊ | 10/121 [00:00<00:00, 7449.92 it/sec, obj=-1] INFO - 20:38:18: 9%|▉ | 11/121 [00:00<00:00, 7514.23 it/sec, obj=0] INFO - 20:38:18: 10%|▉ | 12/121 [00:00<00:00, 7592.65 it/sec, obj=-9] INFO - 20:38:18: 11%|█ | 13/121 [00:00<00:00, 7687.29 it/sec, obj=-8] INFO - 20:38:18: 12%|█▏ | 14/121 [00:00<00:00, 7767.23 it/sec, obj=-7] INFO - 20:38:18: 12%|█▏ | 15/121 [00:00<00:00, 7770.11 it/sec, obj=-6] INFO - 20:38:18: 13%|█▎ | 16/121 [00:00<00:00, 7783.45 it/sec, obj=-5] INFO - 20:38:18: 14%|█▍ | 17/121 [00:00<00:00, 7831.21 it/sec, obj=-4] INFO - 20:38:18: 15%|█▍ | 18/121 [00:00<00:00, 7869.24 it/sec, obj=-3] INFO - 20:38:18: 16%|█▌ | 19/121 [00:00<00:00, 7829.81 it/sec, obj=-2] INFO - 20:38:18: 17%|█▋ | 20/121 [00:00<00:00, 7878.85 it/sec, obj=-1] INFO - 20:38:18: 17%|█▋ | 21/121 [00:00<00:00, 7928.74 it/sec, obj=0] INFO - 20:38:18: 18%|█▊ | 22/121 [00:00<00:00, 7971.21 it/sec, obj=1] INFO - 20:38:18: 19%|█▉ | 23/121 [00:00<00:00, 8013.04 it/sec, obj=-8] INFO - 20:38:18: 20%|█▉ | 24/121 [00:00<00:00, 8056.29 it/sec, obj=-7] INFO - 20:38:18: 21%|██ | 25/121 [00:00<00:00, 8090.86 it/sec, obj=-6] INFO - 20:38:18: 21%|██▏ | 26/121 [00:00<00:00, 8126.07 it/sec, obj=-5] INFO - 20:38:18: 22%|██▏ | 27/121 [00:00<00:00, 8119.17 it/sec, obj=-4] INFO - 20:38:18: 23%|██▎ | 28/121 [00:00<00:00, 8143.15 it/sec, obj=-3] INFO - 20:38:18: 24%|██▍ | 29/121 [00:00<00:00, 8174.93 it/sec, obj=-2] INFO - 20:38:18: 25%|██▍ | 30/121 [00:00<00:00, 8205.36 it/sec, obj=-1] INFO - 20:38:18: 26%|██▌ | 31/121 [00:00<00:00, 8239.76 it/sec, obj=0] INFO - 20:38:18: 26%|██▋ | 32/121 [00:00<00:00, 8273.81 it/sec, obj=1] INFO - 20:38:18: 27%|██▋ | 33/121 [00:00<00:00, 8304.56 it/sec, obj=2] INFO - 20:38:18: 28%|██▊ | 34/121 [00:00<00:00, 8318.15 it/sec, obj=-7] INFO - 20:38:18: 29%|██▉ | 35/121 [00:00<00:00, 8340.00 it/sec, obj=-6] INFO - 20:38:18: 30%|██▉ | 36/121 [00:00<00:00, 8331.67 it/sec, obj=-5] INFO - 20:38:18: 31%|███ | 37/121 [00:00<00:00, 8317.57 it/sec, obj=-4] INFO - 20:38:18: 31%|███▏ | 38/121 [00:00<00:00, 8330.30 it/sec, obj=-3] INFO - 20:38:18: 32%|███▏ | 39/121 [00:00<00:00, 8347.09 it/sec, obj=-2] INFO - 20:38:18: 33%|███▎ | 40/121 [00:00<00:00, 8370.19 it/sec, obj=-1] INFO - 20:38:18: 34%|███▍ | 41/121 [00:00<00:00, 8389.43 it/sec, obj=0] INFO - 20:38:18: 35%|███▍ | 42/121 [00:00<00:00, 8407.42 it/sec, obj=1] INFO - 20:38:18: 36%|███▌ | 43/121 [00:00<00:00, 8422.30 it/sec, obj=2] INFO - 20:38:18: 36%|███▋ | 44/121 [00:00<00:00, 8439.24 it/sec, obj=3] INFO - 20:38:18: 37%|███▋ | 45/121 [00:00<00:00, 8424.18 it/sec, obj=-6] INFO - 20:38:18: 38%|███▊ | 46/121 [00:00<00:00, 8429.66 it/sec, obj=-5] INFO - 20:38:18: 39%|███▉ | 47/121 [00:00<00:00, 8447.56 it/sec, obj=-4] INFO - 20:38:18: 40%|███▉ | 48/121 [00:00<00:00, 8469.06 it/sec, obj=-3] INFO - 20:38:18: 40%|████ | 49/121 [00:00<00:00, 8494.00 it/sec, obj=-2] INFO - 20:38:18: 41%|████▏ | 50/121 [00:00<00:00, 8511.17 it/sec, obj=-1] INFO - 20:38:18: 42%|████▏ | 51/121 [00:00<00:00, 8505.35 it/sec, obj=0] INFO - 20:38:18: 43%|████▎ | 52/121 [00:00<00:00, 8519.35 it/sec, obj=1] INFO - 20:38:18: 44%|████▍ | 53/121 [00:00<00:00, 8534.17 it/sec, obj=2] INFO - 20:38:18: 45%|████▍ | 54/121 [00:00<00:00, 8528.86 it/sec, obj=3] INFO - 20:38:18: 45%|████▌ | 55/121 [00:00<00:00, 8538.58 it/sec, obj=4] INFO - 20:38:18: 46%|████▋ | 56/121 [00:00<00:00, 8557.00 it/sec, obj=-5] INFO - 20:38:18: 47%|████▋ | 57/121 [00:00<00:00, 8576.08 it/sec, obj=-4] INFO - 20:38:18: 48%|████▊ | 58/121 [00:00<00:00, 8594.58 it/sec, obj=-3] INFO - 20:38:18: 49%|████▉ | 59/121 [00:00<00:00, 8613.43 it/sec, obj=-2] INFO - 20:38:18: 50%|████▉ | 60/121 [00:00<00:00, 8632.62 it/sec, obj=-1] INFO - 20:38:18: 50%|█████ | 61/121 [00:00<00:00, 8649.80 it/sec, obj=0] INFO - 20:38:18: 51%|█████ | 62/121 [00:00<00:00, 8665.92 it/sec, obj=1] INFO - 20:38:18: 52%|█████▏ | 63/121 [00:00<00:00, 8529.96 it/sec, obj=2] INFO - 20:38:18: 53%|█████▎ | 64/121 [00:00<00:00, 8525.01 it/sec, obj=3] INFO - 20:38:18: 54%|█████▎ | 65/121 [00:00<00:00, 8535.68 it/sec, obj=4] INFO - 20:38:18: 55%|█████▍ | 66/121 [00:00<00:00, 8536.84 it/sec, obj=5] INFO - 20:38:18: 55%|█████▌ | 67/121 [00:00<00:00, 8547.31 it/sec, obj=-4] INFO - 20:38:18: 56%|█████▌ | 68/121 [00:00<00:00, 8560.32 it/sec, obj=-3] INFO - 20:38:18: 57%|█████▋ | 69/121 [00:00<00:00, 8572.74 it/sec, obj=-2] INFO - 20:38:18: 58%|█████▊ | 70/121 [00:00<00:00, 8583.08 it/sec, obj=-1] INFO - 20:38:18: 59%|█████▊ | 71/121 [00:00<00:00, 8594.39 it/sec, obj=0] INFO - 20:38:18: 60%|█████▉ | 72/121 [00:00<00:00, 8574.63 it/sec, obj=1] INFO - 20:38:18: 60%|██████ | 73/121 [00:00<00:00, 8583.32 it/sec, obj=2] INFO - 20:38:18: 61%|██████ | 74/121 [00:00<00:00, 8594.17 it/sec, obj=3] INFO - 20:38:18: 62%|██████▏ | 75/121 [00:00<00:00, 8604.29 it/sec, obj=4] INFO - 20:38:18: 63%|██████▎ | 76/121 [00:00<00:00, 8615.79 it/sec, obj=5] INFO - 20:38:18: 64%|██████▎ | 77/121 [00:00<00:00, 8629.10 it/sec, obj=6] INFO - 20:38:18: 64%|██████▍ | 78/121 [00:00<00:00, 8639.60 it/sec, obj=-3] INFO - 20:38:18: 65%|██████▌ | 79/121 [00:00<00:00, 8652.34 it/sec, obj=-2] INFO - 20:38:18: 66%|██████▌ | 80/121 [00:00<00:00, 8666.59 it/sec, obj=-1] INFO - 20:38:18: 67%|██████▋ | 81/121 [00:00<00:00, 8658.85 it/sec, obj=0] INFO - 20:38:18: 68%|██████▊ | 82/121 [00:00<00:00, 8663.30 it/sec, obj=1] INFO - 20:38:18: 69%|██████▊ | 83/121 [00:00<00:00, 8675.64 it/sec, obj=2] INFO - 20:38:18: 69%|██████▉ | 84/121 [00:00<00:00, 8687.50 it/sec, obj=3] INFO - 20:38:18: 70%|███████ | 85/121 [00:00<00:00, 8692.96 it/sec, obj=4] INFO - 20:38:18: 71%|███████ | 86/121 [00:00<00:00, 8700.62 it/sec, obj=5] INFO - 20:38:18: 72%|███████▏ | 87/121 [00:00<00:00, 8710.60 it/sec, obj=6] INFO - 20:38:18: 73%|███████▎ | 88/121 [00:00<00:00, 8722.03 it/sec, obj=7] INFO - 20:38:18: 74%|███████▎ | 89/121 [00:00<00:00, 8732.41 it/sec, obj=-2] INFO - 20:38:18: 74%|███████▍ | 90/121 [00:00<00:00, 8727.02 it/sec, obj=-1] INFO - 20:38:18: 75%|███████▌ | 91/121 [00:00<00:00, 8726.94 it/sec, obj=0] INFO - 20:38:18: 76%|███████▌ | 92/121 [00:00<00:00, 8733.98 it/sec, obj=1] INFO - 20:38:18: 77%|███████▋ | 93/121 [00:00<00:00, 8743.03 it/sec, obj=2] INFO - 20:38:18: 78%|███████▊ | 94/121 [00:00<00:00, 8751.52 it/sec, obj=3] INFO - 20:38:18: 79%|███████▊ | 95/121 [00:00<00:00, 8759.65 it/sec, obj=4] INFO - 20:38:18: 79%|███████▉ | 96/121 [00:00<00:00, 8767.05 it/sec, obj=5] INFO - 20:38:18: 80%|████████ | 97/121 [00:00<00:00, 8774.88 it/sec, obj=6] INFO - 20:38:18: 81%|████████ | 98/121 [00:00<00:00, 8782.57 it/sec, obj=7] INFO - 20:38:18: 82%|████████▏ | 99/121 [00:00<00:00, 8792.16 it/sec, obj=8] INFO - 20:38:18: 83%|████████▎ | 100/121 [00:00<00:00, 8782.78 it/sec, obj=-1] INFO - 20:38:18: 83%|████████▎ | 101/121 [00:00<00:00, 8788.16 it/sec, obj=0] INFO - 20:38:18: 84%|████████▍ | 102/121 [00:00<00:00, 8796.71 it/sec, obj=1] INFO - 20:38:18: 85%|████████▌ | 103/121 [00:00<00:00, 8803.30 it/sec, obj=2] INFO - 20:38:18: 86%|████████▌ | 104/121 [00:00<00:00, 8811.03 it/sec, obj=3] INFO - 20:38:18: 87%|████████▋ | 105/121 [00:00<00:00, 8820.74 it/sec, obj=4] INFO - 20:38:18: 88%|████████▊ | 106/121 [00:00<00:00, 8829.24 it/sec, obj=5] INFO - 20:38:18: 88%|████████▊ | 107/121 [00:00<00:00, 8836.37 it/sec, obj=6] INFO - 20:38:18: 89%|████████▉ | 108/121 [00:00<00:00, 8842.70 it/sec, obj=7] INFO - 20:38:18: 90%|█████████ | 109/121 [00:00<00:00, 8824.49 it/sec, obj=8] INFO - 20:38:18: 91%|█████████ | 110/121 [00:00<00:00, 8825.55 it/sec, obj=9] INFO - 20:38:18: 92%|█████████▏| 111/121 [00:00<00:00, 8833.63 it/sec, obj=0] INFO - 20:38:18: 93%|█████████▎| 112/121 [00:00<00:00, 8840.42 it/sec, obj=1] INFO - 20:38:18: 93%|█████████▎| 113/121 [00:00<00:00, 8845.94 it/sec, obj=2] INFO - 20:38:18: 94%|█████████▍| 114/121 [00:00<00:00, 8851.36 it/sec, obj=3] INFO - 20:38:18: 95%|█████████▌| 115/121 [00:00<00:00, 8857.52 it/sec, obj=4] INFO - 20:38:18: 96%|█████████▌| 116/121 [00:00<00:00, 8862.93 it/sec, obj=5] INFO - 20:38:18: 97%|█████████▋| 117/121 [00:00<00:00, 8868.73 it/sec, obj=6] INFO - 20:38:18: 98%|█████████▊| 118/121 [00:00<00:00, 8863.48 it/sec, obj=7] INFO - 20:38:18: 98%|█████████▊| 119/121 [00:00<00:00, 8861.00 it/sec, obj=8] INFO - 20:38:18: 99%|█████████▉| 120/121 [00:00<00:00, 8865.89 it/sec, obj=9] INFO - 20:38:18: 100%|██████████| 121/121 [00:00<00:00, 8870.24 it/sec, obj=10] INFO - 20:38:18: Optimization result: INFO - 20:38:18: Optimizer info: INFO - 20:38:18: Status: None INFO - 20:38:18: Message: None INFO - 20:38:18: Number of calls to the objective function by the optimizer: 0 INFO - 20:38:18: Solution: INFO - 20:38:18: Objective: -10.0 INFO - 20:38:18: Design space: INFO - 20:38:18: +------+-------------+-------+-------------+---------+ INFO - 20:38:18: | Name | Lower bound | Value | Upper bound | Type | INFO - 20:38:18: +------+-------------+-------+-------------+---------+ INFO - 20:38:18: | x[0] | -5 | -5 | 5 | integer | INFO - 20:38:18: | x[1] | -5 | -5 | 5 | integer | INFO - 20:38:18: +------+-------------+-------+-------------+---------+ .. 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.333 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 `_