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 constraint_relaxation(batteries, houses): """ Keeps connecting the closest house and battery, then switches routes until constraints are satisfied """ houses_local = houses batteries_local = batteries # distances contains a number of lists, one for each battery, # containing tuples of houses and distances to the corresponding battery distances = [] for battery in batteries_local: unsorted = [] for house in houses_local: route = Route(house, battery) unsorted.append((route.get_length(), house)) sorted_houses = countSort2(unsorted) distances.append(sorted_houses) while len(houses_local) > 0: closest = distances[0][0] id = 0 for i in range(len(distances)): # check the first element of eacht list if it is closer than the # previous one if len(distances[i]) > 0: if distances[i][0][0] < closest[0]: closest = distances[i][0] id = i # connect the closest house batteries_local[id].connect_house(closest[1]) houses_local.remove(closest[1]) for d in distances: for tuple in d: if tuple[1] == closest[1]: d.remove(tuple) return apply_constraints(batteries_local)
def check_switch(route1, route2): battery1 = route1.get_battery() battery2 = route2.get_battery() house1 = route1.get_house() house2 = route2.get_house() length1 = route1.get_length() length2 = route2.get_length() total_length1 = length1 + length2 route3 = Route(house2, battery1) route4 = Route(house1, battery2) length3 = route3.get_length() length4 = route4.get_length() total_length2 = length3 + length4 change = total_length1 - total_length2 cap_left1 = battery1.get_capacity() - battery1.get_used_cap() \ + house1.get_output() cap_left2 = battery2.get_capacity() - battery2.get_used_cap() \ + house2.get_output() if total_length2 < total_length1 and house2.get_output() \ < cap_left1 and house1.get_output() < cap_left2: return True, change else: return False
def connect_greedy(batteries, houses): """ Goes through each battery, adding the most nearby houses if possible """ random.shuffle(houses) batteries = batteries for house in houses: sorted_batteries = [] for battery in batteries: route = Route(house, battery) cap_left = battery.get_capacity() - battery.get_used_cap() if house.get_output() < cap_left: sorted_batteries.append((route.get_length(), battery)) if len(sorted_batteries) != 0: sorted_batteries = countSort2(sorted_batteries) sorted_batteries[0][1].connect_house(house) return len(houses) == 150
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)