.. 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-50 .. code-block:: Python 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 .. code-block:: none [-8] .. GENERATED FROM PYTHON SOURCE LINES 51-52 Making `rastrigin` into a :class:`~openturns.MemoizeFunction` will make it recall all evaluated points. .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: Python rastrigin = ot.MemoizeFunction(rastrigin) .. GENERATED FROM PYTHON SOURCE LINES 57-58 This example is academic and the point achieving the global minimum of the function is known. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. 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 64-65 The optimization bounds must be specified. .. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: Python lowerbound = [-4.4] * dim upperbound = [5.12] * dim bounds = ot.Interval(lowerbound, upperbound) .. GENERATED FROM PYTHON SOURCE LINES 72-74 Plot the iso-values of the objective function --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 76-82 .. 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 = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_001.png :alt: Rastrigin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-84 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 86-88 Create the problem and set the optimization algorithm ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: Python problem = ot.OptimizationProblem(rastrigin) .. GENERATED FROM PYTHON SOURCE LINES 93-94 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 96-101 .. code-block:: Python 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 102-104 Visualize the starting points of the optimization algorithm ----------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 106-117 .. 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 = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_002.png :alt: Rastrigin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 118-119 We see that the starting points are well spread across the input domain of the function. .. GENERATED FROM PYTHON SOURCE LINES 121-123 Solve the optimization problem ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 125-130 .. 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 131-133 .. code-block:: Python xexact .. rst-class:: sphx-glr-script-out .. code-block:: none [0.0, 0.0] .. GENERATED FROM PYTHON SOURCE LINES 134-135 We can see that the solver found a very accurate approximation of the exact solution. .. GENERATED FROM PYTHON SOURCE LINES 137-143 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 145-147 .. code-block:: Python result.getEvaluationNumber() .. rst-class:: sphx-glr-script-out .. code-block:: none 2722 .. GENERATED FROM PYTHON SOURCE LINES 148-149 Let us view these evaluation points. .. GENERATED FROM PYTHON SOURCE LINES 151-162 .. 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 = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_003.png :alt: Rastrigin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 163-164 How fast did it find the global minimum? .. GENERATED FROM PYTHON SOURCE LINES 166-169 .. code-block:: Python graph = result.drawOptimalValueHistory() view = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_004.png :alt: Optimal value history :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 170-175 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 177-188 .. 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 = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_005.png :alt: Rastrigin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_rastrigin_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 189-190 How many times did all :class:`~openturns.Cobyla` instances combined call `rastrigin`? .. GENERATED FROM PYTHON SOURCE LINES 192-193 .. code-block:: Python rastrigin.getInputHistory().getSize() .. rst-class:: sphx-glr-script-out .. code-block:: none 2722 .. _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 `