-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
292 lines (267 loc) · 10.4 KB
/
app.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import sys
import algorithms
import route_settings
from battery import Battery
from house import House
from route import Route
import matplotlib.pyplot as plt
import helpers
import matplotlib.animation as animation
import numpy as np
ROUTE_COST = 9
def main():
try:
map = sys.argv[1]
except IndexError:
print("Please specify map parameter")
sys.exit(1)
try:
algorithm = sys.argv[2]
except IndexError:
print("Please specify algorithm parameter")
sys.exit(1)
try:
hillclimb = sys.argv[3]
except IndexError:
print("Please specify hillclimb")
sys.exit(1)
try:
runs = int(sys.argv[4])
except IndexError:
print("Please specify amount of runs")
# Choose battery and house map
switcher = {
"1": ("Data/wijk1_batterijen.txt", "Data/wijk1_huizen.txt"),
"2": ("Data/wijk2_batterijen.txt", "Data/wijk2_huizen.txt"),
"3": ("Data/wijk3_batterijen.txt", "Data/wijk3_huizen.txt")
}
try:
houses = import_houses(switcher[map][1])
route_settings.init()
batteries = import_batteries(switcher[map][0], houses)
except IOError:
print("Can't open files")
sys.exit(1)
results = []
# Run algorithm of choice
if algorithm == "1":
print("Connecting houses using random algorithm")
for _ in range(runs):
connected = algorithms.connect_basic(batteries, houses)
if connected:
apply_hillclimb(hillclimb, batteries, houses)
results.append(calculate_costs(batteries))
if runs > 1:
clear_routes(batteries)
elif algorithm == "2":
print("Connecting houses using greedy algorithm")
for _ in range(runs):
connected = algorithms.connect_greedy(batteries, houses)
apply_hillclimb(hillclimb, batteries, houses)
results.append(calculate_costs(batteries))
if runs > 1:
clear_routes(batteries)
elif algorithm == "3":
print("Connecting houses using constraint relaxation algorithm")
for _ in range(runs):
connected = algorithms.constraint_relaxation(batteries, houses)
apply_hillclimb(hillclimb, batteries, houses)
results.append(calculate_costs(batteries))
# re-add houses, as they seem to disappear
for battery in batteries:
for route in battery.get_routes():
houses.append(route.get_house())
if runs > 1:
clear_routes(batteries)
else:
print("We haven't implemented that yet")
sys.exit(1)
print(min(results))
# Print relevant info and plot
if runs == 1:
if connected:
print("All houses connected")
else:
print("Some houses not connected")
print("Houses crossed by routes =",
helpers.check_cross_houses(houses, batteries))
print("Total costs:",
calculate_costs(batteries))
visualize(batteries, houses)
plt.show()
def apply_hillclimb(hillclimb, batteries, houses):
# Apply hillclimbing
try:
if hillclimb == "0":
print("Don't apply hillclimb")
elif hillclimb == "1":
print("Applying hillclimb")
algorithms.hillclimb(batteries, houses)
else:
print("Hillclimb parameter should be 0 or 1")
except IndexError:
print("Please specify hillclimb parameter")
sys.exit(1)
def import_batteries(file, houses):
"""
Imports batteries from map, or places them based on house clusters
"""
batteries = []
try:
with open(file, "r") as f:
coordinates = algorithms.change_batteries(houses)
prompt = input("wilt u de batterijen verplaatsen met k-means of 17 clusters plaatsen? y / n / k ?")
id = 0
for line in f:
lines = line.split(',')
x_battery = None
if "y" in prompt:
for house in houses:
center1 = round(coordinates[id][0], 0)
center2 = round(coordinates[id][1], 0)
if house.get_x() == center1 \
and house.get_y() == center2:
center1 += 1
center2 += 1
x_battery = center1
y_battery = center2
elif "n" in prompt:
x_battery = lines[0]
y_battery = lines[1]
<<<<<<< HEAD
if x_battery is not None:
max_input = lines[2].strip()
max_input = float(max_input)
battery = Battery(id, x_battery, y_battery, max_input)
batteries.append(battery)
id += 1
=======
max_input = lines[2].strip()
max_input = float(max_input)
battery = Battery(id, x_battery, y_battery, max_input)
batteries.append(battery)
id += 1
>>>>>>> refs/remotes/origin/master
if "k" in prompt:
coordinates = algorithms.change_batteries1(houses)
for i in range(0, len(coordinates)):
max_input = 1800
max_input = float(max_input)
for house in houses:
center1 = round(coordinates[i][0], 0)
center2 = round(coordinates[i][1], 0)
if house.get_x() == center1 and house.get_y() == center2:
center1 += 1
center2 += 1
x_battery = center1
y_battery = center2
<<<<<<< HEAD
print(x_battery, y_battery)
=======
>>>>>>> refs/remotes/origin/master
battery = Battery(i, x_battery, y_battery, max_input)
batteries.append(battery)
except IOError:
print("Couldn't open battery file")
sys.exit(1)
return batteries
def import_houses(file):
houses = []
try:
with open(file, "r") as f:
id = 0
for line in f:
house_info = line.split(',')
x_house = int(house_info[0])
y_house = int(house_info[1])
out_put = float(house_info[2])
house = House(id, x_house, y_house, out_put)
houses.append(house)
id += 1
except IOError:
print("Couldn't open house file")
sys.exit(1)
return houses
def calculate_costs(batteries):
cost = 0
for battery in batteries:
for route in battery.routes:
cost += route.get_length()*ROUTE_COST
return cost
def visualize(batteries, houses):
# Show the route in a grid
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
plt.grid()
for house in houses:
plt.plot(house.get_x(), house.get_y(),
'o', color='black', markersize=2)
# Iterate over the batteries to find the route with the corresponding house
for battery in batteries:
plt.plot(battery.get_x(), battery.get_y(),
'X', color='black', markersize=12)
for route in battery.get_routes():
# Check if the route selected is optimal, or if the house has
# a battery closer by
length = route.get_length()
optimal = True
for battery in batteries:
test = Route(route.get_house(), battery)
if test.get_length() < length:
optimal = False
if optimal:
routes = [(tup1, tup2) for tup1,
tup2 in route.get_coordinates()]
plt.plot(*zip(*routes), linewidth=1, linestyle='solid',
marker='o', markersize=1, color='blue')
plt.pause(0.1)
else:
routes = [(tup1, tup2) for tup1,
tup2 in route.get_coordinates()]
plt.plot(*zip(*routes), linewidth=1, linestyle='solid',
marker='o', markersize=1, color='red')
plt.pause(0.1)
def clear_routes(batteries):
for battery in batteries:
battery.clear_all()
<<<<<<< HEAD
def save(batteries, houses):
plt.rcParams['animation.ffmpeg_path'] = r'C:\Users\Joost Bankras\Anaconda3\Lib\site-packages\ffmpeg-20190527-3da8d04-win64-static\bin\ffmpeg.exe'
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
fig2 = plt.figure()
ims = []
ims1 = []
for house in houses:
plt.plot(house.get_x(), house.get_y(),
'o', color='black', markersize=2)
# Iterate over the batteries to find the route with the corresponding house
for battery in batteries:
plt.plot(battery.get_x(), battery.get_y(),
'X', color='black', markersize=12)
for route in battery.get_routes():
# Check if the route selected is optimal, or if the house has
# a battery closer by
length = route.get_length()
optimal = True
for battery in batteries:
test = Route(route.get_house(), battery)
if test.get_length() < length:
optimal = False
if optimal:
routes = [(tup1, tup2) for tup1,
tup2 in route.get_coordinates()]
ims.append(plt.plot(*zip(*routes), linewidth=1, linestyle='solid',
marker='o', markersize=1, color='blue'))
else:
routes = [(tup1, tup2) for tup1,
tup2 in route.get_coordinates()]
ims.append(plt.plot(*zip(*routes), linewidth=1, linestyle='solid',
marker='o', markersize=1, color='red'))
ims1.append(ims)
im_ani = animation.ArtistAnimation(fig2, ims, interval=300, frames=1)
im_ani.save('im1.mp4', writer=writer)
=======
>>>>>>> refs/remotes/origin/master
if __name__ == "__main__":
main()