当前位置: 首页>>代码示例>>Python>>正文


Python GPopulation.bestRaw方法代码示例

本文整理汇总了Python中GPopulation.GPopulation.bestRaw方法的典型用法代码示例。如果您正苦于以下问题:Python GPopulation.bestRaw方法的具体用法?Python GPopulation.bestRaw怎么用?Python GPopulation.bestRaw使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在GPopulation.GPopulation的用法示例。


在下文中一共展示了GPopulation.bestRaw方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: step

# 需要导入模块: from GPopulation import GPopulation [as 别名]
# 或者: from GPopulation.GPopulation import bestRaw [as 别名]
   def step(self):
      """ Just do one step in evolution, one generation """
      genomeMom = None
      genomeDad = None

      newPop = GPopulation(self.internalPop)
      logging.debug("Population was cloned.")

      size_iterate = len(self.internalPop)

      # Odd population size
      if size_iterate % 2 != 0: size_iterate -= 1

      crossover_empty = self.select(popID=self.currentGeneration).crossover.isEmpty()

      for i in xrange(0, size_iterate, 2):
         genomeMom = self.select(popID=self.currentGeneration)
         genomeDad = self.select(popID=self.currentGeneration)

         if not crossover_empty and self.pCrossover >= 1.0:
            for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=2):
               (sister, brother) = it
         else:
            if not crossover_empty and Util.randomFlipCoin(self.pCrossover):
               for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=2):
                  (sister, brother) = it
            else:
               sister = genomeMom.clone()
               brother = genomeDad.clone()

         sister.mutate(pmut=self.pMutation, ga_engine=self)
         brother.mutate(pmut=self.pMutation, ga_engine=self)

         newPop.internalPop.append(sister)
         newPop.internalPop.append(brother)

      if len(self.internalPop) % 2 != 0:
         genomeMom = self.select(popID=self.currentGeneration)
         genomeDad = self.select(popID=self.currentGeneration)

         if Util.randomFlipCoin(self.pCrossover):
            for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=1):
               (sister, brother) = it
         else:
            sister = random.choice([genomeMom, genomeDad])
            sister = sister.clone()
            sister.mutate(pmut=self.pMutation, ga_engine=self)

         newPop.internalPop.append(sister)

      logging.debug("Evaluating the new created population.")
      newPop.evaluate()

      if self.elitism:
         logging.debug("Doing elitism.")
         if self.getMinimax() == Consts.minimaxType["maximize"]:
            for i in xrange(self.nElitismReplacement):
               #re-evaluate before being sure this is the best
               self.internalPop.bestRaw(i).evaluate()
               if self.internalPop.bestRaw(i).score > newPop.bestRaw(i).score:
                  newPop[len(newPop)-1-i] = self.internalPop.bestRaw(i)
         elif self.getMinimax() == Consts.minimaxType["minimize"]:
            for i in xrange(self.nElitismReplacement):
               #re-evaluate before being sure this is the best
               self.internalPop.bestRaw(i).evaluate()
               if self.internalPop.bestRaw(i).score < newPop.bestRaw(i).score:
                  newPop[len(newPop)-1-i] = self.internalPop.bestRaw(i)

      self.internalPop = newPop
      self.internalPop.sort()

      logging.debug("The generation %d was finished.", self.currentGeneration)

      self.currentGeneration += 1

      return (self.currentGeneration == self.nGenerations)
开发者ID:aguirrea,项目名称:pyevolve,代码行数:78,代码来源:GSimpleGA.py

示例2: step

# 需要导入模块: from GPopulation import GPopulation [as 别名]
# 或者: from GPopulation.GPopulation import bestRaw [as 别名]
   def step(self):
      """ Just do one step in evolution, one generation """
      genomeMom = None
      genomeDad = None

      newPop = GPopulation(self.internalPop)

      if (MPI.COMM_WORLD.Get_rank() == 0): ### assumes WE are on top of hierarchy!
          popsize = len(self.internalPop)
          numAdded = 0
          maxTries = 1000
          numTries = 0

          crossover_empty = self.select(popID=self.currentGeneration).crossover.isEmpty()

          ###TODO: enforce constraints!###
          while numAdded < popsize:
             genomeMom = self.select(popID=self.currentGeneration)
             genomeDad = self.select(popID=self.currentGeneration)

             if not crossover_empty and self.pCrossover >= 1.0:
                for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=2):
                   (sister, brother) = it
             else:
                if not crossover_empty and Util.randomFlipCoin(self.pCrossover):
                   for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=2):
                      (sister, brother) = it
                else:
                   sister = genomeMom.clone()
                   brother = genomeDad.clone()
    #               logging.debug("done cloning")

             sister.mutate(pmut=self.pMutation, ga_engine=self)
             brother.mutate(pmut=self.pMutation, ga_engine=self)

             if (numTries > maxTries or self.owner.eval_constraints(sister)):
                newPop.internalPop.append(sister)
                numAdded += 1
                print "successfully added sister"
             if (numAdded < popsize and (numTries > maxTries or self.owner.eval_constraints(brother))):
                newPop.internalPop.append(brother)
                print "successfully added brother"
                numAdded += 1
             numTries += 1

      #end rank0 onlye
    #      print "rank %d start eval pop" % MPI.COMM_WORLD.Get_rank()

      logging.debug("Evaluating the newly created population.")
      newPop.evaluate()
#      print "rank %d done eval pop" % MPI.COMM_WORLD.Get_rank()
#      if (MPI.COMM_WORLD.Get_rank() == 0):
#         print "after eval, new pop's positions are:"
#         for p in newPop:
#            print p.wt_positions

      if (MPI.COMM_WORLD.Get_rank() == 0): ### assumes WE are on top of hierarchy!
          if self.elitism:
             logging.debug("Doing elitism, %d" % self.nElitismReplacement)
             if self.getMinimax() == Consts.minimaxType["maximize"]:
                for i in xrange(self.nElitismReplacement):
                   #re-evaluate before being sure this is the best
    #               self.internalPop.bestRaw(i).evaluate()
                   if self.internalPop.bestRaw(i).score > newPop.bestRaw(i).score:
                      newPop[len(newPop)-1-i] = self.internalPop.bestRaw(i)
             elif self.getMinimax() == Consts.minimaxType["minimize"]:
                for i in xrange(self.nElitismReplacement):
                   #re-evaluate before being sure this is the best
    #               self.internalPop.bestRaw(i).evaluate()
                   if self.internalPop.bestRaw(i).score < newPop.bestRaw(i).score:
                      newPop[len(newPop)-1-i] = self.internalPop.bestRaw(i)

      self.internalPop = newPop
      if (MPI.COMM_WORLD.Get_rank() == 0): ### assumes WE are on top of hierarchy!
         self.internalPop.sort()
#      if (MPI.COMM_WORLD.Get_rank() == 0):
#         print "after sort, internal pop's positions are:"
#         for p in self.internalPop:
#            print p.wt_positions

      logging.debug("The generation %d was finished.", self.currentGeneration)
      self.currentGeneration += 1

      if (MPI.COMM_WORLD.Get_rank() == 0): ### assumes WE are on top of hierarchy!
         self.saveState ()

      return (self.currentGeneration == self.nGenerations)
开发者ID:kilojoules,项目名称:Pyevolve,代码行数:89,代码来源:GSimpleGA.py

示例3: your_func

# 需要导入模块: from GPopulation import GPopulation [as 别名]
# 或者: from GPopulation.GPopulation import bestRaw [as 别名]

#.........这里部分代码省略.........

   def getCurrentGeneration(self):
      """ Gets the current generation

      :rtype: the current generation

      """
      return self.currentGeneration

   def setElitism(self, flag):
      """ Sets the elitism option, True or False

      :param flag: True or False

      """
      if type(flag) != BooleanType:
         Util.raiseException("Elitism option must be True or False", TypeError)
      self.elitism = flag

   def getDBAdapter(self):
      """ Gets the DB Adapter of the GA Engine

      :rtype: a instance from one of the :mod:`DBAdapters` classes

      """
      return self.dbAdapter

   def bestIndividual(self):
      """ Returns the population best individual

      :rtype: the best individual

      """
      return self.internalPop.bestRaw()

   def worstIndividual(self):
      """ Returns the population worst individual

      :rtype: the best individual

      """
      return self.internalPop.worstRaw()

   def __gp_catch_functions(self, prefix):
      """ Internally used to catch functions with some specific prefix
      as non-terminals of the GP core """
      import __main__ as mod_main

      function_set = {}

      main_dict = mod_main.__dict__
      for obj, addr in main_dict.items():
         if obj[0:len(prefix)] == prefix:
            try:
               op_len = addr.func_code.co_argcount
            except:
               continue
            function_set[obj] = op_len

      if len(function_set) <= 0:
         Util.raiseException("No function set found using function prefix '%s' !" % prefix, ValueError)

      self.setParams(gp_function_set=function_set)

   def initialize(self):
      """ Initializes the GA Engine. Create and initialize population """
开发者ID:aguirrea,项目名称:pyevolve,代码行数:70,代码来源:GSimpleGA.py

示例4: step

# 需要导入模块: from GPopulation import GPopulation [as 别名]
# 或者: from GPopulation.GPopulation import bestRaw [as 别名]
   def step(self):
      """ Just do one step in evolution, one generation """
      genomeMom = None
      genomeDad = None

      newPop = GPopulation(self.internalPop)
      logging.debug("Population was cloned.")
      
      size_iterate = len(self.internalPop)

      # Odd population size
      if size_iterate % 2 != 0: size_iterate -= 1

      #Check on the crossover function by picking a random individual - is it empty?
      crossover_empty = self.select(popID=self.currentGeneration).crossover.isEmpty()
      
      for i in xrange(0, size_iterate, 2):
         #Ok, we select 2 parents using the selector (RouletteWheel, etc.)
         genomeMom = self.select(popID=self.currentGeneration)
         genomeDad = self.select(popID=self.currentGeneration)

         if not crossover_empty and self.pCrossover >= 1.0:
            #Crossover all of them
            for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=2):
               (sister, brother) = it
         else:
            #Filp a coin each time to determine if you should crossover
            if not crossover_empty and Util.randomFlipCoin(self.pCrossover):
               for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=2):
                  (sister, brother) = it
            else:
               sister = genomeMom.clone()
               brother = genomeDad.clone()
               #And "pre" mutate them
               sister.premutate(pmut=self.pPreMutation, ga_engine=self)
               brother.premutate(pmut=self.pPreMutation, ga_engine=self)

         #Now each offspring is mutated
         sister.mutate(pmut=self.pMutation, ga_engine=self)
         brother.mutate(pmut=self.pMutation, ga_engine=self)

         newPop.internalPop.append(sister)
         newPop.internalPop.append(brother)

      if len(self.internalPop) % 2 != 0:
         #Odd-numbered population
         genomeMom = self.select(popID=self.currentGeneration)
         genomeDad = self.select(popID=self.currentGeneration)

         if Util.randomFlipCoin(self.pCrossover):
            for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=1):
               (sister, brother) = it
         else:
            sister = random.choice([genomeMom, genomeDad])
            sister = sister.clone()
            #Do the 2 mutations
            sister.premutate(pmut=self.pPreMutation, ga_engine=self)
            sister.mutate(pmut=self.pMutation, ga_engine=self)

         newPop.internalPop.append(sister)

      #---- Evaluate fitness ------
      logging.debug("Evaluating the new created population.")
      newPop.evaluate()

      #Niching methods- Petrowski's clearing
      self.clear()

      if self.elitism:
         #Avoid too much elitism
         if self.nElitismReplacement >= len(self.internalPop):
             self.nElitismReplacement = len(self.internalPop)-1

         logging.debug("Doing elitism.")
         if self.getMinimax() == Consts.minimaxType["maximize"]:
            #Replace the n-th worst new ones with the nth best old ones
            for i in xrange(self.nElitismReplacement):
               if self.internalPop.bestRaw(i).score > newPop.bestRaw(i).score:
                  newPop[len(newPop)-1-i] = self.internalPop.bestRaw(i)
         elif self.getMinimax() == Consts.minimaxType["minimize"]:
            for i in xrange(self.nElitismReplacement):
               if self.internalPop.bestRaw(i).score < newPop.bestRaw(i).score:
                  newPop[len(newPop)-1-i] = self.internalPop.bestRaw(i)

      self.internalPop = newPop
      self.internalPop.sort()

      logging.debug("The generation %d was finished.", self.currentGeneration)

      self.currentGeneration += 1

      return (self.currentGeneration >= self.nGenerations)
开发者ID:neutrons,项目名称:CrystalPlan,代码行数:94,代码来源:GSimpleGA.py


注:本文中的GPopulation.GPopulation.bestRaw方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。