Ejemplo n.º 1
0
    def run(self):
        '''
        Function to run the rank based Ant Colony Optimization algorithm
        '''
        best_ant = Ant(0, 0, [])

        for i in range(self._n_iterations):
            colony = []
            for j in range(self._n_ants):
                ant = Ant(0, self._distances.shape[0],
                          copy.deepcopy(self._vehicles))
                for x in range(len(ant.vehicles)):
                    ant.vehicles[x].put_node_path(0, self._occupancies,
                                                  self._distances, self._times)
                ant.build_path(self._distances, self._times, self._occupancies,
                               self._pheromone, self._alpha, self._beta)
                ant.calculate_distance(self._distances)
                ant.objectve_function(self._distance_cost)
                colony.append(ant)

            n_bests_ants = self.find_n_bests_ants(colony)

            if n_bests_ants[0].of_value < best_ant.of_value:
                best_ant = n_bests_ants[0]

            self.update_pheromone(best_ant, n_bests_ants)

            #print("Iteration: %d Best OF: %f" %(i + 1, best_ant.of_value))

        return best_ant
Ejemplo n.º 2
0
    def run(self):
        '''
        Function to run the rank based Ant Colony Optimization algorithm
        '''
        best_ant = Ant(0, 0)
        for i in range(self.n_iterations):
            print("Iteration: %d Best OF: %f" % (i, best_ant.of()))
            colony = []
            for j in range(self.n_ants):
                ant = Ant(np.random.randint(self.distances.shape[0]),
                          self.distances.shape[0])
                ant.build_path(self.distances, self.pheromone, self.alpha,
                               self.beta)
                ant.calculate_distance(self.distances)
                ant.objectve_function(self.distance_cost)
                colony.append(ant)

            n_bests_ants = self.find_n_bests_ants(colony)

            if n_bests_ants[0].of() < best_ant.of():
                best_ant = n_bests_ants[0]

            self.update_pheromone(best_ant, n_bests_ants)
        print(self.pheromone)
        return best_ant