Skip to content

Commit

Permalink
nicer plotting; new release
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippoAiraldi committed Nov 21, 2022
1 parent 9e33109 commit b408fe9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 8 additions & 6 deletions examples/sensitivity3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def z(x, lam, p):
nlp.init_solver(opts)


# # Use IPOPT to solve the nonlinear optimization
# Use IPOPT to solve the nonlinear optimization
M = nlp.to_function('M', [p, x], [x], ['p', 'x0'], ['x'])

p_values = ((1.4, 1.9), (2.2, 1.9), (2.2, 1.1))
Expand All @@ -69,15 +69,17 @@ def z(x, lam, p):
# How does the optimal solution vary along p?
Z = nlp.to_function('Z', [p, x], [z(x, lam, p)], ['p', 'x0'], ['z'])
fig, ax = plt.subplots(
constrained_layout=True, subplot_kw={'projection': '3d'})
constrained_layout=True,
subplot_kw={'projection': '3d', 'computed_zorder': False})
N = 150
P = np.meshgrid(np.linspace(1, 2.5, N), np.linspace(1, 2, N))
try:
Z_values = io.loadmat('Z_values.mat')['z']
except Exception:
Z_values = Z(np.row_stack((P[0].flat, P[1].flat)), 0).full().reshape(N, N)
io.savemat('Z_values.mat', {'p0': P[0], 'p1': P[1], 'z': Z_values})
ax.plot_surface(P[0], P[1], Z_values, cmap='viridis', antialiased=False, lw=0)
ax.plot_wireframe(P[0], P[1], Z_values, color='k', antialiased=False,
rstride=5, cstride=5, zorder=0)


# Parametric sensitivities of function z(x(p), lam(p))
Expand All @@ -91,11 +93,11 @@ def z(x, lam, p):
Z_ = sol.value(Z).full()
J_ = sol.value(J).full()
H_ = sol.value(H).full()
c = ax.scatter(p_[0], p_[1], Z_, s=200)
c = ax.scatter(p_[0], p_[1], Z_, s=100, zorder=2)
deltaP = P_flat - np.array(p_)[:, None]
S = Z_ + J_.T @ deltaP + 0.5 * np.diag(deltaP.T @ H_ @ deltaP)
ax.plot_surface(
P[0], P[1], S.reshape(N, N), color=c.get_facecolor(), alpha=0.3, lw=0)
ax.plot_surface(P[0], P[1], S.reshape(N, N),
color=c.get_facecolor(), alpha=0.3, lw=0, zorder=1)

ax.set_xlabel('$p_0$')
ax.set_ylabel('$p_1$')
Expand Down
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "csnlp"
version = "1.1.0"
version = "1.1.1"
authors = [
{ name="Filippo Airaldi", email="filippoairaldi@gmail.com" },
]
Expand All @@ -22,6 +22,13 @@ dependencies = [
"casadi >= 3.5.5"
]

[project.optional-dependencies]
all = [
"scipy >= 1.9.3",
"matplotlib >= 3.6.2",
"tikzplotlib >= 0.10.1",
]

[project.urls]
"Homepage" = "https://github.com/FilippoAiraldi/casadi-nlp"
"Bug Tracker" = "https://github.com/FilippoAiraldi/casadi-nlp/issues"

0 comments on commit b408fe9

Please sign in to comment.