PANOC-ALM  quadratic-penalty
Nonconvex constrained optimization
rosenbrock.py
Go to the documentation of this file.
1 
4 
5 # %% Build the problem for PANOC+ALM (CasADi code, independent of panocpy)
6 import casadi as cs
7 
8 name = "minimal_example"
9 
10 # Make decision variables
11 x = cs.SX.sym("x")
12 y = cs.SX.sym("y")
13 
14 # Make a parameter symbol
15 p = cs.SX.sym("p")
16 
17 cost = (1 - x) ** 2 + p * (y - x ** 2) ** 2 # Rosenbrock function (parametrized by p)
18 
19 constraint_g_cubic = (x - 1) ** 3 - y + 1
20 constraint_g_linear = x + y - 2
21 
22 # Collect decision variables into one vector
23 X = cs.vertcat(x, y)
24 
25 cost_function = cs.Function("f", [X, p], [cost])
26 g = cs.vertcat(constraint_g_cubic, constraint_g_linear)
27 g_function = cs.Function("g", [X, p], [g])
28 
29 # %% Generate and compile C-code using `panocpy`
30 import panocpy as pa
31 import numpy as np
32 
33 cgen, n, m, num_p = pa.generate_casadi_problem(cost_function, g_function, name=name)
34 # Code generator, dimension of decision variables, number of constraints (dual dimension), parameter dimension
35 
36 # Compile and load the problem, and set the bounds
37 prob = pa.compile_and_load_problem(cgen, n, m, num_p, name)
38 
39 prob.C.lowerbound = np.array([-1.5, -0.5]) # -1.5 <= x <= 1.5
40 prob.C.upperbound = np.array([1.5, 2.5]) # -0.5 <= y <= 2.5
41 prob.D.lowerbound = np.array([-np.inf, -np.inf]) # g_c <= 0
42 prob.D.upperbound = np.array([0, 0]) # g_l <= 0
43 
44 # %% Construct a PANOC instance to serve as the inner solver
45 
46 innersolver = pa.StructuredPANOCLBFGSSolver() # (with default parameters)
47 
48 # %% Make an ALM solver with default parameters, using the PANOC solver
49 
50 solver = pa.ALMSolver(pa.ALMParams(), innersolver)
51 
52 # Set parameter to some value
53 prob.param = np.array([100.0])
54 
55 # Set initial guesses at arbitrary values
56 x_sol = np.array([1.0, 2.0])
57 y_sol = np.zeros((m,))
58 
59 # Solve the problem
60 x_sol, y_sol, stats = solver(prob, x_sol, y_sol)
61 
62 
63 print(stats["status"])
64 
65 print(f"Obtained solution: {x_sol}")
66 print(f"Analytical solution: {(1., 1.)}")
67 
68 # %% Plot the results
69 
70 import matplotlib.pyplot as plt
71 
72 x = np.linspace(-1.5, 1.5, 200)
73 y = np.linspace(-0.5, 2.5, 200)
74 X, Y = np.meshgrid(x, y)
75 Z = (1 - X) ** 2 + 100 * (Y - X ** 2) ** 2
76 
77 plt.figure()
78 
79 plt.contourf(X, Y, Z)
80 plt.colorbar()
81 plt.xlabel("x")
82 plt.ylabel("y")
83 plt.scatter(1, 1, color="tab:red", label="Analytic")
84 plt.scatter(x_sol[0], x_sol[1], marker="x", color="tab:green", label="PANOC-ALM")
85 plt.legend()
86 plt.show()
87 
88 # %%
pa::StructuredPANOCLBFGSSolver
Second order PANOC solver for ALM.
Definition: decl/structured-panoc-lbfgs.hpp:80
pa::ALMSolver
Augmented Lagrangian Method solver.
Definition: decl/alm.hpp:82
rosenbrock.solver
solver
Definition: rosenbrock.py:50
pa::ALMParams
Parameters for the Augmented Lagrangian solver.
Definition: decl/alm.hpp:13