Exemple #1
0
def run(self):
  """ Performs a random search """
  import standard
  
  def checkpoints():
    result = True
    for chk in self.checkpoints:
      if chk(self) == False: result = False
    return result

  # Checks that self is complete and fills in where possible
  if hasattr(self, "fill_attributes"): self = self.fill_attributes()
  else: self = standard.fill_attributes(self)
  assert hasattr(self, "Individual"), "No Individual type.\n"
  assert hasattr(self, "taboo"), "No taboo operation.\n"
  assert hasattr(self, "evaluation"), "No evaluation operation.\n"
  assert hasattr(self, "checkpoints"), "No checkpoints attribute.\n"

  while checkpoints():
    indiv = self.Individual()
    j = 0
    loop = True
    while loop:
      indiv = self.Individual()
      loop = self.taboo(indiv)
      j += 1
      assert j < max(50*self.popsize, 100), "Could not create offspring.\n"
    indiv.birth = self.current_gen
    self.population = [indiv]
    self.evaluation() 

    self.current_gen += 1
Exemple #2
0
def run(self):
  """ Performs a Genetic Algorithm search """
  import standard
 
  # runs the checkpoints
  def checkpoints():
    result = True
    for chk in self.checkpoints:
      if chk(self) == False: result = False
    return result


  # Checks that self is complete and fills in where possible
  if hasattr(self, "fill_attributes"): self.fill_attributes()
  else: self = standard.fill_attributes(self)
  assert hasattr(self, "Individual"), "No Individual type.\n"
  assert hasattr(self, "taboo"), "No taboo operation.\n"
  assert hasattr(self, "population"), "No population attribute.\n"
  assert hasattr(self, "popsize"), "No popsize attribute.\n"
  assert hasattr(self, "current_gen"), "No current_gen attribute.\n"
  assert hasattr(self, "evaluation"), "No evaluation operation.\n"
  assert hasattr(self, "rate"), "No rate attribute.\n"
  assert hasattr(self, "mating"), "No mating functor.\n"
  assert hasattr(self, "offspring"), "No offspring attribute.\n"
  assert hasattr(self, "comparison"), "No comparison operation.\n"

  # creates population if it does not exist.
  if self.comm.is_root: 
    while len(self.population) < self.popsize:
      j = 0
      loop = True
      while loop:
        indiv = self.Individual()
        loop = self.taboo(indiv)
        j += 1
        assert j < max(50*self.popsize, 100), "Could not create offspring.\n"
      indiv.birth = self.current_gen
      self.population.append(indiv)
  self.population = self.comm.broadcast(self.population)
  
  # evaluates population if need be.
  self.evaluation() 
  # now makes sure evaluation did not create twins.
  dummy, self.population = self.population, []
  for indiv in dummy:
    if any(indiv == u for u in self.population): continue
    self.population.append(indiv)

  # Number of offspring per generation.
  nboffspring = max(int(float(self.popsize)*self.rate), 1)

  # generational loop
  while checkpoints(): 
    if self.comm.do_print:
      print "\nStarting generation ", self.current_gen

    # tries and creates offspring.
    if self.comm.rank == 0:
      while len(self.offspring) < nboffspring:
        j = 0
        loop = True
        while loop:
          indiv = self.mating()
          loop = self.taboo(indiv)
          j += 1
          assert j < max(10*self.popsize, 100), "Could not create offspring.\n"
        indiv.birth = self.current_gen
        self.offspring.append(indiv)
      self.comm.broadcast(self.offspring)
    else: self.offspring = self.comm.broadcast(self.offspring)


    # now evaluates population.
    self.evaluation()

    # finally, sort and replace.
    if self.comm.rank == 0:
      # deal with differences in function sorted between python versions.
      from platform import python_version_tuple
      if python_version_tuple()[0] > 2:
        from functools import cmp_to_key
        self.population = sorted(self.population, key=cmp_to_key(self.comparison))
      else: 
        self.population = sorted(self.population, cmp=self.comparison)
      # Inserts individual one by one ensuring they do not yet exist in the
      # population. This ensures that duplicates are not allowed.
      for indiv in self.offspring:
        if any(indiv == u for u in self.population): continue
        self.population.insert(0, indiv)
        # In case population smaller than expected, use conditional.
        if len(self.population) > self.popsize: self.population.pop(-1)
    self.population = self.comm.broadcast(self.population)
    
    self.offspring = []
    self.current_gen += 1 

  # final stuff before exiting.
  if hasattr(self, "final"): self.final()
  elif self.comm.do_print: print "done"