5 from pprint
import pprint
10 assert str(solver) ==
"PANOCSolver<LBFGS>"
14 assert str(solver) ==
"PANOCSolver<LBFGS>"
25 assert str(
Dir()) ==
"Dir"
27 assert str(solver) ==
"PANOCSolver<Dir>"
32 assert l.cbfgs.α == 100
36 hess_prod =
lambda L, x, v: cs.gradient(cs.jtimes(L, x, v,
False), x)
44 Q = np.array([[1.5, 0.5], [0.5, 1.5]])
47 L = f_ + cs.dot(λ, g_)
if m > 0
else f_
49 f = cs.Function(
"f", [x], [f_])
50 grad_f = cs.Function(
"grad_f", [x], [cs.gradient(f_, x)])
51 g = cs.Function(
"g", [x], [g_])
52 grad_g_prod = cs.Function(
"grad_g_prod", [x, λ], [cs.jtimes(g_, x, λ,
True)])
54 Hess_L = cs.Function(
"Hess_L", [x, λ], [cs.hessian(L, x)[0]])
55 Hess_L_prod = cs.Function(
"Hess_L_prod", [x, λ, v], [
hess_prod(L, x, v)])
61 p.grad_g_prod = grad_g_prod
64 p.hess_L_prod = Hess_L_prod
65 p.D.lowerbound = [-np.inf, 0.5]
66 p.D.upperbound = [+np.inf, +np.inf]
70 Σ = 1e3 * np.ones((m,))
76 x, y, err_z, stats =
solver(p, Σ, ε, x0, y0)
86 almparams =
pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
98 almparams =
pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
117 def __call__(self, problem, Σ, ε, always_overwrite_results, x, y):
119 x, y, err_z, stats = self.
solver(problem, Σ, ε, x, y)
121 def accumulate(acc: dict, s: dict):
122 for k, v
in s.items():
123 if not k
in [
"status",
"ε",
"accumulator"]:
124 acc[k] = acc[k] + v
if k
in acc
else v
126 stats[
"accumulator"] = {
"accumulate": accumulate}
127 return x, y, err_z, stats
131 almparams =
pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
141 x0 = np.zeros((666,))
143 except ValueError
as e:
144 assert e.args[0] ==
"Length of x does not match problem size problem.n"
151 from os.path
import join
152 from tempfile
import TemporaryDirectory
159 x = cs.SX.sym(
"x", n)
160 λ = cs.SX.sym(
"λ", m)
161 v = cs.SX.sym(
"v", n)
163 Q = np.array([[1.5, 0.5], [0.5, 1.5]])
164 f_ = 0.5 * x.T @ Q @ x
166 f = cs.Function(
"f", [x], [f_])
167 g = cs.Function(
"g", [x], [g_])
170 cgen, n, m, num_p = pa.generate_casadi_problem(f, g, name=name)
172 with TemporaryDirectory(prefix=
"")
as tmpdir:
173 cfile = cgen.generate(join(tmpdir,
""))
174 sofile = join(tmpdir, f
"{name}.so")
175 os.system(f
"cc -fPIC -shared -O3 {cfile} -o {sofile}")
177 p = pa.load_casadi_problem(sofile, n, m)
178 p.D.lowerbound = [-np.inf, 0.5]
179 p.D.upperbound = [+np.inf, +np.inf]
184 almparams =
pa.ALMParams(max_iter=20, print_interval=1, preconditioning=
False)
202 f =
lambda x: float(np.cosh(x) - x * x + x)
203 grad_f =
lambda x: np.sinh(x) - 2 * x + 1
214 f =
lambda x: float(np.cosh(x) - x * x + x)
215 grad_f =
lambda x: np.sinh(x) - 2 * x + 1