예제 #1
0
 def constructor(self, random, args):
     """Return a candidate solution for an ant colony optimization."""
     self._use_ants = True
     candidate = []
     while len(candidate) < len(self.components):
         # Find feasible components
         feasible_components = []
         if len(candidate) == 0:
             feasible_components = self.components
         else:
             remaining_capacity = self.capacity - sum([c.element for c in candidate])
             if self.duplicates:
                 feasible_components = [c for c in self.components if c.element <= remaining_capacity]
             else:
                 feasible_components = [c for c in self.components if c not in candidate and c.element <= remaining_capacity]
         if len(feasible_components) == 0:
             break
         else:
             # Choose a feasible component
             if random.random() <= self.bias:
                 next_component = max(feasible_components)
             else:
                 next_component = selectors.fitness_proportionate_selection(random, feasible_components, {'num_selected': 1})[0]
             candidate.append(next_component)
     return candidate
예제 #2
0
 def constructor(self, random, args):
     """Return a candidate solution for an ant colony optimization."""
     self._use_ants = True
     candidate = []
     while len(candidate) < len(self.weights) - 1:
         # Find feasible components
         feasible_components = []
         if len(candidate) == 0:
             feasible_components = self.components
         elif len(candidate) == len(self.weights) - 1:
             first = candidate[0]
             last = candidate[-1]
             feasible_components = [c for c in self.components if c.element[0] == last.element[1] and c.element[1] == first.element[0]]
         else:
             last = candidate[-1]
             already_visited = [c.element[0] for c in candidate]
             already_visited.extend([c.element[1] for c in candidate])
             already_visited = set(already_visited)
             feasible_components = [c for c in self.components if c.element[0] == last.element[1] and c.element[1] not in already_visited]
         if len(feasible_components) == 0:
             candidate = []
         else:
             # Choose a feasible component
             if random.random() <= self.bias:
                 next_component = max(feasible_components)
             else:
                 next_component = selectors.fitness_proportionate_selection(random, feasible_components, {'num_selected': 1})[0]
             candidate.append(next_component)
     return candidate
 def constructor(self, random, args):
     self._use_ants = True
     candidate = []
     remaining_capacity = []
     while len(candidate) < len(self.components):
         feasible_components = []
         if len(candidate) == 0:
             feasible_components = self.components
         else:
             remaining_capacity_weigth = self.capacity[0] - sum(
                 [c.element[0] for c in candidate])
             remaining_capacity_value = self.capacity[1] - sum(
                 [c.element[1] for c in candidate])
             if self.duplicates:
                 feasible_components = [
                     c for c in self.components
                     if c.element[0] <= remaining_capacity_weigth
                     and c.element[1] <= remaining_capacity_value
                 ]
             else:
                 feasible_components = [
                     c for c in self.components if c not in candidate
                     and c.element[0] <= remaining_capacity_weigth
                     and c.element[1] <= remaining_capacity_value
                 ]
         if len(feasible_components) == 0:
             break
         else:
             # Choose a feasible component
             if random.random() <= self.bias:
                 next_component = max(feasible_components)
             else:
                 next_component = \
                 selectors.fitness_proportionate_selection(random, feasible_components, {'num_selected': 1})[0]
             candidate.append(next_component)
     return candidate