Course Solutions Uncategorized (Solved) : Import Random Import String Target Hello World Def Calc Fitness Source Target Fitval 0 Ran Q27939040 . . . .

(Solved) : Import Random Import String Target Hello World Def Calc Fitness Source Target Fitval 0 Ran Q27939040 . . . .

 

import random

import string

Target=”Hello,world!”

def calc_ fitness(source, target): fitval = 0 for i in range(0, len(source)): fitval += (ord(target[i]) – ord(source[i])) ** 2 return(fitval)

def mutate(parent1, parent2):

   child_dna = parent1[‘dna’][:]

   # Mix both DNAs

   start = random.randint(0, len(parent2[‘dna’]) -1)

   stop = random.randint(0, len(parent2[‘dna’]) -1)

   if start > stop:

      stop, start = start, stop

   child_dna[start:stop] =parent2[‘dna’][start:stop]

# Mutate one position

   charpos = random.randint(0, len(child_dna) – 1)

   child_dna[charpos] = chr(ord(child_dna[charpos]) +random.randint(-1,1))

   child_fitness = calc_fitness(child_dna, target)

   return({‘dna’: child_dna, ‘fitness’:child_fitness})

GENSIZE = 20

genepool = []

for i in range(0, GENSIZE):

   dna = [random.choice(string.printable[:-5]) for jin range(0, len(target))]

   fitness =

OR

PayPal Gateway not configured

OR

PayPal Gateway not configured

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post