Ejemplo n.º 1
0
    def modify(self, population):
        """
            Apply the backpropagation mutation in someone in population

            @author     Thiago  F Pappacena
            @param      object  population  The population object
            @return     void
        """
        if random.random() <= self.probability:
            self.stopMaxIter.setIterations( random.randrange(1, self.maxIterations) )

            chromo = random.choice(population.getChromossomes())
            genes = chromo.getGenes()
            weights = tuple(x.getValue() for x in genes)

            # rebuild the net and train
            net = Network.fromPlanifiedWeights(self.netArch, weights)
            self.trainer.setNetwork(net)
            self.trainer.train(self.patterns)
            self.netArch = net.getDimension()

            # copy values to genes
            for gene, weight in zip(genes, net.planify()):
                gene.setValue(weight)
            chromo.setFitness(None)
 def evaluate(self, chromossome):
     """
         Evaluate a chromossome and check it's fitness to patterns
         
         @author     Thiago F Pappacena
         @param      object  chromossome     The chromossome to check
         @return     float   The fitness of the chromossome
     """
     weights = tuple(i.getValue() for i in chromossome.getGenes())
     net = Network.fromPlanifiedWeights(self.netArch, weights)
     error = 0.0
     for p in self.patterns:
         net.clearActivations()
         error += sum( (i[1] - i[0])**2 for i in zip(net.getOutput(p.getInput()), p.getOutput()) ) / 2.0
     return -error