2424 Learner1D ,
2525 Learner2D ,
2626 LearnerND ,
27+ SequenceLearner ,
2728)
2829from adaptive .runner import simple
2930
@@ -116,26 +117,30 @@ def quadratic(x, m: uniform(0, 10), b: uniform(0, 1)):
116117
117118
118119@learn_with (Learner1D , bounds = (- 1 , 1 ))
120+ @learn_with (SequenceLearner , sequence = np .linspace (- 1 , 1 , 201 ))
119121def linear_with_peak (x , d : uniform (- 1 , 1 )):
120122 a = 0.01
121123 return x + a ** 2 / (a ** 2 + (x - d ) ** 2 )
122124
123125
124126@learn_with (LearnerND , bounds = ((- 1 , 1 ), (- 1 , 1 )))
125127@learn_with (Learner2D , bounds = ((- 1 , 1 ), (- 1 , 1 )))
128+ @learn_with (SequenceLearner , sequence = np .random .rand (1000 , 2 ))
126129def ring_of_fire (xy , d : uniform (0.2 , 1 )):
127130 a = 0.2
128131 x , y = xy
129132 return x + math .exp (- (x ** 2 + y ** 2 - d ** 2 ) ** 2 / a ** 4 )
130133
131134
132135@learn_with (LearnerND , bounds = ((- 1 , 1 ), (- 1 , 1 ), (- 1 , 1 )))
136+ @learn_with (SequenceLearner , sequence = np .random .rand (1000 , 3 ))
133137def sphere_of_fire (xyz , d : uniform (0.2 , 1 )):
134138 a = 0.2
135139 x , y , z = xyz
136140 return x + math .exp (- (x ** 2 + y ** 2 + z ** 2 - d ** 2 ) ** 2 / a ** 4 ) + z ** 2
137141
138142
143+ @learn_with (SequenceLearner , sequence = range (1000 ))
139144@learn_with (AverageLearner , rtol = 1 )
140145def gaussian (n ):
141146 return random .gauss (0 , 1 )
@@ -247,7 +252,7 @@ def f(x):
247252 simple (learner , goal = lambda l : l .npoints > 10 )
248253
249254
250- @run_with (Learner1D , Learner2D , LearnerND )
255+ @run_with (Learner1D , Learner2D , LearnerND , SequenceLearner )
251256def test_adding_existing_data_is_idempotent (learner_type , f , learner_kwargs ):
252257 """Adding already existing data is an idempotent operation.
253258
@@ -264,7 +269,7 @@ def test_adding_existing_data_is_idempotent(learner_type, f, learner_kwargs):
264269 N = random .randint (10 , 30 )
265270 control .ask (N )
266271 xs , _ = learner .ask (N )
267- points = [(x , f (x )) for x in xs ]
272+ points = [(x , learner . function (x )) for x in xs ]
268273
269274 for p in points :
270275 control .tell (* p )
@@ -277,13 +282,24 @@ def test_adding_existing_data_is_idempotent(learner_type, f, learner_kwargs):
277282 M = random .randint (10 , 30 )
278283 pls = zip (* learner .ask (M ))
279284 cpls = zip (* control .ask (M ))
280- # Point ordering is not defined, so compare as sets
281- assert set (pls ) == set (cpls )
285+ if learner_type is SequenceLearner :
286+ # The SequenceLearner's points might not be hasable
287+ points , values = zip (* pls )
288+ indices , points = zip (* points )
289+
290+ cpoints , cvalues = zip (* cpls )
291+ cindices , cpoints = zip (* cpoints )
292+ assert (np .array (points ) == np .array (cpoints )).all ()
293+ assert values == cvalues
294+ assert indices == cindices
295+ else :
296+ # Point ordering is not defined, so compare as sets
297+ assert set (pls ) == set (cpls )
282298
283299
284300# XXX: This *should* pass (https://github.com/python-adaptive/adaptive/issues/55)
285301# but we xfail it now, as Learner2D will be deprecated anyway
286- @run_with (Learner1D , xfail (Learner2D ), LearnerND , AverageLearner )
302+ @run_with (Learner1D , xfail (Learner2D ), LearnerND , AverageLearner , SequenceLearner )
287303def test_adding_non_chosen_data (learner_type , f , learner_kwargs ):
288304 """Adding data for a point that was not returned by 'ask'."""
289305 # XXX: learner, control and bounds are not defined
@@ -300,17 +316,29 @@ def test_adding_non_chosen_data(learner_type, f, learner_kwargs):
300316 N = random .randint (10 , 30 )
301317 xs , _ = control .ask (N )
302318
303- ys = [f (x ) for x in xs ]
319+ ys = [learner . function (x ) for x in xs ]
304320 for x , y in zip (xs , ys ):
305321 control .tell (x , y )
306322 learner .tell (x , y )
307323
308324 M = random .randint (10 , 30 )
309325 pls = zip (* learner .ask (M ))
310326 cpls = zip (* control .ask (M ))
311- # Point ordering within a single call to 'ask'
312- # is not guaranteed to be the same by the API.
313- assert set (pls ) == set (cpls )
327+
328+ if learner_type is SequenceLearner :
329+ # The SequenceLearner's points might not be hasable
330+ points , values = zip (* pls )
331+ indices , points = zip (* points )
332+
333+ cpoints , cvalues = zip (* cpls )
334+ cindices , cpoints = zip (* cpoints )
335+ assert (np .array (points ) == np .array (cpoints )).all ()
336+ assert values == cvalues
337+ assert indices == cindices
338+ else :
339+ # Point ordering within a single call to 'ask'
340+ # is not guaranteed to be the same by the API.
341+ assert set (pls ) == set (cpls )
314342
315343
316344@run_with (Learner1D , xfail (Learner2D ), xfail (LearnerND ), AverageLearner )
@@ -334,7 +362,7 @@ def test_point_adding_order_is_irrelevant(learner_type, f, learner_kwargs):
334362 N = random .randint (10 , 30 )
335363 control .ask (N )
336364 xs , _ = learner .ask (N )
337- points = [(x , f (x )) for x in xs ]
365+ points = [(x , learner . function (x )) for x in xs ]
338366
339367 for p in points :
340368 control .tell (* p )
@@ -366,7 +394,7 @@ def test_expected_loss_improvement_is_less_than_total_loss(
366394 xs , loss_improvements = learner .ask (N )
367395
368396 for x in xs :
369- learner .tell (x , f (x ))
397+ learner .tell (x , learner . function (x ))
370398
371399 M = random .randint (50 , 100 )
372400 _ , loss_improvements = learner .ask (M )
@@ -429,7 +457,12 @@ def test_learner_performance_is_invariant_under_scaling(
429457
430458
431459@run_with (
432- Learner1D , Learner2D , LearnerND , AverageLearner , with_all_loss_functions = False
460+ Learner1D ,
461+ Learner2D ,
462+ LearnerND ,
463+ AverageLearner ,
464+ SequenceLearner ,
465+ with_all_loss_functions = False ,
433466)
434467def test_balancing_learner (learner_type , f , learner_kwargs ):
435468 """Test if the BalancingLearner works with the different types of learners."""
@@ -474,6 +507,7 @@ def test_balancing_learner(learner_type, f, learner_kwargs):
474507 AverageLearner ,
475508 maybe_skip (SKOptLearner ),
476509 IntegratorLearner ,
510+ SequenceLearner ,
477511 with_all_loss_functions = False ,
478512)
479513def test_saving (learner_type , f , learner_kwargs ):
@@ -504,6 +538,7 @@ def test_saving(learner_type, f, learner_kwargs):
504538 AverageLearner ,
505539 maybe_skip (SKOptLearner ),
506540 IntegratorLearner ,
541+ SequenceLearner ,
507542 with_all_loss_functions = False ,
508543)
509544def test_saving_of_balancing_learner (learner_type , f , learner_kwargs ):
0 commit comments