Skip to content

Commit b5ed35c

Browse files
committed
hoftix L2O plots
fallback condition to deal with different matplotlib versions handling contour path effects
1 parent 2059876 commit b5ed35c

12 files changed

+621
-599
lines changed

examples/parametric_programming/Part_1_basics.ipynb

Lines changed: 207 additions & 306 deletions
Large diffs are not rendered by default.

examples/parametric_programming/Part_1_basics.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ def NLP_param(a, p, opti_silent=False):
193193
opti.set_value(a_opti, a)
194194
return opti, x, y
195195

196-
for s in range(0, 20):
196+
197+
for s in range(0, 10):
197198
# selected parameters for a single instance problem
198-
p = 0.05*s + 0.5
199-
a = 0.05*s + 0.2
199+
p = 0.1 * s + 0.5
200+
a = 0.1 * s + 0.2
200201
# construct casadi problem
201202
opti, x, y = NLP_param(a, p)
202203
# solve NLP via casadi
@@ -223,15 +224,21 @@ def NLP_param(a, p, opti_silent=False):
223224
alpha=0.6)
224225
fig.colorbar(cp)
225226
ax.set_title('Rosenbrock problem')
227+
226228
cg1 = ax.contour(xx, yy, c1, [0], colors='mediumblue', alpha=0.7)
227-
plt.setp(cg1.collections,
228-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
229+
if hasattr(cg1, 'collections') and cg1.collections:
230+
plt.setp(cg1.collections,
231+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
232+
229233
cg2 = ax.contour(xx, yy, c2, [0], colors='mediumblue', alpha=0.7)
230-
plt.setp(cg2.collections,
231-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
234+
if hasattr(cg2, 'collections') and cg2.collections:
235+
plt.setp(cg2.collections,
236+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
237+
232238
cg3 = ax.contour(xx, yy, c3, [0], colors='mediumblue', alpha=0.7)
233-
plt.setp(cg3.collections,
234-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
239+
if hasattr(cg3, 'collections') and cg3.collections:
240+
plt.setp(cg3.collections,
241+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
235242

236243
# Solution to pNLP via Neuromancer
237244
datapoint = {'a': torch.tensor([[a]]), 'p': torch.tensor([[p]]),

examples/parametric_programming/Part_2_pQP.ipynb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -814,23 +814,27 @@
814814
" cg2 = ax[row_id, column_id].contour(xx, yy, c2, [0], colors='mediumblue', alpha=0.7)\n",
815815
" cg3 = ax[row_id, column_id].contour(xx, yy, c3, [0], colors='mediumblue', alpha=0.7)\n",
816816
" cg4 = ax[row_id, column_id].contour(xx, yy, c4, [0], colors='mediumblue', alpha=0.7)\n",
817-
" plt.setp(cg1.collections,\n",
818-
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
819-
" plt.setp(cg2.collections,\n",
820-
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
821-
" plt.setp(cg3.collections,\n",
822-
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
823-
" plt.setp(cg4.collections,\n",
824-
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
817+
"\n",
818+
" if hasattr(cg1, 'collections') and cg1.collections:\n",
819+
" plt.setp(cg1.collections,\n",
820+
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
821+
" plt.setp(cg2.collections,\n",
822+
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
823+
" plt.setp(cg3.collections,\n",
824+
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
825+
" plt.setp(cg4.collections,\n",
826+
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
827+
"\n",
825828
" if problem_type == 'pQCQP': # constraints for QCQP\n",
826829
" c1 = xx + yy - p\n",
827830
" c2 = - xx**2 - yy**2 + p**2\n",
828831
" cg1 = ax[row_id, column_id].contour(xx, yy, c1, [0], colors='mediumblue', alpha=0.7)\n",
829832
" cg2 = ax[row_id, column_id].contour(xx, yy, c2, [0], colors='mediumblue', alpha=0.7)\n",
830-
" plt.setp(cg1.collections,\n",
831-
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
832-
" plt.setp(cg2.collections,\n",
833-
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
833+
" if hasattr(cg1, 'collections') and cg1.collections:\n",
834+
" plt.setp(cg1.collections,\n",
835+
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
836+
" plt.setp(cg2.collections,\n",
837+
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
834838
" fig.colorbar(cp_plot, ax=ax[row_id,column_id])\n",
835839
"\n",
836840
" # Solve CVXPY problem\n",

examples/parametric_programming/Part_2_pQP.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def QCQP_param(p1, p2):
198198
x ** 2 + y ** 2 - p2 ** 2 <= 0])
199199
return prob, x, y
200200

201+
201202
"""
202203
Plots
203204
"""
@@ -206,8 +207,7 @@ def QCQP_param(p1, p2):
206207
x1 = np.arange(-1.0, 10.0, 0.05)
207208
y1 = np.arange(-1.0, 10.0, 0.05)
208209
xx, yy = np.meshgrid(x1, y1)
209-
fig, ax = plt.subplots(3,3)
210-
210+
fig, ax = plt.subplots(3, 3)
211211
row_id = 0
212212
column_id = 0
213213
for i, p in enumerate(params):
@@ -228,25 +228,29 @@ def QCQP_param(p1, p2):
228228
cg2 = ax[row_id, column_id].contour(xx, yy, c2, [0], colors='mediumblue', alpha=0.7)
229229
cg3 = ax[row_id, column_id].contour(xx, yy, c3, [0], colors='mediumblue', alpha=0.7)
230230
cg4 = ax[row_id, column_id].contour(xx, yy, c4, [0], colors='mediumblue', alpha=0.7)
231-
plt.setp(cg1.collections,
232-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
233-
plt.setp(cg2.collections,
234-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
235-
plt.setp(cg3.collections,
236-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
237-
plt.setp(cg4.collections,
238-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
231+
232+
if hasattr(cg1, 'collections') and cg1.collections:
233+
plt.setp(cg1.collections,
234+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
235+
plt.setp(cg2.collections,
236+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
237+
plt.setp(cg3.collections,
238+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
239+
plt.setp(cg4.collections,
240+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
239241

240242
if problem_type == 'pQCQP': # constraints for QCQP
241243
c1 = xx + yy - p
242-
c2 = - xx**2 - yy**2 + p**2
244+
c2 = - xx ** 2 - yy ** 2 + p ** 2
243245
cg1 = ax[row_id, column_id].contour(xx, yy, c1, [0], colors='mediumblue', alpha=0.7)
244246
cg2 = ax[row_id, column_id].contour(xx, yy, c2, [0], colors='mediumblue', alpha=0.7)
245-
plt.setp(cg1.collections,
246-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
247-
plt.setp(cg2.collections,
248-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
249-
fig.colorbar(cp_plot, ax=ax[row_id,column_id])
247+
if hasattr(cg1, 'collections') and cg1.collections:
248+
plt.setp(cg1.collections,
249+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
250+
plt.setp(cg2.collections,
251+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
252+
fig.colorbar(cp_plot, ax=ax[row_id, column_id])
253+
250254

251255
# Solve CVXPY problem
252256
if problem_type == 'pQP':

examples/parametric_programming/Part_3_pNLP.ipynb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,15 +1020,21 @@
10201020
"c1 = xx - yy\n",
10211021
"c2 = xx ** 2 + yy ** 2 - (p / 2) ** 2\n",
10221022
"c3 = -(xx ** 2 + yy ** 2) + p ** 2\n",
1023+
"\n",
10231024
"cg1 = ax.contour(xx, yy, c1, [0], colors='mediumblue', alpha=0.7)\n",
1024-
"plt.setp(cg1.collections,\n",
1025-
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
1025+
"if hasattr(cg1, 'collections') and cg1.collections:\n",
1026+
" plt.setp(cg1.collections,\n",
1027+
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
1028+
"\n",
10261029
"cg2 = ax.contour(xx, yy, c2, [0], colors='mediumblue', alpha=0.7)\n",
1027-
"plt.setp(cg2.collections,\n",
1028-
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
1030+
"if hasattr(cg2, 'collections') and cg2.collections:\n",
1031+
" plt.setp(cg2.collections,\n",
1032+
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
1033+
"\n",
10291034
"cg3 = ax.contour(xx, yy, c3, [0], colors='mediumblue', alpha=0.7)\n",
1030-
"plt.setp(cg3.collections,\n",
1031-
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
1035+
"if hasattr(cg3, 'collections') and cg3.collections:\n",
1036+
" plt.setp(cg3.collections,\n",
1037+
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
10321038
"\n",
10331039
"# plot optimal solutions CasADi vs Neuromancer\n",
10341040
"ax.plot(sol.value(x), sol.value(y), 'g*', markersize=10, label='CasADi')\n",

examples/parametric_programming/Part_3_pNLP.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,19 @@ def arg_pNLP_problem(prefix=''):
284284
c1 = xx - yy
285285
c2 = xx ** 2 + yy ** 2 - (p / 2) ** 2
286286
c3 = -(xx ** 2 + yy ** 2) + p ** 2
287+
287288
cg1 = ax.contour(xx, yy, c1, [0], colors='mediumblue', alpha=0.7)
288-
plt.setp(cg1.collections,
289-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
289+
if hasattr(cg1, 'collections') and cg1.collections:
290+
plt.setp(cg1.collections,
291+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
290292
cg2 = ax.contour(xx, yy, c2, [0], colors='mediumblue', alpha=0.7)
291-
plt.setp(cg2.collections,
292-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
293+
if hasattr(cg2, 'collections') and cg2.collections:
294+
plt.setp(cg2.collections,
295+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
293296
cg3 = ax.contour(xx, yy, c3, [0], colors='mediumblue', alpha=0.7)
294-
plt.setp(cg3.collections,
295-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
297+
if hasattr(cg3, 'collections') and cg3.collections:
298+
plt.setp(cg3.collections,
299+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
296300

297301
# plot optimal solutions CasADi vs Neuromancer
298302
ax.plot(sol.value(x), sol.value(y), 'g*', markersize=10, label='CasADi')

examples/parametric_programming/Part_4_projectedGradient.ipynb

Lines changed: 305 additions & 220 deletions
Large diffs are not rendered by default.

examples/parametric_programming/Part_4_projectedGradient.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,19 @@ def NLP_param(a, p, opti_silent=False):
245245
alpha=0.6)
246246
fig.colorbar(cp)
247247
ax.set_title('Rosenbrock problem')
248+
248249
cg1 = ax.contour(xx, yy, c1, [0], colors='mediumblue', alpha=0.7)
249-
plt.setp(cg1.collections,
250-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
250+
if hasattr(cg1, 'collections') and cg1.collections:
251+
plt.setp(cg1.collections,
252+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
251253
cg2 = ax.contour(xx, yy, c2, [0], colors='mediumblue', alpha=0.7)
252-
plt.setp(cg2.collections,
253-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
254+
if hasattr(cg2, 'collections') and cg2.collections:
255+
plt.setp(cg2.collections,
256+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
254257
cg3 = ax.contour(xx, yy, c3, [0], colors='mediumblue', alpha=0.7)
255-
plt.setp(cg3.collections,
256-
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
258+
if hasattr(cg3, 'collections') and cg3.collections:
259+
plt.setp(cg3.collections,
260+
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
257261

258262
# Solution to pNLP via Neuromancer
259263
datapoint = {'a': torch.tensor([[a]]), 'p': torch.tensor([[p]]),

examples/parametric_programming/Part_5_cvxpy_layers.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,11 @@
570570
" for k in range(n_con):\n",
571571
" cg1 = ax.contour(xx, yy, -C_samples[k], [0],\n",
572572
" colors='mediumblue', alpha=0.7)\n",
573-
" plt.setp(cg1.collections,\n",
573+
" if hasattr(cg1, 'collections') and cg1.collections:\n",
574+
" plt.setp(cg1.collections,\n",
574575
" path_effects=[patheffects.withTickedStroke()], alpha=0.7)\n",
575576
"\n",
577+
"\n",
576578
" # Solution to pNLP via Neuromancer\n",
577579
" datapoint = {'p': p, 'b_param': b_param,\n",
578580
" 'name': 'test'}\n",

examples/parametric_programming/Part_5_cvxpylayers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ def plot_pNLP(p, b_param, savedir=None):
220220
for k in range(n_con):
221221
cg1 = ax.contour(xx, yy, -C_samples[k], [0],
222222
colors='mediumblue', alpha=0.7)
223-
plt.setp(cg1.collections,
223+
if hasattr(cg1, 'collections') and cg1.collections:
224+
plt.setp(cg1.collections,
224225
path_effects=[patheffects.withTickedStroke()], alpha=0.7)
225226

226227
# Solution to pNLP via Neuromancer

0 commit comments

Comments
 (0)