-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_parser.py
133 lines (111 loc) · 4.88 KB
/
dataset_parser.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
import os
from abc import ABCMeta
import numpy as np
import pandas as pd
import glob
STEPS = 3
CUR_STEP = 2
class DataserParser(metaclass=ABCMeta):
def __init__(self, input_file, output_file):
self.path = input_file#
self.result = output_file#
df = pd.read_csv(self.path, header=None, sep=" ")
matrix = df.as_matrix()
self.rows, self.cols, self.n_vehicles, self.rides, self.bonus, self.steps = matrix[0]
self.ride_information = np.copy(matrix[1:,:])
diff = (self.ride_information[:, 5] - self.ride_information[:, 4])[:,None]
self.ride_information = np.append(self.ride_information, diff, axis=1)
df = pd.DataFrame(self.ride_information, columns=list('abxysfd'))
df = df.sort_values(['d', 's'], ascending=[True, True])
self.ride_information = df.as_matrix()
self.ride_index = df.index.get_values()
self.solution = dict()
for v in range(self.n_vehicles):
self.solution[v] = [0]
self.curr_pos = np.zeros((self.n_vehicles, 4), dtype=int)
self.curr_pos[:, STEPS] = self.curr_pos[:, STEPS] + self.steps
def get_distances(self, rindex, v):
c = self.curr_pos[v, 0:2]
s = self.ride_information[rindex, 0:2]
f = self.ride_information[rindex, 2:4]
man = np.abs(c - s) + np.abs(s - f)
distance = np.sum(man)
if self.curr_pos[v, CUR_STEP] < self.ride_information[rindex, 4]:
distance += self.ride_information[rindex, 4] - self.curr_pos[v, CUR_STEP]
near = np.sum(np.abs(c - s))
return distance, near
def start(self):
rindex = 0
#print("rides {}".format(self.rides))
#print(self.ride_information.shape)
# steps_remaining = self.steps
while rindex < self.rides:
ride_assigned = False
#print("in {}".format(rindex))
for v in range(self.n_vehicles):
c = self.curr_pos[v, 0:2]
s = self.ride_information[rindex, 0:2]
f = self.ride_information[rindex, 2:4]
man = np.abs(c - s) + np.abs(s - f)
distance = np.sum(man)
if self.curr_pos[v, CUR_STEP] < self.ride_information[rindex, 4]:
distance += self.ride_information[rindex, 4] - self.curr_pos[v, CUR_STEP]
# print(c)
# print(s)
# print(f)
if distance < self.curr_pos[v, STEPS]:
self.solution[v][0] += 1
self.solution[v].append(self.ride_index[rindex])
self.curr_pos[v, 0:2] = self.ride_information[rindex, 2:4]
self.curr_pos[v, CUR_STEP] += distance
self.curr_pos[v, STEPS] -= distance
ride_assigned = True
rindex += 1
#print("assign {}".format(rindex))
if rindex >= self.rides:
#print("breaking")
break
if not ride_assigned:
rindex+=1
#print("not assign {}".format(rindex))
if rindex >= self.rides:
#print("breaking out")
break
print("Solution \n {}".format(self.solution))
#print(self.curr_pos)
def start2(self):
for rindex in range(self.rides):
#print("in {}".format(rindex))
nearest = np.inf
vindex = np.inf
leastTravel = np.inf
for v in range(self.n_vehicles):
distance, near = self.get_distances(rindex, v)
if near < nearest and distance < self.curr_pos[v, STEPS]:
vindex = v
nearest = near
if vindex!=np.inf:
distance, near = self.get_distances(rindex, vindex)
self.solution[vindex][0] += 1
self.solution[vindex].append(self.ride_index[rindex])
self.curr_pos[vindex, 0:2] = self.ride_information[rindex, 2:4]
self.curr_pos[vindex, CUR_STEP] += distance
self.curr_pos[vindex, STEPS] -= distance
#print("assign {}".format(rindex))
print("Solution \n {}".format(self.solution))
#print(self.curr_pos)
def save_solution(self):
file = open(self.result, 'w')
for k,v in self.solution.items():
myString = ' '.join([str(x) for x in v])
myString = myString + "\n"
file.write(myString)
file.close()
if __name__ == '__main__':
for file in glob.glob(os.path.join(os.getcwd(), "data2","*.in")):
output_file = file.replace(".in", ".out")
print("output File {}".format(output_file))
dr = DataserParser(input_file=file, output_file=output_file)
dr.start2()
dr.save_solution()
print("########################################")