Skip to content

Commit

Permalink
First unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lindonroberts committed Sep 5, 2024
1 parent e4e4049 commit ea94ca9
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 24 deletions.
4 changes: 4 additions & 0 deletions pybobyqa/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,10 @@ def solve(objfun, x0, args=(), bounds=None, projections=None, npt=None, rhobeg=N
if exit_info is None and projections is not None and type(projections) != list:
exit_info = ExitInformation(EXIT_INPUT_ERROR, "projections must be a list of functions")

if projections is not None and len(projections) == 0:
# empty list given
projections = None

if maxfun <= npt:
warnings.warn("maxfun <= npt: Are you sure your budget is large enough?", RuntimeWarning)

Expand Down
44 changes: 22 additions & 22 deletions pybobyqa/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def runTest(self):
x0 = np.array([-1.2, 1.0])
xl = -1e20 * np.ones((n,))
xu = 1e20 * np.ones((n,))
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
self.assertEqual(model.npt(), npt, 'Wrong npt after initialisation')
self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x0), 'Wrong xopt after initialisation')
self.assertTrue(array_compare(model.fopt(), rosenbrock(x0)), 'Wrong fopt after initialisation')
Expand Down Expand Up @@ -103,7 +103,7 @@ def runTest(self):
x0 = np.array([-1.2, 1.0])
xl = -1e20 * np.ones((n,))
xu = 1e20 * np.ones((n,))
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
# Now add better point
x1 = np.array([1.0, 0.9])
f1 = rosenbrock(x1)
Expand Down Expand Up @@ -131,7 +131,7 @@ def runTest(self):
x0 = np.array([-1.2, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
self.assertTrue(array_compare(model.sl, xl - x0), 'Wrong sl after initialisation')
self.assertTrue(array_compare(model.su, xu - x0), 'Wrong su after initialisation')
x1 = np.array([1.0, 0.9])
Expand Down Expand Up @@ -204,7 +204,7 @@ def runTest(self):
x0 = np.array([-1.2, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
x1 = np.array([1.0, 0.9])
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
x2 = np.array([1.0, 1.0])
Expand All @@ -224,13 +224,13 @@ def runTest(self):
x0 = np.array([-1.2, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
x1 = np.array([1.0, 0.9])
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
x2 = np.array([2.0, 0.9])
model.change_point(2, x2 - model.xbase, rosenbrock(x2))
self.assertAlmostEqual(model.min_objective_value(), -1e20, msg='Wrong min obj value')
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1, abs_tol=1.0)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1, abs_tol=1.0)
self.assertAlmostEqual(model.min_objective_value(), 1.0, msg='Wrong min obj value 3')


Expand All @@ -241,7 +241,7 @@ def runTest(self):
x0 = np.array([-1.2, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1, precondition=False)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1, precondition=False)
x1 = np.array([1.0, 0.9])
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
x2 = np.array([2.0, 0.9])
Expand Down Expand Up @@ -280,7 +280,7 @@ def runTest(self):
x0 = np.array([1.0, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
model = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
x1 = x0 + np.array([1.0, 0.0])
model.change_point(1, x1 - model.xbase, objfun(x1))
x2 = x0 + np.array([0.1, 0.9])
Expand Down Expand Up @@ -323,7 +323,7 @@ def runTest(self):
self.assertTrue(np.allclose(hess, model.model_hess), 'Bad Hessian')

# Build a new model
model2 = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
model2 = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
model2.change_point(1, x1 - model.xbase, objfun(x1))
model2.change_point(2, x2 - model.xbase, objfun(x2))
model2.change_point(3, x3 - model.xbase, objfun(x3))
Expand All @@ -350,7 +350,7 @@ def runTest(self):
# print(model2.model_hess)

# Build a new model
model3 = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
model3 = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
model3.change_point(1, x1 - model.xbase, objfun(x1))
model3.change_point(2, x2 - model.xbase, objfun(x2))
model3.change_point(3, x3 - model.xbase, objfun(x3))
Expand All @@ -376,7 +376,7 @@ def runTest(self):
x0 = np.array([1.0, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
model = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
x1 = x0 + np.array([1.0, 0.0])
model.change_point(1, x1 - model.xbase, objfun(x1))
x2 = x0 + np.array([0.1, 0.9])
Expand Down Expand Up @@ -420,7 +420,7 @@ def runTest(self):
self.assertTrue(np.allclose(hess, model.model_hess), 'Bad Hessian')

# Build a new model
model2 = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
model2 = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
model2.change_point(1, x1 - model.xbase, objfun(x1))
model2.change_point(2, x2 - model.xbase, objfun(x2))
model2.change_point(3, x3 - model.xbase, objfun(x3))
Expand Down Expand Up @@ -448,7 +448,7 @@ def runTest(self):
# print(model2.model_hess)

# Build a new model
model3 = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
model3 = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
model3.change_point(1, x1 - model.xbase, objfun(x1))
model3.change_point(2, x2 - model.xbase, objfun(x2))
model3.change_point(3, x3 - model.xbase, objfun(x3))
Expand All @@ -475,7 +475,7 @@ def runTest(self):
x0 = np.array([1.0, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, objfun(x0), xl, xu, 1)
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
x1 = x0 + np.array([1.0, 0.0])
model.change_point(1, x1 - model.xbase, objfun(x1))
x2 = x0 + np.array([0.1, 0.9])
Expand Down Expand Up @@ -511,7 +511,7 @@ def runTest(self):
x0 = np.array([-1.2, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
x1 = np.array([1.0, 0.9])
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
x2 = np.array([2.0, 0.9])
Expand All @@ -534,7 +534,7 @@ def runTest(self):
x0 = np.array([1.0, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, objfun(x0), xl, xu, 1)
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
x1 = x0 + np.array([1.0, 0.0])
model.change_point(1, x1 - model.xbase, objfun(x1))
x2 = x0 + np.array([0.1, 0.9])
Expand All @@ -559,7 +559,7 @@ def runTest(self):
x0 = np.array([1.0, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, objfun(x0), xl, xu, 1)
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
x1 = x0 + np.array([1.0, 0.0])
model.change_point(1, x1 - model.xbase, objfun(x1))
x2 = x0 + np.array([0.1, 0.9])
Expand All @@ -586,7 +586,7 @@ def runTest(self):
x0 = np.array([1.0, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, objfun(x0), xl, xu, 1)
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
x1 = x0 + np.array([1.0, 0.0])
model.change_point(1, x1 - model.xbase, objfun(x1))
x2 = x0 + np.array([0.1, 0.9])
Expand Down Expand Up @@ -616,7 +616,7 @@ def runTest(self):
delta = 0.5
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
model.add_new_sample(0, rosenbrock(x0))
x1 = x0 + delta * np.array([1.0, 0.0])
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
Expand All @@ -636,7 +636,7 @@ def runTest(self):
x0 = np.array([0.5, 0.5])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, objfun(x0), xl, xu, 1)
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
x1 = np.array([0.05, 0.1])
model.change_point(1, x1 - model.xbase, objfun(x1))
x2 = np.array([0.1, 0.05])
Expand All @@ -660,7 +660,7 @@ def runTest(self):
x0 = np.array([0.5, 0.5])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, objfun(x0), xl, xu, 1)
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
x1 = np.array([0.524, 0.0006])
model.change_point(1, x1 - model.xbase, objfun(x1))
x2 = np.array([0.032, 0.323])
Expand All @@ -681,7 +681,7 @@ def runTest(self):
x0 = np.array([-1.2, 1.0])
xl = -1e2 * np.ones((n,))
xu = 1e2 * np.ones((n,))
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
x1 = np.array([1.0, 0.9])
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
x2 = np.array([2.0, 0.9])
Expand Down
Loading

0 comments on commit ea94ca9

Please sign in to comment.