示例#1
0
def hill_climber_value(steps, houses_to_place):
    '''
    Finds max profit using Hill Climber. Returns a dictionary with saved data.
    Takes 2 arguments.
    Steps: the amount of steps a hill climber has to make. A steps is defined 
    as a succesful move of a house or water.
    Houses_to_place: 1 for 20 houses, 2 for 40, 3 for 60.
    '''
    
    print("Starting hill climber max value")
    
    # save profit and time
    found_profit_per_run = []
    time_needed = []
    
    # 90% change to move house, 10% change to move water
    water_or_house = 0.9
    
    # place houses
    not_placed = True
    
    # start placing
    while(not_placed):
        print("Initialize")
        init.initialize_matrix(houses_to_place, WIDTH, HEIGTH)
        try:
            matrix, houselist, water = init.initialize_matrix(
                                            houses_to_place, WIDTH, HEIGTH)
            not_placed = False
        except:
            not_placed = True

    # calculate initial profit
    fs.calculate_new_vrijstand_to_class(matrix, houselist)
    result = prof.calculate_value(houselist)
    
    found_profit_per_run.append(result)
            
    # save findings
    state = ss.saved_state(result, houselist, water)
    
    # do the hillclimber
    start_run = time.time()
    runtimes = 0
    while (runtimes < steps):
        
        # update
        if( runtimes % (steps / 100) == 0):
            print(runtimes)

        # make a copy of houselist and water           
        houselist = state.get_houselist_copy()
        water = state.get_water_copy()
        
        # random move water or house
        if(water_or_house > random.random()):
            house = random.randint(0, len(houselist) - 1)
            moved, new_matrix = move.move_house(
                                    matrix, houselist[house], 100, 100)
        else:
            water_pool = random.randint(0, len(water.get_pools()) - 1)
            moved, new_matrix = move.move_water(
                                    matrix, water.get_pool(water_pool), 10, 10)
          
        # if a house or water has succesfully been moved.
        if(moved):
            # calculate new profit
            fs.calculate_new_vrijstand_to_class(new_matrix, houselist)
            new_profit = prof.calculate_value(houselist)
            
            # if profit is more, accept it
            if(new_profit >= state.get_total_value()):
                state.set_houselist(houselist)
                state.set_total_value(new_profit)
                state.set_water(water)
                matrix =  new_matrix
            
            # save found data
            found_profit_per_run.append(state.get_total_value())
            time_needed.append(time.time() - start_run)
            
            # One step is completed
            runtimes += 1

    # save final profit
    final_profit = state.get_total_value()
    
    # done
    end_run = time.time() 
    
    # make dictionary to return data
    return_values = {"matrix": matrix, "profit_per_run":found_profit_per_run, 
                     "time_needed":time_needed, "start_time":start_run, 
                     "end_time":end_run, "final_profit":final_profit}
    return return_values
示例#2
0
def simulated_annealing_vrijstand(houses_to_place):
    '''
    Finds max vrijstand using Simulated annealing. Returns a dictionary with saved data.
    Takes 2 arguments.
    Houses_to_place: 1 for 20 houses, 2 for 40, 3 for 60.
    '''
    
    print("simulated annealing")
    
    # save profit and time
    found_profit_per_run = []
    time_needed = []
    
    # 90% change to move house, 10% change to move water
    water_or_house = 0.9
    
    # place houses
    not_placed = True
    
    # start placing
    while(not_placed):
        try:
            matrix, houselist, water = init.initialize_matrix(
                                            houses_to_place, WIDTH, HEIGTH)
            not_placed = False
        except:
            not_placed = True

    # calculate initial profit
    fs.calculate_new_vrijstand_to_class(matrix, houselist)
    result = prof.calculate_free_space(houselist)
    
    found_profit_per_run.append(result)
            
    # save findings
    state = ss.saved_state(result, houselist, water)
    
    # Start Temp Simulated Annealing
    temperature = 50
    start_temp = temperature
    count = 0.0
    
    # do the simulated annealing
    start_run = time.time()
    while(temperature > 0.08):
        
        # update
        if(count % 100 == 0):
            print(count, temperature)
            
        # make a copy of houselist              
        houselist = state.get_houselist_copy()
        water = state.get_water_copy()
        
         # random move water or house
        if(water_or_house > random.random()):
            house = random.randint(0, len(houselist) - 1)
            moved, new_matrix = move.move_house(matrix, houselist[house], 100, 100)
        else:
            water_pool = random.randint(0, len(water.get_pools()) - 1)
            moved, new_matrix = move.move_water(matrix, water.get_pool(water_pool), 10, 10)
          
        
        # if a house or water has succesfully been moved.
        if(moved):
            # calculate new profit
            fs.calculate_new_vrijstand_to_class(new_matrix, houselist)
            new_profit = prof.calculate_free_space(houselist)
            
            # if more accepts
            if(new_profit >= state.get_total_value()):
                state.set_houselist(houselist)
                state.set_total_value(new_profit)
                state.set_water(water)
                matrix =  new_matrix
            # else random accept depending on temperature
            elif((state.get_total_value() - new_profit) / temperature  < random.random()):
                state.set_houselist(houselist)
                state.set_total_value(new_profit)
                state.set_water(water)
                matrix =  new_matrix
            
            # decrease temperature
            count += 1
            temperature = start_temp * ((0.999) ** abs(count))
            
            # save findings
            found_profit_per_run.append(state.get_total_value())
            time_needed.append(time.time() - start_run)
   

    # final profit
    final_profit = state.get_total_value()
    
    # done
    end_run = time.time()
    
    return_values = {"matrix": matrix, "profit_per_run":found_profit_per_run, 
                     "time_needed":time_needed, "start_time":start_run, 
                     "end_time":end_run, "final_profit":final_profit}
    return return_values  
示例#3
0
def hill_climber_value(steps, houses_to_place):
    '''
    Finds max profit using Hill Climber. Returns a dictionary with saved data.
    Takes 2 arguments.
    Steps: the amount of steps a hill climber has to make. A steps is defined 
    as a succesful move of a house or water.
    Houses_to_place: 1 for 20 houses, 2 for 40, 3 for 60.
    '''

    print("Starting hill climber max value")

    # save profit and time
    found_profit_per_run = []
    time_needed = []

    # 90% change to move house, 10% change to move water
    water_or_house = 0.9

    # place houses
    not_placed = True

    # start placing
    while (not_placed):
        print("Initialize")
        init.initialize_matrix(houses_to_place, WIDTH, HEIGTH)
        try:
            matrix, houselist, water = init.initialize_matrix(
                houses_to_place, WIDTH, HEIGTH)
            not_placed = False
        except:
            not_placed = True

    # calculate initial profit
    fs.calculate_new_vrijstand_to_class(matrix, houselist)
    result = prof.calculate_value(houselist)

    found_profit_per_run.append(result)

    # save findings
    state = ss.saved_state(result, houselist, water)

    # do the hillclimber
    start_run = time.time()
    runtimes = 0
    while (runtimes < steps):

        # update
        if (runtimes % (steps / 100) == 0):
            print(runtimes)

        # make a copy of houselist and water
        houselist = state.get_houselist_copy()
        water = state.get_water_copy()

        # random move water or house
        if (water_or_house > random.random()):
            house = random.randint(0, len(houselist) - 1)
            moved, new_matrix = move.move_house(matrix, houselist[house], 100,
                                                100)
        else:
            water_pool = random.randint(0, len(water.get_pools()) - 1)
            moved, new_matrix = move.move_water(matrix,
                                                water.get_pool(water_pool), 10,
                                                10)

        # if a house or water has succesfully been moved.
        if (moved):
            # calculate new profit
            fs.calculate_new_vrijstand_to_class(new_matrix, houselist)
            new_profit = prof.calculate_value(houselist)

            # if profit is more, accept it
            if (new_profit >= state.get_total_value()):
                state.set_houselist(houselist)
                state.set_total_value(new_profit)
                state.set_water(water)
                matrix = new_matrix

            # save found data
            found_profit_per_run.append(state.get_total_value())
            time_needed.append(time.time() - start_run)

            # One step is completed
            runtimes += 1

    # save final profit
    final_profit = state.get_total_value()

    # done
    end_run = time.time()

    # make dictionary to return data
    return_values = {
        "matrix": matrix,
        "profit_per_run": found_profit_per_run,
        "time_needed": time_needed,
        "start_time": start_run,
        "end_time": end_run,
        "final_profit": final_profit
    }
    return return_values
示例#4
0
def build_grid(state, matrix):
    
    colours = get_colours()    
    screen = init_pygame(640, 600)
    tilesize = 2 # width and height    
    
    running = True
    
    # Start Temp Simulated Annealing
    temperature = 1.5*10**6
    start_temp = temperature
    count = 0.0
    
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                
        # copy of houselist              
        houselist = state.get_houselist_copy()
        water = state.get_water_copy()
        
        # Move house
        water_or_house = 0.9
        if(water_or_house > random.random()):
            print("move house")
            house = random.randint(0, len(houselist) - 1)
            moved, new_matrix = mh.move_house(matrix, houselist[house], 10, 10)
        else:
            print("move water")
            water_pool = random.randint(0, len(water.get_pools()) - 1)
            moved, new_matrix = mh.move_water(matrix, water.get_pool(water_pool), 20, 20)
            
        # if a house or water has succesfully been moved.
        if(moved):
            # calculate new profit
            fs.calculate_new_vrijstand_to_class(new_matrix, houselist)
            new_profit = prof.calculate_value(houselist)
            
            # if more accepts
            if(new_profit >= state.get_total_value()):
                state.set_houselist(houselist)
                state.set_total_value(new_profit)
                state.set_water(water)
                matrix =  new_matrix
            # else random accept depending on temperature
            elif((state.get_total_value() - new_profit) / temperature  < random.random()):
                print("Declined yet accepted")
                state.set_houselist(houselist)
                state.set_total_value(new_profit)
                state.set_water(water)
                matrix =  new_matrix
            
            # decrease temperature
            count += 1
            temperature = start_temp * ((0.999) ** abs(count))
            print("temp:                                            " + str(temperature))
            
        # draw         
        for row in range(300):
            for column in range(320):
                pygame.draw.rect(screen, colours[new_matrix[row][column]], 
                                 (column * tilesize, row * tilesize, 10, 10))
        
        caption = "Amstelhaege. Profit: " + str(state.get_total_value())
        pygame.display.set_caption(caption)
        pygame.display.flip()
               
    
    pygame.display.quit()
    pygame.quit()
示例#5
0
def simulated_annealing_vrijstand(houses_to_place):
    '''
    Finds max vrijstand using Simulated annealing. Returns a dictionary with saved data.
    Takes 2 arguments.
    Houses_to_place: 1 for 20 houses, 2 for 40, 3 for 60.
    '''

    print("simulated annealing")

    # save profit and time
    found_profit_per_run = []
    time_needed = []

    # 90% change to move house, 10% change to move water
    water_or_house = 0.9

    # place houses
    not_placed = True

    # start placing
    while (not_placed):
        try:
            matrix, houselist, water = init.initialize_matrix(
                houses_to_place, WIDTH, HEIGTH)
            not_placed = False
        except:
            not_placed = True

    # calculate initial profit
    fs.calculate_new_vrijstand_to_class(matrix, houselist)
    result = prof.calculate_free_space(houselist)

    found_profit_per_run.append(result)

    # save findings
    state = ss.saved_state(result, houselist, water)

    # Start Temp Simulated Annealing
    temperature = 50
    start_temp = temperature
    count = 0.0

    # do the simulated annealing
    start_run = time.time()
    while (temperature > 0.08):

        # update
        if (count % 100 == 0):
            print(count, temperature)

        # make a copy of houselist
        houselist = state.get_houselist_copy()
        water = state.get_water_copy()

        # random move water or house
        if (water_or_house > random.random()):
            house = random.randint(0, len(houselist) - 1)
            moved, new_matrix = move.move_house(matrix, houselist[house], 100,
                                                100)
        else:
            water_pool = random.randint(0, len(water.get_pools()) - 1)
            moved, new_matrix = move.move_water(matrix,
                                                water.get_pool(water_pool), 10,
                                                10)

        # if a house or water has succesfully been moved.
        if (moved):
            # calculate new profit
            fs.calculate_new_vrijstand_to_class(new_matrix, houselist)
            new_profit = prof.calculate_free_space(houselist)

            # if more accepts
            if (new_profit >= state.get_total_value()):
                state.set_houselist(houselist)
                state.set_total_value(new_profit)
                state.set_water(water)
                matrix = new_matrix
            # else random accept depending on temperature
            elif ((state.get_total_value() - new_profit) / temperature <
                  random.random()):
                state.set_houselist(houselist)
                state.set_total_value(new_profit)
                state.set_water(water)
                matrix = new_matrix

            # decrease temperature
            count += 1
            temperature = start_temp * ((0.999)**abs(count))

            # save findings
            found_profit_per_run.append(state.get_total_value())
            time_needed.append(time.time() - start_run)

    # final profit
    final_profit = state.get_total_value()

    # done
    end_run = time.time()

    return_values = {
        "matrix": matrix,
        "profit_per_run": found_profit_per_run,
        "time_needed": time_needed,
        "start_time": start_run,
        "end_time": end_run,
        "final_profit": final_profit
    }
    return return_values