def test_generate_XOR_with_addition(self): minNodes = 5 # and( 1, +(a, b)) geneset = { 'A': Operation(lambda a, b: a, False, False), 'B': Operation(lambda a, b: b, False, False), 'AND': Operation(lambda a, b: a & b, True, True), 'NOT': Operation(lambda a, b: a == 0, True, False), '+': Operation(lambda a, b: a + b, True, True), '1': Operation(lambda a, b: 1, False, False) } rules = [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]] maxNodes = 50 optimalValue = 1000 - minNodes startTime = datetime.datetime.now() fnDisplay = lambda candidate: displayDot(candidate, startTime) fnGetFitness = lambda candidate: getFitness(candidate, geneset, rules) fnCreateGene = lambda index, length: createGene(index, length, geneset) fnMutate = lambda child: mutate(child, fnCreateGene) best = genetic.getBest(fnGetFitness, fnDisplay, minNodes, optimalValue, createGene=fnCreateGene, maxLen=maxNodes, customMutate=fnMutate, customCrossover=crossover) self.assertTrue(best.Fitness >= optimalValue)
def test(self): geneset = [0, 1, 2, 3, 4, 5, 6, 7] optimalValue = 8 + 8 + 8 + 8 startTime = datetime.datetime.now() fnDisplay = lambda candidate: display(candidate, startTime) fnGetFitness = lambda candidate: getFitness(candidate) best = genetic.getBest(fnGetFitness, fnDisplay, 16, optimalValue, geneset) self.assertEqual(best.Fitness, optimalValue)
def test(self): geneset = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!.' startTime = datetime.datetime.now() target = "Not all those who wander are lost." fnDisplay = lambda candidate: display(candidate, startTime) fnGetFitness = lambda candidate: getFitness(candidate, target) best = genetic.getBest(fnGetFitness, fnDisplay, len(target), len(target), geneset) self.assertEqual((''.join(best.Genes)), target)
def test(self): geneset = [1, 2, 3, 4, 5, 6, 7, '+', '-'] minNodes = 5 # (+ (+ ->[a] [a](+ 7 [b]5)) [b]) expectedTotal = 29 maxNodes = 30 optimalValue = 1000 - minNodes startTime = datetime.datetime.now() fnDisplay = lambda candidate: displayRaw(candidate, startTime) fnGetFitness = lambda candidate: getFitness(candidate, expectedTotal) fnCreateGene = lambda index, length: createGene(index, length, geneset) fnCrossover = lambda child, parent: crossover(child, parent) fnMutate = lambda child: mutate(child, fnCreateGene) best = genetic.getBest(fnGetFitness, fnDisplay, minNodes, optimalValue, createGene=fnCreateGene, maxLen=maxNodes, customMutate=fnMutate, customCrossover=fnCrossover) self.assertTrue(best.Fitness >= optimalValue)
def test(self): states = loadData("adjacent_states.csv") rules = buildRules(states) colors = ["Orange", "Yellow", "Green", "Blue"] colorLookup = {} for color in colors: colorLookup[color[0]] = color geneset = list(colorLookup.keys()) optimalValue = len(rules) startTime = datetime.datetime.now() fnDisplay = lambda candidate: display(candidate, startTime) fnGetFitness = lambda candidate: getFitness(candidate, rules) best = genetic.getBest(fnGetFitness, fnDisplay, len(states), optimalValue, geneset) self.assertEqual(best.Fitness, optimalValue) keys = sorted(states.keys()) for index in range(len(states)): print(keys[index] + " is " + colorLookup[best.Genes[index]])
def test_generate_XOR(self): minNodes = 9 # and( not( and(a, b)), not( and( not(a), not(b)))) rules = [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]] maxNodes = 50 optimalValue = 1000 - minNodes startTime = datetime.datetime.now() fnDisplay = lambda candidate: displayDot(candidate, startTime) fnGetFitness = lambda candidate: getFitness(candidate, self.geneset, rules) fnCreateGene = lambda index, length: createGene( index, length, self.geneset) fnMutate = lambda child: mutate(child, fnCreateGene) best = genetic.getBest(fnGetFitness, fnDisplay, minNodes, optimalValue, createGene=fnCreateGene, maxLen=maxNodes, customMutate=fnMutate, customCrossover=crossover) self.assertTrue(best.Fitness >= optimalValue)
# popcpy = g.mutate(pop) # print(popcpy) # popcpy = g.mutate(pop) # print(popcpy) # popcpy = g.mutate(pop) # print(popcpy) # print(type(popcpy)) generations = 0 # print(pop[1]) while generations <= 100: generations += 1 pop = g.mutate(pop) newpop = [0 for _ in range(100)] newpop[0] = pop[g.getBest(pop, fn.rast, -5.12, 5.12, 17)] for i in range(1, len(newpop)): newpop[i] = g.select(pop, fn.rast, -5.12, 5.12, 17) pop = newpop for i in range(100): print(fn.rast(fn.decode(pop[i], -5.12, 5.12, 17)), end=" ") print("\n")