In: Computer Science
write a code in python for evolutionary search method for multivarible
# Python3 program to create target string, starting from
# random string using Genetic Algorithm
import random
# Number of individuals in each generation
POPULATION_SIZE = 100
# Valid genes
GENES = '''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP
QRSTUVWXYZ 1234567890, .-;:_!"#%&/()=?@${[]}'''
# Target string to be generated
TARGET = "This is the Target String"
class Individual(object):
'''
Class representing individual in population
'''
def __init__(self, chromosome):
self.chromosome = chromosome
self.fitness = self.cal_fitness()
@classmethod
def mutated_genes(self):
'''
create random genes for mutation
'''
global GENES
gene = random.choice(GENES)
return gene
@classmethod
def create_gnome(self):
'''
create chromosome or string of genes
'''
global TARGET
gnome_len = len(TARGET)
return [self.mutated_genes() for _ in range(gnome_len)]
def mate(self, par2):
'''
Perform mating and produce new offspring
'''
# chromosome for offspring
child_chromosome = []
for gp1, gp2 in zip(self.chromosome, par2.chromosome):
# random probability
prob = random.random()
# if prob is less than 0.45, insert gene
# from parent 1
if prob < 0.45:
child_chromosome.append(gp1)
# if prob is between 0.45 and 0.90, insert
# gene from parent 2
elif prob < 0.90:
child_chromosome.append(gp2)
# otherwise insert random gene(mutate),
# for maintaining diversity
else:
child_chromosome.append(self.mutated_genes())
# create new Individual(offspring) using
# generated chromosome for offspring
return Individual(child_chromosome)
def cal_fitness(self):
'''
Calculate fittness score, it is the number of
characters in string which differ from target
string.
'''
global TARGET
fitness = 0
for gs, gt in zip(self.chromosome, TARGET):
if gs != gt: fitness+= 1
return fitness
# Driver code
def main():
global POPULATION_SIZE
#current generation
generation = 1
found = False
population = []
# create initial population
for _ in range(POPULATION_SIZE):
gnome = Individual.create_gnome()
population.append(Individual(gnome))
while not found:
# sort the population in increasing order of fitness score
population = sorted(population, key = lambda x:x.fitness)
# if the individual having lowest fitness score ie.
# 0 then we know that we have reached to the target
# and break the loop
if population[0].fitness <= 0:
found = True
break
# Otherwise generate new offsprings for new generation
new_generation = []
# Perform Elitism, that mean 10% of fittest population
# goes to the next generation
s = int((10*POPULATION_SIZE)/100)
new_generation.extend(population[:s])
# From 50% of fittest population, Individuals
# will mate to produce offspring
s = int((90*POPULATION_SIZE)/100)
for _ in range(s):
parent1 = random.choice(population[:50])
parent2 = random.choice(population[:50])
child = parent1.mate(parent2)
new_generation.append(child)
population = new_generation
print("Generation: {}\tString: {}\tFitness: {}".
format(generation,
"".join(population[0].chromosome),
population[0].fitness))
generation += 1
print("Generation: {}\tString: {}\tFitness: {}".
format(generation,
"".join(population[0].chromosome),
population[0].fitness))
if __name__ == '__main__':
main()
output:
===================== RESTART: C:/Users/dell/Desktop/a1.py =====================
Generation: 1 String: qC-)6Bz j)5C
,3A:P;=tAMUg Fitness: 22
Generation: 2 String: qC-)6Bz j)5C
,3A:P;=tAMUg Fitness: 22
Generation: 3 String: qC7S/Bs j)p_
,ZcwP;=t_IUg Fitness: 21
Generation: 4 String: qC7S/Bs j)p_
,ZcwP;=t_IUg Fitness: 21
Generation: 5 String: qMg b#s tcp_
,Zew#@Yt_I{g Fitness: 20
Generation: 6 String: qMg b#s tcp_
,Zew#@Yt_I{g Fitness: 20
Generation: 7 String: TAn/
es]tc5 0gT
IDo"t1i]g Fitness: 18
Generation: 8 String: TAn/
es]tc5 0gT
IDo"t1i]g Fitness: 18
Generation: 9 String: TAn/
es]tc5 0gT
IDo"t1i]g Fitness: 18
Generation: 10 String: #G6Zwos Zme 0,ZQ)g;bt_ing Fitness: 17
Generation: 11 String: #G6Zwos Zme 0,ZQ)g;bt_ing Fitness: 17
Generation: 12 String: qMK/ es the T ZXw{dKt.iA. Fitness: 15
Generation: 13 String: qMK/ es the T ZXw{dKt.iA. Fitness: 15
Generation: 14 String: TG6Z Bs tm!ATa(e)J;bt_ing Fitness: 14
Generation: 15 String: T7K}NWs9the T,s(Ot KtAiNg Fitness: 13
Generation: 16 String: T7K}NWs9the T,s(Ot KtAiNg Fitness: 13
Generation: 17 String: T7K}NWs9the T,s(Ot KtAiNg Fitness: 13
Generation: 18 String: TGBZ es t)e T5lcAt ktoikg Fitness: 12
Generation: 19 String: TA&Z .s the TaoA1t ptuFng Fitness: 10
Generation: 20 String: TAi" 0s the TawA%t btDing Fitness: 8
Generation: 21 String: TAi" 0s the TawA%t btDing Fitness: 8
Generation: 22 String: TAi" 0s the TawA%t btDing Fitness: 8
Generation: 23 String: TAi" 0s the TawA%t btDing Fitness: 8
Generation: 24 String: TAi" 0s the TawA%t btDing Fitness: 8
Generation: 25 String: TGis .s the Tawjst kt.ing Fitness: 7
Generation: 26 String: TGis .s the Tawjst kt.ing Fitness: 7
Generation: 27 String: TGis .s the Tawjst kt.ing Fitness: 7
Generation: 28 String: TGis .s the Tawjst kt.ing Fitness: 7
Generation: 29 String: TGis .s the Tawjst kt.ing Fitness: 7
Generation: 30 String: TGis is the Tawe%t bt}ing Fitness: 6
Generation: 31 String: TGis is the Tawe%t bt}ing Fitness: 6
Generation: 32 String: TGis is the Tawe%t bt}ing Fitness: 6
Generation: 33 String: TGis is the Tawe%t bt}ing Fitness: 6
Generation: 34 String: TGis is the Tawe%t bt}ing Fitness: 6
Generation: 35 String: TGis is the Tawe%t bt}ing Fitness: 6
Generation: 36 String: TGis is the Taw8st StWing Fitness: 5
Generation: 37 String: TGis is the Taw8st StWing Fitness: 5
Generation: 38 String: TGis is the Taw8st StWing Fitness: 5
Generation: 39 String: TGis is the Taw8st StWing Fitness: 5
Generation: 40 String: TGis is the Taw8st StWing Fitness: 5
Generation: 41 String: TGis is the Taw8st StWing Fitness: 5
Generation: 42 String: TGis is the Taw8st StWing Fitness: 5
Generation: 43 String: TGis is the Taw8st StWing Fitness: 5
Generation: 44 String: TGis is the Taw8st StWing Fitness: 5
Generation: 45 String: TGis is the Taw8st StWing Fitness: 5
Generation: 46 String: TGis is the Taw8st StWing Fitness: 5
Generation: 47 String: TGis is the Taw8st StWing Fitness: 5
Generation: 48 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 49 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 50 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 51 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 52 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 53 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 54 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 55 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 56 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 57 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 58 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 59 String: TLis is the TaZ0Ft String Fitness: 4
Generation: 60 String: TGis is the Ta?gFt String Fitness: 3
Generation: 61 String: TGis is the Ta?gFt String Fitness: 3
Generation: 62 String: TGis is the Ta?gFt String Fitness: 3
Generation: 63 String: TGis is the Ta?gFt String Fitness: 3
Generation: 64 String: TGis is the Ta?gFt String Fitness: 3
Generation: 65 String: TGis is the Ta?gFt String Fitness: 3
Generation: 66 String: TGis is the Ta?gFt String Fitness: 3
Generation: 67 String: TGis is the Ta?gFt String Fitness: 3
Generation: 68 String: TGis is the Ta?gFt String Fitness: 3
Generation: 69 String: TGis is the Ta?gFt String Fitness: 3
Generation: 70 String: TGis is the Ta?gFt String Fitness: 3
Generation: 71 String: TGis is the Ta?gFt String Fitness: 3
Generation: 72 String: TGis is the Ta?gFt String Fitness: 3
Generation: 73 String: TGis is the Ta?gFt String Fitness: 3
Generation: 74 String: TGis is the Ta?gFt String Fitness: 3
Generation: 75 String: TLis is the Ta?get String Fitness: 2
Generation: 76 String: TLis is the Ta?get String Fitness: 2
Generation: 77 String: TLis is the Ta?get String Fitness: 2
Generation: 78 String: TLis is the Ta?get String Fitness: 2
Generation: 79 String: TLis is the Ta?get String Fitness: 2
Generation: 80 String: TLis is the Ta?get String Fitness: 2
Generation: 81 String: TLis is the Ta?get String Fitness: 2
Generation: 82 String: TLis is the Ta?get String Fitness: 2
Generation: 83 String: TLis is the Ta?get String Fitness: 2
Generation: 84 String: TLis is the Ta?get String Fitness: 2
Generation: 85 String: TLis is the Ta?get String Fitness: 2
Generation: 86 String: TLis is the Ta?get String Fitness: 2
Generation: 87 String: TLis is the Ta?get String Fitness: 2
Generation: 88 String: TLis is the Ta?get String Fitness: 2
Generation: 89 String: TLis is the Ta?get String Fitness: 2
Generation: 90 String: TLis is the Ta?get String Fitness: 2
Generation: 91 String: TLis is the Ta?get String Fitness: 2
Generation: 92 String: TLis is the Ta?get String Fitness: 2
Generation: 93 String: TLis is the Ta?get String Fitness: 2
Generation: 94 String: TLis is the Ta?get String Fitness: 2
Generation: 95 String: TLis is the Ta?get String Fitness: 2
Generation: 96 String: TLis is the Ta?get String Fitness: 2
Generation: 97 String: TLis is the Ta?get String Fitness: 2
Generation: 98 String: TLis is the Ta?get String Fitness: 2
Generation: 99 String: TLis is the Ta?get String Fitness: 2
Generation: 100 String: TLis is the Ta?get String Fitness: 2
Generation: 101 String: TLis is the Ta?get String Fitness: 2
Generation: 102 String: TLis is the Ta?get String Fitness: 2
Generation: 103 String: TLis is the Ta?get String Fitness: 2
Generation: 104 String: TLis is the Ta?get String Fitness: 2
Generation: 105 String: TLis is the Ta?get String Fitness: 2
Generation: 106 String: TLis is the Ta?get String Fitness: 2
Generation: 107 String: TLis is the Ta?get String Fitness: 2
Generation: 108 String: TLis is the Ta?get String Fitness: 2
Generation: 109 String: TLis is the Ta?get String Fitness: 2
Generation: 110 String: TLis is the Ta?get String Fitness: 2
Generation: 111 String: TLis is the Ta?get String Fitness: 2
Generation: 112 String: TLis is the Ta?get String Fitness: 2
Generation: 113 String: TLis is the Ta?get String Fitness: 2
Generation: 114 String: TLis is the Ta?get String Fitness: 2
Generation: 115 String: TLis is the Ta?get String Fitness: 2
Generation: 116 String: TLis is the Ta?get String Fitness: 2
Generation: 117 String: TLis is the Ta?get String Fitness: 2
Generation: 118 String: TLis is the Ta?get String Fitness: 2
Generation: 119 String: TLis is the Ta?get String Fitness: 2
Generation: 120 String: TLis is the Ta?get String Fitness: 2
Generation: 121 String: TLis is the Ta?get String Fitness: 2
Generation: 122 String: TLis is the Ta?get String Fitness: 2
Generation: 123 String: This is the Ta?get String Fitness: 1
Generation: 124 String: This is the Ta?get String Fitness: 1
Generation: 125 String: This is the Ta?get String Fitness: 1
Generation: 126 String: This is the Ta?get String Fitness: 1
Generation: 127 String: This is the Ta?get String Fitness: 1
Generation: 128 String: This is the Ta?get String Fitness: 1
Generation: 129 String: This is the Ta?get String Fitness: 1
Generation: 130 String: This is the Ta?get String Fitness: 1
Generation: 131 String: This is the Ta?get String Fitness: 1
Generation: 132 String: This is the Ta?get String Fitness: 1
Generation: 133 String: This is the Ta?get String Fitness: 1
Generation: 134 String: This is the Ta?get String Fitness: 1
Generation: 135 String: This is the Ta?get String Fitness: 1
Generation: 136 String: This is the Ta?get String Fitness: 1
Generation: 137 String: This is the Ta?get String Fitness: 1
Generation: 138 String: This is the Ta?get String Fitness: 1
Generation: 139 String: This is the Ta?get String Fitness: 1
Generation: 140 String: This is the Ta?get String Fitness: 1
Generation: 141 String: This is the Ta?get String Fitness: 1
Generation: 142 String: This is the Ta?get String Fitness: 1
Generation: 143 String: This is the Ta?get String Fitness: 1
Generation: 144 String: This is the Ta?get String Fitness: 1
Generation: 145 String: This is the Target String Fitness: 0