Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phschiele committed Mar 8, 2024
1 parent f79b0db commit 4a0272a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ncopt/funs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class f_rosenbrock:
Nonsmooth Rosenbrock function (see 5.1 in Curtis, Overton
"SQP FOR NONSMOOTH CONSTRAINED OPTIMIZATION")
x -> w|x_1^2 x_2| + (1 x_1)^2
x -> w|x_1^2 - x_2| + (1 - x_1)^2
"""

def __init__(self, w=8):
Expand Down
1 change: 1 addition & 0 deletions src/ncopt/sqpgs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ def solve(self):
)

self.status = qp["status"]
self.objective_val = qp["primal objective"]
self.cvx_sol_x = np.array(qp["x"]).squeeze()

self.d = self.cvx_sol_x[: self.dim]
Expand Down
9 changes: 2 additions & 7 deletions tests/test_rosenbrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ def test_rosenbrock_from_zero():
x_k = problem.solve()
np.testing.assert_array_almost_equal(x_k, xstar, decimal=4)

return


def test_rosenbrock_from_rand():
gI = [g]
gE = []
xstar = np.array([1 / np.sqrt(2), 0.5])
x0 = np.random.rand(2)
rng = np.random.default_rng(0)
x0 = rng.random(2)
problem = SQPGS(f, gI, gE, x0=x0, tol=1e-8, max_iter=200, verbose=False)
x_k = problem.solve()
np.testing.assert_array_almost_equal(x_k, xstar, decimal=4)

return


def test_rosenbrock_with_eq():
g1 = g_linear(A=np.eye(2), b=np.ones(2))
Expand All @@ -42,5 +39,3 @@ def test_rosenbrock_with_eq():
problem = SQPGS(f, gI, gE, tol=1e-8, max_iter=200, verbose=False)
x_k = problem.solve()
np.testing.assert_array_almost_equal(x_k, xstar, decimal=4)

return
64 changes: 64 additions & 0 deletions tests/test_subproblem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import numpy as np
import pytest

from ncopt.sqpgs.main import SubproblemSQPGS


@pytest.fixture
def subproblem_ineq() -> SubproblemSQPGS:
dim = 2
p0 = 2
pI = np.array([3])
pE = np.array([], dtype=int)
assert_tol = 1e-5
subproblem = SubproblemSQPGS(dim, p0, pI, pE, assert_tol)
D_f = np.array([[-2.0, 1.0], [-2.04236205, -1.0], [-1.92172864, -1.0]])
D_gI = [np.array([[0.0, 2.0], [0.0, 2.0], [1.41421356, 0.0], [1.41421356, 0.0]])]
subproblem.update(
H=np.eye(2, dtype=float),
rho=0.1,
D_f=D_f,
D_gI=D_gI,
D_gE=[],
f_k=1.0,
gI_k=np.array([-1.0]),
gE_k=np.array([], dtype=float),
)
return subproblem


def test_subproblem_ineq(subproblem_ineq: SubproblemSQPGS):
subproblem_ineq.solve()
assert subproblem_ineq.status == "optimal"
assert np.isclose(subproblem_ineq.objective_val, 0.080804459)


@pytest.fixture
def subproblem_eq() -> SubproblemSQPGS:
dim = 2
p0 = 2
pI = np.array([], dtype=int)
pE = np.array([4, 4])
assert_tol = 1e-5
subproblem = SubproblemSQPGS(dim, p0, pI, pE, assert_tol)
D_gE = [
np.array([[1.0, 0.0], [1.0, 0.0], [1.0, 0.0], [1.0, 0.0], [1.0, 0.0]]),
np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]),
]
subproblem.update(
H=np.eye(2, dtype=float),
rho=0.1,
D_f=np.array([-2.0, 1.0]),
D_gI=[],
D_gE=D_gE,
f_k=1.0,
gI_k=np.array([], dtype=float),
gE_k=np.array([-1.0, -1.0]),
)
return subproblem


def test_subproblem_eq(subproblem_eq: SubproblemSQPGS):
subproblem_eq.solve()
assert subproblem_eq.status == "optimal"
assert np.isclose(subproblem_eq.objective_val, 0.99500000)

0 comments on commit 4a0272a

Please sign in to comment.