.. 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 Click :ref:here  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-44 .. code-block:: default 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.api import configure_logger from gemseo.api import execute_post from gemseo.core.mdofunctions.mdo_function import MDOFunction from matplotlib import pyplot as plt from numpy import cos from numpy import exp from numpy import ones from numpy import sin configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 45-49 Define the objective function ----------------------------- We define the objective function :math:f(x)=\sin(x)-\exp(x) using a :class:.MDOFunction defined by the sum of :class:.MDOFunction objects. .. GENERATED FROM PYTHON SOURCE LINES 49-53 .. 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 54-62 .. 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 62-65 .. code-block:: default design_space = DesignSpace() design_space.add_variable("x", 1, l_b=-2.0, u_b=2.0, value=-0.5 * ones(1)) .. GENERATED FROM PYTHON SOURCE LINES 66-69 Define the optimization problem ------------------------------- Then, we define the :class:.OptimizationProblem with |g|. .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: default problem = OptimizationProblem(design_space) problem.objective = objective .. GENERATED FROM PYTHON SOURCE LINES 73-79 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 79-83 .. code-block:: default opt = OptimizersFactory().execute(problem, "L-BFGS-B", normalize_design_space=True) print("Optimum = ", opt) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 10:08:07: Optimization problem: INFO - 10:08:07: minimize f_1-f_2 = sin(x)-exp(x) INFO - 10:08:07: with respect to x INFO - 10:08:07: over the design space: INFO - 10:08:07: +------+-------------+-------+-------------+-------+ INFO - 10:08:07: | name | lower_bound | value | upper_bound | type | INFO - 10:08:07: +------+-------------+-------+-------------+-------+ INFO - 10:08:07: | x | -2 | -0.5 | 2 | float | INFO - 10:08:07: +------+-------------+-------+-------------+-------+ INFO - 10:08:07: Solving optimization problem with algorithm L-BFGS-B: INFO - 10:08:07: ... 0%| | 0/999 [00:00 .. 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 `_