Ejemplo n.º 1
0
 def run(self):
     """Calculate shortest path with resource constraints.
     """
     start = time()
     self._init_swarm()
     while (self.iter < self.max_iter
            and not check_time_limit_breached(start, self.time_limit)):
         pos_new = self.pos + self._get_vel()
         self._update_best(self.pos, pos_new)
         self.pos = pos_new
         self.fitness = self._get_fitness(self.pos)
         self._global_best()
         # Update local best for each particle
         for i in range(self.swarm_size):
             self._local_best(i)
         if self.iter % 100 == 0:
             log.info("Iteration: {0}. Current best fit: {1}".format(
                 self.iter, self.best_fit))
         # Terminate if feasible path found with total cost <= threshold
         if (self.best_path and self.best_path_cost
                 and self.threshold is not None
                 and self.best_path_cost <= self.threshold):
             break
         self.iter += 1
     if not self.best_path:
         raise Exception("No resource feasible path has been found")
Ejemplo n.º 2
0
 def _terminate(self) -> bool:
     """Check whether time limit is violated or final path with weight
     under the input threshold"""
     if self.time_limit is not None and check_time_limit_breached(
             self.start_time, self.time_limit):
         return True
     return bool(self._check_final_label())
Ejemplo n.º 3
0
 def run(self):
     """
     Calculate shortest path with resource constraints.
     """
     start = time()
     while (self.it < self.max_iter and not self.stop
            and not check_time_limit_breached(start, self.time_limit)):
         self._algorithm()
         self.it += 1
     if not self.best_solution.path:
         raise Exception("No resource feasible path has been found")
Ejemplo n.º 4
0
 def run(self):
     """Calculate shortest path with resource constraints.
     """
     start = time()
     self._init_swarm()
     while (self.iter < self.max_iter and
            not check_time_limit_breached(start, self.time_limit)):
         pos_new = self.pos + self._get_vel()
         new_rands = self.random_state.uniform(0, 1, size=self.swarm_size)
         # Force Source and Sink to be selected
         pos_new[:, [0, -1]] = min(10 * self.lower_bound, np.min(pos_new))
         paths_new = np.empty(len(pos_new), dtype='object')
         paths_new[:] = [
             self._pos2path(p, r) for p, r in zip(pos_new, new_rands)
         ]
         self._update_best(self.pos, pos_new, self.rands, new_rands,
                           self.paths, paths_new)
         self.pos = pos_new
         self.rands = new_rands
         self.fitness = self._get_fitness(self.paths)
         self._global_best()
         # Save best results
         self.st_path = self._best_path
         self.check_feasibility()  # works on st_path
         # Update local best for each particle
         for i in range(self.swarm_size):
             self._local_best(i)
         if self.iter % 100 == 0:
             log.info("Iteration: {0}. Current best fit: {1}".format(
                 self.iter, self.best_fit))
         # Terminate if feasible path found with total cost <= threshold
         if (self.best_path and self.best_path_cost and
                 self.threshold is not None and
                 self.best_path_cost <= self.threshold):
             break
         self.iter += 1
     if not self.best_path:
         raise Exception("No resource feasible path has been found")