示例#1
0
def find_cycle(G: Graph, points: list) -> None:
    if G.repr_type == RepresentationType.EMPTY:
        print('\nGraph is empty! First try to create it.')
    else:
        print('\n[1] Closest Neighbour Algorithm')
        print('[2] Simulated Annealing Algorithm')
        print('[3] Combine two algorithms')

        option = input("\nChoose option\n")

        if option == '1':
            GraphPlotter.plot_points(points, closest_neighbour(G))

        if option == '2':
            print(
                '\nChoose how many times algorithms should be repeated (int)')
            repeat = int(input('Repeats: '))
            print('\nChoose starting temperature (int)')
            temp = int(input('Temperature: '))
            print('\nChoose number of iterations (int)')
            iter_max = int(input('ITER_MAX: '))
            path = simulated_annealing(G, temp, iter_max, repeat)
            if path != []:
                GraphPlotter.plot_points(points, path)

        if option == '3':
            print(
                '\nChoose how many times algorithms should be repeated (int)')
            repeat = int(input('Repeats: '))
            print('\nChoose starting temperature (int)')
            temp = int(input('Temperature: '))
            print('\nChoose number of iterations (int)')
            iter_max = int(input('ITER_MAX: '))
            path = simulated_annealing(G,
                                       temp,
                                       iter_max,
                                       repeat,
                                       path=closest_neighbour(G))
            if path != []:
                GraphPlotter.plot_points(points, path)
示例#2
0
from random import random
from utils.graph_plotter import GraphPlotter
from utils.Graph import Graph, RepresentationType
from utils.graph_generators import get_graph_from_points
from algorithms.salesman import closest_neighbour, simulated_annealing

if __name__ == '__main__':
    G = Graph()
    points = list()

    for _ in range(100):
        points.append((2000 * random() - 1000, 2000 * random() - 1000))

    G.load_data(get_graph_from_points(points),
                RepresentationType.ADJACENCY_MATRIX)

    path = closest_neighbour(G)
    GraphPlotter.plot_points(points, path)
    path = simulated_annealing(G, 500, 100, 3)
    GraphPlotter.plot_points(points, path)

    points = np.loadtxt(os.path.dirname(__file__) + '/inputs/input59.dat',
                        usecols=(0, 1))
    G.load_data(get_graph_from_points(points),
                RepresentationType.ADJACENCY_MATRIX)

    path = closest_neighbour(G)
    GraphPlotter.plot_points(points, path)
    path = simulated_annealing(G, 500, 100, 10)
    GraphPlotter.plot_points(points, path)