7from pprint
import pprint
10solver = pa.PANOCSolver(pa.PANOCParams(), pa.LBFGSDirection(pa.LBFGSParams()))
12assert str(solver) ==
"PANOCSolver<LBFGS>"
14solver = pa.PANOCSolver(pa.PANOCParams(), pa.LBFGSParams())
16assert str(solver) ==
"PANOCSolver<LBFGS>"
19class Dir(pa.PANOCDirection):
27assert str(
Dir()) ==
"Dir"
28solver = pa.PANOCSolver(pa.PANOCParams(),
Dir())
29assert str(solver) ==
"PANOCSolver<Dir>"
31l = pa.LBFGSParams(cbfgs=pa.LBFGSParamsCBFGS(α=5))
34assert l.cbfgs.α == 100
38hess_prod =
lambda L, x, v: cs.gradient(cs.jtimes(L, x, v,
False), x)
46Q = np.array([[1.5, 0.5], [0.5, 1.5]])
49L = f_ + cs.dot(λ, g_)
if m > 0
else f_
51f = cs.Function(
"f", [x], [f_])
52grad_f = cs.Function(
"grad_f", [x], [cs.gradient(f_, x)])
53g = cs.Function(
"g", [x], [g_])
54grad_g_prod = cs.Function(
"grad_g_prod", [x, λ], [cs.jtimes(g_, x, λ,
True)])
56Hess_L = cs.Function(
"Hess_L", [x, λ], [cs.hessian(L, x)[0]])
57Hess_L_prod = cs.Function(
"Hess_L_prod", [x, λ, v], [
hess_prod(L, x, v)])
63p.grad_g_prod = grad_g_prod
66p.hess_L_prod = Hess_L_prod
67p.D.lowerbound = [-np.inf, 0.5]
68p.D.upperbound = [+np.inf, +np.inf]
72Σ = 1e3 * np.ones((m,))
74solver = pa.PANOCSolver(
75 pa.PANOCParams(max_iter=200, print_interval=1),
76 pa.LBFGSParams(memory=5),
78x, y, err_z, stats =
solver(p, Σ, ε, x0, y0)
84solver = pa.PANOCSolver(
85 pa.PANOCParams(max_iter=200, print_interval=1),
86 pa.LBFGSParams(memory=5),
88almparams = pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
89almsolver = pa.ALMSolver(almparams, solver)
96solver = pa.StructuredPANOCLBFGSSolver(
97 pa.StructuredPANOCLBFGSParams(max_iter=200, print_interval=1),
98 pa.LBFGSParams(memory=5),
100almparams = pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
101almsolver = pa.ALMSolver(almparams, solver)
109 pa.PANOCParams(max_iter=200, print_interval=1),
110 pa.LBFGSParams(memory=5),
119 def __call__(self, problem, Σ, ε, always_overwrite_results, x, y):
121 x, y, err_z, stats = self.
solversolver(problem, Σ, ε, x, y)
123 def accumulate(acc: dict, s: dict):
124 for k, v
in s.items():
125 if not k
in [
"status",
"ε",
"accumulator"]:
126 acc[k] = acc[k] + v
if k
in acc
else v
128 stats[
"accumulator"] = {
"accumulate": accumulate}
129 return x, y, err_z, stats
133almparams = pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
134almsolver = pa.ALMSolver(almparams, solver)
143 x0 = np.zeros((666,))
145except ValueError
as e:
146 assert e.args[0] ==
"Length of x does not match problem size problem.n"
158Q = np.array([[1.5, 0.5], [0.5, 1.5]])
159f_ = 0.5 * x.T @ Q @ x
161f = cs.Function(
"f", [x], [f_])
162g = cs.Function(
"g", [x], [g_])
165p = pa.generate_and_compile_casadi_problem(f, g, name=name)
166p.D.lowerbound = [-np.inf, 0.5]
167p.D.upperbound = [+np.inf, +np.inf]
168solver = pa.StructuredPANOCLBFGSSolver(
169 pa.StructuredPANOCLBFGSParams(max_iter=200, print_interval=1),
170 pa.LBFGSParams(memory=5),
172almparams = pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
173almsolver = pa.ALMSolver(almparams, solver)
187p0 = np.array([1.5, 0.5, 1.5])
189Q = cs.vertcat(cs.horzcat(p[0], p[1]), cs.horzcat(p[1], p[2]))
190f_ = 0.5 * x.T @ Q @ x
192f = cs.Function(
"f", [x, p], [f_])
193g = cs.Function(
"g", [x, p], [g_])
196prob = pa.generate_and_compile_casadi_problem(f, g, name=name)
197prob.D.lowerbound = [-np.inf, 0.5]
198prob.D.upperbound = [+np.inf, +np.inf]
200solver = pa.StructuredPANOCLBFGSSolver(
201 pa.StructuredPANOCLBFGSParams(max_iter=200, print_interval=1),
202 pa.LBFGSParams(memory=5),
204almparams = pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
205almsolver = pa.ALMSolver(almparams, solver)
214prob.param = [1, 2, 3]
215assert np.all(prob.param == [1, 2, 3])
216prob1 = pa.ProblemWithParamWithCounters(prob)
219assert np.all(prob.param == [1, 2, 3])
220assert np.all(prob1.param == [1, 2, 3])
221prob1.param = [42, 43, 44]
224assert np.all(prob.param == [1, 2, 3])
225assert np.all(prob1.param == [42, 43, 44])
227print(prob1.f([1, 2]))
228assert prob.f([1, 2]) == 21 / 2
229assert prob1.f([1, 2]) == 390 / 2
230assert prob1.evaluations.f == 2
232prob2 = pa.ProblemWithCounters(prob)
234print(prob2.f([1, 2]))
235assert prob.f([1, 2]) == 21 / 2
236assert prob2.f([1, 2]) == 21 / 2
237prob.param = [2, 1, 3]
239print(prob2.f([1, 2]))
240assert prob.f([1, 2]) == 18 / 2
241assert prob2.f([1, 2]) == 18 / 2
242assert prob1.evaluations.f == 2
243assert prob2.evaluations.f == 4
253print(prob1.evaluations.f)
254print(prob1.evaluations.grad_f)
255print(prob1.evaluations.g)
256print(prob1.evaluations.grad_g_prod)
260f =
lambda x: float(np.cosh(x) - x * x + x)
261grad_f =
lambda x: np.sinh(x) - 2 * x + 1
262C = pa.Box([10], [-2.5])
265 f, grad_f, C, x0, 1e-12, pa.PANOCParams(print_interval=1), pa.LBFGSParams()
272f =
lambda x: float(np.cosh(x) - x * x + x)
273grad_f =
lambda x: np.sinh(x) - 2 * x + 1
274C = pa.Box([10], [-2.5])
275x, stats = pa.panoc(f, grad_f, C, params=pa.PANOCParams(print_interval=1))
282 pa.PANOCParams(max_iter=1e3)
284except RuntimeError
as e: