What is this assignment? Python -
i copied book genetic code , found assignment:
childgenes[index] = alternate \ if newgene == childgenes[index] \ else newgene
the full code this: main.py:
from population import * while true: child = mutate(bestparent) childfitness = get_fitness(child) if bestfitness >= childfitness: continue print(str(child) + "\t" + str(get_fitness(child))) if childfitness >= len(bestparent): break bestfitness = childfitness bestparent = child
population.py:
import random geneset = " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz!.,1234567890-_=+!@#$%^&*():'[]\"" target = input() def generate_parent(length): genes = [] while len(genes) < length: samplesize = min(length - len(genes), len(geneset)) genes.extend(random.sample(geneset, samplesize)) parent = "" in genes: parent += return parent def get_fitness(guess): total = 0 in range(len(target)): if target[i] == guess[i]: total = total + 1 return total """ return sum(1 expected, actual in zip(target, guess) if expected == actual) """ def mutate(parent): index = random.randrange(0, len(parent)) childgenes = list(parent) newgene, alternate = random.sample(geneset, 2) childgenes[index] = alternate \ if newgene == childgenes[index] \ else newgene child = "" in childgenes: child += return child def display(guess): timediff = datetime.datetime.now() - starttime fitness = get_fitness(guess) print(str(guess) + "\t" + str(fitness) + "\t" + str(timediff)) random.seed() bestparent = generate_parent(len(target)) bestfitness = get_fitness(bestparent) print(bestparent)
the assignment in population.py, in mutate function. have never seen kind of variable assignment. this? "\" symbol mean?
it alternative of:
if newgene == childgenes[index]: childgenes[index] = alternate else: childgenes[index] = newgene
wiki
Comments
Post a Comment