-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver_2OPT_fromFullPickle_input.py
191 lines (160 loc) · 6.67 KB
/
solver_2OPT_fromFullPickle_input.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
from parse import read_input_file, write_output_file
import os
import random
import math
from exp_utils import get_logger
from Task import Task
import datetime
import numpy as np
import pickle
import time
# random.seed(123)
work_dir = "./logs"
now = datetime.datetime.now()
logging = get_logger(os.path.join(work_dir, now.strftime('%Y-%m-%d %H:%M:%S') + ' log.txt'))
total_benefit = 0
def solve(tasks, input_path):
"""
Args:
tasks: list[Task], list of igloos to polish
Returns:
output: list of igloos in order of polishing
"""
############################################## CONFIG ##############################################
global opt_dict
global full_opt_dict
MAX_TIME = 1440
# opt = opt_dict.get(input_path, [None, float('-inf')])
# best_plan = opt[0]
# best_plan_benefit = opt[1]
####################################################################################################
epoch_idx = 0
def fitness(output_tasks, tasks):
assert len(output_tasks) == len(set(output_tasks)), "output_tasks contain duplicates!"
MAX_TIME = 1440
time_cum = 0
benefit_cum = 0
idx = 0
while idx < len(output_tasks) and time_cum + tasks[output_tasks[idx] - 1].duration <= MAX_TIME:
id = output_tasks[idx] - 1
time_cum = time_cum + tasks[id].duration
if time_cum <= tasks[id].deadline:
benefit_cum += tasks[id].perfect_benefit
else:
benefit_cum += tasks[id].perfect_benefit * math.exp(-0.0170 * (time_cum - tasks[id].deadline))
idx += 1
return benefit_cum
def postprocessing(output_tasks, tasks):
idx = 0
MAX_TIME = 1440
time_cum = 0
processed_output_taskId = []
while idx < len(output_tasks) and time_cum + tasks[output_tasks[idx] - 1].duration <= MAX_TIME:
id = output_tasks[idx] - 1
time_cum = time_cum + tasks[id].duration
processed_output_taskId.append(tasks[id].task_id)
idx += 1
return processed_output_taskId
unchanged_iteration = 0
count = 0
############################## Initial Input ################################################
opt = opt_dict.get(input_path, [None, float('-inf')])
best_plan = opt[0]
best_plan_benefit = opt[1]
curr_output_tasks = best_plan
############################## TO CHANGE ####################################################
start = time.time()
early_abort_epoch = 20
while True:
curr_benefit = fitness(curr_output_tasks, tasks)
exit_curr_loop = False
i, j, k = random.sample(range(1, len(tasks)), 3)
curr_output_tasks[i], curr_output_tasks[j], curr_output_tasks[k] = curr_output_tasks[k], curr_output_tasks[i], curr_output_tasks[j]
while True:
curr_benefit = fitness(curr_output_tasks, tasks)
for i in range(len(tasks)):
for j in range(i+1, len(tasks)):
less_raito = tasks[curr_output_tasks[j]-1].get_benefit_over_duration_ratio() < tasks[curr_output_tasks[i]-1].get_benefit_over_duration_ratio()
later_ddl = tasks[curr_output_tasks[j]-1].deadline > tasks[curr_output_tasks[i]-1].deadline
if less_raito and later_ddl:
continue
new_output_task = curr_output_tasks[:]
new_output_task[i], new_output_task[j] = new_output_task[j], new_output_task[i]
new_benefit = fitness(new_output_task, tasks)
if new_benefit > curr_benefit:
curr_output_tasks = new_output_task
curr_benefit = new_benefit
exit_curr_loop = True
break
if exit_curr_loop:
break
if exit_curr_loop == False:
break
epoch_idx += 1
exit_curr_loop = False
if curr_benefit > best_plan_benefit:
best_plan_benefit = curr_benefit
best_plan = curr_output_tasks
unchanged_iteration = 0
else:
unchanged_iteration += 1
if unchanged_iteration > early_abort_epoch:
break
end = time.time()
elapsed = end - start
count = count + 1
print(f"{count}. epoch: {epoch_idx} benefit: {curr_benefit} time: {elapsed} best: {best_plan_benefit}")
epoch_idx = 0
end = time.time()
elapsed = end - start
full_opt_dict[input_path] = (best_plan[:], best_plan_benefit)
best_plan = postprocessing(best_plan, tasks)
opt_dict[input_path] = (best_plan, best_plan_benefit)
print(f"benefit: {best_plan_benefit} time: {elapsed}")
return best_plan, best_plan_benefit
inputs_categories = ["large", "medium", "small"]
# print(os.listdir('inputs/'))
# Load optimal output
opt_dict = {}
if os.path.exists("optimum_output.pickle"):
with open("optimum_output.pickle", "rb") as f:
opt_dict = pickle.load(f)
full_opt_dict = {}
if os.path.exists("full_optimum_output.pickle"):
with open("full_optimum_output.pickle") as f:
full_opt_dict = pickle.load(f)
task_idx = 0
for inputs_category in inputs_categories:
for file_name in os.listdir(os.path.join('inputs/', inputs_category)):
if file_name[0] == ".":
continue
input_path = 'inputs/' + inputs_category + "/" + file_name
print(f"task {task_idx}: {input_path}")
output_path = 'outputs/' + inputs_category + "/" + file_name[:-3] + '.out'
tasks = read_input_file(input_path)
output, benefit = solve(tasks, input_path)
total_benefit = total_benefit + benefit
write_output_file(output_path, output)
task_idx += 1
# task_idx = 0
# inputs_category = "large"
# file_name = "large-1.in"
# input_path = 'inputs/' + inputs_category + "/" + file_name
# print(f"task {task_idx}: {input_path}")
# output_path = 'outputs/' + inputs_category + "/" + file_name[:-3] + '.out'
# tasks = read_input_file(input_path)
# output, benefit = solve(tasks, input_path)
# total_benefit = total_benefit + benefit
write_output_file(output_path, output)
logging(str(total_benefit))
with open('optimum_output.pickle', 'wb') as f:
pickle.dump(opt_dict, f)
with open('full_optimum_output.pickle', 'wb') as f:
pickle.dump(full_opt_dict, f)
# Here's an example of how to run your solver.
# if __name__ == '__main__':
# for input_path in os.listdir('inputs/'):
# output_path = 'outputs/' + input_path[:-3] + '.out'
# tasks = read_input_file(input_path)
# output = solve(tasks)
# write_output_file(output_path, output)