Skip to content

Commit ea94ca9

Browse files
committed
First unit tests
1 parent e4e4049 commit ea94ca9

File tree

4 files changed

+234
-24
lines changed

4 files changed

+234
-24
lines changed

pybobyqa/solver.py

+4
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,10 @@ def solve(objfun, x0, args=(), bounds=None, projections=None, npt=None, rhobeg=N
768768
if exit_info is None and projections is not None and type(projections) != list:
769769
exit_info = ExitInformation(EXIT_INPUT_ERROR, "projections must be a list of functions")
770770

771+
if projections is not None and len(projections) == 0:
772+
# empty list given
773+
projections = None
774+
771775
if maxfun <= npt:
772776
warnings.warn("maxfun <= npt: Are you sure your budget is large enough?", RuntimeWarning)
773777

pybobyqa/tests/test_model.py

+22-22
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def runTest(self):
5555
x0 = np.array([-1.2, 1.0])
5656
xl = -1e20 * np.ones((n,))
5757
xu = 1e20 * np.ones((n,))
58-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
58+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
5959
self.assertEqual(model.npt(), npt, 'Wrong npt after initialisation')
6060
self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x0), 'Wrong xopt after initialisation')
6161
self.assertTrue(array_compare(model.fopt(), rosenbrock(x0)), 'Wrong fopt after initialisation')
@@ -103,7 +103,7 @@ def runTest(self):
103103
x0 = np.array([-1.2, 1.0])
104104
xl = -1e20 * np.ones((n,))
105105
xu = 1e20 * np.ones((n,))
106-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
106+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
107107
# Now add better point
108108
x1 = np.array([1.0, 0.9])
109109
f1 = rosenbrock(x1)
@@ -131,7 +131,7 @@ def runTest(self):
131131
x0 = np.array([-1.2, 1.0])
132132
xl = -1e2 * np.ones((n,))
133133
xu = 1e2 * np.ones((n,))
134-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
134+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
135135
self.assertTrue(array_compare(model.sl, xl - x0), 'Wrong sl after initialisation')
136136
self.assertTrue(array_compare(model.su, xu - x0), 'Wrong su after initialisation')
137137
x1 = np.array([1.0, 0.9])
@@ -204,7 +204,7 @@ def runTest(self):
204204
x0 = np.array([-1.2, 1.0])
205205
xl = -1e2 * np.ones((n,))
206206
xu = 1e2 * np.ones((n,))
207-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
207+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
208208
x1 = np.array([1.0, 0.9])
209209
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
210210
x2 = np.array([1.0, 1.0])
@@ -224,13 +224,13 @@ def runTest(self):
224224
x0 = np.array([-1.2, 1.0])
225225
xl = -1e2 * np.ones((n,))
226226
xu = 1e2 * np.ones((n,))
227-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
227+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
228228
x1 = np.array([1.0, 0.9])
229229
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
230230
x2 = np.array([2.0, 0.9])
231231
model.change_point(2, x2 - model.xbase, rosenbrock(x2))
232232
self.assertAlmostEqual(model.min_objective_value(), -1e20, msg='Wrong min obj value')
233-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1, abs_tol=1.0)
233+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1, abs_tol=1.0)
234234
self.assertAlmostEqual(model.min_objective_value(), 1.0, msg='Wrong min obj value 3')
235235

236236

@@ -241,7 +241,7 @@ def runTest(self):
241241
x0 = np.array([-1.2, 1.0])
242242
xl = -1e2 * np.ones((n,))
243243
xu = 1e2 * np.ones((n,))
244-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1, precondition=False)
244+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1, precondition=False)
245245
x1 = np.array([1.0, 0.9])
246246
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
247247
x2 = np.array([2.0, 0.9])
@@ -280,7 +280,7 @@ def runTest(self):
280280
x0 = np.array([1.0, 1.0])
281281
xl = -1e2 * np.ones((n,))
282282
xu = 1e2 * np.ones((n,))
283-
model = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
283+
model = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
284284
x1 = x0 + np.array([1.0, 0.0])
285285
model.change_point(1, x1 - model.xbase, objfun(x1))
286286
x2 = x0 + np.array([0.1, 0.9])
@@ -323,7 +323,7 @@ def runTest(self):
323323
self.assertTrue(np.allclose(hess, model.model_hess), 'Bad Hessian')
324324

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

352352
# Build a new model
353-
model3 = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
353+
model3 = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
354354
model3.change_point(1, x1 - model.xbase, objfun(x1))
355355
model3.change_point(2, x2 - model.xbase, objfun(x2))
356356
model3.change_point(3, x3 - model.xbase, objfun(x3))
@@ -376,7 +376,7 @@ def runTest(self):
376376
x0 = np.array([1.0, 1.0])
377377
xl = -1e2 * np.ones((n,))
378378
xu = 1e2 * np.ones((n,))
379-
model = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
379+
model = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
380380
x1 = x0 + np.array([1.0, 0.0])
381381
model.change_point(1, x1 - model.xbase, objfun(x1))
382382
x2 = x0 + np.array([0.1, 0.9])
@@ -420,7 +420,7 @@ def runTest(self):
420420
self.assertTrue(np.allclose(hess, model.model_hess), 'Bad Hessian')
421421

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

450450
# Build a new model
451-
model3 = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False)
451+
model3 = Model(npt, x0, objfun(x0), xl, xu, [], 1, precondition=False)
452452
model3.change_point(1, x1 - model.xbase, objfun(x1))
453453
model3.change_point(2, x2 - model.xbase, objfun(x2))
454454
model3.change_point(3, x3 - model.xbase, objfun(x3))
@@ -475,7 +475,7 @@ def runTest(self):
475475
x0 = np.array([1.0, 1.0])
476476
xl = -1e2 * np.ones((n,))
477477
xu = 1e2 * np.ones((n,))
478-
model = Model(npt, x0, objfun(x0), xl, xu, 1)
478+
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
479479
x1 = x0 + np.array([1.0, 0.0])
480480
model.change_point(1, x1 - model.xbase, objfun(x1))
481481
x2 = x0 + np.array([0.1, 0.9])
@@ -511,7 +511,7 @@ def runTest(self):
511511
x0 = np.array([-1.2, 1.0])
512512
xl = -1e2 * np.ones((n,))
513513
xu = 1e2 * np.ones((n,))
514-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
514+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
515515
x1 = np.array([1.0, 0.9])
516516
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
517517
x2 = np.array([2.0, 0.9])
@@ -534,7 +534,7 @@ def runTest(self):
534534
x0 = np.array([1.0, 1.0])
535535
xl = -1e2 * np.ones((n,))
536536
xu = 1e2 * np.ones((n,))
537-
model = Model(npt, x0, objfun(x0), xl, xu, 1)
537+
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
538538
x1 = x0 + np.array([1.0, 0.0])
539539
model.change_point(1, x1 - model.xbase, objfun(x1))
540540
x2 = x0 + np.array([0.1, 0.9])
@@ -559,7 +559,7 @@ def runTest(self):
559559
x0 = np.array([1.0, 1.0])
560560
xl = -1e2 * np.ones((n,))
561561
xu = 1e2 * np.ones((n,))
562-
model = Model(npt, x0, objfun(x0), xl, xu, 1)
562+
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
563563
x1 = x0 + np.array([1.0, 0.0])
564564
model.change_point(1, x1 - model.xbase, objfun(x1))
565565
x2 = x0 + np.array([0.1, 0.9])
@@ -586,7 +586,7 @@ def runTest(self):
586586
x0 = np.array([1.0, 1.0])
587587
xl = -1e2 * np.ones((n,))
588588
xu = 1e2 * np.ones((n,))
589-
model = Model(npt, x0, objfun(x0), xl, xu, 1)
589+
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
590590
x1 = x0 + np.array([1.0, 0.0])
591591
model.change_point(1, x1 - model.xbase, objfun(x1))
592592
x2 = x0 + np.array([0.1, 0.9])
@@ -616,7 +616,7 @@ def runTest(self):
616616
delta = 0.5
617617
xl = -1e2 * np.ones((n,))
618618
xu = 1e2 * np.ones((n,))
619-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
619+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
620620
model.add_new_sample(0, rosenbrock(x0))
621621
x1 = x0 + delta * np.array([1.0, 0.0])
622622
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
@@ -636,7 +636,7 @@ def runTest(self):
636636
x0 = np.array([0.5, 0.5])
637637
xl = -1e2 * np.ones((n,))
638638
xu = 1e2 * np.ones((n,))
639-
model = Model(npt, x0, objfun(x0), xl, xu, 1)
639+
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
640640
x1 = np.array([0.05, 0.1])
641641
model.change_point(1, x1 - model.xbase, objfun(x1))
642642
x2 = np.array([0.1, 0.05])
@@ -660,7 +660,7 @@ def runTest(self):
660660
x0 = np.array([0.5, 0.5])
661661
xl = -1e2 * np.ones((n,))
662662
xu = 1e2 * np.ones((n,))
663-
model = Model(npt, x0, objfun(x0), xl, xu, 1)
663+
model = Model(npt, x0, objfun(x0), xl, xu, [], 1)
664664
x1 = np.array([0.524, 0.0006])
665665
model.change_point(1, x1 - model.xbase, objfun(x1))
666666
x2 = np.array([0.032, 0.323])
@@ -681,7 +681,7 @@ def runTest(self):
681681
x0 = np.array([-1.2, 1.0])
682682
xl = -1e2 * np.ones((n,))
683683
xu = 1e2 * np.ones((n,))
684-
model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
684+
model = Model(npt, x0, rosenbrock(x0), xl, xu, [], 1)
685685
x1 = np.array([1.0, 0.9])
686686
model.change_point(1, x1 - model.xbase, rosenbrock(x1))
687687
x2 = np.array([2.0, 0.9])

0 commit comments

Comments
 (0)