.. 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_nlopt.py:
Optimization using NLopt
========================
In this example we are going to explore optimization using OpenTURNS' `NLopt `_ interface.
.. 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)
List available algorithms
.. code-block:: default
for algo in ot.NLopt.GetAlgorithmNames():
print(algo)
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
AUGLAG
AUGLAG_EQ
GD_MLSL
GD_MLSL_LDS
GN_CRS2_LM
GN_DIRECT
GN_DIRECT_L
GN_DIRECT_L_NOSCAL
GN_DIRECT_L_RAND
GN_DIRECT_L_RAND_NOSCAL
GN_DIRECT_NOSCAL
GN_ESCH
GN_ISRES
GN_MLSL
GN_MLSL_LDS
GN_ORIG_DIRECT
GN_ORIG_DIRECT_L
G_MLSL
G_MLSL_LDS
LD_AUGLAG
LD_AUGLAG_EQ
LD_CCSAQ
LD_LBFGS
LD_MMA
LD_SLSQP
LD_TNEWTON
LD_TNEWTON_PRECOND
LD_TNEWTON_PRECOND_RESTART
LD_TNEWTON_RESTART
LD_VAR1
LD_VAR2
LN_AUGLAG
LN_AUGLAG_EQ
LN_BOBYQA
LN_COBYLA
LN_NELDERMEAD
LN_NEWUOA
LN_NEWUOA_BOUND
LN_PRAXIS
LN_SBPLX
More details on NLopt algorithms are available `here `_ .
The optimization algorithm is instanciated from the NLopt name
.. code-block:: default
algo = ot.NLopt('LD_SLSQP')
define the problem
.. code-block:: default
objective = ot.SymbolicFunction(['x1', 'x2'], ['100*(x2-x1^2)^2+(1-x1)^2'])
inequality_constraint = ot.SymbolicFunction(['x1', 'x2'], ['x1-2*x2'])
dim = objective.getInputDimension()
bounds = ot.Interval([-3.] * dim, [5.] * dim)
problem = ot.OptimizationProblem(objective)
problem.setMinimization(True)
problem.setInequalityConstraint(inequality_constraint)
problem.setBounds(bounds)
solve the problem
.. code-block:: default
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^= [0.517441,0.258721]
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_nlopt_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.083 seconds)
.. _sphx_glr_download_auto_numerical_methods_optimization_plot_optimization_nlopt.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_nlopt.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_optimization_nlopt.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_