Ejemplo n.º 1
0
def total_crossover(pop):
    dims = len(pop[0].genes)
    psize = len(pop)
    parent1 = pop[np.random.permutation(psize)]
    parent2 = pop[np.random.permutation(psize)]
    offsprings = np.ndarray(psize, dtype=object)
    tmp = np.random.rand(psize, dims)
    p1_selection = tmp >= 0.5
    p2_selection = tmp < 0.5
    for i in range(psize):
        offsprings[i] = Chromosome(dims)
        offsprings[i].genes[p1_selection[i]] = parent1[i].genes[
            p1_selection[i]]
        offsprings[i].genes[p2_selection[i]] = parent2[i].genes[
            p2_selection[i]]
    return offsprings
Ejemplo n.º 2
0
def crossover(p1, p2, alpha=np.random.rand(), ctype='uniform'):
    if ctype == 'convex':
        return Chromosome(alpha * p1.genes + (1 - alpha) * p2.genes)
    # elif stype=='uniform':
    #   return max(np.random.choice(self.pop,k,False),key=lambda c:c.fitness)
    else:
        raise ValueError('Type of crossover which you entered is wrong')
Ejemplo n.º 3
0
 def initialize(self):
     gen_id = 0
     if self.settings.get('evolution',
                          {}).get('checkpoint',
                                  {}).get('initialize_enabled'):
         gen_id = int(self.initialize_from_save()) + 1
     else:
         pop = []
         for i in range(
                 0,
                 self.settings.get('evolution', {}).get('population_size')):
             c = Chromosome(self.settings)
             pop.append(c)
         self.pop = pop
     return gen_id
def get_pop_init(n, gn, init_func=np.random.rand, p_type='knapsack'):
    """[Initial Population Generator]
  
  Arguments:
      n {[int]} -- [Number of members of population]
      gn {[int]} -- [Number of individual's genes]
      init_func {[function]} -- the function which initialize each chromosome
  
  Returns:
      [np.ndarray] -- [Array of chromosomes]
  """
    if p_type == 'knapsack':
        return np.array([Chromosome(gn, init_func) for _ in range(n)])
    elif p_type == 'double_pole':
        return np.array([ChromosomePole(gn, init_func) for _ in range(n)])