Exemplo n.º 1
0
    def __call__(self, population, signalYield, signalSample, backgroundYield,
                 backgroundSample):
        # Loop over the chromosomes in the population
        for genome in population:

            # Get a nn describe by the chromosome (feed foreward)
            net = nn.create_ffphenotype(genome)
            error = 0.0
            # Loop over the events
            for event in signalSample:
                # Extract variables and weight
                variables = event[1:]
                weight = event[0] / signalYield
                # Not strictly necessary in feedforward nets
                net.flush()
                # Computing the error
                error = error + weight * (
                    1 - net.sactivate(variables)[0])**self.norm

            # Loop over the events
            for event in backgroundSample:
                # Extract variables and weight
                variables = event[1:]
                weight = event[0] / backgroundYield
                # Not strictly necessary in feedforward nets
                net.flush()
                # Computing the error
                error = error + weight * (
                    net.sactivate(variables)[0])**self.norm

            # Set the fitness value to the chomosome
            genome.fitness = 1 - Math.pow(error / 2, 1. / self.norm)
Exemplo n.º 2
0
  def __call__(self, population, signalYield, signalSample, backgroundYield, backgroundSample):
    # Loop over the chromosomes in the population 
    for genome in population:
  
      # Get a nn describe by the chromosome (feed foreward)
      net = nn.create_ffphenotype(genome)   
      error = 0.0
      # Loop over the events
      for event in signalSample:
        # Extract variables and weight
        variables = event[1:]
        weight = event[0]/signalYield
        # Not strictly necessary in feedforward nets
        net.flush()
        # Computing the error
        error = error + weight*(1 - net.sactivate(variables)[0])**self.norm

      # Loop over the events
      for event in backgroundSample:
        # Extract variables and weight
        variables = event[1:]
        weight = event[0]/backgroundYield
        # Not strictly necessary in feedforward nets
        net.flush()
        # Computing the error
        error = error + weight*(net.sactivate(variables)[0])**self.norm
    
      # Set the fitness value to the chomosome
      genome.fitness = 1 - Math.pow(error/2, 1./self.norm)
Exemplo n.º 3
0
    def __call__(self, population, signalYield, signalSample, backgroundYield,
                 backgroundSample):

        # Histogram holders
        signalHistogram = TH1F('signalHistogram', 'signal', self.nbins, 0, 1)
        backgroundHistogram = TH1F('backgroundHistogram', 'background',
                                   self.nbins, 0, 1)

        # Loop over the chromosomes in the population
        for genome in population:

            # Reset histograms
            signalHistogram.Reset()
            backgroundHistogram.Reset()

            # Get a nn describe by the chromosome (feed foreward)
            net = nn.create_ffphenotype(genome)

            # Loop over the events creating signal histogram
            for event in signalSample:
                # Extract variables and weight
                variables = event[1:]
                weight = event[0]
                # Not strictly necessary in feedforward nets
                net.flush()
                # Net output
                output = net.sactivate(variables)[0]
                # Filling histograms
                signalHistogram.Fill(output, weight)

            # Loop over the events creating background histogram
            for event in backgroundSample:
                # Extract variables and weight
                variables = event[1:]
                weight = event[0]
                # Not strictly necessary in feedforward nets
                net.flush()
                # Net output
                output = net.sactivate(variables)[0]
                # Filling histograms
                backgroundHistogram.Fill(output, weight)

            fitness = 0

            # Computing fitness
            for bin in xrange(1, signalHistogram.GetNbinsX() + 1):
                signal = signalHistogram.GetBinContent(bin)
                background = backgroundHistogram.GetBinContent(bin)
                total = signal + background
                if total > 0:
                    fitness = fitness + signal**2 / (signal + background)

            # Adding fitness to genome
            genome.fitness = Math.sqrt(fitness)
Exemplo n.º 4
0
  def __call__(self, population, signalYield, signalSample, backgroundYield, backgroundSample):

    # Histogram holders
    signalHistogram = TH1F('signalHistogram', 'signal', self.nbins, 0, 1)
    backgroundHistogram = TH1F('backgroundHistogram', 'background', self.nbins, 0, 1)

    # Loop over the chromosomes in the population 
    for genome in population:
  
      # Reset histograms
      signalHistogram.Reset()
      backgroundHistogram.Reset()

      # Get a nn describe by the chromosome (feed foreward)
      net = nn.create_ffphenotype(genome)   

      # Loop over the events creating signal histogram
      for event in signalSample:
        # Extract variables and weight
        variables = event[1:]
        weight = event[0]
        # Not strictly necessary in feedforward nets
        net.flush()
        # Net output
        output = net.sactivate(variables)[0]
        # Filling histograms
        signalHistogram.Fill(output, weight)
            
      # Loop over the events creating background histogram
      for event in backgroundSample:
        # Extract variables and weight
        variables = event[1:]
        weight = event[0]
        # Not strictly necessary in feedforward nets
        net.flush()
        # Net output
        output = net.sactivate(variables)[0]
        # Filling histograms
        backgroundHistogram.Fill(output, weight)
    
      fitness = 0
  
      # Computing fitness
      for bin in xrange(1,signalHistogram.GetNbinsX()+1):
        signal = signalHistogram.GetBinContent(bin)
        background = backgroundHistogram.GetBinContent(bin)
        total = signal + background
        if total > 0:
          fitness = fitness + signal**2/(signal+background)

      # Adding fitness to genome
      genome.fitness = Math.sqrt(fitness)
Exemplo n.º 5
0
def eval_fitness(population):
    for chromo in population:
        net = nn.create_ffphenotype(chromo)

        error = 0.0
        #error_stanley = 0.0
        for i, inputs in enumerate(INPUTS):
            net.flush() # not strictly necessary in feedforward nets
            output = net.sactivate(inputs) # serial activation
            error += (output[0] - OUTPUTS[i])**2

            #error_stanley += math.fabs(output[0] - OUTPUTS[i])

        #chromo.fitness = (4.0 - error_stanley)**2 # (Stanley p. 43)
        chromo.fitness = 1 - math.sqrt(error/len(OUTPUTS))
Exemplo n.º 6
0
def eval_fitness(population):
    for chromo in population:
        net = nn.create_ffphenotype(chromo)

        error = 0.0
        #error_stanley = 0.0
        for i, inputs in enumerate(INPUTS):
            net.flush() # not strictly necessary in feedforward nets
            output = net.sactivate(inputs) # serial activation
            error += (output[0] - OUTPUTS[i])**2

            #error_stanley += math.fabs(output[0] - OUTPUTS[i])

        #chromo.fitness = (4.0 - error_stanley)**2 # (Stanley p. 43)
        chromo.fitness = 1 - math.sqrt(error/len(OUTPUTS))
Exemplo n.º 7
0
        #chromo.fitness = (4.0 - error_stanley)**2 # (Stanley p. 43)
        chromo.fitness = 1 - math.sqrt(error/len(OUTPUTS))

population.Population.evaluate = eval_fitness

pop = population.Population()
pop.epoch(300, report=True, save_best=False)

winner = pop.stats[0][-1]
print 'Number of evaluations: %d' %winner.id

# Visualize the winner network (requires PyDot)
visualize.draw_net(winner) # best chromosome

# Plots the evolution of the best/average fitness (requires Biggles)
visualize.plot_stats(pop.stats)
# Visualizes speciation
visualize.plot_species(pop.species_log)

# Let's check if it's really solved the problem
print '\nBest network output:'
brain = nn.create_ffphenotype(winner)
for i, inputs in enumerate(INPUTS):
    output = brain.sactivate(inputs) # serial activation
    print "%1.5f \t %1.5f" %(OUTPUTS[i], output[0])

# saves the winner
#file = open('winner_chromosome', 'w')
#pickle.dump(winner, file)
#file.close()
Exemplo n.º 8
0
    def __call__(self, population, signalYield, signalSample, backgroundYield,
                 backgroundSample):

        # Histogram holders
        signalHistogram = TH1F('signalHistogram', 'signal', self.nbins, 0, 1)
        backgroundHistogram = TH1F('backgroundHistogram', 'background',
                                   self.nbins, 0, 1)

        # Loop over the chromosomes in the population
        for genome in population:

            # Reset histograms
            signalHistogram.Reset()
            backgroundHistogram.Reset()

            # Get a nn describe by the chromosome (feed foreward)
            net = nn.create_ffphenotype(genome)

            # Loop over the events creating signal histogram
            for event in signalSample:
                # Extract variables and weight
                variables = event[1:]
                weight = event[0]
                # Not strictly necessary in feedforward nets
                net.flush()
                # Net output
                output = net.sactivate(variables)[0]
                # Filling histograms
                signalHistogram.Fill(output, weight)

            # Signal overall normalization scale
            signalScale = signalYield / len(signalSample)

            # Loop over the events creating background histogram
            for event in backgroundSample:
                # Extract variables and weight
                variables = event[1:]
                weight = event[0]
                # Not strictly necessary in feedforward nets
                net.flush()
                # Net output
                output = net.sactivate(variables)[0]
                # Filling histograms
                backgroundHistogram.Fill(output, weight)

            # Background overall normalization scale
            backgroundScale = backgroundYield / len(backgroundSample)

            # Random generators
            rcount = TRandom3(int(random.uniform(0, 65535)))
            rsignal = TRandom3(int(random.uniform(0, 65535)))
            rbackground = TRandom3(int(random.uniform(0, 65535)))

            zscore = 0.
            sqweight = 0.

            # Computing weighted z-score
            for bin in xrange(1, signalHistogram.GetNbinsX() + 1):

                newz = 0.
                oldz = 0.

                # Computing the zvalue per bin
                for point in xrange(1, self.mpoints + 1):
                    # Sample background
                    background = Math.gamma_quantile(
                        rbackground.Uniform(),
                        (backgroundHistogram.GetBinContent(bin) /
                         backgroundScale) + 1., backgroundScale)
                    # Background larger that zero
                    if background > 0.:
                        # Sampling signal
                        signal = Math.gamma_quantile(
                            rsignal.Uniform(),
                            (signalHistogram.GetBinContent(bin) / signalScale)
                            + 1., signalScale)
                        # Sampling count
                        count = rcount.Poisson(signal + background)
                        # Computing pvalue
                        pvalue = Math.poisson_cdf_c(
                            count, background) + Math.poisson_pdf(
                                count, background)
                        # Computing zvalue
                        zvalue = self.minz
                        if pvalue < 1.0:
                            zvalue = Math.normal_quantile_c(pvalue, 1)
                        # zvalue iterative average
                        newz = (zvalue + (point - 1) * oldz) / point
                        # Computing relative difference
                        error = math.fabs((newz - oldz) / newz)
                        # Convergency criteria
                        if error < self.error: break
                        # Updating oldz
                        oldz = newz
                        if point == self.mpoints:
                            self.message(
                                'Warning reach maximum number of integration %s points.'
                                % point)

                weight = self.weight(signalHistogram.GetBinCenter(bin))
                zscore = zscore + weight * newz
                sqweight = sqweight + weight**2

            # Fitness function is zscore transform back to 1 - pvalue
            # Set the fitness value to the chomosome
            genome.fitness = 1. - Math.normal_cdf_c(
                zscore / Math.sqrt(sqweight))
Exemplo n.º 9
0
  def __call__(self, population, signalYield, signalSample, backgroundYield, backgroundSample):

    # Histogram holders
    signalHistogram = TH1F('signalHistogram', 'signal', self.nbins, 0, 1)
    backgroundHistogram = TH1F('backgroundHistogram', 'background', self.nbins, 0, 1)

    # Loop over the chromosomes in the population 
    for genome in population:
  
      # Reset histograms
      signalHistogram.Reset()
      backgroundHistogram.Reset()

      # Get a nn describe by the chromosome (feed foreward)
      net = nn.create_ffphenotype(genome)   

      # Loop over the events creating signal histogram
      for event in signalSample:
        # Extract variables and weight
        variables = event[1:]
        weight = event[0]
        # Not strictly necessary in feedforward nets
        net.flush()
        # Net output
        output = net.sactivate(variables)[0]
        # Filling histograms
        signalHistogram.Fill(output, weight)

      # Signal overall normalization scale
      signalScale = signalYield/len(signalSample)
            
      # Loop over the events creating background histogram
      for event in backgroundSample:
        # Extract variables and weight
        variables = event[1:]
        weight = event[0]
        # Not strictly necessary in feedforward nets
        net.flush()
        # Net output
        output = net.sactivate(variables)[0]
        # Filling histograms
        backgroundHistogram.Fill(output, weight)

      # Background overall normalization scale
      backgroundScale = backgroundYield/len(backgroundSample)
  
      # Random generators
      rcount = TRandom3(int(random.uniform(0,65535)))
      rsignal = TRandom3(int(random.uniform(0,65535)))
      rbackground = TRandom3(int(random.uniform(0,65535)))

      zscore = 0.; sqweight = 0.
  
      # Computing weighted z-score
      for bin in xrange(1,signalHistogram.GetNbinsX()+1):

        newz = 0.; oldz = 0.

        # Computing the zvalue per bin
        for point in xrange(1,self.mpoints+1):
          # Sample background
          background = Math.gamma_quantile(rbackground.Uniform(), 
            (backgroundHistogram.GetBinContent(bin)/backgroundScale)+1., backgroundScale
          )
          # Background larger that zero
          if background > 0.:
            # Sampling signal
            signal = Math.gamma_quantile(rsignal.Uniform(),
              (signalHistogram.GetBinContent(bin)/signalScale)+1., signalScale
            )
            # Sampling count
            count = rcount.Poisson(signal+background)
            # Computing pvalue
            pvalue = Math.poisson_cdf_c(count,background) + Math.poisson_pdf(count,background)
            # Computing zvalue
            zvalue = self.minz
            if pvalue < 1.0: zvalue = Math.normal_quantile_c(pvalue,1)
            # zvalue iterative average 
            newz = (zvalue + (point - 1)*oldz)/point
            # Computing relative difference
            error = math.fabs((newz - oldz)/newz)
            # Convergency criteria
            if error < self.error: break
            # Updating oldz
            oldz = newz  
            if point == self.mpoints:
              self.message('Warning reach maximum number of integration %s points.' % point)

        weight = self.weight(signalHistogram.GetBinCenter(bin))
        zscore = zscore + weight * newz
        sqweight = sqweight + weight**2
        
      # Fitness function is zscore transform back to 1 - pvalue
      # Set the fitness value to the chomosome
      genome.fitness = 1. - Math.normal_cdf_c(zscore/Math.sqrt(sqweight))
Exemplo n.º 10
0
        #chromo.fitness = (4.0 - error_stanley)**2 # (Stanley p. 43)
        chromo.fitness = 1 - math.sqrt(error/len(OUTPUTS))

population.Population.evaluate = eval_fitness

pop = population.Population()
pop.epoch(300, report=True, save_best=False)

winner = pop.stats[0][-1]
print('Number of evaluations: %d' %winner.id)

# Visualize the winner network (requires PyDot)
#visualize.draw_net(winner) # best chromosome

# Plots the evolution of the best/average fitness (requires Biggles)
#visualize.plot_stats(pop.stats)
# Visualizes speciation
#visualize.plot_species(pop.species_log)

# Let's check if it's really solved the problem
print('\nBest network output:')
brain = nn.create_ffphenotype(winner)
for i, inputs in enumerate(INPUTS):
    output = brain.sactivate(inputs) # serial activation
    print("%1.5f \t %1.5f" %(OUTPUTS[i], output[0]))

# saves the winner
#file = open('winner_chromosome', 'w')
#pickle.dump(winner, file)
#file.close()
Exemplo n.º 11
0
    # Setting the indir directory
    indir = '%s/scratch/%s' % (
      Common.NeatDirectory, input
    )
    
    # Get channel winner network
    winner = pickle.load(open('%s/winner.info' % indir))
    
    # Collect the list of variables
    variables[key] = winner['variables']
    
    # Collect the net
    net = pickle.load(open('%s/%s/winner.dat' % (indir, winner['training'])))

    # Create phenotype from genome
    nets[key] = nn.create_ffphenotype(net)

    # Creating a variable normalizers
    normalizers[key] = VariableNormalizer(winner['variables'], winner['aves'], winner['stds'])

  # Create a topowriter for adding neat outputs
  topovars = None
  if len(variables) > 1:
    topovars = TopovarWriter(options.output, [], options.input)
  else:
    topovars = TopovarWriter(options.output, variables.itervalues().next(), options.input)

  # Add neat outputs
  for key in nets:
    topovars.addVariable(key, 'double')