-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfer.py
More file actions
76 lines (48 loc) · 1.71 KB
/
infer.py
File metadata and controls
76 lines (48 loc) · 1.71 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
"""
For measuring the performance of baseline heuristics to compare to our models.
"""
import gym
from spicy_env.env_v2 import dantzigs_rule, steepest_edge_rule, random_rule
from tqdm import tqdm
import argparse
import json
from pathlib import Path
def do_inference(model, data_dir):
env = gym.make('spicy-v0', data_dir=data_dir, heuristic = False, full_tableau = True, return_raw_state = True, sort_files = True)
test_set_size = len(env.data_files)
all_rewards = []
for i in tqdm(range(test_set_size)):
total_reward = 0
initial_state = env.reset()
action = model.predict(initial_state)
done = False
while not done:
(state, reward, done, info) = env.step(action)
total_reward += reward
if not done:
action = model.predict(state)
all_rewards.append(total_reward)
return all_rewards
class Predictor:
def __init__(self, rule):
self.rule = rule
def predict(self, state):
return self.rule(state)
BASELINES = {
"dantzig" : Predictor(dantzigs_rule),
"steepest_edge" : Predictor(steepest_edge_rule),
"random" : Predictor(random_rule)
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--baseline", choices=BASELINES.keys(), required=True)
parser.add_argument("--out", required=True)
parser.add_argument("--data_dir", type=str, required=True)
args = parser.parse_args()
model = BASELINES[args.baseline]
results = do_inference(model, args.data_dir)
print("Mean result: ", sum(results) / len(results))
with open(args.out, "w") as file:
json.dump(results, file)
if __name__ == "__main__":
main()