예제 #1
0
def test_simulated_annealling():
    vertices, edges = read_graph('benchmarks/le450_25d.col')
    graph = tuples_to_dict(vertices, edges)
    order = list(range(1, len(graph) + 1))
    shuffle(order)
    solution1 = Solution(graph, order)
    best_solution = simulated_annealing(solution1)

    print('Inicial solution = ' + str(solution1.colors_count))
    print('Best solution = ' + str(best_solution.colors_count))
예제 #2
0
def main():
    #  input_graph = read_graph("input/225-1/easy_03_tsp.txt")
    #  input_graph = read_graph("input/225-1/medium_01_tsp.txt")
    input_graph = read_graph("input/225-1/hard_02.txt")

    no_nodes = input_graph[0]
    network = input_graph[1]

    print no_nodes
    print network
    params = read_parameters("input/parameters.txt")

    population_dimension = params[0]
    number_of_parents = params[1]
    tournament_dimension = params[2]
    mutation_ratio = params[3]
    number_of_steps = params[4]

    population = generate_population(population_dimension, no_nodes, network)

    # assign a random chromosome to be the best
    overall_best = population[0]
    overall_best_fitness = fitness(population[0], network)
    for step in range(number_of_steps):
        fathers = selection(population, number_of_parents,
                            tournament_dimension, network)
        mothers = selection(population, number_of_parents,
                            tournament_dimension, network)
        children = crossover(fathers, mothers)
        population = population + children
        population = mutation(population, mutation_ratio, no_nodes)
        population = survival_selection(population, network,
                                        population_dimension)
        best = min(population, key=lambda x: fitness(x, network))
        best_fitness = fitness(best, network)
        if overall_best_fitness > best_fitness:
            print "new BEST at step {}".format(step)
            print 'best solution = {}'.format(best)
            print 'best fitness = {}'.format(best_fitness)
            print ""
            overall_best = copy.deepcopy(best)
            overall_best_fitness = best_fitness

    print 'overall best = {}'.format(overall_best)
    print 'overall best fitness= {}'.format(overall_best_fitness)
def generate_mod(in_fname: str, out_fname: str):
    H, E = read_graph(in_fname)
    I = V = [i + 1 for i in range(H)]

    R = count()
    saida = []

    for v, i in product(V, I):
        saida.append(f'var x_{v}_{i} binary;')

    for i in I:
        saida.append(f'var w_{i} binary;')

    saida.append('minimize num_colors:')
    for i in I:
        saida.append(f'w_{i} +')

    saida[-1] = saida[-1].replace(' +', ';')

    saida.append('subject to')

    for v in V:
        saida.append(f'r{next(R)}: ' + ' + '.join(f'x_{v}_{i}'
                                                  for i in I) + ' = 1;')

    for (u, v), i in product(E, I):
        saida.append(f'r{next(R)}: x_{u}_{i} + x_{v}_{i} <= w_{i};')

    for i in I:
        saida.append(f'r{next(R)}: w_{i} <= ' + ' + '.join(f'x_{v}_{i}'
                                                           for v in V) + ';')

    for i in islice(I, 1, None):
        saida.append(f'r{next(R)}: w_{i} <= w_{i - 1};')

    saida.append('solve;')
    saida.append('display ' + ' + '.join(f'w_{i}' for i in I) + ';')

    with open(out_fname, 'w') as f:
        f.write('\n'.join(saida))
        f.write('\n')
예제 #4
0
def init_graph(filename: str) -> Graph:
    vertices, edges = read_graph(filename)
    vertex_neighbors = tuples_to_dict(vertices, edges)
    return vertex_neighbors
예제 #5
0
        non_smooth_one_norm=non_smooth_one_norm,
        debug_output=debugging_output)

else:

    folder = from_folder

    from reader import read_graph, read_array, read_matrix, read_number
    import os

    big_matrix = read_matrix(os.path.join(folder, "big_R.dat"))
    vector = read_matrix(os.path.join(folder, "little_r.dat"))

    gamma = read_number(os.path.join(folder, "gamma.dat"))

    graph = read_graph(os.path.join(folder, "graph.dat"))

    kappas = read_array(os.path.join(folder, "kappa.dat"))
    taus = read_array(os.path.join(folder, "tau.dat"))

    kappas.tolist()
    taus.tolist()

    kappas = kappas[0]
    taus = taus[0]

    initial_primal_guess_matrix = read_array(os.path.join(folder, "xi.dat"))
    initial_dual_guess_matrix = read_array(os.path.join(folder, "y.dat"))

    num_nodes = initial_primal_guess_matrix.shape[1]