.. 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_rastrigin.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_rastrigin.py: Optimization of the Rastrigin test function =========================================== The Rastrigin function is defined by: .. math:: f(\vect{x}) = A + \sum_{i=1}^n \left[x_i^2 - A\cos(2 \pi x_i)\right] where :math:`A=10` and :math:`\vect{x}\in[-5.12,5.12]^n`. It has a global minimum at :math:`\vect{x} = \vect{0}` where :math:`f(\vect{x})= - 10`. This function has many local minima, so optimization algorithms must be run from multiple starting points. In our example, we consider the bidimensional case, i.e. :math:`n=2`. **References**: - Rastrigin, L. A. "Systems of extremal control." Mir, Moscow (1974). - Rudolph, G. "Globale Optimierung mit parallelen Evolutionsstrategien". Diplomarbeit. Department of Computer Science, University of Dortmund, July 1990. .. GENERATED FROM PYTHON SOURCE LINES 26-28 Definition of the problem ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-45 .. code-block:: default import openturns as ot import openturns.viewer as viewer import numpy as np ot.Log.Show(ot.Log.NONE) def rastriginPy(X): A = 10.0 delta = [x**2 - A * np.cos(2 * np.pi * x) for x in X] y = A + sum(delta) return [y] dim = 2 rastrigin = ot.PythonFunction(dim, 1, rastriginPy) print(rastrigin([1.0, 1.0])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [-8] .. GENERATED FROM PYTHON SOURCE LINES 46-47 Making `rastrigin` into a :class:`~openturns.MemoizeFunction` will make it recall all evaluated points. .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: default rastrigin = ot.MemoizeFunction(rastrigin) .. GENERATED FROM PYTHON SOURCE LINES 52-53 This example is academic and the point achieving the global minimum of the function is known. .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: default xexact = [0.0] * dim print(xexact) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.0, 0.0] .. GENERATED FROM PYTHON SOURCE LINES 59-60 The optimization bounds must be specified. .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: default lowerbound = [-4.4] * dim upperbound = [5.12] * dim bounds = ot.Interval(lowerbound, upperbound) .. GENERATED FROM PYTHON SOURCE LINES 67-69 Plot the iso-values of the objective function --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 71-76 .. code-block:: default graph = rastrigin.draw(lowerbound, upperbound, [100]*dim) graph.setTitle("Rastrigin function") view = viewer.View(graph, legend_kw={'bbox_to_anchor':(1,1), 'loc':"upper left"}) view.getFigure().tight_layout() .. image:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_001.png :alt: Rastrigin function :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-78 We see that the Rastrigin function has several local minima. However, there is only one single global minimum at :math:`\vect{x}^\star=(0, 0)`. .. GENERATED FROM PYTHON SOURCE LINES 80-82 Create the problem and set the optimization algorithm ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: default problem = ot.OptimizationProblem(rastrigin) .. GENERATED FROM PYTHON SOURCE LINES 87-88 We use the :class:`~openturns.Cobyla` algorithm and run it from multiple starting points selected by a :class:`~openturns.LowDiscrepancyExperiment`. .. GENERATED FROM PYTHON SOURCE LINES 90-95 .. code-block:: default size = 64 distribution = ot.ComposedDistribution([ot.Uniform(lowerbound[0], upperbound[0])] * dim) experiment = ot.LowDiscrepancyExperiment(ot.SobolSequence(), distribution, size) solver = ot.MultiStart(ot.Cobyla(problem), experiment.generate()) .. GENERATED FROM PYTHON SOURCE LINES 96-98 Visualize the starting points of the optimization algorithm ----------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 100-111 .. code-block:: default startingPoints = solver.getStartingPoints() graph = rastrigin.draw(lowerbound, upperbound, [100]*dim) graph.setTitle("Rastrigin function") cloud = ot.Cloud(startingPoints) cloud.setPointStyle("bullet") cloud.setColor("black") graph.add(cloud) graph.setLegends([""]) # sphinx_gallery_thumbnail_number = 2 view = viewer.View(graph) .. image:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_002.png :alt: Rastrigin function :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-113 We see that the starting points are well spread accross the input domain of the function. .. GENERATED FROM PYTHON SOURCE LINES 115-117 Solve the optimization problem ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 119-124 .. code-block:: default solver.run() result = solver.getResult() xoptim = result.getOptimalPoint() print(xoptim) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [2.94029e-06,6.14939e-06] .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: default xexact .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.0, 0.0] .. GENERATED FROM PYTHON SOURCE LINES 128-129 We can see that the solver found a very accurate approximation of the exact solution. .. GENERATED FROM PYTHON SOURCE LINES 131-137 Analyze the optimization process -------------------------------- :class:`~openturns.MultiStart` ran an instance of :class:`~openturns.Cobyla` from each starting point. Let us focus on the instance that found the global minimum. How many times did it evaluate `rastrigin`? .. GENERATED FROM PYTHON SOURCE LINES 139-141 .. code-block:: default result.getEvaluationNumber() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 41 .. GENERATED FROM PYTHON SOURCE LINES 142-143 Let us view these evaluation points. .. GENERATED FROM PYTHON SOURCE LINES 145-155 .. code-block:: default inputSample = result.getInputSample() graph = rastrigin.draw(lowerbound, upperbound, [100]*dim) graph.setTitle("Rastrigin function") cloud = ot.Cloud(inputSample) cloud.setPointStyle("bullet") cloud.setColor("black") graph.add(cloud) view = viewer.View(graph, legend_kw={'bbox_to_anchor':(1,1), 'loc':"upper left"}) view.getFigure().tight_layout() .. image:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_003.png :alt: Rastrigin function :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 156-157 How fast did it find the global minimum? .. GENERATED FROM PYTHON SOURCE LINES 159-162 .. code-block:: default graph = result.drawOptimalValueHistory() view = viewer.View(graph) .. image:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_004.png :alt: Optimal value history :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 163-168 Let us now analyze the :class:`~openturns.MultiStart` process as a whole. Since `rastrigin` is a :class:`~openturns.MemoizeFunction`, it has a :meth:`~openturns.MemoizeFunction.getInputHistory` method which lets us see all points it was evaluated on since its creation. .. GENERATED FROM PYTHON SOURCE LINES 170-180 .. code-block:: default inputSample = rastrigin.getInputHistory() graph = rastrigin.draw(lowerbound, upperbound, [100]*dim) graph.setTitle("Rastrigin function") cloud = ot.Cloud(inputSample) cloud.setPointStyle("bullet") cloud.setColor("black") graph.add(cloud) view = viewer.View(graph, legend_kw={'bbox_to_anchor':(1,1), 'loc':"upper left"}) view.getFigure().tight_layout() .. image:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_005.png :alt: Rastrigin function :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 181-182 How many times did all :class:`~openturns.Cobyla` instances combined call `rastrigin`? .. GENERATED FROM PYTHON SOURCE LINES 184-185 .. code-block:: default rastrigin.getInputHistory().getSize() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2339 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.232 seconds) .. _sphx_glr_download_auto_numerical_methods_optimization_plot_optimization_rastrigin.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_rastrigin.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_optimization_rastrigin.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_