.. 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_2.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_2.py: Analytical test case # 2 ======================== .. GENERATED FROM PYTHON SOURCE LINES 24-29 In this example, we consider a simple optimization problem to illustrate algorithms interfaces and optimization libraries integration. Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 29-42 .. code-block:: Python from __future__ import annotations from numpy import cos from numpy import exp from numpy import sin from gemseo import execute_algo from gemseo import execute_post 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 .. GENERATED FROM PYTHON SOURCE LINES 43-47 Define the objective function ----------------------------- We define the objective function :math:`f(x)=\sin(x)-\exp(x)` using an :class:`.MDOFunction` defined by the sum of :class:`.MDOFunction` objects. .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: Python f_1 = MDOFunction(sin, name="f_1", jac=cos, expr="sin(x)") f_2 = MDOFunction(exp, name="f_2", jac=exp, expr="exp(x)") objective = f_1 - f_2 .. GENERATED FROM PYTHON SOURCE LINES 52-60 .. seealso:: The following operators are implemented: addition, subtraction and multiplication. The minus operator is also defined. Define the design space ----------------------- Then, we define the :class:`.DesignSpace` with |g|. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python design_space = DesignSpace() design_space.add_variable("x", lower_bound=-2.0, upper_bound=2.0, value=-0.5) .. GENERATED FROM PYTHON SOURCE LINES 64-67 Define the optimization problem ------------------------------- Then, we define the :class:`.OptimizationProblem` with |g|. .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python problem = OptimizationProblem(design_space) problem.objective = objective .. GENERATED FROM PYTHON SOURCE LINES 71-77 Solve the optimization problem using an optimization algorithm -------------------------------------------------------------- Finally, we solve the optimization problems with |g| interface. Solve the problem ^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python optimization_result = execute_algo(problem, algo_name="L-BFGS-B") optimization_result .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:25:00: Optimization problem: INFO - 16:25:00: minimize [f_1-f_2] = sin(x)-exp(x) INFO - 16:25:00: with respect to x INFO - 16:25:00: over the design space: INFO - 16:25:00: +------+-------------+-------+-------------+-------+ INFO - 16:25:00: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:25:00: +------+-------------+-------+-------------+-------+ INFO - 16:25:00: | x | -2 | -0.5 | 2 | float | INFO - 16:25:00: +------+-------------+-------+-------------+-------+ INFO - 16:25:00: Solving optimization problem with algorithm L-BFGS-B: INFO - 16:25:00: 1%| | 6/1000 [00:00<00:00, 2374.13 it/sec, feas=True, obj=-1.24] INFO - 16:25:00: 1%| | 7/1000 [00:00<00:00, 2171.45 it/sec, feas=True, obj=-1.24] INFO - 16:25:00: Optimization result: INFO - 16:25:00: Optimizer info: INFO - 16:25:00: Status: 0 INFO - 16:25:00: Message: CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL INFO - 16:25:00: Solution: INFO - 16:25:00: Objective: -1.2361083418592416 INFO - 16:25:00: Design space: INFO - 16:25:00: +------+-------------+--------------------+-------------+-------+ INFO - 16:25:00: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:25:00: +------+-------------+--------------------+-------------+-------+ INFO - 16:25:00: | x | -2 | -1.292695718944152 | 2 | float | INFO - 16:25:00: +------+-------------+--------------------+-------------+-------+ .. raw:: html
Optimization result:
  • Design variables: [-1.29269572]
  • Objective function: -1.2361083418592416
  • Feasible solution: True


.. GENERATED FROM PYTHON SOURCE LINES 81-82 Note that you can get all the optimization algorithms names: .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python get_available_opt_algorithms() .. rst-class:: sphx-glr-script-out .. code-block:: none ['Augmented_Lagrangian_order_0', 'Augmented_Lagrangian_order_1', 'Scipy_MILP', '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', 'SLSQP', 'L-BFGS-B', 'TNC', 'NELDER-MEAD', 'COBYQA'] .. GENERATED FROM PYTHON SOURCE LINES 85-88 Save the optimization results ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ We can serialize the results for further exploitation. .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: Python problem.to_hdf("my_optim.hdf5") .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:25:00: Exporting the optimization problem to the file my_optim.hdf5 .. GENERATED FROM PYTHON SOURCE LINES 91-93 Post-process the results ^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. 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_opt_2_001.png :alt: Evolution of the optimization variables :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_2_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_2_002.png :alt: Evolution of the objective value :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_2_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_2_003.png :alt: Evolution of the distance to the optimum :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_2_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 96-100 .. note:: We can also save this plot using the arguments ``save=False`` and ``file_path='file_path'``. .. GENERATED FROM PYTHON SOURCE LINES 102-106 Solve the optimization problem using a DOE algorithm ---------------------------------------------------- We can also see this optimization problem as a trade-off problem and solve it by means of a design of experiments (DOE). .. GENERATED FROM PYTHON SOURCE LINES 106-111 .. code-block:: Python problem.reset() optimization_result = execute_algo( problem, algo_name="PYDOE_LHS", n_samples=10, algo_type="doe" ) optimization_result .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:25:01: Optimization problem: INFO - 16:25:01: minimize [f_1-f_2] = sin(x)-exp(x) INFO - 16:25:01: with respect to x INFO - 16:25:01: over the design space: INFO - 16:25:01: +------+-------------+-------+-------------+-------+ INFO - 16:25:01: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:25:01: +------+-------------+-------+-------------+-------+ INFO - 16:25:01: | x | -2 | -0.5 | 2 | float | INFO - 16:25:01: +------+-------------+-------+-------------+-------+ INFO - 16:25:01: Solving optimization problem with algorithm PYDOE_LHS: INFO - 16:25:01: 10%|█ | 1/10 [00:00<00:00, 7332.70 it/sec, feas=True, obj=-5.17] INFO - 16:25:01: 20%|██ | 2/10 [00:00<00:00, 5622.39 it/sec, feas=True, obj=-1.15] INFO - 16:25:01: 30%|███ | 3/10 [00:00<00:00, 5822.73 it/sec, feas=True, obj=-1.24] INFO - 16:25:01: 40%|████ | 4/10 [00:00<00:00, 6078.70 it/sec, feas=True, obj=-1.13] INFO - 16:25:01: 50%|█████ | 5/10 [00:00<00:00, 6301.54 it/sec, feas=True, obj=-2.91] INFO - 16:25:01: 60%|██████ | 6/10 [00:00<00:00, 6442.86 it/sec, feas=True, obj=-1.75] INFO - 16:25:01: 70%|███████ | 7/10 [00:00<00:00, 6590.38 it/sec, feas=True, obj=-1.14] INFO - 16:25:01: 80%|████████ | 8/10 [00:00<00:00, 6700.17 it/sec, feas=True, obj=-1.05] INFO - 16:25:01: 90%|█████████ | 9/10 [00:00<00:00, 6664.68 it/sec, feas=True, obj=-1.23] INFO - 16:25:01: 100%|██████████| 10/10 [00:00<00:00, 6361.75 it/sec, feas=True, obj=-1] INFO - 16:25:01: Optimization result: INFO - 16:25:01: Optimizer info: INFO - 16:25:01: Status: None INFO - 16:25:01: Message: None INFO - 16:25:01: Solution: INFO - 16:25:01: Objective: -5.174108803965848 INFO - 16:25:01: Design space: INFO - 16:25:01: +------+-------------+-------------------+-------------+-------+ INFO - 16:25:01: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:25:01: +------+-------------+-------------------+-------------+-------+ INFO - 16:25:01: | x | -2 | 1.815526693601343 | 2 | float | INFO - 16:25:01: +------+-------------+-------------------+-------------+-------+ .. raw:: html
Optimization result:
  • Design variables: [1.81552669]
  • Objective function: -5.174108803965848
  • Feasible solution: True


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.292 seconds) .. _sphx_glr_download_examples_optimization_problem_plot_simple_opt_2.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_2.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simple_opt_2.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_simple_opt_2.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_