9x1, x2 = cs.SX.sym(
"x1"), cs.SX.sym(
"x2")
14f_expr = (1 - x1) ** 2 + p * (x2 - x1 ** 2) ** 2
16 (x1 - 0.5) ** 3 - x2 + 1,
23f = cs.Function(
"f", [x, p], [f_expr])
24g = cs.Function(
"g", [x, p], [g_expr])
30prob = pa.generate_and_compile_casadi_problem(f, g)
34prob.C.lowerbound = [-0.25, -0.5]
35prob.C.upperbound = [1.5, 2.5]
36prob.D.lowerbound = [-np.inf, -np.inf]
37prob.D.upperbound = [0, 0]
43innersolver = pa.StructuredPANOCLBFGSSolver(
44 pa.StructuredPANOCLBFGSParams(
46 stop_crit=pa.PANOCStopCrit.ApproxKKT,
56def cb(it): iterates.append(np.copy(it.x))
59innersolver.set_progress_callback(cb)
76x0 = np.array([0.1, 1.8])
77y0 = np.zeros((prob.m,))
80x_sol, y_sol, stats =
solver(prob, x0, y0)
84print(f
"Solution: {x_sol}")
85print(f
"Multipliers: {y_sol}")
86print(f
"Cost: {prob.f(x_sol)}")
87from pprint
import pprint
92import matplotlib.pyplot
as plt
93from matplotlib
import patheffects
95cost_function_v = np.vectorize(prob.f, signature=
'(n)->()')
96constraint_g_v = np.vectorize(prob.g, signature=
'(n)->(m)')
98x = np.linspace(-1.5, 1.5, 256)
99y = np.linspace(-0.5, 2.5, 256)
100X, Y = np.meshgrid(x, y)
101XY = np.vstack([[X], [Y]]).T
103plt.figure(figsize=(10, 6))
106plt.contourf(X, Y, Zf, 32)
112fx = [patheffects.withTickedStroke(spacing=7, linewidth=0.8)]
113cgc = plt.contour(X, Y, Zgc, [0], colors=
'black', linewidths=0.8, linestyles=
'-')
114plt.setp(cgc.collections, path_effects=fx)
115cgl = plt.contour(X, Y, Zgl, [0], colors=
'black', linewidths=0.8, linestyles=
'-')
116plt.setp(cgl.collections, path_effects=fx)
117xl = plt.contour(X, Y, -X, [-prob.C.lowerbound[0]], colors=
'black', linewidths=0.8, linestyles=
'-')
118plt.setp(xl.collections, path_effects=fx)
120plt.title(
"PANOC+ALM Rosenbrock example")
125xy = np.array(iterates)
126plt.plot(xy[:,0], xy[:,1],
'r:.', markersize=4, linewidth=1)
127plt.plot(x_sol[0], x_sol[1],
'ro', markersize=10, fillstyle=
'none')