forked from mdolab/pyoptsparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhs015VarPlot.py
More file actions
77 lines (64 loc) · 1.98 KB
/
hs015VarPlot.py
File metadata and controls
77 lines (64 loc) · 1.98 KB
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
"""
This script shows a possible post-processing approach once the history file is generated.
It visualizes the design space, and plots the objective and constraint contours.
It also reads in the history files and plots the path of the optimization.
In order to visualize the optimization history, the history files must be available.
They can be generated by running the hs015.py script for each optimizer, e.g.
>>> python hs015.py --storeHistory --opt slsqp
>>> python hs015.py --storeHistory --opt conmin
"""
# External modules
import matplotlib.pyplot as plt
import numpy as np
# First party modules
from pyoptsparse import History
db = {}
opts = ["ipopt", "slsqp", "snopt", "conmin", "nlpqlp", "psqp"]
for opt in opts:
fileName = f"{opt}_hs015_Hist.hst"
try:
db[opt] = History(fileName)
except FileNotFoundError:
pass
obj = {}
xuser = {}
for opt in db.keys():
val = db[opt].getValues()
obj[opt] = val["obj"]
xuser[opt] = val["xvars"]
# Generate the Rosenbrock contours
delta = 0.1
x = np.arange(-2.5, 1.5, delta)
y = np.arange(-6.5, 3.0, delta)
X, Y = np.meshgrid(x, y)
# objective
Z = 100 * (Y - X**2) ** 2 + (1 - X) ** 2
# and the constraint contours
A = X * Y
B = X + Y**2
# plot the contours and constraints
plt.figure()
levels = [250, 500, 1000, 2000, 3000, 6000]
CS = plt.contour(X, Y, Z, levels, colors="k")
levels = np.arange(1.0, 1.01)
CS1 = plt.contour(X, Y, A, levels, colors="g")
levels = np.arange(0.0, 0.01)
CS2 = plt.contour(X, Y, B, levels, colors="b")
plt.clabel(CS, inline=1)
plt.clabel(CS1, inline=1)
plt.clabel(CS2, inline=1)
# set the one sided variable
xupper = [0.5, 0.5]
yupper = [-7, 3.0]
# Now plot the optimizer output
styleList = ["ko-", "ro-", "bo-", "go-", "mo-", "co-", "ks--"]
counter = 0
for opt in db.keys():
plt.plot(xuser[opt][:, 0], xuser[opt][:, 1], styleList[counter], label=opt)
counter += 1
plt.plot(xupper, yupper, "k")
plt.legend(loc=3)
plt.xlabel("x1")
plt.ylabel("x2")
plt.title("Simple optimizer comparison")
plt.show()