Skip to content

Commit efe8fbb

Browse files
committed
v0.2.0 API for pure (explicit) math models (classic solvers-like)
1 parent d6d1973 commit efe8fbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+9016
-118
lines changed

examples/object_oriented/nqueens/persistence/DomainBuilderNQueens.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ def build_domain_from_scratch(self):
3636
return chess_field
3737

3838
def build_from_solution(self, solution, initial_domain=None):
39-
raise Exception("build_from_solution() not implemented for DomainBuilderNQueens")
39+
domain = self.build_domain_from_scratch()
40+
for i, row_id in enumerate( solution.variable_values_dict.values() ):
41+
domain.queens[i].row.row_id = row_id
42+
return domain
43+
4044

4145
def build_from_domain(self, domain):
4246
return super().build_from_domain(domain)

examples/object_oriented/nqueens/scripts/solve_nqueens.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
from examples.object_oriented.nqueens.persistence.CotwinBuilderNQueens import CotwinBuilderNQueens
1313
from greyjack.agents.termination_strategies import *
1414
from greyjack.agents import *
15-
from greyjack.Solver import Solver
15+
from greyjack.SolverOOP import SolverOOP
1616
from greyjack.agents.base.LoggingLevel import LoggingLevel
1717
from greyjack.agents.base.ParallelizationBackend import ParallelizationBackend
1818
from greyjack.agents import *
1919

2020
if __name__ == "__main__":
2121

2222
# build domain model
23-
domain_builder = DomainBuilderNQueens(1024, random_seed=45)
23+
domain_builder = DomainBuilderNQueens(16, random_seed=45)
2424
cotwin_builder = CotwinBuilderNQueens(use_incremental_score_calculator=True)
2525

2626
#termination_strategy = StepsLimit(step_count_limit=1000)
@@ -40,11 +40,14 @@
4040
mutation_rate_multiplier=None, move_probas=[0, 1, 0, 0, 0, 0],
4141
migration_frequency=10, compare_to_global_frequency=10, termination_strategy=termination_strategy)"""
4242

43-
solver = Solver(domain_builder, cotwin_builder, agent,
43+
solver = SolverOOP(domain_builder, cotwin_builder, agent,
4444
ParallelizationBackend.Multiprocessing, LoggingLevel.FreshOnly,
4545
n_jobs=10, score_precision=[0])
4646
solution = solver.solve()
4747
#print( "Cotwin solution looks that: " )
4848
#print( solution )
4949

50+
domain = domain_builder.build_from_solution(solution)
51+
print(domain)
52+
5053
print( "done" )

examples/object_oriented/nqueens/scripts/solve_nqueens_ortools.py

Lines changed: 0 additions & 77 deletions
This file was deleted.

examples/object_oriented/tsp/score/IncrementalScoreCalculatorTSP.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def all_in_one_constraint(self, planning_entity_dfs, problem_fact_dfs, delta_dfs
2323
# with huge performance boost on large datasets
2424

2525
# from experiments: common performance up to ~2 times lower (depends on dataset size)
26-
# than in fully Rust version of Solver. Need to keep in mind the opportunity of
26+
# than in fully Rust version of SolverOOP. Need to keep in mind the opportunity of
2727
# rewriting constraints on Rust in Python version for production (if it's neccessary).
2828

2929
path_stops_df = planning_entity_dfs["path_stops"]

examples/object_oriented/tsp/scripts/solve_tsp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from greyjack.agents.termination_strategies import *
1515
from greyjack.agents.base.LoggingLevel import LoggingLevel
1616
from greyjack.agents.base.ParallelizationBackend import ParallelizationBackend
17-
from greyjack import Solver
17+
from greyjack import SolverOOP
1818
from greyjack.agents import *
1919

2020
if __name__ == "__main__":
@@ -53,7 +53,7 @@
5353
mutation_rate_multiplier=None, move_probas=[0, 0.2, 0.2, 0.2, 0.2, 0.2],
5454
migration_frequency=10, termination_strategy=termination_strategy)"""
5555

56-
solver = Solver(domain_builder, cotwin_builder, agent,
56+
solver = SolverOOP(domain_builder, cotwin_builder, agent,
5757
ParallelizationBackend.Multiprocessing, LoggingLevel.Info,
5858
n_jobs=10, score_precision=[0, 0])
5959
solution = solver.solve()

examples/object_oriented/vrp/scripts/solve_vrp.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from greyjack.agents.base.ParallelizationBackend import ParallelizationBackend
1616
from greyjack.agents.base.LoggingLevel import LoggingLevel
1717
from greyjack.agents.termination_strategies import *
18-
from greyjack.Solver import Solver
18+
from greyjack.SolverOOP import SolverOOP
1919
from greyjack.agents import *
2020

2121
if __name__ == "__main__":
@@ -33,18 +33,18 @@
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)
4242

4343
#termination_strategy = StepsLimit(step_count_limit=1000)
44-
#termination_strategy = TimeSpentLimit(time_seconds_limit=60)
45-
termination_strategy = ScoreNoImprovement(time_seconds_limit=15)
44+
termination_strategy = TimeSpentLimit(time_seconds_limit=60)
45+
#termination_strategy = ScoreNoImprovement(time_seconds_limit=15)
4646
#termination_strategy = ScoreLimit(score_to_compare=[0])
47-
agent = TabuSearch(neighbours_count=1280, tabu_entity_rate=0.8,
47+
agent = TabuSearch(neighbours_count=128, tabu_entity_rate=0.8,
4848
mutation_rate_multiplier=None, move_probas=[0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
4949
migration_frequency=10, termination_strategy=termination_strategy)
5050
"""agent = GeneticAlgorithm(population_size=128, crossover_probability=0.5, p_best_rate=0.05,
@@ -57,8 +57,8 @@
5757
mutation_rate_multiplier=None, move_probas=[0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
5858
migration_frequency=10, termination_strategy=termination_strategy)"""
5959

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

@@ -76,7 +76,7 @@
7676
agent = LateAcceptance(late_acceptance_size=64, tabu_entity_rate=0.05,
7777
mutation_rate_multiplier=None, move_probas=None,
7878
termination_strategy=termination_strategy)
79-
solver = Solver(domain_builder, cotwin_builder, agent,
79+
solver = SolverOOP(domain_builder, cotwin_builder, agent,
8080
ParallelizationBackend.Multiprocessing, LoggingLevel.FreshOnly,
8181
n_jobs=10, score_precision=[0, 0, 0],
8282
initial_solution=solution)
@@ -100,7 +100,7 @@
100100
agent = TabuSearch(neighbours_count=128, tabu_entity_rate=0.0,
101101
mutation_rate_multiplier=None, move_probas=[0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
102102
migration_frequency=10, termination_strategy=termination_strategy)
103-
solver = Solver(domain_builder, cotwin_builder, agent,
103+
solver = SolverOOP(domain_builder, cotwin_builder, agent,
104104
ParallelizationBackend.Multiprocessing, LoggingLevel.FreshOnly,
105105
n_jobs=10, score_precision=[0, 0, 0],
106106
initial_solution=domain)

0 commit comments

Comments
 (0)