Skip to content

Commit 5c6f2c2

Browse files
committed
v0.0.9 Added termination criterions and GJSolution from prototype (autumn 2024)
1 parent e40a304 commit 5c6f2c2

File tree

10 files changed

+282
-0
lines changed

10 files changed

+282
-0
lines changed

greyjack/greyjack/agents/__init__.py

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
import numpy as np
3+
4+
class GJSolution():
5+
def __init__(self, variable_values_dict, score):
6+
self.variable_values_dict = variable_values_dict
7+
for var_name in self.variable_values_dict.keys():
8+
var_value = self.variable_values_dict[var_name]
9+
10+
if "float" in str(type(var_value)):
11+
var_value = float( var_value )
12+
if "int" in str(type(var_value)):
13+
var_value = int( var_value )
14+
15+
self.variable_values_dict[var_name] = var_value
16+
17+
18+
self.score = score
19+
20+
def __repr__(self):
21+
22+
solution_string = ""
23+
24+
for var_name in self.variable_values_dict.keys():
25+
var_string = var_name + " = {}".format( self.variable_values_dict[var_name] )
26+
solution_string += var_string + "\n"
27+
28+
solution_string += "Score: " + str(self.score) + "\n"
29+
30+
return solution_string
31+
32+
def __eq__(self, other):
33+
return self.score == other.score
34+
35+
def __ne__(self, other):
36+
return self.score != other.score
37+
38+
def __le__(self, other):
39+
return self.score <= other.score
40+
41+
def __ge__(self, other):
42+
return self.score >= other.score
43+
44+
def __lt__(self, other):
45+
return self.score < other.score
46+
47+
def __gt__(self, other):
48+
return self.score > other.score
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
import numpy as np
3+
from greyjack.score_calculation.scores.SimpleScore import SimpleScore
4+
from greyjack.score_calculation.scores.HardSoftScore import HardSoftScore
5+
6+
7+
class Individual:
8+
9+
def __init__(self, variable_values=None, score=None):
10+
self.variable_values = variable_values
11+
self.score = score
12+
13+
def copy(self):
14+
individual = Individual(self.variable_values.copy(), self.scopy())
15+
return individual
16+
17+
def __lt__(self, other):
18+
return self.score < other.score
19+
20+
def as_list(self):
21+
return [self.variable_values.tolist(), self.score.as_list()]
22+
23+
@staticmethod
24+
def from_list(list_individual):
25+
# TODO: generalize to more level scores
26+
score_len = len(list_individual[1])
27+
if score_len == 1:
28+
score_type = SimpleScore
29+
elif score_len == 2:
30+
score_type = HardSoftScore
31+
else:
32+
raise ("Can't define score type while inverse converting of list of individuals-lists")
33+
34+
variable_array = np.array( list_individual[0], dtype=np.float64 )
35+
score = score_type( *list_individual[1] )
36+
individual = Individual( variable_array, score )
37+
38+
return individual
39+
40+
@staticmethod
41+
def convert_individuals_to_lists(individuals_list):
42+
43+
list_of_lists = []
44+
for individual in individuals_list:
45+
list_of_lists.append( individual.as_list() )
46+
47+
return list_of_lists
48+
49+
@staticmethod
50+
def convert_lists_to_individuals(list_of_lists):
51+
52+
# TODO: generalize to more level scores
53+
score_len = len(list_of_lists[0][1])
54+
if score_len == 1:
55+
score_type = SimpleScore
56+
elif score_len == 2:
57+
score_type = HardSoftScore
58+
else:
59+
raise ("Can't define score type while inverse converting of list of individuals-lists")
60+
61+
individuals_list = []
62+
for list_individual in list_of_lists:
63+
variable_array = np.array( list_individual[0], dtype=np.float64 )
64+
score = score_type( *list_individual[1] )
65+
individual = Individual( variable_array, score )
66+
individuals_list.append( individual )
67+
68+
return individuals_list
69+
70+

greyjack/greyjack/agents/base/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
from greyjack.agents.termination_strategies.TerminationStrategy import TerminationStrategy
3+
4+
class ScoreLimit(TerminationStrategy):
5+
def __init__(self, score_to_compare):
6+
7+
self.score_to_compare = score_to_compare
8+
self.current_best_score = None
9+
10+
pass
11+
12+
def update(self, agent):
13+
14+
self.current_best_score = agent.current_top_individual.score
15+
16+
pass
17+
18+
def is_accomplish(self):
19+
20+
if self.current_best_score <= self.score_to_compare:
21+
return True
22+
23+
return False
24+
25+
def get_accomplish_rate(self):
26+
27+
accomplish_rate = self.current_best_sget_fitness_value() / (self.score_to_compare.get_fitness_value() + 1e-10)
28+
29+
return accomplish_rate
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
from greyjack.agents.termination_strategies.TerminationStrategy import TerminationStrategy
3+
from datetime import datetime
4+
5+
class ScoreNoImprovement(TerminationStrategy):
6+
def __init__(self, time_seconds_limit=15):
7+
8+
self.time_seconds_limit = time_seconds_limit
9+
self.start_time = None
10+
self.current_best_score = None
11+
self.time_delta = None
12+
13+
pass
14+
15+
def update(self, agent):
16+
17+
if self.start_time is None:
18+
self.start_time = datetime.now()
19+
self.time_delta = (self.start_time - self.start_time).seconds
20+
self.current_best_score = agent.current_top_individual.score
21+
return
22+
23+
# to prevent updates from migrants
24+
if self.is_accomplish():
25+
return
26+
27+
current_score = agent.current_top_individual.score
28+
29+
if current_score < self.current_best_score:
30+
self.current_best_score = current_score
31+
self.start_time = datetime.now()
32+
self.time_delta = (self.start_time - self.start_time).seconds
33+
else:
34+
self.time_delta = (datetime.now() - self.start_time).seconds
35+
36+
pass
37+
38+
def is_accomplish(self):
39+
40+
if self.time_delta >= self.time_seconds_limit:
41+
return True
42+
43+
return False
44+
45+
def get_accomplish_rate(self):
46+
47+
accomplish_rate = float(self.time_delta) / float(self.time_seconds_limit)
48+
49+
return accomplish_rate
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
from greyjack.agents.termination_strategies.TerminationStrategy import TerminationStrategy
3+
4+
class StepsLimit(TerminationStrategy):
5+
def __init__(self, step_count_limit=10000):
6+
7+
self.step_count_limit = step_count_limit
8+
self.steps_made = 0
9+
10+
pass
11+
12+
def update(self, agent):
13+
self.steps_made += 1
14+
pass
15+
16+
def is_accomplish(self):
17+
18+
if self.steps_made > self.step_count_limit:
19+
return True
20+
21+
return False
22+
23+
def get_accomplish_rate(self):
24+
25+
accomplish_rate = float(self.steps_made) / float(self.step_count_limit)
26+
27+
return accomplish_rate
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
from abc import ABC, abstractmethod
3+
4+
class TerminationStrategy(ABC):
5+
6+
@abstractmethod
7+
def update(self, agent):
8+
pass
9+
10+
@abstractmethod
11+
def is_accomplish(self):
12+
pass
13+
14+
# how far from start of termination is the prcoess of solving
15+
@abstractmethod
16+
def get_accomplish_rate(self):
17+
pass
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
from greyjack.agents.termination_strategies.TerminationStrategy import TerminationStrategy
3+
from datetime import datetime
4+
5+
class TimeSpentLimit(TerminationStrategy):
6+
def __init__(self, time_seconds_limit=5*60):
7+
8+
self.time_seconds_limit = time_seconds_limit
9+
self.start_time = None
10+
self.time_delta = None
11+
12+
pass
13+
14+
def update(self, agent):
15+
16+
if self.start_time is None:
17+
self.start_time = datetime.now()
18+
self.time_delta = (self.start_time - self.start_time).seconds
19+
return
20+
21+
self.time_delta = (datetime.now() - self.start_time).seconds
22+
23+
pass
24+
25+
def is_accomplish(self):
26+
27+
if self.time_delta >= self.time_seconds_limit:
28+
return True
29+
30+
return False
31+
32+
def get_accomplish_rate(self):
33+
34+
accomplish_rate = float(self.time_delta) / float(self.time_seconds_limit)
35+
36+
return accomplish_rate
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
from greyjack.agents.termination_strategies.ScoreLimit import ScoreLimit
4+
from greyjack.agents.termination_strategies.StepsLimit import StepsLimit
5+
from greyjack.agents.termination_strategies.TimeSpentLimit import TimeSpentLimit
6+
from greyjack.agents.termination_strategies.ScoreNoImprovement import ScoreNoImprovement

0 commit comments

Comments
 (0)