-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_balans.py
More file actions
86 lines (73 loc) · 3.11 KB
/
main_balans.py
File metadata and controls
86 lines (73 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# ALNS for adaptive large neighborhood
from alns.accept import SimulatedAnnealing
from alns.select import MABSelector
from alns.stop import MaxIterations, MaxRuntime
# MABWiser for contextual multi-armed bandits
from mabwiser.mab import LearningPolicy
# Balans meta-solver for solving mixed integer programming problems
from balans.solver import Balans, DestroyOperators, RepairOperators
import numpy as np
# Destroy operators
destroy_ops = [DestroyOperators.Crossover,
# DestroyOperators.Dins,
DestroyOperators.Mutation_25,
DestroyOperators.Mutation_50,
DestroyOperators.Mutation_75,
DestroyOperators.Local_Branching_10,
DestroyOperators.Local_Branching_25,
DestroyOperators.Local_Branching_50,
DestroyOperators.Proximity_005,
DestroyOperators.Proximity_015,
DestroyOperators.Proximity_030,
# DestroyOperators.Random_Objective
DestroyOperators.Rens_25,
DestroyOperators.Rens_50,
DestroyOperators.Rens_75,
DestroyOperators.Rins_25,
DestroyOperators.Rins_50,
DestroyOperators.Rins_75]
# Repair operators
repair_ops = [RepairOperators.Repair]
# Rewards
reward_best, reward_better, reward_accept, reward_reject = 1, 1, 0, 0
# Bandit selector
selector = MABSelector(scores=[reward_best, reward_better, reward_accept, reward_reject],
num_destroy=len(destroy_ops),
num_repair=len(repair_ops),
learning_policy=LearningPolicy.ThompsonSampling())
# Acceptance criterion
# accept = HillClimbing()
accept = SimulatedAnnealing(start_temperature=20, end_temperature=1, step=0.1)
# Stopping condition
# stop = MaxRuntime(300)
stop = MaxIterations(10)
# Balans
# To use the default config shipped with the package:
# from balans.utils import ConfigFactory
# balans = Balans(config=ConfigFactory.DEFAULT_CONFIG_PATH)
balans = Balans(destroy_ops=destroy_ops,
repair_ops=repair_ops,
selector=selector,
accept=accept,
stop=stop,
seed=1283,
n_mip_jobs=4,
mip_solver="scip",
timelimit_first_solution=10,
timelimit_alns_iteration=5,
timelimit_local_branching_iteration=10,
timelimit_crossover_random_feasible=5,
big_m=1000)
# Run
instance_path = "tests/data/correctness_mk_5_maximize.lp"
result = balans.solve(instance_path)
# print("Best solution:", result.best_state.solution())
print("Best objective:", result.best_state.objective())
print("Objective trace:", result.statistics.objectives)
# print("Runtime trace:", result.statistics.runtimes)
print("Runtime trace (cumulative):", np.cumsum(result.statistics.runtimes))
print("Operator to reward (best, better, accept, reject) counts:", dict(result.statistics.destroy_operator_counts))
# The installed CLI entry point lives in balans/main.py.
# from balans.main_balans import main
# if __name__ == "__main__":
# main()