.. 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-21 .. code-block:: default 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 22-23 define the objective function .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: default objective = ot.SymbolicFunction(['x1', 'x2', 'x3', 'x4'], [ 'x1 + 2 * x2 - 3 * x3 + 4 * x4']) .. GENERATED FROM PYTHON SOURCE LINES 27-28 define the constraints .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: default inequality_constraint = ot.SymbolicFunction( ['x1', 'x2', 'x3', 'x4'], ['x1-x3']) .. GENERATED FROM PYTHON SOURCE LINES 32-33 define the problem bounds .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: default dim = objective.getInputDimension() bounds = ot.Interval([-3.] * dim, [5.] * dim) .. GENERATED FROM PYTHON SOURCE LINES 37-38 define the problem .. GENERATED FROM PYTHON SOURCE LINES 38-43 .. code-block:: default problem = ot.OptimizationProblem(objective) problem.setMinimization(True) problem.setInequalityConstraint(inequality_constraint) problem.setBounds(bounds) .. GENERATED FROM PYTHON SOURCE LINES 44-45 solve the problem .. GENERATED FROM PYTHON SOURCE LINES 45-51 .. code-block:: default algo = ot.Cobyla() algo.setProblem(problem) startingPoint = [0.0] * dim algo.setStartingPoint(startingPoint) algo.run() .. GENERATED FROM PYTHON SOURCE LINES 52-53 retrieve results .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. 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 57-58 draw optimal value history .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. 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.067 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 `_