Beispiel #1
0
def local_search_accept_error(nb_step=10, version=2, p=.1):
    cur_sol = (ls.first_solution(graph, terminals))
    act_gain = ls.gain(cur_sol)
    l_act = [act_gain]
    l_new = [act_gain]
    for i in range(nb_step):
        print(i)
        new_sol = ls.neighbors_of_solution(graph, cur_sol, terminals)
        new_gain = ls.gain(new_sol)
        l_new.append(new_gain)
        r = random.random()
        if new_gain < act_gain or r < p:
            act_gain = new_gain
            cur_sol = new_sol
        l_act.append(act_gain)
    return l_act, l_new
Beispiel #2
0
def variation_crossover(terminals, population, lda):
    for i in range(lda):
        to_merge = random.sample(population, 2)
        population.append(fusion(to_merge[0][1], to_merge[1][1], terminals))

        population[-1] = (ls.gain(population[-1]), population[-1],
                          to_merge[0][2] + to_merge[1][2] + 1)
    return population
Beispiel #3
0
def random_solution(graph, terminals, n):
    sols = []
    first_sol = ls.first_solution(graph, terminals)
    for i in range(n):
        n_graph = ls.neighbors_of_solution(graph, first_sol, terminals, 10)
        poids = ls.gain(n_graph)
        sols.append((poids, n_graph, 0))
    return sols
Beispiel #4
0
def variation_mutation(terminals, population, lda):
    for i in range(lda):
        to_mutate = random.sample(population, 1)
        population.append(
            ls.neighbors_of_solution(graph, to_mutate[0][1], terminals, 2, 1))

        population[-1] = (ls.gain(population[-1]), population[-1],
                          to_mutate[0][2] + 1)
    return population
Beispiel #5
0
def local_search_only_better(nb_step=10, version=2):
    cur_sol = (ls.first_solution(graph, terminals))
    act_gain = ls.gain(cur_sol)
    l_act = [act_gain]
    l_new = [act_gain]
    for i in range(nb_step):
        print(i)
        new_sol = ls.neighbors_of_solution(graph, cur_sol, terminals, version,
                                           5)
        new_gain = ls.gain(new_sol)
        l_new.append(new_gain)
        if new_gain < act_gain:
            act_gain = new_gain
            cur_sol = new_sol
        l_act.append(act_gain)
    print(ls.final_value(cur_sol))
    print(ls.gain(cur_sol))
    return l_act, l_new
Beispiel #6
0
def simulated_anhilling(nb_step=10, heat_strategy=heat_strategy_linear):
    cur_sol = (ls.first_solution(graph, terminals))
    act_gain = ls.gain(cur_sol)
    l_act = [act_gain]
    l_new = [act_gain]
    l_seuils = [1]
    for nb_step_act in range(nb_step):
        new_sol = ls.neighbors_of_solution(graph, cur_sol, terminals)
        new_gain = ls.gain(new_sol)
        l_new.append(new_gain)
        r = random.random()
        r_seuil = heat_strategy(nb_step_act + 1, act_gain, new_gain)
        l_seuils.append(r_seuil)
        if r < r_seuil:
            act_gain = new_gain
            cur_sol = new_sol
        l_act.append(act_gain)
    return l_act, l_new, l_seuils