7
7
import zmq
8
8
import sys
9
9
10
- from greyjack .agents .base .Individual import Individual
11
10
from greyjack .agents .termination_strategies import *
12
11
from greyjack .score_calculation .score_requesters import OOPScoreRequester
13
12
from greyjack .score_calculation .score_calculators import PlainScoreCalculator , IncrementalScoreCalculator
13
+ from greyjack .agents .base .individuals .Individual import Individual
14
14
15
15
current_platform = sys .platform
16
16
@@ -25,6 +25,8 @@ def __init__(self, migration_rate, migration_frequency, termination_strategy):
25
25
self .agent_id = None
26
26
self .population_size = None
27
27
self .population = None
28
+ self .individual_type = None
29
+ self .score_variant = None
28
30
self .agent_top_individual = None
29
31
self .logger = None
30
32
self .logging_level = None
@@ -85,7 +87,10 @@ def _build_cotwin(self):
85
87
domain = self .domain_builder .build_from_domain (self .initial_solution )
86
88
87
89
self .cotwin = self .cotwin_builder .build_cotwin (domain , is_already_initialized )
88
-
90
+
91
+ def _define_individual_type (self ):
92
+ self .score_variant = self .cotwin .score_calculator .score_variant
93
+ self .individual_type = Individual .get_related_individual_type (self .cotwin .score_calculator .score_variant )
89
94
90
95
# implements by concrete metaheuristics
91
96
def _build_metaheuristic_base (self ):
@@ -106,6 +111,7 @@ def solve(self):
106
111
107
112
try :
108
113
self ._build_cotwin ()
114
+ self ._define_individual_type ()
109
115
self ._build_metaheuristic_base ()
110
116
self ._build_logger ()
111
117
self .init_population ()
@@ -167,13 +173,13 @@ def init_population(self):
167
173
scores = self .score_requester .request_score_plain (samples )
168
174
169
175
for i in range (self .population_size ):
170
- self .population .append (Individual (samples [i ].copy (), scores [i ]))
176
+ self .population .append (self . individual_type (samples [i ].copy (), scores [i ]))
171
177
172
178
else :
173
179
generated_sample = self .score_requester .variables_manager .sample_variables ()
174
180
deltas = [[(i , val ) for i , val in enumerate (generated_sample )]]
175
181
scores = self .score_requester .request_score_incremental (generated_sample , deltas )
176
- self .population .append (Individual (generated_sample , scores [0 ]))
182
+ self .population .append (self . individual_type (generated_sample , scores [0 ]))
177
183
178
184
def step_plain (self ):
179
185
new_population = []
@@ -183,7 +189,7 @@ def step_plain(self):
183
189
for score in scores :
184
190
score .round (self .score_precision )
185
191
186
- candidates = [Individual (samples [i ].copy (), scores [i ]) for i in range (len (samples ))]
192
+ candidates = [self . individual_type (samples [i ].copy (), scores [i ]) for i in range (len (samples ))]
187
193
new_population = self .metaheuristic_base .build_updated_population (self .population , candidates )
188
194
189
195
self .population = new_population
@@ -246,7 +252,7 @@ def _send_updates_windows(self):
246
252
# assuming that all updates are sorted by agents themselves
247
253
# (individual with id == 0 is best in update-subpopulation)
248
254
migrants = self .population [:migrants_count ]
249
- migrants = Individual .convert_individuals_to_lists (migrants )
255
+ migrants = self . individual_type .convert_individuals_to_lists (migrants )
250
256
request = {"agent_id" : self .agent_id ,
251
257
"round_robin_status_dict" : self .round_robin_status_dict ,
252
258
"request_type" : "put_updates" ,
@@ -298,14 +304,14 @@ def _get_updates_windows(self):
298
304
if self .metaheuristic_base .metaheuristic_name == "LSHADE" :
299
305
history_migrant = updates_reply ["history_archive" ]
300
306
if (history_migrant is not None and len (self .history_archive ) > 0 ):
301
- history_migrant = Individual .from_list (history_migrant )
307
+ history_migrant = self . individual_type .from_list (history_migrant )
302
308
rand_id = self .generator .integers (0 , len (self .history_archive ), 1 )[0 ]
303
309
#if updates_reply["history_archive"] < self.history_archive[-1]:
304
310
if history_migrant < self .history_archive [rand_id ]:
305
311
self .history_archive [rand_id ] = history_migrant
306
312
307
313
migrants = updates_reply ["migrants" ]
308
- migrants = Individual .convert_lists_to_individuals (migrants )
314
+ migrants = self . individual_type .convert_lists_to_individuals (migrants )
309
315
n_migrants = len (migrants )
310
316
311
317
# population already sorted after step
@@ -350,7 +356,7 @@ def _send_updates_linux(self):
350
356
# assuming that all updates are sorted by agents themselves
351
357
# (individual with id == 0 is best in update-subpopulation)
352
358
migrants = self .population [:migrants_count ]
353
- migrants = Individual .convert_individuals_to_lists (migrants )
359
+ migrants = self . individual_type .convert_individuals_to_lists (migrants )
354
360
request = {"agent_id" : self .agent_id ,
355
361
"round_robin_status_dict" : self .round_robin_status_dict ,
356
362
"request_type" : "put_updates" ,
@@ -388,14 +394,14 @@ def _get_updates_linux(self):
388
394
if self .metaheuristic_base .metaheuristic_name == "LSHADE" :
389
395
history_migrant = updates_reply ["history_archive" ]
390
396
if (history_migrant is not None and len (self .history_archive ) > 0 ):
391
- history_migrant = Individual .from_list (history_migrant )
397
+ history_migrant = self . individual_type .from_list (history_migrant )
392
398
rand_id = self .generator .integers (0 , len (self .history_archive ), 1 )[0 ]
393
399
#if updates_reply["history_archive"] < self.history_archive[-1]:
394
400
if history_migrant < self .history_archive [rand_id ]:
395
401
self .history_archive [rand_id ] = history_migrant
396
402
397
403
migrants = updates_reply ["migrants" ]
398
- migrants = Individual .convert_lists_to_individuals (migrants )
404
+ migrants = self . individual_type .convert_lists_to_individuals (migrants )
399
405
n_migrants = len (migrants )
400
406
401
407
# population already sorted after step
@@ -429,6 +435,7 @@ def send_candidate_to_master_windows(self, step_id):
429
435
agent_publication ["status" ] = self .agent_status
430
436
agent_publication ["candidate" ] = self .agent_top_individual .as_list ()
431
437
agent_publication ["step" ] = step_id
438
+ agent_publication ["score_variant" ] = self .score_variant
432
439
agent_publication = orjson .dumps ( agent_publication )
433
440
self .socket_publisher .send ( agent_publication )
434
441
@@ -438,4 +445,5 @@ def send_candidate_to_master_linux(self, step_id):
438
445
agent_publication ["status" ] = self .agent_status
439
446
agent_publication ["candidate" ] = self .agent_top_individual .as_list ()
440
447
agent_publication ["step" ] = step_id
448
+ agent_publication ["score_variant" ] = self .score_variant
441
449
self .socket_publisher .send ( agent_publication )
0 commit comments