24
24
Learner1D ,
25
25
Learner2D ,
26
26
LearnerND ,
27
+ SequenceLearner ,
27
28
)
28
29
from adaptive .runner import simple
29
30
@@ -116,26 +117,30 @@ def quadratic(x, m: uniform(0, 10), b: uniform(0, 1)):
116
117
117
118
118
119
@learn_with (Learner1D , bounds = (- 1 , 1 ))
120
+ @learn_with (SequenceLearner , sequence = np .linspace (- 1 , 1 , 201 ))
119
121
def linear_with_peak (x , d : uniform (- 1 , 1 )):
120
122
a = 0.01
121
123
return x + a ** 2 / (a ** 2 + (x - d ) ** 2 )
122
124
123
125
124
126
@learn_with (LearnerND , bounds = ((- 1 , 1 ), (- 1 , 1 )))
125
127
@learn_with (Learner2D , bounds = ((- 1 , 1 ), (- 1 , 1 )))
128
+ @learn_with (SequenceLearner , sequence = np .random .rand (1000 , 2 ))
126
129
def ring_of_fire (xy , d : uniform (0.2 , 1 )):
127
130
a = 0.2
128
131
x , y = xy
129
132
return x + math .exp (- (x ** 2 + y ** 2 - d ** 2 ) ** 2 / a ** 4 )
130
133
131
134
132
135
@learn_with (LearnerND , bounds = ((- 1 , 1 ), (- 1 , 1 ), (- 1 , 1 )))
136
+ @learn_with (SequenceLearner , sequence = np .random .rand (1000 , 3 ))
133
137
def sphere_of_fire (xyz , d : uniform (0.2 , 1 )):
134
138
a = 0.2
135
139
x , y , z = xyz
136
140
return x + math .exp (- (x ** 2 + y ** 2 + z ** 2 - d ** 2 ) ** 2 / a ** 4 ) + z ** 2
137
141
138
142
143
+ @learn_with (SequenceLearner , sequence = range (1000 ))
139
144
@learn_with (AverageLearner , rtol = 1 )
140
145
def gaussian (n ):
141
146
return random .gauss (0 , 1 )
@@ -247,7 +252,7 @@ def f(x):
247
252
simple (learner , goal = lambda l : l .npoints > 10 )
248
253
249
254
250
- @run_with (Learner1D , Learner2D , LearnerND )
255
+ @run_with (Learner1D , Learner2D , LearnerND , SequenceLearner )
251
256
def test_adding_existing_data_is_idempotent (learner_type , f , learner_kwargs ):
252
257
"""Adding already existing data is an idempotent operation.
253
258
@@ -264,7 +269,7 @@ def test_adding_existing_data_is_idempotent(learner_type, f, learner_kwargs):
264
269
N = random .randint (10 , 30 )
265
270
control .ask (N )
266
271
xs , _ = learner .ask (N )
267
- points = [(x , f (x )) for x in xs ]
272
+ points = [(x , learner . function (x )) for x in xs ]
268
273
269
274
for p in points :
270
275
control .tell (* p )
@@ -277,13 +282,24 @@ def test_adding_existing_data_is_idempotent(learner_type, f, learner_kwargs):
277
282
M = random .randint (10 , 30 )
278
283
pls = zip (* learner .ask (M ))
279
284
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 )
282
298
283
299
284
300
# XXX: This *should* pass (https://github.com/python-adaptive/adaptive/issues/55)
285
301
# 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 )
287
303
def test_adding_non_chosen_data (learner_type , f , learner_kwargs ):
288
304
"""Adding data for a point that was not returned by 'ask'."""
289
305
# XXX: learner, control and bounds are not defined
@@ -300,17 +316,29 @@ def test_adding_non_chosen_data(learner_type, f, learner_kwargs):
300
316
N = random .randint (10 , 30 )
301
317
xs , _ = control .ask (N )
302
318
303
- ys = [f (x ) for x in xs ]
319
+ ys = [learner . function (x ) for x in xs ]
304
320
for x , y in zip (xs , ys ):
305
321
control .tell (x , y )
306
322
learner .tell (x , y )
307
323
308
324
M = random .randint (10 , 30 )
309
325
pls = zip (* learner .ask (M ))
310
326
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 )
314
342
315
343
316
344
@run_with (Learner1D , xfail (Learner2D ), xfail (LearnerND ), AverageLearner )
@@ -334,7 +362,7 @@ def test_point_adding_order_is_irrelevant(learner_type, f, learner_kwargs):
334
362
N = random .randint (10 , 30 )
335
363
control .ask (N )
336
364
xs , _ = learner .ask (N )
337
- points = [(x , f (x )) for x in xs ]
365
+ points = [(x , learner . function (x )) for x in xs ]
338
366
339
367
for p in points :
340
368
control .tell (* p )
@@ -366,7 +394,7 @@ def test_expected_loss_improvement_is_less_than_total_loss(
366
394
xs , loss_improvements = learner .ask (N )
367
395
368
396
for x in xs :
369
- learner .tell (x , f (x ))
397
+ learner .tell (x , learner . function (x ))
370
398
371
399
M = random .randint (50 , 100 )
372
400
_ , loss_improvements = learner .ask (M )
@@ -429,7 +457,12 @@ def test_learner_performance_is_invariant_under_scaling(
429
457
430
458
431
459
@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 ,
433
466
)
434
467
def test_balancing_learner (learner_type , f , learner_kwargs ):
435
468
"""Test if the BalancingLearner works with the different types of learners."""
@@ -474,6 +507,7 @@ def test_balancing_learner(learner_type, f, learner_kwargs):
474
507
AverageLearner ,
475
508
maybe_skip (SKOptLearner ),
476
509
IntegratorLearner ,
510
+ SequenceLearner ,
477
511
with_all_loss_functions = False ,
478
512
)
479
513
def test_saving (learner_type , f , learner_kwargs ):
@@ -504,6 +538,7 @@ def test_saving(learner_type, f, learner_kwargs):
504
538
AverageLearner ,
505
539
maybe_skip (SKOptLearner ),
506
540
IntegratorLearner ,
541
+ SequenceLearner ,
507
542
with_all_loss_functions = False ,
508
543
)
509
544
def test_saving_of_balancing_learner (learner_type , f , learner_kwargs ):
0 commit comments