-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
32 lines (25 loc) · 772 Bytes
/
example.py
File metadata and controls
32 lines (25 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy as np
import scipy.sparse
import polysolve
class Quadratic(polysolve.Problem):
def value(self, x):
y = x - np.array([-2.0, 3.0, 1.0])
return float(y @ y)
def gradient(self, x):
return 2.0 * (x - np.array([-2.0, 3.0, 1.0]))
def hessian(self, x):
return 2.0 * scipy.sparse.eye(x.size, format="csc")
def post_step(self, iter_num, solver_info, x, grad):
print(f"Iteration {iter_num}: x = {x}, grad = {grad}")
x, result = polysolve.minimize(
Quadratic(),
np.zeros(3),
{
"solver": "Newton",
"line_search": {"method": "Backtracking"},
"max_iterations": 100,
},
{"solver": "Eigen::SimplicialLDLT"},
)
print("Optimal point:", x)
print("Optimal value:", result)