7from pprint
import pprint
12assert str(solver) ==
"PANOCSolver<LBFGS>"
16assert str(solver) ==
"PANOCSolver<LBFGS>"
27assert str(
Dir()) ==
"Dir"
29assert str(solver) ==
"PANOCSolver<Dir>"
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,))
78x, y, err_z, stats =
solver(p, Σ, ε, x0, y0)
88almparams =
pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
100almparams =
pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
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)
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]
172almparams =
pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
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]
204almparams =
pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
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
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
272f =
lambda x: float(np.cosh(x) - x * x + x)
273grad_f =
lambda x: np.sinh(x) - 2 * x + 1
284except RuntimeError
as e:
Augmented Lagrangian Method solver.
Second order PANOC solver for ALM.
PANOCStats panoc(ObjFunT &ψ, ObjGradFunT &grad_ψ, const Box &C, rvec x, real_t ε, const PANOCParams ¶ms, PANOCDirection< DirectionProviderT > direction, vec_allocator &alloc)
Parameters for the Augmented Lagrangian solver.
Parameters for the LBFGS and SpecializedLBFGS classes.
Tuning parameters for the PANOC algorithm.
Tuning parameters for the second order PANOC algorithm.
Problem description for minimization problems.