示例#1
0
def ACO(number_loc, number_cars, D, max_it, start_nodes, resturants, customers):
    # Parameters
    number_of_orders = len(resturants)
    n = number_loc
    Q = 1
    tau_0 = 10*Q/(n*np.mean(D))     # Initial Phromone
    alpha = 1                       # Phromone Exponential Weight
    beta = 1                        # Heuristic Exponential Weight
    rho = 0.05                      # Evaporation rate

    # Multi cars parameteres
    number_ants = number_cars
    nodes_per_ant = multi_parameters(number_of_orders, number_ants)
    print("Nodes per ant", nodes_per_ant)

    # Initialization
    tau = np.ones((n,n))*tau_0      # Phromone Matrix
    eta = np.zeros((n,n))           # Heuristic Information Matrix
    for i in range(n):
        for j in range(n):
            if i != j:
                d_ij = D[i][j]
                eta[i][j] = 1/d_ij

    best_costs = np.zeros((number_ants, max_it))      # Array to hold best cost values
    
    # Best ant
    best_solutions = []
    for d in range(number_ants):
        a = Ant()
        a.cost = np.inf
        best_solutions.append(a)
    

    # Iterations
    for it in range(max_it):
        # Create Ant colony 
        ant = []
        for a in range(number_ants):
            the_ant = Ant()
            the_ant.add_to_tour(start_nodes[a])

            ant.append(the_ant)

        # Move ants
        for k in range(number_ants):

            other_ant_tours = find_other_tours(ant, number_ants, k)
    
            while len(ant[k].tour) <= nodes_per_ant[k]*2:
                i = ant[k].tour[-1]
                P = (np.asarray(tau[i,:])**alpha)*(np.asarray(eta[i,:])**beta)
                # Not allowed to choose a node that have been visited before
                P[ant[k].tour] = 0
                # Not allowed to choose a next node that is not a resturant
                P[customers] = 0
                # Not allowed to choose a next node where other ants has been at
                P[other_ant_tours] = 0
                # Only allowed to choose a next node that is a resutrant that has an order
                for i in range(n):
                    if i not in resturants:
                        P[i] = 0

                P = P/np.sum(P)

                new_resturant = Roulette(P)

                i=0
                for resturant in resturants:
                    if resturant == new_resturant:
                        new_customer = customers[i]
                    i+=1
                

                ant[k].add_to_tour(new_resturant)
                ant[k].add_to_tour(new_customer)

            ant[k].cost = tour_length(ant[k].tour, D)

        cost_sum = 0
        best_cost_sum = 0
        for k in range(number_ants):
            cost_sum += ant[k].cost
            best_cost_sum += best_solutions[k].cost

        if cost_sum < best_cost_sum:
            for k in range(number_ants):
                best_solutions[k] = ant[k]
    
        # Update Phromones, this is where the cooperation is done
        for k in range(number_ants):
            tour = ant[k].tour.copy()    # Erase copy() to add start node as end node
            tour.append(tour[0])

            number_of_orders_for_this_ant = nodes_per_ant[k]

            for l in range(number_of_orders_for_this_ant):
                i = tour[l]
                j = tour[l+1]

                tau[i][j] = tau[i][j]+Q/ant[k].cost

        # Evaporation
        tau = (1-rho)*tau

        # Store Best Cost
        for k in range(number_ants):
            best_costs[k][it] = best_solutions[k].cost      

        # Show iteration information
        total_best_cost = np.sum(best_costs, axis = 0)
        print('Iteration', it, ': Best Cost = ', total_best_cost[it])
    
    best_tours = info_and_tours(best_costs, start_nodes, number_ants, best_solutions)

    return(best_tours)
示例#2
0
def ACO(number_loc, number_cars, D, max_it, x, y):
    # Parameters
    n = number_loc
    Q = 1
    tau_0 = 10 * Q / (n * np.mean(D))  # Initial Phromone
    alpha = 1  # Phromone Exponential Weight
    beta = 1  # Heuristic Exponential Weight
    rho = 0.05  # Evaporation rate

    # Multi cars parameters
    number_ants = number_cars
    nodes_per_ant, start_nodes = multi_parameters(n, number_ants)

    # Initialization
    tau = np.ones((n, n)) * tau_0  # Phromone Matrix
    eta = np.zeros((n, n))  # Heuristic Information Matrix
    for i in range(n):
        for j in range(n):
            if i != j:
                d_ij = D[i][j]
                eta[i][j] = 1 / d_ij

    best_costs = np.zeros(
        (number_ants, max_it))  # Array to hold best cost values

    # Best ant
    best_solutions = []
    for d in range(number_ants):
        a = Ant()
        a.cost = np.inf
        best_solutions.append(a)

    plt.ion()
    for it in range(max_it):
        # Create Ant colony
        ant = []
        for a in range(number_ants):
            the_ant = Ant()
            the_ant.add_to_tour(start_nodes[a])
            ant.append(the_ant)

        # Move ants
        for k in range(number_ants):
            other_ant_tours = find_other_tours(ant, number_ants, k)

            while len(ant[k].tour) < nodes_per_ant[k]:
                i = ant[k].tour[-1]
                P = (np.asarray(tau[i, :])**alpha) * (np.asarray(eta[i, :])**
                                                      beta)
                P[ant[k].
                  tour] = 0  # Cannot go back to its own already visited nodes
                P[other_ant_tours] = 0  # Cannot go to nodes that other ants has been at
                P = P / np.sum(P)
                j = Roulette(P)
                ant[k].add_to_tour(j)

            ant[k].cost = tour_length(ant[k].tour, D)

        cost_sum = 0
        best_cost_sum = 0
        for k in range(number_ants):
            cost_sum += ant[k].cost
            best_cost_sum += best_solutions[k].cost

        if cost_sum < best_cost_sum:
            for k in range(number_ants):
                best_solutions[k] = ant[k]

        # Update Phromones
        for k in range(number_ants):
            tour = ant[k].tour.copy(
            )  # Erase copy() to add start node as end node
            tour.append(tour[0])

            for l in range(nodes_per_ant[k]):
                i = tour[l]
                j = tour[l + 1]

                tau[i][j] = tau[i][j] + Q / ant[k].cost

        # Evaporation
        tau = (1 - rho) * tau

        # Store Best Cost
        for k in range(number_ants):
            best_costs[k][it] = best_solutions[k].cost

        # Show iteration information
        total_best_cost = np.sum(best_costs, axis=0)
        print('Iteration', it, ': Best Cost = ', total_best_cost[it])

        # Plot best tours for each iteration, showing the progress
        best_it_tours = []
        for i in range(number_ants):
            best_it_tours.append(best_solutions[i].tour)
        fig = plt.figure(1, figsize=(14, 7.5))
        plot_solution(x, y, best_it_tours, start_nodes)
        plt.pause(10**(-16))
        plt.clf()

    best_tours = info_and_tours(best_costs, start_nodes, number_ants,
                                best_solutions)
    #plot_solution(x,y, best_tours)
    #plt.show()
    return (best_tours)