def Create_water(grid): water_options = [1, 2, 3, 4] water_bodies = 1 #random.choice(water_options) if water_bodies > 1: water_coordinates = MakeWater(water_bodies) for body in range(water_bodies): if water_coordinates != None: if body == 0: grid = Area().update_grid(grid, water_coordinates[body], "water") elif body > 0: if Area().watercheck(grid, water_coordinates[body]) == True: grid = Area().update_grid(grid, water_coordinates[body], "water") else: return None elif water_bodies == 1: water_coordinates = MakeWater(water_bodies) if water_coordinates != None: grid = Area().update_grid(grid, water_coordinates[0], "water") return water_coordinates
def create_water(grid): # create 1-4 bodies of water water_options = [1, 2, 3, 4] water_bodies = random.choice(water_options) # if more than 1 water body check if all the water bodies fit if water_bodies > 1: water_coordinates = make_water(water_bodies) for body in range(water_bodies): if water_coordinates != None: if body == 0: grid = Area().update_grid(grid, water_coordinates[body], "water") elif body > 0: if Area().water_check(grid, water_coordinates[body]) == True: grid = Area().update_grid(grid, water_coordinates[body], "water") else: return None # if only 1 water body place this elif water_bodies == 1: water_coordinates = make_water(water_bodies) if water_coordinates != None: grid = Area().update_grid(grid, water_coordinates[0], "water") # return the water coordinates return water_coordinates
def rotate_house(coordinate_list, nr_of_houses, grid): # randomly pick one house from the coordinate list rotate_house = randint(0, (nr_of_houses -1)) cord_rotate_house = coordinate_list[rotate_house] # determine height and width of the houses width = cord_rotate_house[2] - cord_rotate_house[0] height = cord_rotate_house[1] - cord_rotate_house[3] # set coordinates for rotated house x_l = cord_rotate_house[0] y_u = cord_rotate_house[1] x_r = cord_rotate_house[0] + height y_d = cord_rotate_house[1] - width # set new coordinates new_cord = [x_l, y_u, x_r, y_d, cord_rotate_house[4]] # determine space needed first if cord_rotate_house[4] == 1: build = single elif cord_rotate_house[4] == 2: build = bungalow elif cord_rotate_house[4] == 3: build = maison cord = (cord_rotate_house[0], cord_rotate_house[1]) space_cords = build(cord).spacehouse(cord_rotate_house) # clear the space the houses were using if Area().create_space(space_cords, grid) == True: # determine space needed for new houses if new_cord[4] == 1: build = single elif new_cord[4] == 2: build = bungalow elif new_cord[4] == 3: build = maison cord = (new_cord[0], new_cord[1]) new_space_cords = build(cord).spacehouse(new_cord) # check if there is enough space to place house if Area().housecheck(grid, new_cord) == True: # check if there is space to place house with extra space if Area().spacecheck(grid, new_space_cords) == True: # if everything is true swap the houses grid = Area().update_grid(grid, new_cord, "house") coordinate_list[rotate_house] = new_cord # if the houses were swapped return the grid information return [coordinate_list, grid]
def give_worth(self, house_coordinates, grid): space_vertical = Area().calculate_space_vertical( house_coordinates, grid) space_horizontal = Area().calculate_space_horizontal( house_coordinates, grid) if space_horizontal != None and space_vertical != None: if space_horizontal < space_vertical: space = space_horizontal else: space = space_vertical extra_space = (int(space - self.space)) / 2 price = self.price * (1 + (self.percentage * extra_space)) return price
def random_algoritme(nr_of_houses, distribution): # create water coordinates for the grid water_coordinates = None while water_coordinates == None: total_value = 0 grid = Area().make_basic_grid() water_coordinates = create_water(grid) # after water is succesful determine house distribution # and build the grid if water_coordinates != None: if distribution == "A": coordinate_list = build_amstelhaege(nr_of_houses, grid) elif distribution == "B": coordinate_list = random_amstelhaege(nr_of_houses, grid) else: print("Enter A or B") # determine worth of the houses build for cordinate in coordinate_list: cord = (cordinate[0], cordinate[1]) build = house_type(cordinate[4]) price = build(cord).give_worth(cordinate, grid) if price != None: total_value += price # return the neighbourhood specifics return ([coordinate_list, water_coordinates, total_value, grid])
def move_house(coordinate_list, nr_of_houses, grid): # randomly pick one house from the coordinate list move_house = randint(0, (nr_of_houses -1)) cord_move_house = coordinate_list[move_house] new_cord = move(cord_move_house) # determine space needed first build = house_type(cord_move_house[4]) cord = (cord_move_house[0], cord_move_house[1]) space_cords = build(cord).space_house(cord_move_house) # determine space needed after move build = house_type(new_cord[4]) cord = (new_cord[0], new_cord[1]) new_space_cords = build(cord).space_house(new_cord) if (new_space_cords[0] < 0 or new_space_cords[3] < 0 or new_space_cords[1] > 320 or new_space_cords[2] > 360): return None # clear the space the house was using grid = Area().create_space(space_cords, grid) # check if there is enough space to place house if Area().house_check(grid, new_cord) == True: # check if there is space to place house with extra space if Area().space_check(grid, new_space_cords) == True: # if everything is true move the house grid = reset(grid, new_cord, new_space_cords) coordinate_list[move_house] = new_cord else: grid = reset(grid, cord_move_house, space_cords) return None else: grid = reset(grid, cord_move_house, space_cords) return None # if the house was moved return the grid information return [coordinate_list, grid, cord_move_house, space_cords, move_house, new_space_cords]
def Set_house_in_list(build, cord, coordinate_list, grid): # create coordinates for house house_coordinates = build(cord).coordinates_house() if house_coordinates != None: # if house coordinates are given create coordinates for house with space space_coordinates = build(cord).spacehouse(house_coordinates) # check room for house if Area().housecheck(grid, house_coordinates) == True: # check room for house with space around if Area().spacecheck(grid, space_coordinates) == True: coordinate_list.append(house_coordinates) # set house in grid grid = Area().update_grid(grid, house_coordinates, "house") grid = Area().update_grid(grid, space_coordinates, "space") return True # if not enough room for house or space return false elif Area().spacecheck(grid, space_coordinates) != True: return False elif Area().housecheck(grid, house_coordinates) != True: return False
def cancel_change(current_coordinate_list, grid, old_house_cords, old_space_cords, coordinate_number, new_space_cords): if len(old_house_cords) == 2 and len(new_space_cords) == 2: grid = Area().create_space(new_space_cords[0], grid) grid = Area().create_space(new_space_cords[1], grid) grid = reset(grid, old_house_cords[0], old_space_cords[0]) grid = reset(grid, old_house_cords[1], old_space_cords[1]) current_coordinate_list[coordinate_number[0]] = old_house_cords[0] current_coordinate_list[coordinate_number[1]] = old_house_cords[1] elif len(new_space_cords) == 1: grid = Area().create_space(new_space_cords[0], grid) grid = reset(grid, old_house_cords[0], old_space_cords[0]) grid = reset(grid, old_house_cords[1], old_space_cords[1]) current_coordinate_list[coordinate_number[0]] = old_house_cords[0] current_coordinate_list[coordinate_number[1]] = old_house_cords[1] else: grid = Area().create_space(new_space_cords, grid) grid = reset(grid, old_house_cords, old_space_cords) current_coordinate_list[coordinate_number] = old_house_cords return [current_coordinate_list, grid]
def Build_Amstelhaege(amount, grid): build_single = int(amount * 0.6) build_bungalow = int(amount * 0.25) build_maison = int(amount * 0.15) coordinate_list = [] housecount = 0 # while housecount < amount: # housetype = randint(1, 3) # if housetype == 1: # build = single # if housetype == 2: # build = bungalow # if housetype == 3: # build = maison # cord = Randomizer(1) # if Set_house_in_list(build, cord, coordinate_list, grid) == True: # housecount += 1 while housecount < build_maison: cord = Randomizer(1) build = maison if Set_house_in_list(build, cord, coordinate_list, grid) == True: housecount += 1 while housecount < (build_maison + build_bungalow): cord = Randomizer(1) build = bungalow if Set_house_in_list(build, cord, coordinate_list, grid) == True: housecount += 1 while housecount < amount: cord = Randomizer(1) build = single if Set_house_in_list(build, cord, coordinate_list, grid) == True: housecount += 1 Area().fillgrid(grid) return coordinate_list
def Random(nr_of_houses): # make empty coordinate list water_coordinates = None while water_coordinates == None: total_value = 0 grid = Area().make_basic_grid() water_coordinates = Create_water(grid) if water_coordinates != None: coordinate_list = Build_Amstelhaege(nr_of_houses, grid) for cordinate in coordinate_list: cord = (cordinate[0], cordinate[1]) if cordinate[4] == 1: build = single elif cordinate[4] == 2: build = bungalow elif cordinate[4] == 3: build = maison price = build(cord).giveworth(cordinate, grid) if price != None: total_value += price return ([coordinate_list, water_coordinates, total_value, grid])
def main(): nr_of_houses = int(input("Would you like 20, 40 or 60 houses?")) if nr_of_houses != 20 and nr_of_houses != 40 and nr_of_houses != 60: print("invalid number of houses") exit(0) alg = input("Select A for Random, B for Hill Climber") if alg != "A" and alg != "B": print("this is not what I wanted") exit(0) repeats = int(input("How many times do you want to run the algoritm?")) with open('scores.csv', 'w', newline='') as csvfile: fieldnames = ['algoritme', 'score', 'housecount'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) start = time.time() best_gridvalues = [] if alg == "A": for repeat in range(repeats): gridvalue = Random(int(nr_of_houses)) writer.writeheader() writer.writerow({ 'algoritme': 'Random', 'score': gridvalue[2], 'housecount': nr_of_houses }) if len(best_gridvalues) != 0: if best_gridvalues[2] > gridvalue[2]: pass else: best_gridvalues = gridvalue else: best_gridvalues = gridvalue coordinate_list = best_gridvalues[0] water_coordinates = best_gridvalues[1] total_value = best_gridvalues[2] end = time.time() print(end - start) Area().makegrid(coordinate_list, water_coordinates, total_value) if alg == "B": for repeat in range(repeats): final = HillClimber(nr_of_houses) writer.writeheader() writer.writerow({ 'algoritme': 'HillClimber', 'score': final[2], 'housecount': nr_of_houses }) if len(best_gridvalues) != 0: if best_gridvalues[2] > final[2]: pass else: best_gridvalues = final else: best_gridvalues = final coordinate_list = final[0] water_coordinates = final[1] total_value = final[2] end = time.time() print(end - start) Area().makegrid(coordinate_list, water_coordinates, total_value)
def main(): # ask user for nr of houses they want nr_of_houses = int(input("Would you like 20, 40 or 60 houses? ")) if nr_of_houses != 20 and nr_of_houses != 40 and nr_of_houses != 60: print("invalid number of houses") exit(0) best_gridvalues = [] # ask what algoritm they want alg = input( "Select A for Random, B for Hill Climber, C for Simulated Annealing, or D for Kerkhof algoritm " ) if alg != "A" and alg != "B" and alg != "C" and alg != "D": print("this is not what I wanted") exit(0) # if uses picks A, run random algoritm elif alg == "A": distribution = input( "Required (A) or random (B) distribution of houses? ") if distribution != "A" and distribution != "B": print("Choose a distribution please") exit(0) starttime = time.time() gridvalue = random_algoritme(nr_of_houses, distribution) # if uses picks B or C, choose starting grid elif alg == "B" or alg == "C": starting_state = (input("Is starting state Random(A) or Kerkhof(B)? ")) if starting_state == "A": distribution = input( "Required (A) or random (B) distribution of houses? ") starttime = time.time() start = random_algoritme(nr_of_houses, distribution) # if startinf state is B then start with kerkhof elif starting_state == "B": starttime = time.time() start = kerkhof(nr_of_houses) else: print("Enter A or B for starting state") exit(0) print("start value: {}".format(start[2])) # if B then run hill climber if alg == "B": gridvalue = hill_climber(nr_of_houses, start) # if C then run simulated annealing elif alg == "C": gridvalue = simulated_annealing(nr_of_houses, start) # if D then run kerkhof algoritm elif alg == "D": starttime = time.time() gridvalue = kerkhof(nr_of_houses) end = time.time() coordinate_list = gridvalue[0] water_coordinates = gridvalue[1] total_value = gridvalue[2] # print runtime, value of grid and grid print("time: {}".format(end - starttime)) print("final value: {}".format(total_value)) Area().make_grid(coordinate_list, water_coordinates, total_value)
def house_swap(coordinate_list, nr_of_houses, grid): # randomly pick two houses from the coordinate list house_one = randint(0, (nr_of_houses -1)) house_two = randint(0, (nr_of_houses - 1)) if house_one == house_two: return None # look at old coordinates old_cords_one = coordinate_list[house_one] old_cords_two = coordinate_list[house_two] # create new coordinates new_cord_one = create_coordinates(old_cords_two, old_cords_one[4], 1, 0) new_cord_two = create_coordinates(old_cords_one, old_cords_two[4], 1, 0) # create space coordinates of old coordinates build = house_type(old_cords_one[4]) cord_one = (old_cords_one[0], old_cords_one[1]) space_old_cords_one = build(cord_one).space_house(old_cords_one) build = house_type(old_cords_two[4]) cord_two = (old_cords_two[0], old_cords_two[1]) space_old_cords_two = build(cord_two).space_house(old_cords_two) # create coordinates with space for new coordinates build = house_type(new_cord_one[4]) cord_one_new = (new_cord_one[0], new_cord_one[1]) new_space_cords_one = build(cord_one_new).space_house(new_cord_one) # check if space is in grid if (new_space_cords_one[0] < 0 or new_space_cords_one[3] < 0 or new_space_cords_one[1] > 320 or new_space_cords_one[2] > 360): return None # also for the second house build = house_type(new_cord_two[4]) cord_two_new = (new_cord_two[0], new_cord_two[1]) new_space_cords_two = build(cord_two_new).space_house(new_cord_two) if (new_space_cords_two[0] < 0 or new_space_cords_two[3] < 0 or new_space_cords_two[1] > 320 or new_space_cords_two[2] > 360): return None # clear the space the houses were using grid = Area().create_space(space_old_cords_one, grid) grid = Area().create_space(space_old_cords_two, grid) # check if there is enough space to place house if Area().house_check(grid, new_cord_one) == True: if Area().space_check(grid, new_space_cords_one) == True: grid = reset(grid, new_cord_one, new_space_cords_one) coordinate_list[house_one] = new_cord_two if Area().house_check(grid, new_cord_two) == True: if Area().space_check(grid, new_space_cords_two) == True: grid = reset(grid, new_cord_two, new_space_cords_two) coordinate_list[house_two] = new_cord_one else: cancel = cancel_change(coordinate_list, grid, [old_cords_one, old_cords_two], [space_old_cords_one, space_old_cords_two], [house_one, house_two], [new_space_cords_one]) coordinate_list = cancel[0] grid = cancel[1] return None else: cancel = cancel_change(coordinate_list, grid, [old_cords_one, old_cords_two], [space_old_cords_one, space_old_cords_two], [house_one, house_two], [new_space_cords_one]) coordinate_list = cancel[0] grid = cancel[1] return None else: grid = reset(grid, old_cords_one, space_old_cords_one) grid = reset(grid, old_cords_two, space_old_cords_two) return None else: grid = reset(grid, old_cords_one, space_old_cords_one) grid = reset(grid, old_cords_two, space_old_cords_two) return None # if the houses were swapped return the grid information return [coordinate_list, grid, [old_cords_one, old_cords_two], [space_old_cords_one, space_old_cords_two], [house_one, house_two], [new_space_cords_one, new_space_cords_two]]
def reset(grid, house_coordinates, space_coordinates): grid = Area().update_grid(grid, house_coordinates, "house") grid = Area().update_grid(grid, space_coordinates, "space") return grid
def kerkhof(nr_of_houses): # initialising empty grid grid = Area().make_basic_grid() # if 20 houses are chosen if nr_of_houses == 20: # placing water water = 1 water_coordinates = [[135, 272, 225, 14]] # checking if there is place for water for body in range(water): if Area().water_check(grid, water_coordinates[body]) == True: grid = Area().update_grid(grid, water_coordinates[body], "water") location_space = [] location_list = [] total_value = 0 # placing 9 singles for i in range(9): size = Single([0, 0]).give_size() # choosing coordinates house_next = [ 24 + (21 + size[1]) * i, 300, 24 + size[1] + (21 + size[1]) * i, 300 - size[0], 1 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Single(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 3 singles for i in range(3): size = Single([0, 0]).give_size() # choosing coordinates house_next = [ 24 + (21 + size[1]) * i, 36, 24 + size[1] + (21 + size[1]) * i, 36 - size[0], 1 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Single(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 1 bungalow size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [28, 170, 28 + size[1], 170 - size[0], 2] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 2 bungalows for i in range(2): size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [ 71, 105 + size[0] + (70 + size[0]) * i, 71 + size[1], 105 + (70 + size[0]) * i, 2 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 2 bungalows for i in range(2): size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [ 295, 30 + size[0] + (size[0] + 82) * i, 295 + size[1], 30 + (size[0] + 82) * i, 2 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 1 maison size = Maison([0, 0]).give_size() # choosing coordinates house_next = [135 - size[1], 149 + size[0], 135, 171 - size[0], 3] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Maison(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 2 maisons for i in range(2): size = Maison([0, 0]).give_size() # choosing coordinates house_next = [ 225, 80 + size[0] + (size[0] + 80) * i, 225 + size[1], 80 + (size[0] + 80) * i, 3 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Maison(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # determining worth for coordinate in location_list: cords = [coordinate[0], coordinate[1]] build = house_type(coordinate[4]) price = build(cords).give_worth(coordinate, grid) if price != None: total_value += price return ([location_list, water_coordinates, total_value, grid]) # if 40 houses are chosen if nr_of_houses == 40: # placing water water = 2 water_coordinates = [[36, 294, 164, 204], [193, 120, 321, 30]] # checking if there is place for water for body in range(water): if Area().water_check(grid, water_coordinates[body]) == True: grid = Area().update_grid(grid, water_coordinates[body], "water") location_space = [] location_list = [] total_value = 0 # placing 16 singles for i in range(8): for j in range(2): size = Single([0, 0]).give_size() # choosing coordinates house_next = [ 20 + (size[1] + 27) * i, 310 - 280 * j, 20 + size[1] + (27 + size[1]) * i, 310 - size[0] - 280 * j, 1 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Single(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 8 singles for i in range(4): for j in range(2): size = Single([0, 0]).give_size() # choosing coordinates house_next = [ 20 + 301 * j, 280 - (size[0] + 55) * i, 20 + size[1] + 301 * j, 280 - size[0] - (55 + size[0]) * i, 1 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Single(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 6 bungalows for i in range(3): for j in range(2): size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [ 321 - 300 * j, 250 - (size[0] + 55) * i, 321 + size[1] - 300 * j, 250 - size[0] - (55 + size[0]) * i, 2 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 2 bungalows for i in range(2): size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [ 101 + 140 * i, 204 - 64 * i, 101 + size[1] + 140 * i, 204 - size[0] - 64 * i, 2 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 1 bungalow size = Bungalow([0, 0]).give_size() house_next = [165, 259, 165 + size[1], 259 - size[0], 2] cords = [house_next[0], house_next[1]] if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 1 bungalow size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [193 - size[1], 85, 193, 85 - size[0], 2] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 6 maisons for i in range(6): size = Maison([0, 0]).give_size() # choosing coordinates house_next = [ 65 + (size[1] + 20) * i, 65 + (size[0] + 20) * i, 65 + size[1] + (20 + size[1]) * i, 65 - size[0] + (20 + size[0]) * i, 3 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Maison(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # determining worth for coordinate in location_list: cords = [coordinate[0], coordinate[1]] build = house_type(coordinate[4]) price = build(cords).give_worth(coordinate, grid) if price != None: total_value += price return ([location_list, water_coordinates, total_value, grid]) # if 60 houses are chosen if nr_of_houses == 60: # placing water water = 4 water_coordinates = [[28, 224, 73, 96], [286, 224, 331, 96], [116, 74, 244, 29], [116, 291, 244, 246]] # checking if there is place for water for body in range(water): if Area().water_check(grid, water_coordinates[body]) == True: grid = Area().update_grid(grid, water_coordinates[body], "water") location_space = [] location_list = [] total_value = 0 # placing 24 singles for i in range(12): for j in range(2): size = Single([0, 0]).give_size() # choosing coordinates house_next = [ 12 + 29 * i, 307 - 278 * j, 12 + size[1] + 29 * i, 307 - 278 * j - size[0], 1 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Single(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 12 singles for i in range(6): for j in range(2): size = Single([0, 0]).give_size() # choosing coordinates house_next = [ 331 - 319 * j, 29 + 26 + size[0] + (23 + size[0]) * i, 331 - 319 * j + size[1], 29 + 26 + (size[0] + 23) * i, 1 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Single(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 4 bungalows for i in range(2): for j in range(2): size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [ 244 + 44 * j, 224 + size[0] + (14 + size[0]) * i, 244 + 44 * j + size[1], 224 + (14 + size[0]) * i, 2 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 4 bungalows for i in range(2): for j in range(2): size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [ 244 + 44 * j, 29 + 13 + size[0] + (14 + size[0]) * i, 244 + 44 * j + size[1], 29 + 13 + (14 + size[0]) * i, 2 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 4 bungalows for i in range(2): for j in range(2): size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [ 57 + (29 + size[1]) * i, 96 - (14 + size[0]) * j, 57 + size[1] + (29 + size[1]) * i, 96 - size[0] - (14 + size[0]) * j, 2 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 2 bungalows for i in range(2): size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [ 57 + (29 + size[1]) * i, 244, 57 + size[1] + (29 + size[1]) * i, 244 - size[0], 2 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 1 bungalow size = Bungalow([0, 0]).give_size() # choosing coordinates house_next = [ 86 + size[1], 278, 86 + size[1] + size[1], 278 - size[0], 2 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Bungalow(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 1 maison size = Maison([0, 0]).give_size() # choosing coordinates house_next = [170, 171, 170 + size[1], 171 - size[0], 3] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Maison(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 4 maisons for i in range(2): for j in range(2): size = Maison([0, 0]).give_size() # choosing coordinates house_next = [ 145 + (size[1] + 28) * i, 246 - 150 * j, 145 + size[1] + (size[1] + 28) * i, 246 - size[0] - 150 * j, 3 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Maison(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # placing 4 maisons for i in range(2): for j in range(2): size = Maison([0, 0]).give_size() # choosing coordinates house_next = [ 73 + 192 * j, 196 - (size[0] + 28) * i, 73 + size[1] + 192 * j, 196 - size[0] - (size[0] + 28) * i, 3 ] cords = [house_next[0], house_next[1]] # checking if there is place for house if Area().house_check(grid, house_next) == True: space = Maison(cords).space_house(house_next) # checking if there is place for space if Area().space_check(grid, space) == True: # if all true place house and space location_list.append(house_next) grid = Area().update_grid(grid, house_next, "house") location_space.append(space) grid = Area().update_grid(grid, space, "space") # determining worth for coordinate in location_list: cords = [coordinate[0], coordinate[1]] build = house_type(coordinate[4]) price = build(cords).give_worth(coordinate, grid) if price != None: total_value += price return ([location_list, water_coordinates, total_value, grid])
def house_swap(coordinate_list, nr_of_houses, grid): # randomly pick two houses from the coordinate list house_one = randint(0, (nr_of_houses -1)) house_two = randint(0, (nr_of_houses - 1)) house_cords_one = coordinate_list[house_one] house_cords_two = coordinate_list[house_two] # determine height and width of the houses width_one = house_cords_one[2] - house_cords_one[0] height_one = house_cords_one[1] - house_cords_one[3] width_two = house_cords_two[2] - house_cords_two[0] height_two = house_cords_two[1] - house_cords_two[3] # create new coordinates for the houses # take the left upper corner # determine new coordinates with height and width x_l_one = house_cords_two[0] y_u_one = house_cords_two[1] x_r_one = house_cords_two[0] + width_one y_d_one = house_cords_two[1] - height_one new_cord_one = [x_l_one, y_u_one, x_r_one, y_d_one, house_cords_one[4]] x_l_two = house_cords_one[0] y_u_two = house_cords_one[1] x_r_two = house_cords_one[0] + width_two y_d_two = house_cords_one[1] - height_two new_cord_two = [x_l_two, y_u_two, x_r_two, y_d_two, house_cords_two[4]] # determine the space needed for old houses if house_cords_one[4] == 1: build = single elif house_cords_one[4] == 2: build = bungalow elif house_cords_one[4] == 3: build = maison cord = (x_l_one, y_u_one) space_cords_one = build(cord).spacehouse(new_cord_one) if house_cords_two[4] == 1: build = single elif house_cords_two[4] == 2: build = bungalow elif house_cords_two[4] == 3: build = maison cord = (x_l_two, y_u_two) space_cords_two = build(cord).spacehouse(new_cord_two) # clear the space the houses were using if Area().create_space(space_cords_one, grid) == True: if Area().create_space(space_cords_two, grid) == True: # determine space needed for new houses if new_cord_one[4] == 1: build = single elif new_cord_one[4] == 2: build = bungalow elif new_cord_one[4] == 3: build = maison cord = (x_l_one, y_d_one) space_cords_one = build(cord).spacehouse(new_cord_one) if new_cord_two[4] == 1: build = single elif new_cord_two[4] == 2: build = bungalow elif new_cord_two[4] == 3: build = maison cord = (x_l_two, y_d_two) space_cords_two = build(cord).spacehouse(new_cord_two) # check if there is enough space to place house if Area().housecheck(grid, new_cord_one) == True: if Area().housecheck(grid, new_cord_two) == True: # check if there is space to place house with extra space if Area().spacecheck(grid, space_cords_one) == True: if Area().spacecheck(grid, space_cords_two) == True: # if everything is true swap the houses grid = Area().update_grid(grid, new_cord_one, "house") coordinate_list[house_one] = new_cord_two grid = Area().update_grid(grid, new_cord_two, "house") coordinate_list[house_two] = new_cord_one # if the houses were swapped return the grid information return [coordinate_list, grid]
def move_house(coordinate_list, nr_of_houses, grid): # randomly pick one house from the coordinate list move_house = randint(0, (nr_of_houses -1)) cord_move_house = coordinate_list[move_house] # choose in which direction the house will move direction = randint(0, 3) # if direction is 0 move left if direction == 0: x_l = cord_move_house[0] - 2 y_u = cord_move_house[1] x_r = cord_move_house[2] - 2 y_d = cord_move_house[3] # if direction is 1 move right elif direction == 1: x_l = cord_move_house[0] + 2 y_u = cord_move_house[1] x_r = cord_move_house[2] + 2 y_d = cord_move_house[3] # if direction is 2 move up elif direction == 2: x_l = cord_move_house[0] y_u = cord_move_house[1] + 2 x_r = cord_move_house[2] y_d = cord_move_house[3] + 2 # if direction is 3 move down elif direction == 3: x_l = cord_move_house[0] y_u = cord_move_house[1] - 2 x_r = cord_move_house[2] y_d = cord_move_house[3] - 2 # set new coordinates new_cord = [x_l, y_u, x_r, y_d, cord_move_house[4]] # determine space needed first if cord_move_house[4] == 1: build = single elif cord_move_house[4] == 2: build = bungalow elif cord_move_house[4] == 3: build = maison cord = (cord_move_house[0], cord_move_house[1]) space_cords = build(cord).spacehouse(cord_move_house) # clear the space the houses were using if Area().create_space(space_cords, grid) == True: # determine space needed for new houses if new_cord[4] == 1: build = single elif new_cord[4] == 2: build = bungalow elif new_cord[4] == 3: build = maison cord = (new_cord[0], new_cord[1]) new_space_cords = build(cord).spacehouse(new_cord) # check if there is enough space to place house if Area().housecheck(grid, new_cord) == True: # check if there is space to place house with extra space if Area().spacecheck(grid, new_space_cords) == True: # if everything is true swap the houses grid = Area().update_grid(grid, new_cord, "house") coordinate_list[move_house] = new_cord # if the houses were swapped return the grid information return [coordinate_list, grid]