.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_numerical_methods/optimization/plot_optimization_constraints.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_auto_numerical_methods_optimization_plot_optimization_constraints.py: Optimization with constraints ============================= .. GENERATED FROM PYTHON SOURCE LINES 6-13 In this example we are going to expose methods to solve a generic optimization problem in the form .. math:: \min_{x\in B} f(x) \\ g(x) = 0 \\ h(x) \ge 0 .. GENERATED FROM PYTHON SOURCE LINES 15-22 .. code-block:: default from __future__ import print_function import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt import math as m ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 23-24 define the objective function .. GENERATED FROM PYTHON SOURCE LINES 24-27 .. code-block:: default objective = ot.SymbolicFunction(['x1', 'x2', 'x3', 'x4'], [ 'x1 + 2 * x2 - 3 * x3 + 4 * x4']) .. GENERATED FROM PYTHON SOURCE LINES 28-29 define the constraints .. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: default inequality_constraint = ot.SymbolicFunction( ['x1', 'x2', 'x3', 'x4'], ['x1-x3']) .. GENERATED FROM PYTHON SOURCE LINES 33-34 define the problem bounds .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: default dim = objective.getInputDimension() bounds = ot.Interval([-3.] * dim, [5.] * dim) .. GENERATED FROM PYTHON SOURCE LINES 38-39 define the problem .. GENERATED FROM PYTHON SOURCE LINES 39-44 .. code-block:: default problem = ot.OptimizationProblem(objective) problem.setMinimization(True) problem.setInequalityConstraint(inequality_constraint) problem.setBounds(bounds) .. GENERATED FROM PYTHON SOURCE LINES 45-46 solve the problem .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: default algo = ot.Cobyla() algo.setProblem(problem) startingPoint = [0.0] * dim algo.setStartingPoint(startingPoint) algo.run() .. GENERATED FROM PYTHON SOURCE LINES 53-54 retrieve results .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: default result = algo.getResult() print('x^=', result.getOptimalPoint()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none x^= [5,-3,5,-3] .. GENERATED FROM PYTHON SOURCE LINES 58-59 draw optimal value history .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: default graph = result.drawOptimalValueHistory() view = viewer.View(graph) plt.show() .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_constraints_001.png :alt: Optimal value history :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_constraints_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.136 seconds) .. _sphx_glr_download_auto_numerical_methods_optimization_plot_optimization_constraints.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_optimization_constraints.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_optimization_constraints.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_