Skip to content

Commit 8f57571

Browse files
committed
v0.0.15 Wrapped Individual (necessary for wrapping metaheuristic bases)
1 parent 571fe87 commit 8f57571

File tree

25 files changed

+304
-124
lines changed

25 files changed

+304
-124
lines changed

examples/object_oriented/nqueens/score/NQueensScoreCalculator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from greyjack.score_calculation.score_calculators.PlainScoreCalculator import PlainScoreCalculator
44
from greyjack.score_calculation.scores.SimpleScore import SimpleScore
5+
from greyjack.score_calculation.scores.ScoreVariants import ScoreVariants
56
import polars as pl
67

78
#import os
@@ -14,7 +15,7 @@ def __init__(self):
1415

1516
super().__init__()
1617

17-
self.score_type = "SimpleScore"
18+
self.score_variant = ScoreVariants.SimpleScore
1819

1920
#self.add_constraint("different_rows", self.different_rows)
2021
#self.add_constraint("different_descending_diagonals", self.different_descending_diagonals)

greyjack/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

greyjack/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "greyjack"
3-
version = "0.0.13"
3+
version = "0.0.15"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -17,4 +17,10 @@ ndarray = "0.16.1"
1717
ndarray-rand = "0.15.0"
1818
ndarray-stats = "0.6.0"
1919
chrono = "0.4.39"
20-
polars = { version = "0.46.0", features = ["lazy", "ndarray", "serde", "abs"] }
20+
polars = { version = "0.46.0", features = ["lazy", "ndarray", "serde", "abs"] }
21+
22+
#[profile.release]
23+
#lto = true
24+
#codegen-units = 1
25+
#debug = true
26+
#opt-level = 3

greyjack/greyjack/Solver.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import random
55

66
from greyjack.agents.base.GJSolution import GJSolution
7-
from greyjack.agents.base.Individual import Individual
7+
from greyjack.agents.base.individuals.Individual import Individual
88
from pathos.multiprocessing import ProcessPool
99
from pathos.threading import ThreadPool
1010
import multiprocessing
@@ -127,8 +127,9 @@ def solve(self):
127127
agent_id = agent_publication["agent_id"]
128128
agent_status = agent_publication["status"]
129129
local_step = agent_publication["step"]
130+
score_variant = agent_publication["score_variant"]
130131
solution_candidate = agent_publication["candidate"]
131-
solution_candidate = Individual.from_list(solution_candidate)
132+
solution_candidate = Individual.get_related_individual_type_by_value(score_variant).from_list(solution_candidate)
132133

133134
new_best_flag = False
134135
if current_best_candidate is None:

greyjack/greyjack/agents/base/Agent.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import zmq
88
import sys
99

10-
from greyjack.agents.base.Individual import Individual
1110
from greyjack.agents.termination_strategies import *
1211
from greyjack.score_calculation.score_requesters import OOPScoreRequester
1312
from greyjack.score_calculation.score_calculators import PlainScoreCalculator, IncrementalScoreCalculator
13+
from greyjack.agents.base.individuals.Individual import Individual
1414

1515
current_platform = sys.platform
1616

@@ -25,6 +25,8 @@ def __init__(self, migration_rate, migration_frequency, termination_strategy):
2525
self.agent_id = None
2626
self.population_size = None
2727
self.population = None
28+
self.individual_type = None
29+
self.score_variant = None
2830
self.agent_top_individual = None
2931
self.logger = None
3032
self.logging_level = None
@@ -85,7 +87,10 @@ def _build_cotwin(self):
8587
domain = self.domain_builder.build_from_domain(self.initial_solution)
8688

8789
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)
8994

9095
# implements by concrete metaheuristics
9196
def _build_metaheuristic_base(self):
@@ -106,6 +111,7 @@ def solve(self):
106111

107112
try:
108113
self._build_cotwin()
114+
self._define_individual_type()
109115
self._build_metaheuristic_base()
110116
self._build_logger()
111117
self.init_population()
@@ -167,13 +173,13 @@ def init_population(self):
167173
scores = self.score_requester.request_score_plain(samples)
168174

169175
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]))
171177

172178
else:
173179
generated_sample = self.score_requester.variables_manager.sample_variables()
174180
deltas = [[(i, val) for i, val in enumerate(generated_sample)]]
175181
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]))
177183

178184
def step_plain(self):
179185
new_population = []
@@ -183,7 +189,7 @@ def step_plain(self):
183189
for score in scores:
184190
score.round(self.score_precision)
185191

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))]
187193
new_population = self.metaheuristic_base.build_updated_population(self.population, candidates)
188194

189195
self.population = new_population
@@ -246,7 +252,7 @@ def _send_updates_windows(self):
246252
# assuming that all updates are sorted by agents themselves
247253
# (individual with id == 0 is best in update-subpopulation)
248254
migrants = self.population[:migrants_count]
249-
migrants = Individual.convert_individuals_to_lists(migrants)
255+
migrants = self.individual_type.convert_individuals_to_lists(migrants)
250256
request = {"agent_id": self.agent_id,
251257
"round_robin_status_dict": self.round_robin_status_dict,
252258
"request_type": "put_updates",
@@ -298,14 +304,14 @@ def _get_updates_windows(self):
298304
if self.metaheuristic_base.metaheuristic_name == "LSHADE":
299305
history_migrant = updates_reply["history_archive"]
300306
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)
302308
rand_id = self.generator.integers(0, len(self.history_archive), 1)[0]
303309
#if updates_reply["history_archive"] < self.history_archive[-1]:
304310
if history_migrant < self.history_archive[rand_id]:
305311
self.history_archive[rand_id] = history_migrant
306312

307313
migrants = updates_reply["migrants"]
308-
migrants = Individual.convert_lists_to_individuals(migrants)
314+
migrants = self.individual_type.convert_lists_to_individuals(migrants)
309315
n_migrants = len(migrants)
310316

311317
# population already sorted after step
@@ -350,7 +356,7 @@ def _send_updates_linux(self):
350356
# assuming that all updates are sorted by agents themselves
351357
# (individual with id == 0 is best in update-subpopulation)
352358
migrants = self.population[:migrants_count]
353-
migrants = Individual.convert_individuals_to_lists(migrants)
359+
migrants = self.individual_type.convert_individuals_to_lists(migrants)
354360
request = {"agent_id": self.agent_id,
355361
"round_robin_status_dict": self.round_robin_status_dict,
356362
"request_type": "put_updates",
@@ -388,14 +394,14 @@ def _get_updates_linux(self):
388394
if self.metaheuristic_base.metaheuristic_name == "LSHADE":
389395
history_migrant = updates_reply["history_archive"]
390396
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)
392398
rand_id = self.generator.integers(0, len(self.history_archive), 1)[0]
393399
#if updates_reply["history_archive"] < self.history_archive[-1]:
394400
if history_migrant < self.history_archive[rand_id]:
395401
self.history_archive[rand_id] = history_migrant
396402

397403
migrants = updates_reply["migrants"]
398-
migrants = Individual.convert_lists_to_individuals(migrants)
404+
migrants = self.individual_type.convert_lists_to_individuals(migrants)
399405
n_migrants = len(migrants)
400406

401407
# population already sorted after step
@@ -429,6 +435,7 @@ def send_candidate_to_master_windows(self, step_id):
429435
agent_publication["status"] = self.agent_status
430436
agent_publication["candidate"] = self.agent_top_individual.as_list()
431437
agent_publication["step"] = step_id
438+
agent_publication["score_variant"] = self.score_variant
432439
agent_publication = orjson.dumps( agent_publication )
433440
self.socket_publisher.send( agent_publication )
434441

@@ -438,4 +445,5 @@ def send_candidate_to_master_linux(self, step_id):
438445
agent_publication["status"] = self.agent_status
439446
agent_publication["candidate"] = self.agent_top_individual.as_list()
440447
agent_publication["step"] = step_id
448+
agent_publication["score_variant"] = self.score_variant
441449
self.socket_publisher.send( agent_publication )

greyjack/greyjack/agents/base/Individual.py

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import numpy as np
3+
from greyjack.score_calculation.scores.ScoreVariants import ScoreVariants
4+
from greyjack.agents.base.individuals.IndividualSimple import IndividualSimple
5+
from greyjack.agents.base.individuals.IndividualHardSoft import IndividualHardSoft
6+
from greyjack.agents.base.individuals.IndividualHardMediumSoft import IndividualHardMediumSoft
7+
8+
9+
class Individual:
10+
11+
def get_related_individual_type(score_variant):
12+
13+
if score_variant == ScoreVariants.SimpleScore:
14+
return IndividualSimple
15+
if score_variant == ScoreVariants.HardSoftScore:
16+
return IndividualHardSoft
17+
if score_variant == ScoreVariants.HardMediumSoftScore:
18+
return IndividualHardMediumSoft
19+
20+
raise Exception("score_variant unrecognized")
21+
22+
def get_related_individual_type_by_value(score_variant_value):
23+
24+
if score_variant_value == ScoreVariants.SimpleScore.value:
25+
return IndividualSimple
26+
if score_variant_value == ScoreVariants.HardSoftScore.value:
27+
return IndividualHardSoft
28+
if score_variant_value == ScoreVariants.HardMediumSoftScore.value:
29+
return IndividualHardMediumSoft
30+
31+
raise Exception("score_variant unrecognized")
32+
33+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
from greyjack.greyjack import IndividualHardMediumSoft
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
from greyjack.greyjack import IndividualHardSoft
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
from greyjack.greyjack import IndividualSimple

0 commit comments

Comments
 (0)