def setClosestBattery(self): """setClosestBattery. Iterates over all batteries in the district and saves a reference of the closest battery of the same type for each battery. """ for b in self.batteries: b.closestBatteryDistance = 10000 for b2 in self.batteries: distance = manhattan(b, b2) if b.maxCapacity == b2.maxCapacity and distance < b.closestBatteryDistance and not distance == 0: b.closestBattery = b2 b.closestBatteryDistance = manhattan(b, b2)
def connectRandomBattery(self, batteries, district): shuffle(batteries) for battery in batteries: distance = manhattan(self, battery) # Save all possible connections possible_connection = (battery, distance) self.possible_connections.append(possible_connection) # Connect if capacity is enough if battery.capacity > self.output: # Safe distance in House object self.distance = distance # Connect to battery self.connection = battery # Sort back to fix plotting bug batteries.sort(key = lambda x: x.id) if self.connection != possible_connection[0]: district.nthChoiceHouses.append(self) # Catch error if no connection could be made if not self.connection == set(): self.connection.capacity -= self.output # Append reference to houses connected to battery self.connection.connectedHouses.append(self) else: print("Error: House",str(self.id),"COULD NOT BE CONNECTED!") self.connection = "NOT CONNECTED!" district.disconnectedHouses.append(self) # even in district zelf laten maken. district.allConnected = False # Sort list with possible connections self.possible_connections.sort(key = lambda x: x[1])
def changeLocation(self, location): """changeLocation. Changes the location of the battery and recalculates distance for all connected houses. """ self.location = (location[0], location[1]) # Change distance when battery is moved for house in self.connectedHouses: house.distance = manhattan(house, self)
def simultaneousSwitch(house1, house2): """Switches two houses to eachothers batteries""" # 1. Remove house from list of connected houses in battery object house1.connection.connectedHouses.remove(house1) house2.connection.connectedHouses.remove(house2) # 2. "Reset" capacity of batteries house1.connection.capacity += house1.output house2.connection.capacity += house2.output # 3. Change connections house1.connection, house2.connection = house2.connection, house1.connection # 4. Append house to battery list of connected houses house1.connection.connectedHouses.append(house1) house2.connection.connectedHouses.append(house2) # 5. Recalculate capacity of batteries house1.connection.capacity -= house1.output house2.connection.capacity -= house2.output # 6. Recalculate distance house1.distance = manhattan(house1, house1.connection) house2.distance = manhattan(house1, house2.connection)
def switch(house, battery): """Moves one house to another battery""" # 1. Update capacity of old battery if house.connection != "NOT CONNECTED!": house.connection.capacity += house.output # 2. Remove house from list of connected houses in battery object house.connection.connectedHouses.remove(house) # 3. Change connection house.connection = battery # 4. Append house to battery list of connected houses house.connection.connectedHouses.append(house) # 5. Update remaining capacity of new battery house.connection.capacity -= house.output # 6. Recalculate distance house.distance = manhattan(house, house.connection)
def connectNearestBattery(self, batteries, district): """connectNearestBattery. Connects a house to the nearest battery if possible if it fits in the capacity. """ for battery in batteries: distance = manhattan(self, battery) # Save all possible connections possible_connection = (battery, distance) self.possible_connections.append(possible_connection) # Check if distance of this battery is closer than last if distance < self.distance and battery.capacity > self.output: # Safe distance in House object self.distance = distance # Connect to battery self.connection = battery if self.connection != possible_connection[0]: district.nthChoiceHouses.append(self) # Catch error if no connection could be made if self.connection == "NOT CONNECTED!": district.disconnectedHouses.append(self) district.allConnected = False elif not self.connection == set(): #print("connection: ", self.connection) if type(self.connection) is str: print("!!",self.id, self.connection) self.connection.capacity -= self.output # Append reference to houses connected to battery self.connection.connectedHouses.append(self) else: #print("Error: House", str(self.id), "COULD NOT BE CONNECTED!") self.connection = "NOT CONNECTED!" district.disconnectedHouses.append(self) district.allConnected = False