.. 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-47 .. 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 48-49 Making `rastrigin` into a :class:`~openturns.MemoizeFunction` will make it recall all evaluated points. .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: default rastrigin = ot.MemoizeFunction(rastrigin) .. GENERATED FROM PYTHON SOURCE LINES 54-55 This example is academic and the point achieving the global minimum of the function is known. .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. 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 61-62 The optimization bounds must be specified. .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: default lowerbound = [-4.4] * dim upperbound = [5.12] * dim bounds = ot.Interval(lowerbound, upperbound) .. GENERATED FROM PYTHON SOURCE LINES 69-71 Plot the iso-values of the objective function --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. 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-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 80-81 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 83-85 Create the problem and set the optimization algorithm ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: default problem = ot.OptimizationProblem(rastrigin) .. GENERATED FROM PYTHON SOURCE LINES 90-91 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 93-100 .. 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 101-103 Visualize the starting points of the optimization algorithm ----------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 105-116 .. code-block:: default 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 117-118 We see that the starting points are well spread across the input domain of the function. .. GENERATED FROM PYTHON SOURCE LINES 120-122 Solve the optimization problem ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 124-129 .. code-block:: default solver.run() result = solver.getResult() xoptim = result.getOptimalPoint() print(xoptim) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [3.86439e-06,-5.49779e-06] .. GENERATED FROM PYTHON SOURCE LINES 130-132 .. code-block:: default xexact .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.0, 0.0] .. GENERATED FROM PYTHON SOURCE LINES 133-134 We can see that the solver found a very accurate approximation of the exact solution. .. GENERATED FROM PYTHON SOURCE LINES 136-142 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 144-146 .. code-block:: default result.getEvaluationNumber() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 41 .. GENERATED FROM PYTHON SOURCE LINES 147-148 Let us view these evaluation points. .. GENERATED FROM PYTHON SOURCE LINES 150-161 .. 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-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 162-163 How fast did it find the global minimum? .. GENERATED FROM PYTHON SOURCE LINES 165-168 .. code-block:: default 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 169-174 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 176-187 .. 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-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 188-189 How many times did all :class:`~openturns.Cobyla` instances combined call `rastrigin`? .. GENERATED FROM PYTHON SOURCE LINES 191-192 .. code-block:: default rastrigin.getInputHistory().getSize() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2327 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.845 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 `_