-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart4.py
186 lines (151 loc) Β· 6.73 KB
/
part4.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
import math
import robot as r
import part2
def run(inputDir, outputDir, filename):
robots, locations1 = loadData(inputDir, filename)
outputPath(robots, locations1, outputDir, filename)
def loadData(inputDir, filename):
f = open(inputDir + "/" + filename, "r")
num_robots = 0
num_locations = 0
num_robots, num_locations, num_obstacles = f.readline().split()
num_robots = int(num_robots)
num_locations = int(num_locations)
robots = [] # (name, moveEff, cleanEff)
for rbt in range(num_robots):
robots.append(f.readline().split())
locations = {}
for i in range(num_locations):
location = f.readline().split() # (x, y, timeRequired)
location.insert(0, i)
location[1] = int(location[1])
location[2] = int(location[2])
location[3] = int(location[3])
key = str(location[1])+" "+str(location[2])
if locations.get(key):
prev_loc = locations[key]
locations[key][3] = max(location[3], prev_loc[3])
else:
locations[key] = location
return robots, locations
def outputPath(robot_list, original_location_list, outputDir, filename):
robot_energies = []
for index, next_robot in enumerate(robot_list):
total_energy = 0
robot = r.Robot(next_robot[0], index, int(next_robot[2]))
locations_list = list(original_location_list.values()).copy()
# Goes through each location until all have been cleaned
while len(locations_list) > 0:
target = locations_list[0] # [index, x, y, time]
# Set target to closest location
for location in locations_list:
dist_location = calculate_distance(robot.x, robot.y, location[1], location[2])
dist_target = calculate_distance(robot.x, robot.y, target[1], target[2])
if dist_location < dist_target:
target = location.copy()
print("Num locations left: {0}".format(len(locations_list)))
print(calculate_distance(robot.x, robot.y, target[1], target[2]))
while calculate_distance(robot.x, robot.y, target[1], target[2]) > 0:
delta_x = target[1]-robot.x;
delta_y = target[2]-robot.y;
if (delta_x > 0 and delta_y > 0):
robot.x += 1
robot.y += 1
# situation where robot moves diagonal to top right
total_energy += robot.moveEff
elif (delta_x < 0 and delta_y > 0):
robot.x -= 1
robot.y += 1
#situation where robot moves diagonal to top left
total_energy += robot.moveEff
elif (delta_x > 0 and delta_y < 0):
robot.x += 1
robot.y -= 1
#situation where robot moves diagonal to bottom right
total_energy += robot.moveEff
elif (delta_x < 0 and delta_y < 0):
robot.x -= 1
robot.y -= 1
#situation where robot moves diagonal to bottom left
total_energy += robot.moveEff
elif (delta_x > 0):
robot.x += 1
# situation where robot moves to right
total_energy += robot.moveEff
elif (delta_x < 0):
robot.x -= 1
# situation where robot moves to left
total_energy += robot.moveEff
elif (delta_y > 0):
robot.y += 1
# situation where robot moves up
total_energy += robot.moveEff
elif (delta_y < 0):
robot.y -= 1
# situation where robot moves down
total_energy += robot.moveEff
# Clean target
print(locations_list)
total_energy += target[3] * robot.cleanEff
locations_list.remove(target)
# Store total energy
robot_energies.append(total_energy)
# Now we rank the robots
best_index = 0
best_robot = robot_energies[best_index];
for i, robot_energy in enumerate(robot_energies):
if (best_robot > robot_energy) :
best_robot = robot_energy
best_index = i
print(robot_list[best_index])
cur_pos_x, cur_pos_y = part2.outputPath(robot_list[best_index], original_location_list, outputDir, filename)
# this method returns the x and y position after the robot finishes cleaning
print(cur_pos_x, cur_pos_y)
# Return to origin --> (best_index, 0)
out = open(outputDir + "/" + filename.split(".")[0]+".out.txt", "a")
while calculate_distance(cur_pos_x, cur_pos_y, best_index, 0) > 0:
delta_x = best_index - cur_pos_x;
delta_y = 0 - cur_pos_y;
if (delta_x > 0 and delta_y > 0):
cur_pos_x += 1
cur_pos_y += 1
out.write("move {0} {1}\n".format(cur_pos_x, cur_pos_y))
# situation where robot moves diagonal to top right
elif (delta_x < 0 and delta_y > 0):
cur_pos_x -= 1
cur_pos_y += 1
out.write("move {0} {1}\n".format(cur_pos_x, cur_pos_y))
#situation where robot moves diagonal to top left
elif (delta_x > 0 and delta_y < 0):
cur_pos_x += 1
cur_pos_y -= 1
out.write("move {0} {1}\n".format(cur_pos_x, cur_pos_y))
#situation where robot moves diagonal to bottom right
elif (delta_x < 0 and delta_y < 0):
cur_pos_x -= 1
cur_pos_y -= 1
out.write("move {0} {1}\n".format(cur_pos_x, cur_pos_y))
#situation where robot moves diagonal to bottom left
elif (delta_x > 0):
cur_pos_x += 1
out.write("move {0} {1}\n".format(cur_pos_x, cur_pos_y))
# situation where robot moves to right
elif (delta_x < 0):
cur_pos_x -= 1
out.write("move {0} {1}\n".format(cur_pos_x, cur_pos_y))
# situation where robot moves to left
elif (delta_y > 0):
cur_pos_y += 1
out.write("move {0} {1}\n".format(cur_pos_x, cur_pos_y))
# situation where robot moves up
elif (delta_y < 0):
cur_pos_y -= 1
out.write("move {0} {1}\n".format(cur_pos_x, cur_pos_y))
# situation where robot moves down
out.write("rest \n")
for index, next_robot in enumerate(robot_list):
if (index != best_index):
out.write("\nRobot " + next_robot[0] + "\n")
out.write("rest\n")
def calculate_distance(robot_x, robot_y, location_x, location_y):
return math.sqrt((robot_x - location_x)**2 + (robot_y - location_y)**2)