29
29
from moptipy .operators .bitstrings .op1_flip1 import Op1Flip1
30
30
from moptipy .operators .bitstrings .op1_m_over_n_flip import Op1MoverNflip
31
31
from moptipy .operators .bitstrings .op2_uniform import Op2Uniform
32
+ from moptipy .operators .op0_forward import Op0Forward
32
33
from moptipy .spaces .bitstrings import BitStrings
33
34
from moptipy .utils .types import type_name_of
34
35
@@ -39,7 +40,8 @@ class MyAlgorithm(Algorithm2):
39
40
def __init__ (self ) -> None :
40
41
super ().__init__ ("dummy" , Op0Random (), Op1Flip1 (), Op2Uniform ())
41
42
self .ea = EA (self .op0 , self .op1 , self .op2 , 10 , 10 , 0.3 )
42
- self .rls = RLS (self .op0 , self .op1 , seeded = True )
43
+ self .fwd = Op0Forward ()
44
+ self .rls = RLS (self .fwd , self .op1 )
43
45
44
46
def solve (self , process : Process ) -> None :
45
47
"""Apply an EA for 100 FEs, followed by RLS."""
@@ -73,21 +75,25 @@ def solve(self, process: Process) -> None:
73
75
74
76
fnew2 : Union [int , float ]
75
77
fes2 : int
76
- with for_fes (from_starting_point (process , x1 , fnew ), 100 ) as z :
77
- assert str (z ) == f"forFEs_100_fromStart_{ process } "
78
- assert z .has_best ()
79
- assert z .get_best_f () == fnew
80
- assert z .get_consumed_fes () == 0
81
- self .rls .solve (z )
82
- fnew2 = z .get_best_f ()
83
- fes2 = z .get_consumed_fes ()
84
- assert fes2 > 0
85
- assert (fnew2 == 0 ) or (fes2 == 100 )
86
- assert (fnew2 >= 0 ) and (fes2 <= 100 )
87
- assert fnew2 <= fnew
78
+ with from_starting_point (process , x1 , fnew ) as z1 :
79
+ assert str (z1 ) == f"fromStart_{ process } "
80
+ with for_fes (z1 , 100 ) as z :
81
+ self .fwd .forward_to (z .get_copy_of_best_x )
82
+ assert str (z ) == f"forFEs_100_fromStart_{ process } "
83
+ assert z .has_best ()
84
+ assert z .get_best_f () == fnew
85
+ assert z .get_consumed_fes () == 0
86
+ self .rls .solve (z )
87
+ self .fwd .stop_forwarding ()
88
+ fnew2 = z .get_best_f ()
89
+ fes2 = z .get_consumed_fes ()
90
+ assert fes2 > 0
91
+ assert (fnew2 == 0 ) or (fes2 == 100 )
92
+ assert (fnew2 >= 0 ) and (fes2 <= 100 )
93
+ assert fnew2 <= fnew
88
94
89
95
allfes = process .get_consumed_fes ()
90
- assert allfes == 1 + fes + fes2
96
+ assert allfes == fes + fes2
91
97
assert process .get_best_f () == fnew2
92
98
if fnew2 > 0 :
93
99
assert process .evaluate (x1 ) == fnew2
@@ -105,8 +111,8 @@ def test_from_start_for_fes():
105
111
with exp .execute () as p :
106
112
assert p .has_best ()
107
113
assert p .get_best_f () >= 0
108
- assert (p .get_best_f () == 0 ) or (p .get_consumed_fes () == 202 )
109
- assert (p .get_best_f () >= 0 ) and (p .get_consumed_fes () <= 202 )
114
+ assert (p .get_best_f () == 0 ) or (p .get_consumed_fes () == 200 )
115
+ assert (p .get_best_f () >= 0 ) and (p .get_consumed_fes () <= 200 )
110
116
111
117
112
118
class MyAlgorithm2 (Algorithm2 ):
@@ -161,6 +167,7 @@ def test_without_should_terminate():
161
167
162
168
class _OneMaxRegAlgo (Algorithm0 ):
163
169
"""The one-max algorithm."""
170
+
164
171
def __init__ (self , op0 : Op0Random , f : OneMax ):
165
172
"""Initialize."""
166
173
super ().__init__ ("om" , op0 )
@@ -231,11 +238,11 @@ def test_for_fes_process_no_ss_no_log_reg_norm():
231
238
algorithm2 : Algorithm = _OneMaxRegAlgo (Op0Random (), objective )
232
239
algorithm : __RegisterForFEs = __RegisterForFEs (algorithm1 , algorithm2 )
233
240
234
- with Execution ()\
235
- .set_solution_space (space )\
236
- .set_objective (objective )\
237
- .set_algorithm (algorithm )\
238
- .set_max_fes (100 )\
241
+ with Execution () \
242
+ .set_solution_space (space ) \
243
+ .set_objective (objective ) \
244
+ .set_algorithm (algorithm ) \
245
+ .set_max_fes (100 ) \
239
246
.execute () as process :
240
247
assert type_name_of (process ) \
241
248
== "moptipy.api._process_no_ss._ProcessNoSS"
@@ -256,6 +263,7 @@ def test_for_fes_process_no_ss_no_log_reg_norm():
256
263
257
264
class _MOAlgoForFEs (MOAlgorithm , Algorithm0 ):
258
265
"""The algorithm for multi-objective optimization."""
266
+
259
267
def __init__ (self , op0 : Op0Random ):
260
268
"""Initialize."""
261
269
Algorithm0 .__init__ (self , "om" , op0 )
@@ -299,14 +307,14 @@ def test_for_fes_mo_process_no_ss_no_log():
299
307
algorithm : Algorithm = _MOAlgoForFEs (Op0Random ())
300
308
ams = int (random .integers (2 , 5 ))
301
309
302
- with MOExecution ()\
303
- .set_solution_space (space )\
304
- .set_objective (problem )\
305
- .set_archive_pruner (pruner )\
306
- .set_archive_max_size (ams )\
307
- .set_archive_pruning_limit (int (ams + random .integers (0 , 3 )))\
308
- .set_algorithm (algorithm )\
309
- .set_max_fes (100 )\
310
+ with MOExecution () \
311
+ .set_solution_space (space ) \
312
+ .set_objective (problem ) \
313
+ .set_archive_pruner (pruner ) \
314
+ .set_archive_max_size (ams ) \
315
+ .set_archive_pruning_limit (int (ams + random .integers (0 , 3 ))) \
316
+ .set_algorithm (algorithm ) \
317
+ .set_max_fes (100 ) \
310
318
.execute () as process :
311
319
assert isinstance (process , MOProcess )
312
320
assert type_name_of (process ) \
@@ -332,6 +340,7 @@ def test_for_fes_mo_process_no_ss_no_log():
332
340
333
341
class _MOWithoutShouldTerminate (MOAlgorithm , Algorithm0 ):
334
342
"""The algorithm for multi-objective optimization."""
343
+
335
344
def __init__ (self , op0 : Op0Random ):
336
345
"""Initialize."""
337
346
Algorithm0 .__init__ (self , "om" , op0 )
@@ -370,12 +379,12 @@ def test_without_should_terminate_mo_process_no_ss_no_log():
370
379
algorithm : Algorithm = _MOWithoutShouldTerminate (Op0Random ())
371
380
ams = int (random .integers (2 , 5 ))
372
381
373
- with MOExecution ()\
374
- .set_solution_space (space )\
375
- .set_objective (problem )\
376
- .set_archive_pruning_limit (int (ams + random .integers (0 , 3 )))\
377
- .set_algorithm (algorithm )\
378
- .set_max_fes (100 )\
382
+ with MOExecution () \
383
+ .set_solution_space (space ) \
384
+ .set_objective (problem ) \
385
+ .set_archive_pruning_limit (int (ams + random .integers (0 , 3 ))) \
386
+ .set_algorithm (algorithm ) \
387
+ .set_max_fes (100 ) \
379
388
.execute () as process :
380
389
assert isinstance (process , MOProcess )
381
390
assert type_name_of (process ) \
0 commit comments