예제 #1
0
 def build_products_to_end(self, aco):
     end = self.spec.get_end()
     products_to_end = []
     for i in range(len(self.product_locations)):
         products_to_end.append(
             aco.find_shortest_route(
                 PathSpecification(self.product_locations[i], end)))
     return products_to_end
예제 #2
0
 def build_start_to_products(self, aco):
     start = self.spec.get_start()
     start_to_products = []
     for i in range(len(self.product_locations)):
         start_to_products.append(
             aco.find_shortest_route(
                 PathSpecification(start, self.product_locations[i])))
     return start_to_products
예제 #3
0
 def build_distance_matrix(self, aco):
     number_of_product = len(self.product_locations)
     product_to_product = []
     for i in range(number_of_product):
         product_to_product.append([])
         for j in range(number_of_product):
             start = self.product_locations[i]
             end = self.product_locations[j]
             product_to_product[i].append(aco.find_shortest_route(PathSpecification(start, end)))
     return product_to_product
    def build_distance_matrix(self, aco):
        number_of_product = len(self.product_locations)
        product_to_product = []

        for i in range(number_of_product):
            product_to_product.append([])
        print("number of products", number_of_product)

        for i in range(number_of_product):
            print(i)
            product_to_product[i].append([])
            for j in range(i + 1, number_of_product):
                start = self.product_locations[i]
                end = self.product_locations[j]
                print("start to end")
                product_to_product[i].append(aco.find_shortest_route(PathSpecification(start, end)))
                aco.reset()
                print("end to start")
                product_to_product[j].append(aco.find_shortest_route(PathSpecification(end, start)))
                aco.reset()

        return product_to_product
예제 #5
0
    def read_specification(coordinates, product_file):
        try:
            f = open(product_file, "r")
            lines = f.read().splitlines()

            firstline = re.compile("[:,;]\\s*").split(lines[0])
            product_locations = []
            number_of_products = int(firstline[0])
            for i in range(number_of_products):
                line = re.compile("[:,;]\\s*").split(lines[i + 1])
                product = int(line[0])
                x = int(line[1])
                y = int(line[2])
                product_locations.append(Coordinate(x, y))
            spec = PathSpecification.read_coordinates(coordinates)
            return TSPData(product_locations, spec)
        except FileNotFoundError:
            print("Error reading file " + product_file)
            traceback.print_exc()
            sys.exit()
# Driver function for Assignment 1
if __name__ == "__main__":
    # parameters
    # easy best - 38
    # medium best - 125
    # hard maze - 797

    gen = 20
    no_gen = 400
    q = 2000
    evap = 0.15
    stopping_criteria = 5

    # construct the optimization objects
    maze = Maze.create_maze("./../data/hard maze.txt")
    spec = PathSpecification.read_coordinates("./../data/hard coordinates.txt")
    aco = AntColonyOptimization(maze, gen, no_gen, q, evap, stopping_criteria)

    # save starting time
    start_time = int(round(time.time() * 1000))

    # run optimization
    shortest_route = aco.find_shortest_route(spec)

    # print time taken
    print("Time taken: " +
          str((int(round(time.time() * 1000)) - start_time) / 1000.0))

    plt.plot([i for i in range(len(aco.avg_per_gens))],
             aco.avg_per_gens,
             color='blue')