-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrash_test.py
More file actions
78 lines (64 loc) · 2.55 KB
/
Copy pathcrash_test.py
File metadata and controls
78 lines (64 loc) · 2.55 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
# Compares the pulp solver solutions with solutions from bruteforce solver
from problem import Problem
from solver import PulpSolver
import numpy as np
import itertools
import time
class BruteforceSolver:
def __init__(self):
self.elapsed = 0
def solve(self, p: Problem): #-> tuple(list[int], float):
self.elapsed -= time.time()
# initialize starting solution
best_schedule = None
best_reward = None
# calculate the max possible value of the total reward, this is
# used to stop the bruteforce loop if this values is reached
max_possible_value = np.sum([p.p[i] for i in range(p.nProducts) if p.t[i] < p.d[i]])
# main loop - enumerate all possible schedules
schedule0 = list(range(p.nProducts))
for schedule in itertools.permutations(schedule0):
sched_as_list = list(schedule)
if not p.checkSchedule(sched_as_list):
continue
cur_reward = p.objectiveFcn(sched_as_list)
if best_reward is None or cur_reward > best_reward:
# update maximum
best_reward = cur_reward
best_schedule = sched_as_list
if best_reward == max_possible_value:
break
self.elapsed += time.time()
return best_schedule, best_reward
if __name__ == '__main__':
# number of samples
Nsamples = 10000
# fix seed for debug purposes
np.random.seed(1)
seeds = np.random.randint(0, 1000000000, Nsamples)
# create both solvers and start crash test
bruteforceSolver = BruteforceSolver()
pulpSolver = PulpSolver()
for n in range(Nsamples):
print(f"seeds[{n}] = {seeds[n]}")
np.random.seed(seeds[n])
# create random problem
p = Problem()
p.randomize()
print(p)
# call brute-force solver
schedule_0, reward_0 = bruteforceSolver.solve(p)
print(schedule_0)
print(f"objective: {reward_0}")
# call pulp solver
schedule, reward = pulpSolver.solve(p)
pulpSolver.debugPrint(p)
# compare solutions
assert p.checkSchedule(schedule)
print(f"objective: {p.objectiveFcn(schedule)}")
assert np.isclose(reward, reward_0, 1e-10, 1e-10)
print("\n\n")
# print statistics
print("All tests are Ok")
print(f"Bruteforce solver time: {bruteforceSolver.elapsed}")
print(f"PuLP solver time: {pulpSolver.elapsed}")