.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/optimization_problem/plot_simple_optimization_example.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_optimization_example.py: How to solve an optimization problem ==================================== .. GENERATED FROM PYTHON SOURCE LINES 26-29 Although the |g| library is dedicated to the :term:`MDO`, it can also be used for mono-disciplinary optimization problems. This example presents some analytical test cases. .. GENERATED FROM PYTHON SOURCE LINES 31-33 Imports ******* .. GENERATED FROM PYTHON SOURCE LINES 33-45 .. code-block:: Python from __future__ import annotations import numpy as np from scipy import optimize from gemseo import configure_logger from gemseo import create_design_space from gemseo import create_discipline from gemseo import create_scenario from gemseo import execute_post from gemseo import get_available_opt_algorithms .. GENERATED FROM PYTHON SOURCE LINES 46-71 Optimization based on a design of experiments ********************************************* Let :math:`(P)` be a simple optimization problem: .. math:: (P) = \left\{ \begin{aligned} & \underset{x\in\mathbb{N}^2}{\text{minimize}} & & f(x) = x_1 + x_2 \\ & \text{subject to} & & -5 \leq x \leq 5 \end{aligned} \right. In this section, we will see how to use |g| to solve this problem :math:`(P)` by means of a Design Of Experiments (:term:`DOE`) Define the objective function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Firstly, by means of the :func:`.create_discipline` high-level function, we create a :class:`.Discipline` of :class:`.AutoPyDiscipline` type from a Python function. We also configure the |g| logger with the :func:`.configure_logger` function. .. GENERATED FROM PYTHON SOURCE LINES 71-82 .. code-block:: Python configure_logger() def f(x1=0.0, x2=0.0): y = x1 + x2 return y discipline = create_discipline("AutoPyDiscipline", py_func=f) .. GENERATED FROM PYTHON SOURCE LINES 83-84 Now, we want to minimize this :class:`.Discipline` over a design of experiments (DOE). .. GENERATED FROM PYTHON SOURCE LINES 86-91 Define the design space ~~~~~~~~~~~~~~~~~~~~~~~ For that, by means of the :func:`.create_design_space` API function, we define the :class:`.DesignSpace` :math:`[-5, 5]\times[-5, 5]` by using its :meth:`~.DesignSpace.add_variable` method. .. GENERATED FROM PYTHON SOURCE LINES 91-96 .. code-block:: Python design_space = create_design_space() design_space.add_variable("x1", 1, lower_bound=-5, upper_bound=5, type_="integer") design_space.add_variable("x2", 1, lower_bound=-5, upper_bound=5, type_="integer") .. GENERATED FROM PYTHON SOURCE LINES 97-102 Define the DOE scenario ~~~~~~~~~~~~~~~~~~~~~~~ Then, by means of the :func:`.create_scenario` high-level function, we define a :class:`.DOEScenario` from the :class:`.Discipline` and the :class:`.DesignSpace` defined above: .. GENERATED FROM PYTHON SOURCE LINES 102-111 .. code-block:: Python scenario = create_scenario( discipline, "y", design_space, formulation_name="DisciplinaryOpt", scenario_type="DOE", ) .. GENERATED FROM PYTHON SOURCE LINES 112-120 Execute the DOE scenario ~~~~~~~~~~~~~~~~~~~~~~~~ Lastly, we solve the :class:`.OptimizationProblem` included in the :class:`.DOEScenario` defined above by minimizing the objective function over a design of experiments included in the :class:`.DesignSpace`. Precisely, we choose a `full factorial design `_ of size :math:`11^2`: .. GENERATED FROM PYTHON SOURCE LINES 120-123 .. code-block:: Python scenario.execute(algo_name="PYDOE_FULLFACT", n_samples=11**2) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 20:38:19: *** Start DOEScenario execution *** INFO - 20:38:19: DOEScenario INFO - 20:38:19: Disciplines: f INFO - 20:38:19: MDO formulation: DisciplinaryOpt INFO - 20:38:19: Optimization problem: INFO - 20:38:19: minimize y(x1, x2) INFO - 20:38:19: with respect to x1, x2 INFO - 20:38:19: over the design space: INFO - 20:38:19: +------+-------------+-------+-------------+---------+ INFO - 20:38:19: | Name | Lower bound | Value | Upper bound | Type | INFO - 20:38:19: +------+-------------+-------+-------------+---------+ INFO - 20:38:19: | x1 | -5 | None | 5 | integer | INFO - 20:38:19: | x2 | -5 | None | 5 | integer | INFO - 20:38:19: +------+-------------+-------+-------------+---------+ INFO - 20:38:19: Solving optimization problem with algorithm PYDOE_FULLFACT: INFO - 20:38:19: 1%| | 1/121 [00:00<00:00, 551.74 it/sec, obj=-10] INFO - 20:38:19: 2%|▏ | 2/121 [00:00<00:00, 957.06 it/sec, obj=-9] INFO - 20:38:19: 2%|▏ | 3/121 [00:00<00:00, 1281.88 it/sec, obj=-8] INFO - 20:38:19: 3%|▎ | 4/121 [00:00<00:00, 1573.11 it/sec, obj=-7] INFO - 20:38:19: 4%|▍ | 5/121 [00:00<00:00, 1826.95 it/sec, obj=-6] INFO - 20:38:19: 5%|▍ | 6/121 [00:00<00:00, 2054.86 it/sec, obj=-5] INFO - 20:38:19: 6%|▌ | 7/121 [00:00<00:00, 2256.39 it/sec, obj=-4] INFO - 20:38:19: 7%|▋ | 8/121 [00:00<00:00, 2417.12 it/sec, obj=-3] INFO - 20:38:19: 7%|▋ | 9/121 [00:00<00:00, 2574.95 it/sec, obj=-2] INFO - 20:38:19: 8%|▊ | 10/121 [00:00<00:00, 2724.81 it/sec, obj=-1] INFO - 20:38:19: 9%|▉ | 11/121 [00:00<00:00, 2861.59 it/sec, obj=0] INFO - 20:38:19: 10%|▉ | 12/121 [00:00<00:00, 2986.16 it/sec, obj=-9] INFO - 20:38:19: 11%|█ | 13/121 [00:00<00:00, 3083.70 it/sec, obj=-8] INFO - 20:38:19: 12%|█▏ | 14/121 [00:00<00:00, 3185.60 it/sec, obj=-7] INFO - 20:38:19: 12%|█▏ | 15/121 [00:00<00:00, 3283.64 it/sec, obj=-6] INFO - 20:38:19: 13%|█▎ | 16/121 [00:00<00:00, 3375.19 it/sec, obj=-5] INFO - 20:38:19: 14%|█▍ | 17/121 [00:00<00:00, 3457.12 it/sec, obj=-4] INFO - 20:38:19: 15%|█▍ | 18/121 [00:00<00:00, 3533.53 it/sec, obj=-3] INFO - 20:38:19: 16%|█▌ | 19/121 [00:00<00:00, 3586.49 it/sec, obj=-2] INFO - 20:38:19: 17%|█▋ | 20/121 [00:00<00:00, 3652.30 it/sec, obj=-1] INFO - 20:38:19: 17%|█▋ | 21/121 [00:00<00:00, 3712.87 it/sec, obj=0] INFO - 20:38:19: 18%|█▊ | 22/121 [00:00<00:00, 3772.16 it/sec, obj=1] INFO - 20:38:19: 19%|█▉ | 23/121 [00:00<00:00, 3829.65 it/sec, obj=-8] INFO - 20:38:19: 20%|█▉ | 24/121 [00:00<00:00, 3867.50 it/sec, obj=-7] INFO - 20:38:19: 21%|██ | 25/121 [00:00<00:00, 3912.45 it/sec, obj=-6] INFO - 20:38:19: 21%|██▏ | 26/121 [00:00<00:00, 3958.18 it/sec, obj=-5] INFO - 20:38:19: 22%|██▏ | 27/121 [00:00<00:00, 4002.62 it/sec, obj=-4] INFO - 20:38:19: 23%|██▎ | 28/121 [00:00<00:00, 4046.74 it/sec, obj=-3] INFO - 20:38:19: 24%|██▍ | 29/121 [00:00<00:00, 4088.57 it/sec, obj=-2] INFO - 20:38:19: 25%|██▍ | 30/121 [00:00<00:00, 4113.54 it/sec, obj=-1] INFO - 20:38:19: 26%|██▌ | 31/121 [00:00<00:00, 4151.19 it/sec, obj=0] INFO - 20:38:19: 26%|██▋ | 32/121 [00:00<00:00, 4187.50 it/sec, obj=1] INFO - 20:38:19: 27%|██▋ | 33/121 [00:00<00:00, 4222.97 it/sec, obj=2] INFO - 20:38:19: 28%|██▊ | 34/121 [00:00<00:00, 4257.54 it/sec, obj=-7] INFO - 20:38:19: 29%|██▉ | 35/121 [00:00<00:00, 4282.15 it/sec, obj=-6] INFO - 20:38:19: 30%|██▉ | 36/121 [00:00<00:00, 4311.55 it/sec, obj=-5] INFO - 20:38:19: 31%|███ | 37/121 [00:00<00:00, 4339.62 it/sec, obj=-4] INFO - 20:38:19: 31%|███▏ | 38/121 [00:00<00:00, 4369.19 it/sec, obj=-3] INFO - 20:38:19: 32%|███▏ | 39/121 [00:00<00:00, 4399.74 it/sec, obj=-2] INFO - 20:38:19: 33%|███▎ | 40/121 [00:00<00:00, 4427.64 it/sec, obj=-1] INFO - 20:38:19: 34%|███▍ | 41/121 [00:00<00:00, 4439.45 it/sec, obj=0] INFO - 20:38:19: 35%|███▍ | 42/121 [00:00<00:00, 4462.25 it/sec, obj=1] INFO - 20:38:19: 36%|███▌ | 43/121 [00:00<00:00, 4485.55 it/sec, obj=2] INFO - 20:38:19: 36%|███▋ | 44/121 [00:00<00:00, 4508.46 it/sec, obj=3] INFO - 20:38:19: 37%|███▋ | 45/121 [00:00<00:00, 4532.42 it/sec, obj=-6] INFO - 20:38:19: 38%|███▊ | 46/121 [00:00<00:00, 4556.33 it/sec, obj=-5] INFO - 20:38:19: 39%|███▉ | 47/121 [00:00<00:00, 4565.15 it/sec, obj=-4] INFO - 20:38:19: 40%|███▉ | 48/121 [00:00<00:00, 4586.34 it/sec, obj=-3] INFO - 20:38:19: 40%|████ | 49/121 [00:00<00:00, 4528.89 it/sec, obj=-2] INFO - 20:38:19: 41%|████▏ | 50/121 [00:00<00:00, 4544.21 it/sec, obj=-1] INFO - 20:38:19: 42%|████▏ | 51/121 [00:00<00:00, 4553.30 it/sec, obj=0] INFO - 20:38:19: 43%|████▎ | 52/121 [00:00<00:00, 4571.16 it/sec, obj=1] INFO - 20:38:19: 44%|████▍ | 53/121 [00:00<00:00, 4588.01 it/sec, obj=2] INFO - 20:38:19: 45%|████▍ | 54/121 [00:00<00:00, 4605.94 it/sec, obj=3] INFO - 20:38:19: 45%|████▌ | 55/121 [00:00<00:00, 4622.98 it/sec, obj=4] INFO - 20:38:19: 46%|████▋ | 56/121 [00:00<00:00, 4641.18 it/sec, obj=-5] INFO - 20:38:19: 47%|████▋ | 57/121 [00:00<00:00, 4649.55 it/sec, obj=-4] INFO - 20:38:19: 48%|████▊ | 58/121 [00:00<00:00, 4664.81 it/sec, obj=-3] INFO - 20:38:19: 49%|████▉ | 59/121 [00:00<00:00, 4682.47 it/sec, obj=-2] INFO - 20:38:19: 50%|████▉ | 60/121 [00:00<00:00, 4699.85 it/sec, obj=-1] INFO - 20:38:19: 50%|█████ | 61/121 [00:00<00:00, 4714.61 it/sec, obj=0] INFO - 20:38:19: 51%|█████ | 62/121 [00:00<00:00, 4731.57 it/sec, obj=1] INFO - 20:38:19: 52%|█████▏ | 63/121 [00:00<00:00, 4739.24 it/sec, obj=2] INFO - 20:38:19: 53%|█████▎ | 64/121 [00:00<00:00, 4750.31 it/sec, obj=3] INFO - 20:38:19: 54%|█████▎ | 65/121 [00:00<00:00, 4763.01 it/sec, obj=4] INFO - 20:38:19: 55%|█████▍ | 66/121 [00:00<00:00, 4776.20 it/sec, obj=5] INFO - 20:38:19: 55%|█████▌ | 67/121 [00:00<00:00, 4789.08 it/sec, obj=-4] INFO - 20:38:19: 56%|█████▌ | 68/121 [00:00<00:00, 4791.32 it/sec, obj=-3] INFO - 20:38:19: 57%|█████▋ | 69/121 [00:00<00:00, 4801.04 it/sec, obj=-2] INFO - 20:38:19: 58%|█████▊ | 70/121 [00:00<00:00, 4813.29 it/sec, obj=-1] INFO - 20:38:19: 59%|█████▊ | 71/121 [00:00<00:00, 4825.80 it/sec, obj=0] INFO - 20:38:19: 60%|█████▉ | 72/121 [00:00<00:00, 4837.64 it/sec, obj=1] INFO - 20:38:19: 60%|██████ | 73/121 [00:00<00:00, 4845.68 it/sec, obj=2] INFO - 20:38:19: 61%|██████ | 74/121 [00:00<00:00, 4849.74 it/sec, obj=3] INFO - 20:38:19: 62%|██████▏ | 75/121 [00:00<00:00, 4859.99 it/sec, obj=4] INFO - 20:38:19: 63%|██████▎ | 76/121 [00:00<00:00, 4870.91 it/sec, obj=5] INFO - 20:38:19: 64%|██████▎ | 77/121 [00:00<00:00, 4881.74 it/sec, obj=6] INFO - 20:38:19: 64%|██████▍ | 78/121 [00:00<00:00, 4892.63 it/sec, obj=-3] INFO - 20:38:19: 65%|██████▌ | 79/121 [00:00<00:00, 4903.37 it/sec, obj=-2] INFO - 20:38:19: 66%|██████▌ | 80/121 [00:00<00:00, 4907.70 it/sec, obj=-1] INFO - 20:38:19: 67%|██████▋ | 81/121 [00:00<00:00, 4917.76 it/sec, obj=0] INFO - 20:38:19: 68%|██████▊ | 82/121 [00:00<00:00, 4928.04 it/sec, obj=1] INFO - 20:38:19: 69%|██████▊ | 83/121 [00:00<00:00, 4936.15 it/sec, obj=2] INFO - 20:38:19: 69%|██████▉ | 84/121 [00:00<00:00, 4945.97 it/sec, obj=3] INFO - 20:38:19: 70%|███████ | 85/121 [00:00<00:00, 4949.13 it/sec, obj=4] INFO - 20:38:19: 71%|███████ | 86/121 [00:00<00:00, 4956.51 it/sec, obj=5] INFO - 20:38:19: 72%|███████▏ | 87/121 [00:00<00:00, 4965.97 it/sec, obj=6] INFO - 20:38:19: 73%|███████▎ | 88/121 [00:00<00:00, 4975.85 it/sec, obj=7] INFO - 20:38:19: 74%|███████▎ | 89/121 [00:00<00:00, 4985.28 it/sec, obj=-2] INFO - 20:38:19: 74%|███████▍ | 90/121 [00:00<00:00, 4994.14 it/sec, obj=-1] INFO - 20:38:19: 75%|███████▌ | 91/121 [00:00<00:00, 4996.29 it/sec, obj=0] INFO - 20:38:19: 76%|███████▌ | 92/121 [00:00<00:00, 5003.90 it/sec, obj=1] INFO - 20:38:19: 77%|███████▋ | 93/121 [00:00<00:00, 5012.66 it/sec, obj=2] INFO - 20:38:19: 78%|███████▊ | 94/121 [00:00<00:00, 5022.16 it/sec, obj=3] INFO - 20:38:19: 79%|███████▊ | 95/121 [00:00<00:00, 5031.11 it/sec, obj=4] INFO - 20:38:19: 79%|███████▉ | 96/121 [00:00<00:00, 5040.73 it/sec, obj=5] INFO - 20:38:19: 80%|████████ | 97/121 [00:00<00:00, 5043.67 it/sec, obj=6] INFO - 20:38:19: 81%|████████ | 98/121 [00:00<00:00, 5050.21 it/sec, obj=7] INFO - 20:38:19: 82%|████████▏ | 99/121 [00:00<00:00, 5058.24 it/sec, obj=8] INFO - 20:38:19: 83%|████████▎ | 100/121 [00:00<00:00, 5066.50 it/sec, obj=-1] INFO - 20:38:19: 83%|████████▎ | 101/121 [00:00<00:00, 5073.96 it/sec, obj=0] INFO - 20:38:19: 84%|████████▍ | 102/121 [00:00<00:00, 5082.31 it/sec, obj=1] INFO - 20:38:19: 85%|████████▌ | 103/121 [00:00<00:00, 5083.53 it/sec, obj=2] INFO - 20:38:19: 86%|████████▌ | 104/121 [00:00<00:00, 5090.06 it/sec, obj=3] INFO - 20:38:19: 87%|████████▋ | 105/121 [00:00<00:00, 5097.72 it/sec, obj=4] INFO - 20:38:19: 88%|████████▊ | 106/121 [00:00<00:00, 5105.72 it/sec, obj=5] INFO - 20:38:19: 88%|████████▊ | 107/121 [00:00<00:00, 5111.92 it/sec, obj=6] INFO - 20:38:19: 89%|████████▉ | 108/121 [00:00<00:00, 5119.05 it/sec, obj=7] INFO - 20:38:19: 90%|█████████ | 109/121 [00:00<00:00, 5119.70 it/sec, obj=8] INFO - 20:38:19: 91%|█████████ | 110/121 [00:00<00:00, 5125.92 it/sec, obj=9] INFO - 20:38:19: 92%|█████████▏| 111/121 [00:00<00:00, 5132.26 it/sec, obj=0] INFO - 20:38:19: 93%|█████████▎| 112/121 [00:00<00:00, 5138.79 it/sec, obj=1] INFO - 20:38:19: 93%|█████████▎| 113/121 [00:00<00:00, 5144.32 it/sec, obj=2] INFO - 20:38:19: 94%|█████████▍| 114/121 [00:00<00:00, 5146.94 it/sec, obj=3] INFO - 20:38:19: 95%|█████████▌| 115/121 [00:00<00:00, 5147.54 it/sec, obj=4] INFO - 20:38:19: 96%|█████████▌| 116/121 [00:00<00:00, 5150.85 it/sec, obj=5] INFO - 20:38:19: 97%|█████████▋| 117/121 [00:00<00:00, 5155.96 it/sec, obj=6] INFO - 20:38:19: 98%|█████████▊| 118/121 [00:00<00:00, 5161.68 it/sec, obj=7] INFO - 20:38:19: 98%|█████████▊| 119/121 [00:00<00:00, 5166.74 it/sec, obj=8] INFO - 20:38:19: 99%|█████████▉| 120/121 [00:00<00:00, 5167.04 it/sec, obj=9] INFO - 20:38:19: 100%|██████████| 121/121 [00:00<00:00, 5170.71 it/sec, obj=10] INFO - 20:38:19: Optimization result: INFO - 20:38:19: Optimizer info: INFO - 20:38:19: Status: None INFO - 20:38:19: Message: None INFO - 20:38:19: Number of calls to the objective function by the optimizer: 0 INFO - 20:38:19: Solution: INFO - 20:38:19: Objective: -10.0 INFO - 20:38:19: Design space: INFO - 20:38:19: +------+-------------+-------+-------------+---------+ INFO - 20:38:19: | Name | Lower bound | Value | Upper bound | Type | INFO - 20:38:19: +------+-------------+-------+-------------+---------+ INFO - 20:38:19: | x1 | -5 | -5 | 5 | integer | INFO - 20:38:19: | x2 | -5 | -5 | 5 | integer | INFO - 20:38:19: +------+-------------+-------+-------------+---------+ INFO - 20:38:19: *** End DOEScenario execution *** .. GENERATED FROM PYTHON SOURCE LINES 124-126 The optimum results can be found in the execution log. It is also possible to extract them from the :attr:`.BaseScenario.optimization_result`. .. GENERATED FROM PYTHON SOURCE LINES 126-132 .. code-block:: Python optimization_result = scenario.optimization_result print( f"The solution of P is (x*,f(x*)) = ({optimization_result.x_opt}, {optimization_result.f_opt})" ) .. rst-class:: sphx-glr-script-out .. code-block:: none The solution of P is (x*,f(x*)) = ([-5. -5.], -10.0) .. GENERATED FROM PYTHON SOURCE LINES 133-155 Optimization based on a quasi-Newton method by means of the `SciPy `_ library ************************************************************************************************* Let :math:`(P)` be a simple optimization problem: .. math:: (P) = \left\{ \begin{aligned} & \underset{x}{\text{minimize}} & & f(x) = \sin(x) - \exp(x) \\ & \text{subject to} & & -2 \leq x \leq 2 \end{aligned} \right. In this section, we will see how to use |g| to solve this problem :math:`(P)` by means of an optimizer directly used from the `SciPy `_ library. Define the objective function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Firstly, we create the objective function and its gradient as standard Python functions: .. GENERATED FROM PYTHON SOURCE LINES 155-167 .. code-block:: Python def g(x=0): y = np.sin(x) - np.exp(x) return y def dgdx(x=0): y = np.cos(x) - np.exp(x) return y .. GENERATED FROM PYTHON SOURCE LINES 168-173 Minimize the objective function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Now, we can minimize this Python function over its design space by means of the `L-BFGS-B algorithm `_ implemented in the function ``scipy.optimize.fmin_l_bfgs_b``. .. GENERATED FROM PYTHON SOURCE LINES 173-178 .. code-block:: Python x_0 = -0.5 * np.ones(1) opt = optimize.fmin_l_bfgs_b(g, x_0, fprime=dgdx, bounds=[(-0.2, 2.0)]) x_opt, f_opt, _ = opt .. GENERATED FROM PYTHON SOURCE LINES 179-180 Then, we can display the solution of our optimization problem with the following code: .. GENERATED FROM PYTHON SOURCE LINES 180-183 .. code-block:: Python print(f"The solution of P is (x*,f(x*)) = ({x_opt[0]}, {f_opt}).") .. rst-class:: sphx-glr-script-out .. code-block:: none The solution of P is (x*,f(x*)) = (-0.2, -1.017400083873043). .. GENERATED FROM PYTHON SOURCE LINES 184-189 .. seealso:: You can find the SciPy implementation of the L-BFGS-B algorithm `by clicking here `_. .. GENERATED FROM PYTHON SOURCE LINES 191-215 Optimization based on a quasi-Newton method by means of the |g| optimization interface ************************************************************************************** Let :math:`(P)` be a simple optimization problem: .. math:: (P) = \left\{ \begin{aligned} & \underset{x}{\text{minimize}} & & f(x) = \sin(x) - \exp(x) \\ & \text{subject to} & & -2 \leq x \leq 2 \end{aligned} \right. In this section, we will see how to use |g| to solve this problem :math:`(P)` by means of an optimizer from `SciPy `_ called through the optimization interface of |g|. Define the objective function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Firstly, by means of the :func:`.create_discipline` high-level function, we create an :class:`.Discipline` of :class:`.AutoPyDiscipline` type from a Python function: .. GENERATED FROM PYTHON SOURCE LINES 215-229 .. code-block:: Python def g(x=0): y = np.sin(x) - np.exp(x) return y def dgdx(x=0): y = np.cos(x) - np.exp(x) return y discipline = create_discipline("AutoPyDiscipline", py_func=g, py_jac=dgdx) .. GENERATED FROM PYTHON SOURCE LINES 230-239 Now, we can to minimize this :class:`.Discipline` over a design space, by means of a quasi-Newton method from the initial point :math:`0.5`. Define the design space ~~~~~~~~~~~~~~~~~~~~~~~ For that, by means of the :func:`.create_design_space` high-level function, we define the :class:`.DesignSpace` :math:`[-2., 2.]` with initial value :math:`0.5` by using its :meth:`~.DesignSpace.add_variable` method. .. GENERATED FROM PYTHON SOURCE LINES 239-245 .. code-block:: Python design_space = create_design_space() design_space.add_variable( "x", 1, lower_bound=-2.0, upper_bound=2.0, value=-0.5 * np.ones(1) ) .. GENERATED FROM PYTHON SOURCE LINES 246-251 Define the optimization problem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Then, by means of the :func:`.create_scenario` high-level function, we define an :class:`.MDOScenario` from the :class:`.Discipline` and the :class:`.DesignSpace` defined above: .. GENERATED FROM PYTHON SOURCE LINES 251-260 .. code-block:: Python scenario = create_scenario( discipline, "y", design_space, formulation_name="DisciplinaryOpt", scenario_type="MDO", ) .. GENERATED FROM PYTHON SOURCE LINES 261-270 Execute the optimization problem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Lastly, we solve the :class:`.OptimizationProblem` included in the :class:`.MDOScenario` defined above by minimizing the objective function over the :class:`.DesignSpace`. Precisely, we choose the `L-BFGS-B algorithm `_ implemented in the function ``scipy.optimize.fmin_l_bfgs_b`` and indirectly called by means of the class :class:`.OptimizationLibraryFactory` and of its function :meth:`~.BaseAlgoFactory.execute`: .. GENERATED FROM PYTHON SOURCE LINES 270-273 .. code-block:: Python scenario.execute(algo_name="L-BFGS-B", max_iter=100) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 20:38:19: *** Start MDOScenario execution *** INFO - 20:38:19: MDOScenario INFO - 20:38:19: Disciplines: g INFO - 20:38:19: MDO formulation: DisciplinaryOpt INFO - 20:38:19: Optimization problem: INFO - 20:38:19: minimize y(x) INFO - 20:38:19: with respect to x INFO - 20:38:19: over the design space: INFO - 20:38:19: +------+-------------+-------+-------------+-------+ INFO - 20:38:19: | Name | Lower bound | Value | Upper bound | Type | INFO - 20:38:19: +------+-------------+-------+-------------+-------+ INFO - 20:38:19: | x | -2 | -0.5 | 2 | float | INFO - 20:38:19: +------+-------------+-------+-------------+-------+ INFO - 20:38:19: Solving optimization problem with algorithm L-BFGS-B: INFO - 20:38:19: 1%| | 1/100 [00:00<00:00, 545.92 it/sec, obj=-1.09] INFO - 20:38:19: 2%|▏ | 2/100 [00:00<00:00, 728.18 it/sec, obj=-1.04] INFO - 20:38:19: 3%|▎ | 3/100 [00:00<00:00, 941.91 it/sec, obj=-1.24] INFO - 20:38:19: 4%|▍ | 4/100 [00:00<00:00, 1036.65 it/sec, obj=-1.23] INFO - 20:38:19: 5%|▌ | 5/100 [00:00<00:00, 1111.60 it/sec, obj=-1.24] INFO - 20:38:19: 6%|▌ | 6/100 [00:00<00:00, 1166.38 it/sec, obj=-1.24] INFO - 20:38:19: 7%|▋ | 7/100 [00:00<00:00, 1218.21 it/sec, obj=-1.24] INFO - 20:38:19: Optimization result: INFO - 20:38:19: Optimizer info: INFO - 20:38:19: Status: 0 INFO - 20:38:19: Message: CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL INFO - 20:38:19: Number of calls to the objective function by the optimizer: 0 INFO - 20:38:19: Solution: INFO - 20:38:19: Objective: -1.2361083418592416 INFO - 20:38:19: Design space: INFO - 20:38:19: +------+-------------+--------------------+-------------+-------+ INFO - 20:38:19: | Name | Lower bound | Value | Upper bound | Type | INFO - 20:38:19: +------+-------------+--------------------+-------------+-------+ INFO - 20:38:19: | x | -2 | -1.292695718944152 | 2 | float | INFO - 20:38:19: +------+-------------+--------------------+-------------+-------+ INFO - 20:38:19: *** End MDOScenario execution *** .. GENERATED FROM PYTHON SOURCE LINES 274-276 The optimization results are displayed in the log file. They can also be obtained using the following code: .. GENERATED FROM PYTHON SOURCE LINES 276-282 .. code-block:: Python optimization_result = scenario.optimization_result print( f"The solution of P is (x*,f(x*)) = ({optimization_result.x_opt}, {optimization_result.f_opt})." ) .. rst-class:: sphx-glr-script-out .. code-block:: none The solution of P is (x*,f(x*)) = ([-1.29269572], -1.2361083418592416). .. GENERATED FROM PYTHON SOURCE LINES 283-291 .. seealso:: You can find the `SciPy `_ implementation of the `L-BFGS-B algorithm `_ algorithm `by clicking here `_. In order to get the list of available optimization algorithms, use: .. GENERATED FROM PYTHON SOURCE LINES 292-296 .. code-block:: Python algo_list = get_available_opt_algorithms() print(f"Available algorithms: {algo_list}") .. rst-class:: sphx-glr-script-out .. code-block:: none Available algorithms: ['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 297-302 Saving and post-processing ************************** After the resolution of the :class:`~gemseo.algos.optimization_problem.OptimizationProblem`, we can export the results into an :term:`HDF` file: .. GENERATED FROM PYTHON SOURCE LINES 302-306 .. code-block:: Python problem = scenario.formulation.optimization_problem problem.to_hdf("my_optim.hdf5") .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 20:38:19: Exporting the optimization problem to the file my_optim.hdf5 .. GENERATED FROM PYTHON SOURCE LINES 307-310 We can also post-process the optimization history by means of the function :func:`.execute_post`, either from the :class:`~gemseo.algos.optimization_problem.OptimizationProblem`: .. GENERATED FROM PYTHON SOURCE LINES 310-313 .. code-block:: Python execute_post(problem, post_name="OptHistoryView", save=False, show=True) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_001.png :alt: Evolution of the optimization variables :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_002.png :alt: Evolution of the objective value :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_003.png :alt: Evolution of the distance to the optimum :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 314-315 or from the :term:`HDF` file created above: .. GENERATED FROM PYTHON SOURCE LINES 315-317 .. code-block:: Python execute_post("my_optim.hdf5", post_name="OptHistoryView", save=False, show=True) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_004.png :alt: Evolution of the optimization variables :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_004.png :class: sphx-glr-multi-img * .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_005.png :alt: Evolution of the objective value :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_005.png :class: sphx-glr-multi-img * .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_006.png :alt: Evolution of the distance to the optimum :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_optimization_example_006.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 20:38:19: Importing the optimization problem from the file my_optim.hdf5 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.667 seconds) .. _sphx_glr_download_examples_optimization_problem_plot_simple_optimization_example.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_optimization_example.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simple_optimization_example.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_simple_optimization_example.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_