.. 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 :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_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 :class:`~openturns.Pagmo` solver. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: Python 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:: Python for algo in ot.Pagmo.GetAlgorithmNames(): print(algo) .. rst-class:: sphx-glr-script-out .. code-block:: none gaco de sade de1220 gwo ihs pso pso_gen sea sga simulated_annealing bee_colony cmaes xnes nsga2 moead moead_gen mhaco nspso .. GENERATED FROM PYTHON SOURCE LINES 21-22 Create the problem, from ZDT test suite .. GENERATED FROM PYTHON SOURCE LINES 22-28 .. code-block:: Python 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 29-30 We create the first generation of points by sampling into the bounding box .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python pop0 = ot.ComposedDistribution([ot.Uniform(0.0, 1.0)] * 2).getSample(100) .. GENERATED FROM PYTHON SOURCE LINES 33-34 We create the algorithm that should evolve over 10 generations .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python algo = ot.Pagmo(zdt1, "nsga2", pop0) algo.setMaximumIterationNumber(10) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Benefit from parallel evaluations if the function allows it .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python algo.setBlockSize(8) .. GENERATED FROM PYTHON SOURCE LINES 42-43 We run the algo .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python algo.run() pop1 = algo.getResult().getFinalPoints() .. GENERATED FROM PYTHON SOURCE LINES 47-48 We list the available Pareto fronts .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: Python fronts = algo.getResult().getParetoFrontsIndices() len(fronts) .. rst-class:: sphx-glr-script-out .. code-block:: none 9 .. GENERATED FROM PYTHON SOURCE LINES 52-53 We show the Pareto front .. GENERATED FROM PYTHON SOURCE LINES 53-68 .. code-block:: Python graph = ot.Graph("Pareto front", "y1", "y2", True, "upper right") front = algo.getResult().getFinalValues().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 .. GENERATED FROM PYTHON SOURCE LINES 69-70 We show the Pareto front from successive generations .. GENERATED FROM PYTHON SOURCE LINES 70-95 .. code-block:: Python fronts = [] for gen in range(5): algo = ot.Pagmo(zdt1, "nsga2", pop0) algo.setMaximumIterationNumber(gen) algo.run() front0 = algo.getResult().getParetoFrontsIndices()[0] fronts.append(algo.getResult().getFinalValues().select(front0)) graph = ot.Graph("Successive fronts", "y1", "y2", True, "upper right") 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.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 .. code-block:: none [ v0 v1 ] 0 : [ 0.00210762 4.90217 ] 1 : [ 0.0245595 4.48078 ] 2 : [ 0.085785 2.443 ] 3 : [ 0.169217 2.15559 ] [ v0 v1 ] 0 : [ 0.00210762 4.90217 ] 1 : [ 0.0245595 4.48078 ] 2 : [ 0.0245595 4.48078 ] 3 : [ 0.0301332 3.74995 ] 4 : [ 0.0509366 2.2659 ] 5 : [ 0.159386 1.93377 ] [ v0 v1 ] 0 : [ 0.00210762 1.44946 ] [ v0 v1 ] 0 : [ 0.000163801 1.46979 ] 1 : [ 0.00210762 1.33046 ] [ v0 v1 ] 0 : [ 0.000163801 1.46979 ] 1 : [ 0.000163801 1.46979 ] 2 : [ 0.00210762 1.33046 ] 3 : [ 0.00260066 1.26379 ] 4 : [ 0.0233772 1.17114 ] .. _sphx_glr_download_auto_numerical_methods_optimization_plot_optimization_pagmo.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_pagmo.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_optimization_pagmo.py `