.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_numerical_methods/optimization/plot_ego.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_ego.py: EfficientGlobalOptimization examples ==================================== .. GENERATED FROM PYTHON SOURCE LINES 7-19 The EGO algorithm [jones1998]_ is an adaptative optimization method based on kriging. An initial design of experiment is used to build a first metamodel. At each iteration a new point that maximizes a criterion is chosen as optimizer candidate. The criterion uses a tradeoff between the metamodel value and the conditional variance. Then the new point is evaluated using the original model and the metamodel is relearnt on the extended design of experiments. .. GENERATED FROM PYTHON SOURCE LINES 22-33 .. code-block:: Python from openturns.usecases import branin_function from openturns.usecases import ackley_function import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.RandomGenerator.SetSeed(0) ot.ResourceMap.SetAsString("KrigingAlgorithm-LinearAlgebra", "LAPACK") ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 34-38 Ackley test-case ---------------- We first apply the EGO algorithm on the :ref:`Ackley function`. .. GENERATED FROM PYTHON SOURCE LINES 40-42 Define the problem ^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 44-45 The Ackley model is defined in the usecases module in a data class `AckleyModel` : .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python am = ackley_function.AckleyModel() .. GENERATED FROM PYTHON SOURCE LINES 48-49 We get the Ackley function : .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python model = am.model .. GENERATED FROM PYTHON SOURCE LINES 52-53 We specify the domain of the model : .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python dim = am.dim lowerbound = [-4.0] * dim upperbound = [4.0] * dim .. GENERATED FROM PYTHON SOURCE LINES 58-59 We know that the global minimum is at the center of the domain. It is stored in the `AckleyModel` data class. .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python xexact = am.x0 .. GENERATED FROM PYTHON SOURCE LINES 62-63 The minimum value attained `fexact` is : .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python fexact = model(xexact) fexact .. raw:: html
class=Point name=Unnamed dimension=1 values=[4.44089e-16]


.. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: Python graph = model.draw(lowerbound, upperbound, [100] * dim) graph.setTitle("Ackley function") view = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_001.png :alt: Ackley function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-73 We see that the Ackley function has many local minimas. The global minimum, however, is unique and located at the center of the domain. .. GENERATED FROM PYTHON SOURCE LINES 75-83 Create the initial kriging ^^^^^^^^^^^^^^^^^^^^^^^^^^ Before using the EGO algorithm, we must create an initial kriging. In order to do this, we must create a design of experiment which fills the space. In this situation, the :class:`~openturns.LHSExperiment` is a good place to start (but other design of experiments may allow one to better fill the space). We use a uniform distribution in order to create a LHS design. The length of the first LHS is set to ten times the problem dimension as recommended in [jones1998]_. .. GENERATED FROM PYTHON SOURCE LINES 85-94 .. code-block:: Python listUniformDistributions = [ ot.Uniform(lowerbound[i], upperbound[i]) for i in range(dim) ] distribution = ot.JointDistribution(listUniformDistributions) sampleSize = 10 * dim experiment = ot.LHSExperiment(distribution, sampleSize) inputSample = experiment.generate() outputSample = model(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 95-102 .. code-block:: Python graph = ot.Graph( "Initial LHS design of experiment - n=%d" % (sampleSize), "$x_0$", "$x_1$", True ) cloud = ot.Cloud(inputSample) graph.add(cloud) view = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_002.png :alt: Initial LHS design of experiment - n=20 :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 103-105 We now create the kriging metamodel. We selected the :class:`~openturns.MaternModel` covariance model with a constant basis as recommended in [leriche2021]_. .. GENERATED FROM PYTHON SOURCE LINES 107-112 .. code-block:: Python covarianceModel = ot.MaternModel([1.0] * dim, [0.5], 2.5) basis = ot.ConstantBasisFactory(dim).build() kriging = ot.KrigingAlgorithm(inputSample, outputSample, covarianceModel, basis) kriging.run() .. GENERATED FROM PYTHON SOURCE LINES 113-117 Create the optimization problem ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ We finally create the :class:`~openturns.OptimizationProblem` and solve it with class:`~openturns.EfficientGlobalOptimization`. .. GENERATED FROM PYTHON SOURCE LINES 119-124 .. code-block:: Python problem = ot.OptimizationProblem() problem.setObjective(model) bounds = ot.Interval(lowerbound, upperbound) problem.setBounds(bounds) .. GENERATED FROM PYTHON SOURCE LINES 125-129 In order to show the various options, we configure them all, even if most could be left to default settings in this case. The most important method is :class:`~openturns.EfficientGlobalOptimization.setMaximumCallsNumber` which limits the number of iterations that the algorithm can perform. In the Ackley example, we choose to perform 30 iterations of the algorithm. .. GENERATED FROM PYTHON SOURCE LINES 131-136 .. code-block:: Python algo = ot.EfficientGlobalOptimization(problem, kriging.getResult()) algo.setMaximumCallsNumber(30) algo.run() result = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 137-139 .. code-block:: Python result.getIterationNumber() .. rst-class:: sphx-glr-script-out .. code-block:: none 30 .. GENERATED FROM PYTHON SOURCE LINES 140-142 .. code-block:: Python result.getOptimalPoint() .. raw:: html
class=Point name=Unnamed dimension=2 values=[-0.0062633,-0.0052574]


.. GENERATED FROM PYTHON SOURCE LINES 143-145 .. code-block:: Python result.getOptimalValue() .. raw:: html
class=Point name=Unnamed dimension=1 values=[0.0249089]


.. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: Python fexact .. raw:: html
class=Point name=Unnamed dimension=1 values=[4.44089e-16]


.. GENERATED FROM PYTHON SOURCE LINES 149-155 Compared to the minimum function value, we see that the EGO algorithm provides solution which is accurate. Indeed, the optimal point is in the neighbourhood of the exact solution, and this is quite an impressive success given the limited amount of function evaluations: only 20 evaluations for the initial DOE and 30 iterations of the EGO algorithm, for a total equal to 50 function evaluations. .. GENERATED FROM PYTHON SOURCE LINES 157-162 .. code-block:: Python graph = result.drawOptimalValueHistory() optimum_curve = ot.Curve(ot.Sample([[0, fexact[0]], [29, fexact[0]]])) graph.add(optimum_curve) view = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_003.png :alt: Optimal value history :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 163-165 .. code-block:: Python inputHistory = result.getInputSample() .. GENERATED FROM PYTHON SOURCE LINES 166-178 .. code-block:: Python graph = model.draw(lowerbound, upperbound, [100] * dim) graph.setTitle("Ackley function") cloud = ot.Cloud(inputSample, "initial") cloud.setPointStyle("bullet") cloud.setColor("black") graph.add(cloud) cloud = ot.Cloud(inputHistory, "solution") cloud.setPointStyle("diamond") cloud.setColor("forestgreen") graph.add(cloud) view = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_004.png :alt: Ackley function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 179-181 We see that the initial (black) points are dispersed in the whole domain, while the solution points are much closer to the solution. .. GENERATED FROM PYTHON SOURCE LINES 183-187 Branin test-case ---------------- We now take a look at the :ref:`Branin-Hoo` function. .. GENERATED FROM PYTHON SOURCE LINES 189-191 Define the problem ^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 193-194 The Branin model is defined in the usecases module in a data class `BraninModel` : .. GENERATED FROM PYTHON SOURCE LINES 194-196 .. code-block:: Python bm = branin_function.BraninModel() .. GENERATED FROM PYTHON SOURCE LINES 197-198 We load the dimension, .. GENERATED FROM PYTHON SOURCE LINES 198-200 .. code-block:: Python dim = bm.dim .. GENERATED FROM PYTHON SOURCE LINES 201-202 the domain boundaries, .. GENERATED FROM PYTHON SOURCE LINES 202-205 .. code-block:: Python lowerbound = bm.lowerbound upperbound = bm.upperbound .. GENERATED FROM PYTHON SOURCE LINES 206-207 and we load the model function and its noise : .. GENERATED FROM PYTHON SOURCE LINES 207-210 .. code-block:: Python objectiveFunction = bm.model noise = bm.noiseModel .. GENERATED FROM PYTHON SOURCE LINES 211-212 We build a sample out of the three minima : .. GENERATED FROM PYTHON SOURCE LINES 212-214 .. code-block:: Python xexact = ot.Sample([bm.xexact1, bm.xexact2, bm.xexact3]) .. GENERATED FROM PYTHON SOURCE LINES 215-216 The minimum value attained `fexact` is : .. GENERATED FROM PYTHON SOURCE LINES 216-219 .. code-block:: Python fexact = objectiveFunction(xexact) fexact .. raw:: html
y0
0-1.04741
1-1.04741
2-1.04741


.. GENERATED FROM PYTHON SOURCE LINES 220-224 .. code-block:: Python graph = objectiveFunction.draw(lowerbound, upperbound, [100] * dim) graph.setTitle("Branin function") view = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_005.png :alt: Branin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 225-226 The Branin function has three local minimas. .. GENERATED FROM PYTHON SOURCE LINES 228-230 Create the initial kriging ^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 232-239 .. code-block:: Python distribution = ot.JointDistribution([ot.Uniform(0.0, 1.0)] * dim) sampleSize = 10 * dim experiment = ot.LHSExperiment(distribution, sampleSize) inputSample = experiment.generate() outputSample = objectiveFunction(inputSample) noiseSample = noise(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 240-247 .. code-block:: Python graph = ot.Graph( "Initial LHS design of experiment - n=%d" % (sampleSize), "$x_0$", "$x_1$", True ) cloud = ot.Cloud(inputSample) graph.add(cloud) view = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_006.png :alt: Initial LHS design of experiment - n=20 :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 248-252 .. code-block:: Python covarianceModel = ot.MaternModel([1.0] * dim, [0.5], 2.5) basis = ot.ConstantBasisFactory(dim).build() kriging = ot.KrigingAlgorithm(inputSample, outputSample, covarianceModel, basis) .. GENERATED FROM PYTHON SOURCE LINES 253-256 .. code-block:: Python kriging.setNoise([x[0] for x in noiseSample]) kriging.run() .. GENERATED FROM PYTHON SOURCE LINES 257-259 Create and solve the problem ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 261-262 We define the problem : .. GENERATED FROM PYTHON SOURCE LINES 262-267 .. code-block:: Python problem = ot.OptimizationProblem() problem.setObjective(objectiveFunction) bounds = ot.Interval(lowerbound, upperbound) problem.setBounds(bounds) .. GENERATED FROM PYTHON SOURCE LINES 268-269 We configure the algorithm, with the model noise: .. GENERATED FROM PYTHON SOURCE LINES 269-272 .. code-block:: Python algo = ot.EfficientGlobalOptimization(problem, kriging.getResult(), noise) algo.setMaximumCallsNumber(30) .. GENERATED FROM PYTHON SOURCE LINES 273-274 We run the algorithm and get the result: .. GENERATED FROM PYTHON SOURCE LINES 274-277 .. code-block:: Python algo.run() result = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 278-280 .. code-block:: Python result.getIterationNumber() .. rst-class:: sphx-glr-script-out .. code-block:: none 30 .. GENERATED FROM PYTHON SOURCE LINES 281-283 .. code-block:: Python result.getOptimalPoint() .. raw:: html
class=Point name=Unnamed dimension=2 values=[0.528177,0.167468]


.. GENERATED FROM PYTHON SOURCE LINES 284-286 .. code-block:: Python result.getOptimalValue() .. raw:: html
class=Point name=Unnamed dimension=1 values=[-1.04293]


.. GENERATED FROM PYTHON SOURCE LINES 287-289 .. code-block:: Python fexact .. raw:: html
y0
0-1.04741
1-1.04741
2-1.04741


.. GENERATED FROM PYTHON SOURCE LINES 290-292 .. code-block:: Python inputHistory = result.getInputSample() .. GENERATED FROM PYTHON SOURCE LINES 293-305 .. code-block:: Python graph = objectiveFunction.draw(lowerbound, upperbound, [100] * dim) graph.setTitle("Branin function") cloud = ot.Cloud(inputSample, "initial") cloud.setPointStyle("bullet") cloud.setColor("black") graph.add(cloud) cloud = ot.Cloud(inputHistory, "solution") cloud.setPointStyle("diamond") cloud.setColor("forestgreen") graph.add(cloud) view = viewer.View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_007.png :alt: Branin function :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 306-307 We see that the EGO algorithm reached the different optima locations. .. GENERATED FROM PYTHON SOURCE LINES 309-315 .. code-block:: Python graph = result.drawOptimalValueHistory() optimum_curve = ot.Curve(ot.Sample([[0, fexact[0][0]], [29, fexact[0][0]]])) graph.add(optimum_curve) view = viewer.View(graph, axes_kw={"xticks": range(0, result.getIterationNumber(), 5)}) plt.show() .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_008.png :alt: Optimal value history :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_ego_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 316-317 Reset default settings .. GENERATED FROM PYTHON SOURCE LINES 317-318 .. code-block:: Python ot.ResourceMap.Reload() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.282 seconds) .. _sphx_glr_download_auto_numerical_methods_optimization_plot_ego.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_ego.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_ego.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_ego.zip `