Пример #1
0
 def testRecombination(self):
     chromosomes = [[21, 21], [2, 2], [3, 3], [19, 19], [6, 6]]
     rand = MockRandom([3, 4, 2, 0, 0, 3, 4, 1, 2, 0])
     offspring = es.recombination(chromosomes, 2, es.discreteGlobalRecombination, 
         es.intermediateGlobalRecombination, rand.sample)
     self.assertEqual(offspring, [[3, 12], [6, 12.5]])
Пример #2
0
5. Use death penalty approach to weed out offspring that do not meet constraints
6. Create more offspring using steps 3, 4, and 5 until desired number of offspring reached
7. Do survivor selection using (mu, lambda) approach
8. Compare best of current generation with best overall. If current is better, make it the best overall
9. Repeat until maxGenerations reached
"""
random.seed(seed)

parents = es.createGeneration0(numParents, fitness, initialSigma, random.uniform)
parents = es.rank(parents, len(parents), fitness)
currentBest = parents[0]

for i in xrange(maxGenerations):
    validOffspring = []
    offspringNeeded = numOffspring
    while offspringNeeded > 0:
        offspring = es.recombination(parents, offspringNeeded, vRecombination, sRecombination, random.sample)      
        mutatedOffspring = es.mutation(offspring, random.gauss)
        validOffspring.extend(es.deathPenalty(mutatedOffspring, fitness))
        offspringNeeded = numOffspring - len(validOffspring)
    parents = survivorSelection(parents, validOffspring, fitness)
    currentBest = es.compareBestFitness(currentBest, parents[0], fitness)

    if log['sample'] is not None  and i % log['sample'] == 0:
        print "Generation {}:".format(i),
        print parents[0]
        print "Fitness = {}\n".format(fitness.calculate(*parents[0]))

print "Best x values: ",
print currentBest
print "Fitness = {}".format(fitness.calculate(*currentBest))