.. 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 :ref:`Go to the end ` 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 =========================================== .. GENERATED FROM PYTHON SOURCE LINES 7-26 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 28-30 Definition of the problem ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-48 .. code-block:: Python import openturns as ot import openturns.viewer as otv import math as m def rastriginPy(X): A = 10.0 delta = [x**2 - A * m.cos(2 * m.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 .. code-block:: none [-8] .. GENERATED FROM PYTHON SOURCE LINES 49-50 Making `rastrigin` into a :class:`~openturns.MemoizeFunction` will make it recall all evaluated points. .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python rastrigin = ot.MemoizeFunction(rastrigin) .. GENERATED FROM PYTHON SOURCE LINES 55-56 This example is academic and the point achieving the global minimum of the function is known. .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: Python xexact = [0.0] * dim print(xexact) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.0, 0.0] .. GENERATED FROM PYTHON SOURCE LINES 62-63 The optimization bounds must be specified. .. GENERATED FROM PYTHON SOURCE LINES 65-69 .. code-block:: Python lowerbound = [-4.4] * dim upperbound = [5.12] * dim bounds = ot.Interval(lowerbound, upperbound) .. GENERATED FROM PYTHON SOURCE LINES 70-72 Plot the iso-values of the objective function --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 74-80 .. code-block:: Python graph = rastrigin.draw(lowerbound, upperbound, [100] * dim) graph.setTitle("Rastrigin function") graph.setLegendPosition("upper left") graph.setLegendCorner([1.0, 1.0]) view = otv.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_001.svg :alt: Rastrigin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-82 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 84-86 Create the problem and set the optimization algorithm ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: Python problem = ot.OptimizationProblem(rastrigin) .. GENERATED FROM PYTHON SOURCE LINES 91-92 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 94-99 .. code-block:: Python size = 64 distribution = ot.JointDistribution([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 100-102 Visualize the starting points of the optimization algorithm ----------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 104-115 .. code-block:: Python startingPoints = solver.getStartingSample() 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 = otv.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_002.svg :alt: Rastrigin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-117 We see that the starting points are well spread across the input domain of the function. .. GENERATED FROM PYTHON SOURCE LINES 119-121 Solve the optimization problem ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 123-128 .. code-block:: Python solver.run() result = solver.getResult() xoptim = result.getOptimalPoint() print(xoptim) .. rst-class:: sphx-glr-script-out .. code-block:: none [1.60785e-06,-3.76144e-06] .. GENERATED FROM PYTHON SOURCE LINES 129-131 .. code-block:: Python xexact .. rst-class:: sphx-glr-script-out .. code-block:: none [0.0, 0.0] .. GENERATED FROM PYTHON SOURCE LINES 132-133 We can see that the solver found a very accurate approximation of the exact solution. .. GENERATED FROM PYTHON SOURCE LINES 135-141 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 143-145 .. code-block:: Python result.getCallsNumber() .. rst-class:: sphx-glr-script-out .. code-block:: none 2722 .. GENERATED FROM PYTHON SOURCE LINES 146-147 Let us view these evaluation points. .. GENERATED FROM PYTHON SOURCE LINES 149-160 .. code-block:: Python 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) graph.setLegendCorner([1.0, 1.0]) graph.setLegendPosition("upper left") view = otv.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_003.svg :alt: Rastrigin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 161-162 How fast did it find the global minimum? .. GENERATED FROM PYTHON SOURCE LINES 164-167 .. code-block:: Python graph = result.drawOptimalValueHistory() view = otv.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_004.svg :alt: Optimal value history :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 168-173 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 175-186 .. code-block:: Python 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) graph.setLegendCorner([1.0, 1.0]) graph.setLegendPosition("upper left") view = otv.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_005.svg :alt: Rastrigin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 187-188 How many times did all :class:`~openturns.Cobyla` instances combined call `rastrigin`? .. GENERATED FROM PYTHON SOURCE LINES 188-190 .. code-block:: Python print(rastrigin.getInputHistory().getSize()) .. rst-class:: sphx-glr-script-out .. code-block:: none 2722 .. GENERATED FROM PYTHON SOURCE LINES 191-192 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_numerical_methods_optimization_plot_optimization_rastrigin.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_optimization_rastrigin.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_optimization_rastrigin.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_optimization_rastrigin.zip `