File: plot_optimization_pagmo.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (93 lines) | stat: -rw-r--r-- 2,567 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Multi-objective optimization using Pagmo
========================================
"""

# %%
# In this example we are going to explore optimization using the :class:`~openturns.Pagmo` solver.

# %%
import openturns as ot
import openturns.viewer as otv

# %%
# List available algorithms
for algo in ot.Pagmo.GetAlgorithmNames():
    print(algo)

# %%
# Create the problem, from ZDT test suite
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))

# %%
# We create the first generation of points by sampling into the bounding box
pop0 = ot.JointDistribution([ot.Uniform(0.0, 1.0)] * 2).getSample(100)

# %%
# We create the algorithm that should evolve over 10 generations
algo = ot.Pagmo(zdt1, "nsga2", pop0)
algo.setMaximumIterationNumber(10)

# %%
# Benefit from parallel evaluations if the function allows it
algo.setBlockSize(8)

# %%
# We run the algo
algo.run()
pop1 = algo.getResult().getFinalPoints()

# %%
# We list the available Pareto fronts
fronts = algo.getResult().getParetoFrontsIndices()
len(fronts)

# %%
# We show the Pareto front
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.setLegend(f"front {0}")
graph.add(curve)
graph.setGrid(True)
_ = otv.View(graph)

# %%
# We show the Pareto front from successive generations
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)
_ = otv.View(graph)


otv.View.ShowAll()

# %%