File: plot_optimization_nlopt.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 (55 lines) | stat: -rw-r--r-- 1,357 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
"""
Optimization using NLopt
========================
"""

# %%
# In this example we are going to explore optimization using the interface to the `NLopt <https://nlopt.readthedocs.io/en/latest/>`_ library.

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

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

# %%
# More details on NLopt algorithms are available `here <https://nlopt.readthedocs.io/en/latest/NLopt_Algorithms/>`_ .

# %%
# The optimization algorithm is instantiated from the NLopt name
algo = ot.NLopt("LD_SLSQP")

# %%
# define the problem
objective = ot.SymbolicFunction(["x1", "x2"], ["100*(x2-x1^2)^2+(1-x1)^2"])
inequality_constraint = ot.SymbolicFunction(["x1", "x2"], ["x1-2*x2"])
dim = objective.getInputDimension()
bounds = ot.Interval([-3.0] * dim, [5.0] * dim)
problem = ot.OptimizationProblem(objective)
problem.setMinimization(True)
problem.setInequalityConstraint(inequality_constraint)
problem.setBounds(bounds)

# %%
# solve the problem
algo.setProblem(problem)
startingPoint = [0.0] * dim
algo.setStartingPoint(startingPoint)
algo.run()

# %%
# retrieve results
result = algo.getResult()
print("x^=", result.getOptimalPoint())

# %%
# draw optimal value history
graph = result.drawOptimalValueHistory()
view = otv.View(graph)

# %%
# Display all figures
otv.View.ShowAll()