.. 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_pagmo.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_pagmo.py: Multi-objective optimization using Pagmo ======================================== .. GENERATED FROM PYTHON SOURCE LINES 6-7 In this example we are going to explore optimization using the `Pagmo `_ solver. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: default import openturns as ot from openturns.viewer import View ot.Log.Show(ot.Log.NONE) ot.RandomGenerator.SetSeed(0) .. GENERATED FROM PYTHON SOURCE LINES 16-17 List available algorithms .. GENERATED FROM PYTHON SOURCE LINES 17-20 .. code-block:: default for algo in ot.Pagmo.GetAlgorithmNames(): print(algo) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none gaco de sade de1220 gwo ihs pso pso_gen sea sga simulated_annealing bee_colony cmaes xnes nsga2 moead mhaco nspso .. GENERATED FROM PYTHON SOURCE LINES 21-22 More details on Pagmo algorithms are available `here `_ . .. GENERATED FROM PYTHON SOURCE LINES 24-25 Create the problem, from ZDT test suite .. GENERATED FROM PYTHON SOURCE LINES 25-30 .. code-block:: default f = ot.SymbolicFunction( ['x1', 'x2'], ['x1', 'var g := 1.0 + 9.0 * (x1 + x2); g * (1.0 - sqrt(x1 / g))']) zdt1 = ot.OptimizationProblem(f) zdt1.setBounds(ot.Interval([0.0] * 2, [1.0] * 2)) .. GENERATED FROM PYTHON SOURCE LINES 31-32 We create the first generation of points by sampling into the bounding box .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: default pop0 = ot.ComposedDistribution([ot.Uniform(0.0, 1.0)] * 2).getSample(100) .. GENERATED FROM PYTHON SOURCE LINES 35-36 We create the algorithm that should evolve over 180 generations .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: default algo = ot.Pagmo(zdt1, 'nsga2', pop0) algo.setGenerationNumber(180) .. GENERATED FROM PYTHON SOURCE LINES 40-41 Benefit from parallel evaluations if the function allows it .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: default algo.setBlockSize(8) .. GENERATED FROM PYTHON SOURCE LINES 44-45 We run the algo .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: default algo.run() pop1 = algo.getResult().getFinalPoints() .. GENERATED FROM PYTHON SOURCE LINES 49-50 We list the available Pareto fronts .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: default fronts = algo.getResult().getParetoFrontsIndices() len(fronts) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 18 .. GENERATED FROM PYTHON SOURCE LINES 54-55 We show the Pareto front .. GENERATED FROM PYTHON SOURCE LINES 55-71 .. code-block:: default graph = ot.Graph("Pareto front", "X1", "X2", True, 'topright') front = algo.getResult().getFinalPoints().select( fronts[0]).sortAccordingToAComponent(0) data = ot.Sample(2*front.getSize()-1, 2) for i in range(front.getSize()): data[2*i] = front[i] if i != front.getSize()-1: data[2*i+1, 0] = front[i+1, 0] data[2*i+1, 1] = front[i, 1] curve = ot.Curve(data) curve.setColor('blue') curve.setLegend(f"front {0}") graph.add(curve) graph.setGrid(True) View(graph) .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_pagmo_001.png :alt: Pareto front :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_pagmo_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 72-73 We show the Pareto front from successive generations .. GENERATED FROM PYTHON SOURCE LINES 73-100 .. code-block:: default fronts = [] for gen in range(5): algo = ot.Pagmo(zdt1, 'nsga2', pop0) algo.setGenerationNumber(gen) algo.run() front0 = algo.getResult().getParetoFrontsIndices()[0] fronts.append(algo.getResult().getFinalPoints().select(front0)) graph = ot.Graph("Successive fronts", "X1", "X2", True, 'topright') palette = ot.Drawable.BuildDefaultPalette(len(fronts)) for k in range(len(fronts)): front = fronts[k].sortAccordingToAComponent(0) print(front) data = ot.Sample(2*front.getSize()-1, 2) for i in range(front.getSize()): data[2*i] = front[i] if i != front.getSize()-1: data[2*i+1, 0] = front[i+1, 0] data[2*i+1, 1] = front[i, 1] curve = ot.Curve(data) curve.setColor(palette[k]) curve.setLegend(f"generation {k}") graph.add(curve) graph.setGrid(True) View(graph) View.ShowAll() .. image-sg:: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_pagmo_002.png :alt: Successive fronts :srcset: /auto_numerical_methods/optimization/images/sphx_glr_plot_optimization_pagmo_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [ v0 v1 ] 0 : [ 0.00210762 0.442878 ] 1 : [ 0.0245595 0.400443 ] 2 : [ 0.085785 0.130402 ] 3 : [ 0.169217 0.0363444 ] 4 : [ 0.383362 0.0199465 ] 5 : [ 0.7411 0.00930063 ] 6 : [ 0.751194 0.00603288 ] [ v0 v1 ] 0 : [ 0.00210762 0.442878 ] 1 : [ 0.0245595 0.400443 ] 2 : [ 0.0245595 0.400443 ] 3 : [ 0.0301332 0.314479 ] 4 : [ 0.0509366 0.130402 ] 5 : [ 0.104095 0.103427 ] 6 : [ 0.159386 0.0155397 ] 7 : [ 0.7411 0.00930063 ] [ v0 v1 ] 0 : [ 0.00210762 0.0540917 ] 1 : [ 0.130451 0.0367106 ] 2 : [ 0.159386 0.0155397 ] 3 : [ 0.159386 0.0155397 ] [ v0 v1 ] 0 : [ 0.000163801 0.0537685 ] 1 : [ 0.00210762 0.040612 ] 2 : [ 0.0529645 0.0363444 ] 3 : [ 0.0805554 0.0155397 ] [ v0 v1 ] 0 : [ 0.000163801 0.0537685 ] 1 : [ 0.000163801 0.0537685 ] 2 : [ 0.00210762 0.040612 ] 3 : [ 0.00260066 0.0332253 ] 4 : [ 0.0233772 0.0153673 ] 5 : [ 0.107405 0.00566359 ] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.348 seconds) .. _sphx_glr_download_auto_numerical_methods_optimization_plot_optimization_pagmo.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_pagmo.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_optimization_pagmo.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_