.. 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 ============================= 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 .. 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) define the objective function .. code-block:: default objective = ot.SymbolicFunction(['x1', 'x2', 'x3', 'x4'], ['x1 + 2 * x2 - 3 * x3 + 4 * x4']) define the constraints .. code-block:: default inequality_constraint = ot.SymbolicFunction(['x1', 'x2', 'x3', 'x4'], ['x1-x3']) define the problem bounds .. code-block:: default dim = objective.getInputDimension() bounds = ot.Interval([-3.] * dim, [5.] * dim) define the problem .. code-block:: default problem = ot.OptimizationProblem(objective) problem.setMinimization(True) problem.setInequalityConstraint(inequality_constraint) problem.setBounds(bounds) solve the problem .. code-block:: default algo = ot.Cobyla() algo.setProblem(problem) startingPoint = [0.0] * dim algo.setStartingPoint(startingPoint) algo.run() retrieve results .. 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] draw optimal value history .. code-block:: default graph = result.drawOptimalValueHistory() view = viewer.View(graph) plt.show() .. image:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_constraints_001.png :alt: Optimal value history :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.076 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 `_