-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtest.py
197 lines (177 loc) · 7.55 KB
/
test.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import copy
import json
import os
import random
import time as time
import gym
import pandas as pd
import torch
import numpy as np
import pynvml
import PPO_model
from env.load_data import nums_detec
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
def main():
# PyTorch initialization
# gpu_tracker = MemTracker() # Used to monitor memory (of gpu)
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if device.type=='cuda':
torch.cuda.set_device(device)
torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
torch.set_default_tensor_type('torch.FloatTensor')
print("PyTorch device: ", device.type)
torch.set_printoptions(precision=None, threshold=np.inf, edgeitems=None, linewidth=None, profile=None, sci_mode=False)
# Load config and init objects
with open("./config.json", 'r') as load_f:
load_dict = json.load(load_f)
env_paras = load_dict["env_paras"]
model_paras = load_dict["model_paras"]
train_paras = load_dict["train_paras"]
test_paras = load_dict["test_paras"]
env_paras["device"] = device
model_paras["device"] = device
env_test_paras = copy.deepcopy(env_paras)
num_ins = test_paras["num_ins"]
if test_paras["sample"]:
env_test_paras["batch_size"] = test_paras["num_sample"]
else:
env_test_paras["batch_size"] = 1
model_paras["actor_in_dim"] = model_paras["out_size_ma"] * 2 + model_paras["out_size_ope"] * 2
model_paras["critic_in_dim"] = model_paras["out_size_ma"] + model_paras["out_size_ope"]
data_path = "./data_test/{0}/".format(test_paras["data_path"])
test_files = os.listdir(data_path)
test_files.sort(key=lambda x: x[:-4])
test_files = test_files[:num_ins]
mod_files = os.listdir('./model/')[:]
memories = PPO_model.Memory()
model = PPO_model.PPO(model_paras, train_paras)
rules = test_paras["rules"]
envs = [] # Store multiple environments
# Detect and add models to "rules"
if "DRL" in rules:
for root, ds, fs in os.walk('./model/'):
for f in fs:
if f.endswith('.pt'):
rules.append(f)
if len(rules) != 1:
if "DRL" in rules:
rules.remove("DRL")
# Generate data files and fill in the header
str_time = time.strftime("%Y%m%d_%H%M%S", time.localtime(time.time()))
save_path = './save/test_{0}'.format(str_time)
os.makedirs(save_path)
writer = pd.ExcelWriter(
'{0}/makespan_{1}.xlsx'.format(save_path, str_time)) # Makespan data storage path
writer_time = pd.ExcelWriter('{0}/time_{1}.xlsx'.format(save_path, str_time)) # time data storage path
file_name = [test_files[i] for i in range(num_ins)]
data_file = pd.DataFrame(file_name, columns=["file_name"])
data_file.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()
writer.close()
data_file.to_excel(writer_time, sheet_name='Sheet1', index=False)
writer_time.save()
writer_time.close()
# Rule-by-rule (model-by-model) testing
start = time.time()
for i_rules in range(len(rules)):
rule = rules[i_rules]
# Load trained model
if rule.endswith('.pt'):
if device.type == 'cuda':
model_CKPT = torch.load('./model/' + mod_files[i_rules])
else:
model_CKPT = torch.load('./model/' + mod_files[i_rules], map_location='cpu')
print('\nloading checkpoint:', mod_files[i_rules])
model.policy.load_state_dict(model_CKPT)
model.policy_old.load_state_dict(model_CKPT)
print('rule:', rule)
# Schedule instance by instance
step_time_last = time.time()
makespans = []
times = []
for i_ins in range(num_ins):
test_file = data_path + test_files[i_ins]
with open(test_file) as file_object:
line = file_object.readlines()
ins_num_jobs, ins_num_mas, _ = nums_detec(line)
env_test_paras["num_jobs"] = ins_num_jobs
env_test_paras["num_mas"] = ins_num_mas
# Environment object already exists
if len(envs) == num_ins:
env = envs[i_ins]
# Create environment object
else:
# Clear the existing environment
meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
if meminfo.used / meminfo.total > 0.7:
envs.clear()
# DRL-S, each env contains multiple (=num_sample) copies of one instance
if test_paras["sample"]:
env = gym.make('fjsp-v0', case=[test_file] * test_paras["num_sample"],
env_paras=env_test_paras, data_source='file')
# DRL-G, each env contains one instance
else:
env = gym.make('fjsp-v0', case=[test_file], env_paras=env_test_paras, data_source='file')
envs.append(copy.deepcopy(env))
print("Create env[{0}]".format(i_ins))
# Schedule an instance/environment
# DRL-S
if test_paras["sample"]:
makespan, time_re = schedule(env, model, memories, flag_sample=test_paras["sample"])
makespans.append(torch.min(makespan))
times.append(time_re)
# DRL-G
else:
time_s = []
makespan_s = [] # In fact, the results obtained by DRL-G do not change
for j in range(test_paras["num_average"]):
makespan, time_re = schedule(env, model, memories)
makespan_s.append(makespan)
time_s.append(time_re)
env.reset()
makespans.append(torch.mean(torch.tensor(makespan_s)))
times.append(torch.mean(torch.tensor(time_s)))
print("finish env {0}".format(i_ins))
print("rule_spend_time: ", time.time() - step_time_last)
# Save makespan and time data to files
data = pd.DataFrame(torch.tensor(makespans).t().tolist(), columns=[rule])
data.to_excel(writer, sheet_name='Sheet1', index=False, startcol=i_rules + 1)
writer.save()
writer.close()
data = pd.DataFrame(torch.tensor(times).t().tolist(), columns=[rule])
data.to_excel(writer_time, sheet_name='Sheet1', index=False, startcol=i_rules + 1)
writer_time.save()
writer_time.close()
for env in envs:
env.reset()
print("total_spend_time: ", time.time() - start)
def schedule(env, model, memories, flag_sample=False):
# Get state and completion signal
state = env.state
dones = env.done_batch
done = False # Unfinished at the beginning
last_time = time.time()
i = 0
while ~done:
i += 1
with torch.no_grad():
actions = model.policy_old.act(state, memories, dones, flag_sample=flag_sample, flag_train=False)
state, rewards, dones = env.step(actions) # environment transit
done = dones.all()
spend_time = time.time() - last_time # The time taken to solve this environment (instance)
# print("spend_time: ", spend_time)
# Verify the solution
gantt_result = env.validate_gantt()[0]
if not gantt_result:
print("Scheduling Error!!!!!!")
return copy.deepcopy(env.makespan_batch), spend_time
if __name__ == '__main__':
main()