parameters: (self, currentGeneration, paramName)
return_statement: return nextGeneration
docstring: Master function that takes an existing set of paramters, sorts them by score
          and then returns a new set of parameters based on the specification file
docstring_summary: Master function that takes an existing set of paramters, sorts them by score
          and then returns a new set of parameters based on the specification file
docstring_tokens: Master function that takes an existing set of paramters sorts them by score and then returns a new set of parameters based on the specification file
function: def generationGenerator(self, currentGeneration, paramName):
          """
          Master function that takes an existing set of paramters, sorts them by score
          and then returns a new set of parameters based on the specification file
          """
  
  	numTrials = self.jConf['learningParams']['numTrials']
  
          params = self.jConf["learningParams"][paramName]
  
          useAvg = params['useAverage']
  
          nextGeneration = LastUpdatedOrderedDict()
  
          # Are we doing monteCarlo or starting a new trial?
          if (len(currentGeneration) == 0 or params['monteCarlo']):
  
              # Starting a new trial - load previous results, if any, unless doing monteCarlo
              if (not (params['learning'] and params['monteCarlo'])):
                  for i in range(0, params['startingControllers']):
                      inFile = self.path + self.jConf['filePrefix'] + "_" + str(i) + self.jConf['fileSuffix']
                      # We want the IO error if this fails
                      fin = open(inFile, 'r')
                      jControl = json.load(fin)
                      fin.close()
                      controller = {}
                      # Check how controller was generated
                      try:
                          controller['params'] = jControl[paramName]['params']
                      except KeyError:
                          controller['params'] = jControl[paramName]
                      except TypeError:
                          controller['params'] = jControl[paramName]
                      controller['paramID'] = str(self.paramID)
                      controller['scores'] = []
                      nextGeneration.__setitem__(controller['paramID'], controller)
  
                      self.paramID += 1
  
              # Fill in remaining population with random parameters
              for i in range(len(nextGeneration), params['populationSize']):
  		if(params['monteCarlo'] and params['populationSize'] != numTrials):
  	            raise NTRTMasterError("Number of trials must equal population size!")
                  if (i < 0):
                      raise NTRTMasterError("Number of controllers greater than population size!")
  
                  controller = self.__getNewParams(paramName)
                  nextGeneration.__setitem__(controller['paramID'], controller)
                  self.paramID += 1
  
          elif (not params['learning']):
              # Not learning, return previous controllers
              nextGeneration = currentGeneration
  
          else:
              # learning, not doing monteCarlo, have a previous generation
  
              popSize = len(currentGeneration)
  
              # order the prior population
  
              for k, controller in currentGeneration.iteritems() :
                  scores = controller['scores']
                  controller['maxScore'] = max(scores)
                  controller['avgScore'] = sum(scores) / float(len(scores))
  
  
              if (useAvg):
                  key = lambda x: x[1]['avgScore']
              else:
                  key = lambda x: x[1]['maxScore']
              sortedGeneration = collections.OrderedDict(sorted(currentGeneration.items(), None, key, True))
  
              scoreDump = open('scoreDump.txt', 'a')
              scoreDump.write(json.dumps(sortedGeneration))
              scoreDump.write('\n')
              scoreDump.close()
  
              totalScore = 0
  
              # Get probabilities for children (for mating)
  
              if (useAvg):
  
                  for controller in sortedGeneration.itervalues():
                      pass
  
                  floor = controller['avgScore']
  
                  for controller in sortedGeneration.itervalues():
                      totalScore += controller['avgScore'] - floor
  
                  first = True
                  c1 = {}
  
                  # All scores are the same for some reason, don't divide by zero
                  if totalScore == 0.0:
                      totalScore = 1
  
                  for c in sortedGeneration.itervalues():
                      if first:
                          c['probability'] = (c['avgScore'] - floor) / totalScore
                          first = False
                      else:
                          c['probability'] = (c['avgScore'] - floor) / totalScore + c1['probability']
                      c1 = c
  
              else:
                  for controller in sortedGeneration.itervalues():
                      pass
  
                  floor = controller['maxScore']
  
                  for controller in sortedGeneration.itervalues():
                      totalScore += controller['maxScore'] - floor
  
                  first = True
                  c1 = {}
                  # All scores are the same for some reason, don't divide by zero
                  if totalScore == 0.0:
                      totalScore = 1
                  for c in sortedGeneration.itervalues():
                      if first:
                          c['probability'] = (c['maxScore'] - floor) / totalScore
                          first = False
                      else:
                          c['probability'] = (c['maxScore'] - floor) / totalScore + c1['probability']
                      c1 = c
  
              # How many of the best controllers are we keeping?
              numElites = popSize - (params['numberToMutate'] + params['numberOfChildren'])
  
              if (numElites < 0):
                  raise NTRTMasterError("Population slated to grow in size! Please adjust population size, number to mutate and/or number of children")
  
              # Copy elites to new generation
              count = 0
              for c in sortedGeneration.itervalues():
                  # Stop when number of elites has been reached
                  if (count >= numElites):
                      break
  
                  nextGeneration.__setitem__(c['paramID'], c)
                  if c['params'] == None:
                      raise NTRTMasterError("Found it!")
                  count += 1
  
              # Add 'asexual' mutations to next generation.
              # TODO: make option that assigns mutations to random controllers, rather than just mutating top N
              count = 0
              for c in sortedGeneration.itervalues():
                  if (count >= params['numberToMutate']):
                      break
                  cNew = {}
                  cNew['params'] = self.__mutateParams(c['params'], paramName)
                  cNew['paramID'] = str(self.paramID)
                  cNew['scores'] = []
  
                  nextGeneration.__setitem__(cNew['paramID'], cNew)
                  self.paramID += 1
  
                  if cNew['params'] == None:
                      raise NTRTMasterError("Found it!")
  
                  count += 1
  
              # Add children to new generation
              for i in range(params['numberOfChildren']):
  
                  c1Prob = random.random()
                  c2Prob = random.random()
  
                  c1 = self.__getControllerFromProbability(sortedGeneration, c1Prob)
                  c2 = self.__getControllerFromProbability(sortedGeneration, c2Prob)
  
                  while (c1 == c2):
                      c2Prob = random.random()
                      c2 = self.__getControllerFromProbability(sortedGeneration, c2Prob)
  
                  cNew = {}
                  cNew['params'] = self.__getChildController(c1['params'], c2['params'], params)
  
                  if (random.random() >= params['childMutationChance']):
                      cNew['params'] = self.__mutateParams(cNew['params'], paramName)
  
                  cNew['paramID'] = str(self.paramID)
                  cNew['scores'] = []
  
                  nextGeneration.__setitem__(cNew['paramID'], cNew)
                  self.paramID += 1
  
                  if cNew['params'] == None:
                      raise NTRTMasterError("Found it!")
  
              if (len(nextGeneration) != popSize):
                  raise NTRTMasterError("Failed to generate the correct number of controllers")
  
              if (params['numberOfStates'] > 0):
                  for c in nextGeneration.itervalues():
                      c['params']['neuralFilename'] = "logs/bestParameters-test_fb-"+ c['paramID'] +".nnw"
                      self.__writeToNNW(c['params']['neuralParams'], self.path + c['params']['neuralFilename'])
  
          return nextGeneration
function_tokens: def generationGenerator ( self , currentGeneration , paramName ) : numTrials = self . jConf [ 'learningParams' ] [ 'numTrials' ] params = self . jConf [ "learningParams" ] [ paramName ] useAvg = params [ 'useAverage' ] nextGeneration = LastUpdatedOrderedDict ( ) # Are we doing monteCarlo or starting a new trial? if ( len ( currentGeneration ) == 0 or params [ 'monteCarlo' ] ) : # Starting a new trial - load previous results, if any, unless doing monteCarlo if ( not ( params [ 'learning' ] and params [ 'monteCarlo' ] ) ) : for i in range ( 0 , params [ 'startingControllers' ] ) : inFile = self . path + self . jConf [ 'filePrefix' ] + "_" + str ( i ) + self . jConf [ 'fileSuffix' ] # We want the IO error if this fails fin = open ( inFile , 'r' ) jControl = json . load ( fin ) fin . close ( ) controller = { } # Check how controller was generated try : controller [ 'params' ] = jControl [ paramName ] [ 'params' ] except KeyError : controller [ 'params' ] = jControl [ paramName ] except TypeError : controller [ 'params' ] = jControl [ paramName ] controller [ 'paramID' ] = str ( self . paramID ) controller [ 'scores' ] = [ ] nextGeneration . __setitem__ ( controller [ 'paramID' ] , controller ) self . paramID += 1 # Fill in remaining population with random parameters for i in range ( len ( nextGeneration ) , params [ 'populationSize' ] ) : if ( params [ 'monteCarlo' ] and params [ 'populationSize' ] != numTrials ) : raise NTRTMasterError ( "Number of trials must equal population size!" ) if ( i < 0 ) : raise NTRTMasterError ( "Number of controllers greater than population size!" ) controller = self . __getNewParams ( paramName ) nextGeneration . __setitem__ ( controller [ 'paramID' ] , controller ) self . paramID += 1 elif ( not params [ 'learning' ] ) : # Not learning, return previous controllers nextGeneration = currentGeneration else : # learning, not doing monteCarlo, have a previous generation popSize = len ( currentGeneration ) # order the prior population for k , controller in currentGeneration . iteritems ( ) : scores = controller [ 'scores' ] controller [ 'maxScore' ] = max ( scores ) controller [ 'avgScore' ] = sum ( scores ) / float ( len ( scores ) ) if ( useAvg ) : key = lambda x : x [ 1 ] [ 'avgScore' ] else : key = lambda x : x [ 1 ] [ 'maxScore' ] sortedGeneration = collections . OrderedDict ( sorted ( currentGeneration . items ( ) , None , key , True ) ) scoreDump = open ( 'scoreDump.txt' , 'a' ) scoreDump . write ( json . dumps ( sortedGeneration ) ) scoreDump . write ( '\n' ) scoreDump . close ( ) totalScore = 0 # Get probabilities for children (for mating) if ( useAvg ) : for controller in sortedGeneration . itervalues ( ) : pass floor = controller [ 'avgScore' ] for controller in sortedGeneration . itervalues ( ) : totalScore += controller [ 'avgScore' ] - floor first = True c1 = { } # All scores are the same for some reason, don't divide by zero if totalScore == 0.0 : totalScore = 1 for c in sortedGeneration . itervalues ( ) : if first : c [ 'probability' ] = ( c [ 'avgScore' ] - floor ) / totalScore first = False else : c [ 'probability' ] = ( c [ 'avgScore' ] - floor ) / totalScore + c1 [ 'probability' ] c1 = c else : for controller in sortedGeneration . itervalues ( ) : pass floor = controller [ 'maxScore' ] for controller in sortedGeneration . itervalues ( ) : totalScore += controller [ 'maxScore' ] - floor first = True c1 = { } # All scores are the same for some reason, don't divide by zero if totalScore == 0.0 : totalScore = 1 for c in sortedGeneration . itervalues ( ) : if first : c [ 'probability' ] = ( c [ 'maxScore' ] - floor ) / totalScore first = False else : c [ 'probability' ] = ( c [ 'maxScore' ] - floor ) / totalScore + c1 [ 'probability' ] c1 = c # How many of the best controllers are we keeping? numElites = popSize - ( params [ 'numberToMutate' ] + params [ 'numberOfChildren' ] ) if ( numElites < 0 ) : raise NTRTMasterError ( "Population slated to grow in size! Please adjust population size, number to mutate and/or number of children" ) # Copy elites to new generation count = 0 for c in sortedGeneration . itervalues ( ) : # Stop when number of elites has been reached if ( count >= numElites ) : break nextGeneration . __setitem__ ( c [ 'paramID' ] , c ) if c [ 'params' ] == None : raise NTRTMasterError ( "Found it!" ) count += 1 # Add 'asexual' mutations to next generation. # TODO: make option that assigns mutations to random controllers, rather than just mutating top N count = 0 for c in sortedGeneration . itervalues ( ) : if ( count >= params [ 'numberToMutate' ] ) : break cNew = { } cNew [ 'params' ] = self . __mutateParams ( c [ 'params' ] , paramName ) cNew [ 'paramID' ] = str ( self . paramID ) cNew [ 'scores' ] = [ ] nextGeneration . __setitem__ ( cNew [ 'paramID' ] , cNew ) self . paramID += 1 if cNew [ 'params' ] == None : raise NTRTMasterError ( "Found it!" ) count += 1 # Add children to new generation for i in range ( params [ 'numberOfChildren' ] ) : c1Prob = random . random ( ) c2Prob = random . random ( ) c1 = self . __getControllerFromProbability ( sortedGeneration , c1Prob ) c2 = self . __getControllerFromProbability ( sortedGeneration , c2Prob ) while ( c1 == c2 ) : c2Prob = random . random ( ) c2 = self . __getControllerFromProbability ( sortedGeneration , c2Prob ) cNew = { } cNew [ 'params' ] = self . __getChildController ( c1 [ 'params' ] , c2 [ 'params' ] , params ) if ( random . random ( ) >= params [ 'childMutationChance' ] ) : cNew [ 'params' ] = self . __mutateParams ( cNew [ 'params' ] , paramName ) cNew [ 'paramID' ] = str ( self . paramID ) cNew [ 'scores' ] = [ ] nextGeneration . __setitem__ ( cNew [ 'paramID' ] , cNew ) self . paramID += 1 if cNew [ 'params' ] == None : raise NTRTMasterError ( "Found it!" ) if ( len ( nextGeneration ) != popSize ) : raise NTRTMasterError ( "Failed to generate the correct number of controllers" ) if ( params [ 'numberOfStates' ] > 0 ) : for c in nextGeneration . itervalues ( ) : c [ 'params' ] [ 'neuralFilename' ] = "logs/bestParameters-test_fb-" + c [ 'paramID' ] + ".nnw" self . __writeToNNW ( c [ 'params' ] [ 'neuralParams' ] , self . path + c [ 'params' ] [ 'neuralFilename' ] ) return nextGeneration