def simulated_annealing(route, roads, init_temp):
    current_best_distance = distance_check(route, roads)
    temp = init_temp
    final_temp = init_temp / 100
    story = []
    story.append(current_best_distance)
    #if the current best distance is already optimal the loop will be skipped entirely
    swap_roof = len(route) - 2
    swapped_point = rnd.randint(0, swap_roof)
    while temp >= final_temp * 2:
        alt_route = next_distance(route, swapped_point, roads)
        cost = alt_route[1] - current_best_distance
        if cost < 0:
            story.append(alt_route[1])
            current_best_distance = alt_route[1]
            route = alt_route[0]
            swapped_point = rnd.randint(0, swap_roof)
            temp = cool(temp, final_temp)
        elif rnd.uniform(0, 1) < acceptance_probability(cost, temp):
            story.append(alt_route[1])
            current_best_distance = alt_route[1]
            route = alt_route[0]
            swapped_point = rnd.randint(0, swap_roof)
            temp = cool(temp, final_temp)
        else:
            if (swapped_point < swap_roof):
                swapped_point += 1
            else:
                swapped_point = rnd.randint(0, swap_roof)
                temp = cool(temp, final_temp)
    return story
def score_pop(Population, roads):
    for gene in Population.genes:
        gene[0] = distance_check(gene[1], roads)
    # sorts the population
    Population.genes = sorted(Population.genes, key=lambda x: x[0])
    if not Population.scored:
        Population.scored = True
    return Population
Exemplo n.º 3
0
def hill_climb(optimization_goal, route, roads):
    current_best_distance = distance_check(route, roads)
    story = []
    story.append(distance_check(route, roads))
    #if the current best distance is already optimal the loop will be skipped entirely
    while current_best_distance > optimization_goal:
        # keep tabs on the part of the list you're messing with
        swapped_member = 2
        while swapped_member < int(len(roads)) - 3:
            alt_route = next_distance(route, swapped_member, roads)
            if alt_route[1] < current_best_distance:
                current_best_distance = alt_route[1]
                story.append(alt_route[1])
                route = alt_route[0]
                break
            else:
                swapped_member += 1
        if swapped_member >= int(len(roads)) - 3:
            optimization_goal = maxsize
    return story
Exemplo n.º 4
0
def steep_climb(optimization_goal, route, roads):
    current_best_distance = distance_check(route, roads)
    story = []
    story.append(current_best_distance)
    while current_best_distance > optimization_goal:
        alt_route = best_distance(route, roads)
        if alt_route[1] < current_best_distance:
            story.append(alt_route[1])
            current_best_distance = alt_route[1]
            route = alt_route[0]
        else:
            optimization_goal = maxsize
    return story
def steep_simulated_annealing(route, roads, init_temp, temp_reduction):
    current_best_distance = distance_check(route, roads)
    temp = init_temp
    final_temp = init_temp / temp_reduction
    story = []
    story.append(current_best_distance)
    #if the current best distance is already optimal the loop will be skipped entirely
    while temp >= final_temp * 2:
        alt_route = best_distance(route, roads)
        cost = alt_route[1] - current_best_distance
        if cost < 0:
            story.append(alt_route[1])
            current_best_distance = alt_route[1]
            route = alt_route[0]
        elif rnd.uniform(0, 1) < acceptance_probability(cost, temp):
            rnd.shuffle(alt_route[0])
            explored = alt_route[0]
            story.append(distance_check(explored, roads))
            current_best_distance = distance_check(explored, roads)
            route = explored
        temp = cool(temp, final_temp)
    return story