Skip to content

Commit 8b358e9

Browse files
committed
v0.2.1 Rounding bug fix; incremental score calculation for MathModel (currently works slow, turned off by default, will try to improve later)
1 parent efe8fbb commit 8b358e9

File tree

13 files changed

+126
-32
lines changed

13 files changed

+126
-32
lines changed

examples/object_oriented/vrp/scripts/solve_vrp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
#file_path = Path(data_dir_path, data_dir_path, "belgium", "basic", "air", "belgium-n1000-k40.vrp") #optimum: ~57.7; first_fit: ~195.3; RoutingModel: from 67.3 to 74 (depends on time)
3434
# multi-depot with timewindows
3535
#file_path = Path(data_dir_path, "belgium", "multidepot-timewindowed", "air", "belgium-tw-d2-n50-k10.vrp") # optimum: ~15.98; first_fit: ~27.89
36-
#file_path = Path(data_dir_path, "belgium", "multidepot-timewindowed", "air", "belgium-tw-d5-n500-k20.vrp") # optimum: ~43.3; first_fit: ~124.884
36+
file_path = Path(data_dir_path, "belgium", "multidepot-timewindowed", "air", "belgium-tw-d5-n500-k20.vrp") # optimum: ~43.3; first_fit: ~124.884
3737
#file_path = Path(data_dir_path, "belgium", "multidepot-timewindowed", "air", "belgium-tw-d8-n1000-k40.vrp") # optimum: ~58.1; first_fit: ~154.565
38-
file_path = Path(data_dir_path, "belgium", "multidepot-timewindowed", "air", "belgium-tw-d10-n2750-k55.vrp") # optimum: ~111; first_fit: ~380.9
38+
#file_path = Path(data_dir_path, "belgium", "multidepot-timewindowed", "air", "belgium-tw-d10-n2750-k55.vrp") # optimum: ~111; first_fit: ~380.9
3939

4040
domain_builder = DomainBuilder(file_path)
4141
cotwin_builder = CotwinBuilder(use_incremental_score_calculator=True, use_greed_init=True)
@@ -58,7 +58,7 @@
5858
migration_frequency=10, termination_strategy=termination_strategy)"""
5959

6060
solver = SolverOOP(domain_builder, cotwin_builder, agent,
61-
ParallelizationBackend.Multiprocessing, LoggingLevel.Info,
61+
ParallelizationBackend.Multiprocessing, LoggingLevel.FreshOnly,
6262
n_jobs=10, score_precision=[0, 0, 0])
6363
solution = solver.solve()
6464

examples/pure_math/minlp/solve_minlp_gj.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@
1717
from greyjack.agents.base.LoggingLevel import LoggingLevel
1818
from greyjack.agents.termination_strategies import *
1919

20-
from examples.pure_math.minlp.gj_files.ann_compressor import *
20+
from examples.pure_math.minlp.gj_files.rsyn0840m import *
2121

2222
if __name__ == "__main__":
2323

2424
math_model = build_math_model()
2525

2626
#termination_strategy = StepsLimit(step_count_limit=1000)
27-
#termination_strategy = TimeSpentLimit(time_seconds_limit=10)
27+
#termination_strategy = TimeSpentLimit(time_seconds_limit=60)
2828
termination_strategy = ScoreNoImprovement(time_seconds_limit=15)
2929
#termination_strategy = ScoreLimit(score_to_compare=[0, 0])
30-
"""agent = TabuSearch(neighbours_count=1000, tabu_entity_rate=0.2,
30+
"""agent = TabuSearch(neighbours_count=1000, tabu_entity_rate=0.8,
3131
mutation_rate_multiplier=1.0, move_probas=[0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
32-
migration_frequency=10, compare_to_global_frequency=1, termination_strategy=termination_strategy)"""
33-
"""agent = GeneticAlgorithm(population_size=512, crossover_probability=0.8, p_best_rate=0.2,
34-
tabu_entity_rate=0.0, mutation_rate_multiplier=1.0, move_probas=[0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
35-
migration_rate=0.00001, migration_frequency=10, termination_strategy=termination_strategy)"""
36-
agent = LateAcceptance(late_acceptance_size=200, tabu_entity_rate=0.0,
32+
compare_to_global_frequency=10, termination_strategy=termination_strategy)"""
33+
agent = GeneticAlgorithm(population_size=256, crossover_probability=0.8, p_best_rate=0.5,
34+
tabu_entity_rate=0.8, mutation_rate_multiplier=1.0, move_probas=[0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
35+
migration_rate=0.00001, migration_frequency=10, termination_strategy=termination_strategy)
36+
"""agent = LateAcceptance(late_acceptance_size=200, tabu_entity_rate=0.8,
3737
mutation_rate_multiplier=1.0, move_probas=[0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
38-
compare_to_global_frequency=1000, termination_strategy=termination_strategy)
39-
"""agent = SimulatedAnnealing(initial_temperature=[1.0, 1.0], cooling_rate=0.9999, tabu_entity_rate=0.2,
40-
mutation_rate_multiplier=None, move_probas=[0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
41-
compare_to_global_frequency=10000, termination_strategy=termination_strategy)"""
38+
compare_to_global_frequency=10000, termination_strategy=termination_strategy)"""
39+
"""agent = SimulatedAnnealing(initial_temperature=[1.0, 1.0], cooling_rate=0.9999, tabu_entity_rate=0.8,
40+
mutation_rate_multiplier=1.0, move_probas=[0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
41+
compare_to_global_frequency=1000, termination_strategy=termination_strategy)"""
4242

4343
solver = SolverPureMath(math_model, agent,
4444
ParallelizationBackend.Multiprocessing, LoggingLevel.FreshOnly,

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "greyjack"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2024"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

greyjack/greyjack/agents/LateAcceptance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(
1414
mutation_rate_multiplier=None,
1515
move_probas=None,
1616
migration_frequency=999_999_999_999, # probably, better use only comparing with global. Need more researching.
17-
compare_to_global_frequency=10, # too often comparing significally decreases common performance for fast-stepping metaheuristics
17+
compare_to_global_frequency=1000, # too often comparing significally decreases common performance for fast-stepping metaheuristics
1818
termination_strategy=None,
1919
):
2020

@@ -36,7 +36,7 @@ def _build_metaheuristic_base(self):
3636
elif isinstance(self.cotwin, MathModel):
3737
self.score_requester = PureMathScoreRequester(self.cotwin)
3838
score_variant = self.cotwin.score_variant
39-
self.cotwin.score_calculator.is_incremental = False #TODO: change to True when I implement incremental score calculation for MathModel
39+
self.cotwin.score_calculator.is_incremental = False # if True, currently works badder. Will try improve later
4040
else:
4141
raise Exception("Cotwin must be either subclass of CotwinBase, either be instance of MathModel")
4242
semantic_groups_dict = self.score_requester.variables_manager.semantic_groups_map.copy()

greyjack/greyjack/agents/SimulatedAnnealing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(
1616
mutation_rate_multiplier=None,
1717
move_probas=None,
1818
migration_frequency=999_999_999,
19-
compare_to_global_frequency=10, # too often comparing significally decreases common performance for fast-stepping metaheuristics
19+
compare_to_global_frequency=1000, # too often comparing significally decreases common performance for fast-stepping metaheuristics
2020
termination_strategy=None,
2121
):
2222

@@ -42,7 +42,7 @@ def _build_metaheuristic_base(self):
4242
elif isinstance(self.cotwin, MathModel):
4343
self.score_requester = PureMathScoreRequester(self.cotwin)
4444
score_variant = self.cotwin.score_variant
45-
self.cotwin.score_calculator.is_incremental = False #TODO: change to True when I implement incremental score calculation for MathModel
45+
self.cotwin.score_calculator.is_incremental = False # if True, currently works badder. Will try improve later
4646
else:
4747
raise Exception("Cotwin must be either subclass of CotwinBase, either be instance of MathModel")
4848
semantic_groups_dict = self.score_requester.variables_manager.semantic_groups_map.copy()

greyjack/greyjack/agents/TabuSearch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(
1313
tabu_entity_rate,
1414
mutation_rate_multiplier=None,
1515
move_probas=None,
16-
migration_frequency=None,
16+
migration_frequency=999_999_999,
1717
compare_to_global_frequency=1, # Tabu is usually not too fast-stepping due to high neighbours_count
1818
termination_strategy=None,
1919
):
@@ -39,7 +39,7 @@ def _build_metaheuristic_base(self):
3939
elif isinstance(self.cotwin, MathModel):
4040
self.score_requester = PureMathScoreRequester(self.cotwin)
4141
score_variant = self.cotwin.score_variant
42-
self.cotwin.score_calculator.is_incremental = False #TODO: change to True when I implement incremental score calculation for MathModel
42+
self.cotwin.score_calculator.is_incremental = False # if True, currently works badder. Will try improve later
4343
else:
4444
raise Exception("Cotwin must be either subclass of CotwinBase, either be instance of MathModel")
4545

greyjack/greyjack/agents/base/GJSolution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def __init__(self, variable_names, discrete_ids, variable_values, score_list, sc
66

77
if discrete_ids is not None:
88
for discrete_id in discrete_ids:
9-
variable_values[discrete_id] = int(variable_values[discrete_id])
9+
variable_values[discrete_id] = int(np.rint(variable_values[discrete_id]))
1010

1111
if score_precision is not None:
1212
for i in range(len(score_list)):

greyjack/greyjack/pure_math/MathModel.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
from pprint import pprint
33
from greyjack.score_calculation.scores.ScoreVariants import ScoreVariants
4+
import random
5+
import numpy as np
46

57
class MathModel():
68

@@ -16,6 +18,7 @@ def __init__(self):
1618
self.score_variant = ScoreVariants.HardSoftScore
1719
self.score_type = None
1820
self.score_calculator = ScoreCalculatorStub()
21+
self.variable_to_constraint_index = None
1922

2023
def get_individual_hard_scores(self, absolute):
2124

@@ -64,7 +67,48 @@ def get_sum_soft_score(self, is_fitting):
6467

6568
return sum_soft_score
6669

67-
#def update_model_by_solution(self, gj_solution):
70+
def _build_variable_to_constraint_index(self, variables_manager, var_name_to_arr_id_map, discrete_var_names):
71+
72+
min_trials_count = 100
73+
variable_to_constraint_index = {}
74+
for var_name in self.variables.keys():
75+
76+
variable_to_constraint_index[var_name] = set()
77+
78+
value_before = self.variables[var_name]
79+
already_checked_values = set()
80+
current_trials_count = 0
81+
different_value_checked = False
82+
while current_trials_count < min_trials_count or not different_value_checked:
83+
84+
value_after = variables_manager.get_column_random_value(var_name_to_arr_id_map[var_name])
85+
if var_name in discrete_var_names:
86+
value_after = int(np.rint(value_after))
87+
if value_after == value_before:
88+
continue
89+
else:
90+
different_value_checked = True
91+
92+
if value_after in already_checked_values:
93+
current_trials_count += 1
94+
continue
95+
else:
96+
already_checked_values.add(value_after)
97+
98+
for constraint_name in self.constraints.keys():
99+
penalty_before = self.constraints[constraint_name].get_hard_score(self.variables, self.utility)
100+
self.variables[var_name] = value_after
101+
penalty_after = self.constraints[constraint_name].get_hard_score(self.variables, self.utility)
102+
self.variables[var_name] = value_before
103+
104+
if abs(penalty_before) - abs(penalty_after) != 0.0:
105+
variable_to_constraint_index[var_name].add(constraint_name)
106+
107+
current_trials_count += 1
108+
109+
return variable_to_constraint_index
110+
111+
68112

69113

70114
def explain_solution(self, gj_solution):
@@ -94,6 +138,8 @@ def explain_solution(self, gj_solution):
94138

95139
pass
96140

141+
142+
97143
# filthy way to not rewrite core OOP Agent code
98144
class ScoreCalculatorStub():
99145
def __init__(self):

0 commit comments

Comments
 (0)