Beispiel #1
0
def Evaluate(set):
  global signalYield, signalSample, backgroundYield, backgroundSample

  # Reading variable list
  variables = open('%s/inputvars.txt' % set['directory']).readlines()
  variables = [variable.rstrip() for variable in variables]

  # Setting the number of input and output in the neat config file
  file = open ('%s/neat.config' % set['directory'])
  template = string.Template(file.read())
  file.close()
  file = open ('%s/neat.config' % set['directory'], 'w')
  file.write(template.safe_substitute(input_nodes = len(variables), output_nodes = 1))
  file.close()

  # Read neat configuration file
  config.load('%s/neat.config' % set['directory'])
  
  print('Training: Reading signal files')

  # Signal topovars
  signals = TopovarReader(variables)

  # Adding signal files
  for sample in Common.TrainingSignals:
    signals.add('%s/%s/%s.root' % (
        Common.SampleLocation, Common.TrainingSample, Common.filename(set, sample)
      )
    )

  # Creating a variable normalizer
  normalizer = VariableNormalizer(variables)

  # Saving the population in buffer
  signalSample = signals.sample(compress = True)
  
  # Adding the sample to the normalizer
  normalizer.add(signalSample)
    
  # Compute the total weight
  signalYield = normalizer.getTotalWeight()

  print ('Training: Reading background files')

  # Background topovars
  backgrounds = TopovarReader(variables)

  # Adding background files
  for sample in Common.TrainingBackgrounds:
    backgrounds.add('%s/%s/%s.root' % (
        Common.SampleLocation, Common.TrainingSample, Common.filename(set, sample)
      )
    )

  # Saving the population in buffer
  backgroundSample = backgrounds.sample(compress = True)

  # Adding the sample to the normalizer
  normalizer.add(backgroundSample)

  # Compute the total weight
  backgroundYield = normalizer.getTotalWeight() - signalYield

  # Reporting the normalization
  normalizer.report()

  # Normalization of the variables
  normalizer.normalizeSample(signalSample)
  normalizer.normalizeSample(backgroundSample)

  # NEAT training
  chromosome.node_gene_type = genome.NodeGene  
  population.Population.evaluate = FitnessFunctionWrapper
  pop = population.Population()
  pop.epoch(int(set['number_generations']), report=True, save_best=False, checkpoint_interval = None)
  winner = pop.stats[0][-1]
  print 'Training: Number of evaluations: %d' % winner.id
  print 'Training: Best NN fitness: %0.2f' % winner.fitness

  # Save the best network
  file = open('%s/winner.dat' % set['directory'], 'w')
  pickle.dump(winner, file)
  file.close()
  # Save the best network
  file = open('%s/winner-fitness.txt' % set['directory'], 'w')
  file.write('%f' % winner.fitness)
  file.close()
Beispiel #2
0
def Evaluate(set):
    global signalYield, signalSample, backgroundYield, backgroundSample

    # Reading variable list
    variables = open('%s/inputvars.txt' % set['directory']).readlines()
    variables = [variable.rstrip() for variable in variables]

    # Setting the number of input and output in the neat config file
    file = open('%s/neat.config' % set['directory'])
    template = string.Template(file.read())
    file.close()
    file = open('%s/neat.config' % set['directory'], 'w')
    file.write(
        template.safe_substitute(input_nodes=len(variables), output_nodes=1))
    file.close()

    # Read neat configuration file
    config.load('%s/neat.config' % set['directory'])

    print('Training: Reading signal files')

    # Signal topovars
    signals = TopovarReader(variables)

    # Adding signal files
    for sample in Common.TrainingSignals:
        signals.add('%s/%s/%s.root' %
                    (Common.SampleLocation, Common.TrainingSample,
                     Common.filename(set, sample)))

    # Creating a variable normalizer
    normalizer = VariableNormalizer(variables)

    # Saving the population in buffer
    signalSample = signals.sample(compress=True)

    # Adding the sample to the normalizer
    normalizer.add(signalSample)

    # Compute the total weight
    signalYield = normalizer.getTotalWeight()

    print('Training: Reading background files')

    # Background topovars
    backgrounds = TopovarReader(variables)

    # Adding background files
    for sample in Common.TrainingBackgrounds:
        backgrounds.add('%s/%s/%s.root' %
                        (Common.SampleLocation, Common.TrainingSample,
                         Common.filename(set, sample)))

    # Saving the population in buffer
    backgroundSample = backgrounds.sample(compress=True)

    # Adding the sample to the normalizer
    normalizer.add(backgroundSample)

    # Compute the total weight
    backgroundYield = normalizer.getTotalWeight() - signalYield

    # Reporting the normalization
    normalizer.report()

    # Normalization of the variables
    normalizer.normalizeSample(signalSample)
    normalizer.normalizeSample(backgroundSample)

    # NEAT training
    chromosome.node_gene_type = genome.NodeGene
    population.Population.evaluate = FitnessFunctionWrapper
    pop = population.Population()
    pop.epoch(int(set['number_generations']),
              report=True,
              save_best=False,
              checkpoint_interval=None)
    winner = pop.stats[0][-1]
    print 'Training: Number of evaluations: %d' % winner.id
    print 'Training: Best NN fitness: %0.2f' % winner.fitness

    # Save the best network
    file = open('%s/winner.dat' % set['directory'], 'w')
    pickle.dump(winner, file)
    file.close()
    # Save the best network
    file = open('%s/winner-fitness.txt' % set['directory'], 'w')
    file.write('%f' % winner.fitness)
    file.close()