Esempio n. 1
0
def tsp():
    ants_num = int(ants_entry.get())
    iter_num = int(iter_entry.get())

    import paco
    import matplotlib.pyplot as plt
    num_cities = 48
    # Intialize the ACO algorithm with some parameter values
    aco = paco.ACO(num_cities,
                   initial_pheromone=1,
                   alpha=1,
                   beta=3,
                   pheromone_deposit=2,
                   evaporation_constant=0.6)

    with open("./att48.txt") as f:
        content = f.readlines()
    content = [x.strip() for x in content]

    for line in content:
        items = line.split()
        num = int(items[0]) - 1
        x_cord = int(items[1])
        y_cord = int(items[2])
        aco.add_cities(paco.City(num, x_cord, y_cord))

    # run the aco algorithm and return the shortest path
    shortest_path = aco.get_best_path(num_ants=ants_num, num_steps=iter_num)
    # print the shortest path length found in the aco
    print("Number of ants used: {}".format(ants_num))
    print("Shortest route found: {0:.3f}".format(aco.shortest_path_len))

    #plt.axes([0.15, 0.15, 0.8, 0.8])
    plt.figure(1, figsize=[8, 6], facecolor='#F0F0F0')
    plt.margins(0.1, 0.1)
    for i, c in enumerate(aco.cities):  # output the cities to a plot
        if i == 0:
            plt.title("Visualization of ACO algorithms on TSP (48 cities)")
            plt.ylabel("Y - Coordinates of the cities")
            plt.xlabel("X - Coordinates of the cities")
            plt.plot(c.x, c.y,
                     'gx')  # the first city to be printed will be green
        else:
            plt.plot(c.x, c.y, 'ro')

    for i in range(
            0,
            len(shortest_path) - 1
    ):  # plot connecting lines between each city visited in the order they are visited
        plt.plot([shortest_path[i].x, shortest_path[i + 1].x],
                 [shortest_path[i].y, shortest_path[i + 1].y],
                 'c-',
                 linewidth=2.0,
                 alpha=0.4)
        plt.pause(0.05)

    plt.show()
Esempio n. 2
0
import paco
import random
import math
import matplotlib.pyplot as plt

num_cities = 48
# Intialize the ACO algorithm with some parameter values
aco = paco.ACO(num_cities,
               initial_pheromone=1,
               alpha=1,
               beta=3,
               pheromone_deposit=2,
               evaporation_constant=0.6)

with open("./att48.txt") as f:
    content = f.readlines()
content = [x.strip() for x in content]

for line in content:
    items = line.split()
    num = int(items[0]) - 1
    x_cord = int(items[1])
    y_cord = int(items[2])
    aco.add_cities(paco.City(num, x_cord, y_cord))

# run the aco algorithm and return the shortest path
ants = 20
shortest_path = aco.get_best_path(num_ants=ants, num_steps=10)
# print the shortest path length found in the aco
print("Number of ants used: {}".format(ants))
print("Shortest route found: {0:.3f}".format(aco.shortest_path_len))
Esempio n. 3
0
import paco
import random
import math
import matplotlib.pyplot as plt

num_cities = 100  # configure how many cities to populate the world with
world_x = 100  # set the max x,y dimensions for the world
world_y = 100
increment = 360.0 / num_cities  # calculate the increment required if we want to lay the cities out in a circle

aco = paco.ACO(num_cities,
               initial_pheromone=1,
               alpha=1,
               beta=3,
               epsilon=0.1,
               pheromone_deposit=2,
               evaporation_constant=0.6
               )  # intialize the aco algorithm with our parameters
# see website for discussion of the above parameters

for i in range(0, num_cities):
    #aco.add_cities(paco.City(i, random.uniform(0,world_x), random.uniform(0,world_y))) # use this line for randomly distributed cities

    angle = i * increment  # use this line and the next for cities distributed in a circle
    aco.add_cities(
        paco.City(i, math.sin(math.radians(angle)),
                  math.cos(math.radians(
                      angle))))  # populate the aco instance with the cities

shortest_path = aco.get_best_path(
    num_ants=3,