.. 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 23-28 In this example, we consider a simple optimization problem to illustrate algorithms interfaces and optimization libraries integration. Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 28-45 .. code-block:: default from __future__ import annotations 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.opt_factory import OptimizersFactory from gemseo.algos.opt_problem import OptimizationProblem from gemseo.core.mdofunctions.mdo_function import MDOFunction from numpy import cos from numpy import exp from numpy import ones from numpy import sin configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 46-50 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 50-54 .. code-block:: default 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 55-63 .. 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 63-66 .. code-block:: default design_space = DesignSpace() design_space.add_variable("x", l_b=-2.0, u_b=2.0, value=-0.5 * ones(1)) .. GENERATED FROM PYTHON SOURCE LINES 67-70 Define the optimization problem ------------------------------- Then, we define the :class:`.OptimizationProblem` with |g|. .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: default problem = OptimizationProblem(design_space) problem.objective = objective .. GENERATED FROM PYTHON SOURCE LINES 74-80 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 80-83 .. code-block:: default opt = OptimizersFactory().execute(problem, "L-BFGS-B", normalize_design_space=True) opt .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:23:04: Optimization problem: INFO - 08:23:04: minimize [f_1-f_2] = sin(x)-exp(x) INFO - 08:23:04: with respect to x INFO - 08:23:04: over the design space: INFO - 08:23:04: +------+-------------+-------+-------------+-------+ INFO - 08:23:04: | name | lower_bound | value | upper_bound | type | INFO - 08:23:04: +------+-------------+-------+-------------+-------+ INFO - 08:23:04: | x | -2 | -0.5 | 2 | float | INFO - 08:23:04: +------+-------------+-------+-------------+-------+ INFO - 08:23:04: Solving optimization problem with algorithm L-BFGS-B: INFO - 08:23:04: ... 0%| | 0/999 [00:00
Optimization result:
  • Design variables: [-1.29269572]
  • Objective function: -1.2361083418592416
  • Feasible solution: True


.. GENERATED FROM PYTHON SOURCE LINES 84-85 Note that you can get all the optimization algorithms names: .. GENERATED FROM PYTHON SOURCE LINES 85-87 .. code-block:: default OptimizersFactory().algorithms .. rst-class:: sphx-glr-script-out .. code-block:: none ['MMA', 'NLOPT_MMA', 'NLOPT_COBYLA', 'NLOPT_SLSQP', 'NLOPT_BOBYQA', 'NLOPT_BFGS', 'NLOPT_NEWUOA', 'PDFO_COBYLA', 'PDFO_BOBYQA', 'PDFO_NEWUOA', 'PSEVEN', 'PSEVEN_FD', 'PSEVEN_MOM', 'PSEVEN_NCG', 'PSEVEN_NLS', 'PSEVEN_POWELL', 'PSEVEN_QP', 'PSEVEN_SQP', 'PSEVEN_SQ2P', 'PYMOO_GA', 'PYMOO_NSGA2', 'PYMOO_NSGA3', 'PYMOO_UNSGA3', 'PYMOO_RNSGA3', 'DUAL_ANNEALING', 'SHGO', 'DIFFERENTIAL_EVOLUTION', 'LINEAR_INTERIOR_POINT', 'REVISED_SIMPLEX', 'SIMPLEX', 'Scipy_MILP', 'SLSQP', 'L-BFGS-B', 'TNC', 'SBO'] .. GENERATED FROM PYTHON SOURCE LINES 88-91 Save the optimization results ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ We can serialize the results for further exploitation. .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: default problem.to_hdf("my_optim.hdf5") .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:23:04: Export optimization problem to file: my_optim.hdf5 .. GENERATED FROM PYTHON SOURCE LINES 94-96 Post-process the results ^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 96-98 .. code-block:: default execute_post(problem, "OptHistoryView", show=True, save=False) .. 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: Distance to the optimum :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_2_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_2_004.png :alt: Hessian diagonal approximation :srcset: /examples/optimization_problem/images/sphx_glr_plot_simple_opt_2_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 99-103 .. note:: We can also save this plot using the arguments ``save=False`` and ``file_path='file_path'``. .. GENERATED FROM PYTHON SOURCE LINES 105-109 Solve the optimization problem using a DOE algorithm ---------------------------------------------------- We can also see this optimization problem as a trade-off and solve it by means of a design of experiments (DOE). .. GENERATED FROM PYTHON SOURCE LINES 109-111 .. code-block:: default opt = DOEFactory().execute(problem, "lhs", n_samples=10, normalize_design_space=True) opt .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:23:05: Optimization problem: INFO - 08:23:05: minimize [f_1-f_2] = sin(x)-exp(x) INFO - 08:23:05: with respect to x INFO - 08:23:05: over the design space: INFO - 08:23:05: +------+-------------+--------------------+-------------+-------+ INFO - 08:23:05: | name | lower_bound | value | upper_bound | type | INFO - 08:23:05: +------+-------------+--------------------+-------------+-------+ INFO - 08:23:05: | x | -2 | -1.292695718944152 | 2 | float | INFO - 08:23:05: +------+-------------+--------------------+-------------+-------+ INFO - 08:23:05: Solving optimization problem with algorithm lhs: INFO - 08:23:05: ... 0%| | 0/10 [00:00
Optimization result:
  • Design variables: [1.81552669]
  • Objective function: -5.174108803965849
  • Feasible solution: True


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