コード例 #1
0
    def calcFitnesses(self):
        '''
        calculate fitness of every chromosome
        '''
        costs = []
        routes = []

        for c in range(self.n_chromosomes):
            cost = 0
            truck_routes = []
            for t in range(self.n_trucks):
                if (len(self.truck_loads[c][t]) > 0):
                    if (len(self.truck_loads[c][t]) <= 9):
                        truck_distance, path = self.brute_force_solution(
                            self.truck_loads[c][t])
                    else:
                        truck_distance_mat = self.submat(
                            self.truck_loads[c][t])
                        ac = AntColony(distances=truck_distance_mat,
                                       n_ants=20,
                                       n_best=10,
                                       n_iterations=100,
                                       decay=0.95,
                                       alpha=1,
                                       beta=4)
                        path, truck_distance = ac.run()
                    cost += (truck_distance * self.transportation_costs[t])
                    truck_routes.append(path)
                else:
                    truck_routes.append(())
            costs.append(cost)
            routes.append(truck_routes)
        self.costs = np.array(costs)
        self.fitnesses = (self.fitness_counter - self.costs) + 1
        self.routes = routes
コード例 #2
0
    def calcFitnessesOffspring(self):
        '''
        calculate fitness of every chromosome
        '''
        costs = []
        routes = []

        for c in range(np.shape(self.offspring)[2]):
            cost = 0
            truck_routes = []
            for t in range(self.n_trucks):
                if (len(self.offspring_loads[c][t]) > 0):
                    truck_distance_mat = self.submat(
                        self.offspring_loads[c][t])
                    if (len(self.offspring_loads[c][t]) > 3):
                        ac = AntColony(distances=truck_distance_mat,
                                       n_ants=20,
                                       n_best=10,
                                       n_iterations=10,
                                       decay=0.95,
                                       alpha=1,
                                       beta=4)
                        path, truck_distance = ac.run()
                    else:
                        truck_distance = sum([
                            truck_distance_mat[i, (i + 1) %
                                               len(truck_distance_mat)]
                            for i in range(len(truck_distance_mat))
                        ])
                        path = [(i, (i + 1) % len(truck_distance_mat))
                                for i in range(len(truck_distance_mat))]
                    cost += (truck_distance * self.transportation_costs[t])
                    truck_routes.append(path)
                else:
                    truck_routes.append(())
            costs.append(cost)
            routes.append(truck_routes)
        self.offspring_costs = np.array(costs)
        self.offspring_fitnesses = (self.fitness_counter - self.costs) + 1
        self.routes = routes
コード例 #3
0
import numpy as np

from ant_colony import AntColony

distances = np.array([[np.inf, 2, 2, 5, 7], [2, np.inf, 4, 8, 2],
                      [2, 4, np.inf, 1, 3], [5, 8, 1, np.inf, 2],
                      [7, 2, 3, 2, np.inf]])

ant_colony = AntColony(distances, 1, 1, 100, 0.95, alpha=1, beta=1)
shortest_path = ant_colony.run()
print("shorted_path: {}".format(shortest_path))
コード例 #4
0
Ant Colony Optimization 
stolen from: 
https://raw.githubusercontent.com/Akavall/AntColonyOptimization/master/ant_colony.py

'''
n_ants = 100
n_best = 25
n_iterations = 300
decay = 0.1

distCopy = distances.copy()
np.fill_diagonal(distCopy,np.inf)

colony = AntColony(distCopy, n_ants, n_best, n_iterations, decay, alpha=1.0, beta=1.5,coords=coords)

shortes_path = colony.run()


# only city indices
route = [s[1]-1 for s in shortes_path[0][:-1]]

# demands sorted by traveling route
demandRoute = demands[route]

# genetic algorithm
gA = GenAlgo(coords, demands, route, demandRoute, distances[1:,1:], depotDistances, vehicleCapacities, vehicleCosts)

routes = []
demandRoutes = []
topCosts = []
assignments = []
コード例 #5
0
# Made by - Pratik Luitel , Anish Dulal, Stuti Kafle
# available at - https://github.com/pratikluitel/ACOVisualization
# make a venv and install dependencies in requirements.txt before you proceed

import numpy as np

from ant_colony import AntColony

# making a random large city with 100 streets

large_distances = np.random.randint(1, 10, size=(100, 100))
large_distances = (large_distances + large_distances.T).astype('float64')
np.fill_diagonal(large_distances, float('inf'))

large_ant_colony = AntColony(large_distances, 10, 5, 50, 0.15, alpha=1, beta=1)
print(large_ant_colony.run())
コード例 #6
0
# Made by - Pratik Luitel , Anish Dulal, Stuti Kafles
# available at - https://github.com/pratikluitel/ACOVisualization
# make a venv and install dependencies in requirements.txt before you proceed

import numpy as np

from ant_colony import AntColony

# making a city with 5 streets

small_distances = np.array([[np.inf, 2, 2, 5, 7, 4], [2, np.inf, 4, 8, 2, 3],
                            [2, 4, np.inf, 1, 3, 6], [5, 8, 1, np.inf, 2, 8],
                            [7, 2, 3, 2, np.inf, 1], [4, 3, 6, 8, 1, np.inf]])

small_ant_colony = AntColony(small_distances,
                             5,
                             2,
                             10,
                             0.95,
                             alpha=1,
                             beta=1,
                             graph=False)
small_ant_colony.run()