예제 #1
0
 def testDeathPenalty(self):
     fitness = MockFitness()
     offspring = [[8], [23], [1], [-1], [0], [15]]
     survivors = es.deathPenalty(offspring, fitness)
     self.assertEqual(survivors, [[8], [1], [0], [15]])
예제 #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))