def createBoard(self, boardLength, boardHeight, n_houses, n_batteries): """ """ batteryList = [] houseList = [] housePositionList = [] for x in range(n_houses): newPosition = [randint(0,boardLength), randint(0,boardHeight)] for x in housePositionList: if (tuple(x) == tuple(newPosition)): newPosition[0] = (newPosition[0] + randint(-2,2)) % boardLength newPosition[1] = (newPosition[1] + randint(-2,2)) % boardHeight housePositionList.append(newPosition) for x in range(n_houses): houseList.append(solarHouse.solarpanelHouse("house" + str(x), randint(5,10), position = housePositionList[x])) batteryPositionList =[] for x in range(n_batteries): newPosition = [randint(0,boardLength), randint(0,boardHeight)] for x in batteryPositionList: if (tuple(x) == tuple(newPosition)): newPosition[0] = (newPosition[0] + 1) % boardLength newPosition[1] = (newPosition[1] + 1) % boardHeight for x in housePositionList: if (tuple(x) == tuple(newPosition)): newPosition[0] = (newPosition[0] + randint(-2,2)) % boardLength newPosition[1] = (newPosition[1] + randint(-2,2)) % boardHeight batteryPositionList.append(newPosition) Battery.createBatteries(n_batteries, 1000, batteryPositionList, [], batteryList, houseList) # batteryList.append(Battery.battery( position = [randint(0, boardLength), randint(0, boardHeight)] , "A", 500, [], False)) return houseList, batteryList
def __init__(self, postion): self.boundaryX = 3 self.boundaryY = 5 self.house1 = solarHouse.solarpanelHouse( list(np.array([1, 1]) + position)) self.house2 = solarHouse.solarpanelHouse( list(np.array([1, 2]) + position)) self.house3 = solarHouse.solarpanelHouse( list(np.array([1, 3]) + position)) self.house4 = solarHouse.solarpanelHouse( list(np.array([1, 4]) + position)) self.house5 = solarHouse.solarpanelHouse( list(np.array([2, 1]) + position)) self.house6 = solarHouse.solarpanelHouse( list(np.array([2, 2]) + position)) self.house7 = solarHouse.solarpanelHouse( list(np.array([2, 3]) + position)) self.house8 = solarHouse.solarpanelHouse( list(np.array([2, 4]) + position))
def k_means(n_Batteries, n_Houses, boardLength, boardHeight): # initiation somethingChanged = True houseList = [] batteryList = [] # build batteries for x in xrange(n_Batteries): newBatt = Battery.battery() newBatt.name = 'Battery : ' + str(x) newBatt.position = [ randint(boardLength / 4, 3 * (boardLength / 4)), randint(boardHeight / 4, 3 * (boardHeight / 4)) ] batteryList.append(newBatt) # build houses for x in xrange(n_Houses): newHouse = solarHouse.solarpanelHouse() newHouse.position = [randint(0, boardLength), randint(0, boardHeight)] houseList.append(newHouse) it = 0 while (somethingChanged): it += 1 # assignment of houses for house in houseList: distances = [] for battery in batteryList: distances.append( manhattenDistance(battery.position, house.position)) index = np.argmin(distances) house.batteryAssignment = batteryList[index] # relocating batteries changedlist = [] for battery in batteryList: # save old location oldPosition = tuple(battery.position) # initiate values for new battery-position n = 0 positionSum = np.array([0, 0]) # check per house if it is assignet to current battery for house in houseList: # if so, count its position in if (house.batteryAssignment == battery): n += 1 positionSum += house.position battery.position = list(positionSum / n) # save wether the battery moved since last time if (tuple(battery.position) == oldPosition): changedlist.append(True) else: changedlist.append(False) # stop if no battery changed if (all(i == True for i in changedlist)): somethingChanged = False print it return houseList, batteryList