Question

In: Computer Science

write a code in python for evolutionary search method for multivarible

write a code in python for evolutionary search method for multivarible

Solutions

Expert Solution

# 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

Related Solutions

1. Write a python code that uses the Runge Kutta Method method to approximate the solutions...
1. Write a python code that uses the Runge Kutta Method method to approximate the solutions to each of the following initial-value problems and compare/plot the results to the actual values. a) y′=te^(3t) − 2y, 0 < t < 1, y(0) = 0 with h = 0.5; actual solution y(t)=1/5te^(3t) − 1/25e^(3t) + 1/25e^(−2t). - Use the Runge Kutta method to approximate/plot the solutions to each of the following initial-value b) ?′=1+(?−?)2,2<?<3,?(2)=1y′=1+(t−y)2,2 c) ?′=1+??,1<?<1,?(1)=2y′=1+yt,1
Write a python code that uses the Euler Implicit method to approximate/plot the solutions to each...
Write a python code that uses the Euler Implicit method to approximate/plot the solutions to each of the following initial-value y′=−ty+4t/y, 0 ≤ t ≤ 1, y(0)=1, with h=0.1 y′=y2+yt, 1 ≤ t ≤ 3, y(l)=−2, with h=0.2
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy...
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy your Bisect code and modify).   Evaluation of F(x) and F'(x) should be done in a subprogram FCN(x).   The code should ask for input of: x0, TOL, maxIT (and should print output similar to Bisect code).   Debug on a simple problem, like x2−3 = 0.   Then use it to find root of F(x) in [1,2] with TOL=1.e-12. Now consider the problem of finding zeros of      ...
Python code for Multivariable Unconstrained *unidirectional search*(topic:- optimization techniques)
Python code for Multivariable Unconstrained *unidirectional search*(topic:- optimization techniques)
ASAP! write a program including   binary-search and merge-sort in Python. You also need to  modify the code posted...
ASAP! write a program including   binary-search and merge-sort in Python. You also need to  modify the code posted and use your variable names and testArrays.  
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
write a python program that will search through a range of launch angles (this will require...
write a python program that will search through a range of launch angles (this will require a loop statement) the target is 500 meters away and the projectile was launched at 100m/s, the initial height of the projectile can be between 2 and 30 meters the print statement will state the initial launch angle required to hit the target from the projectile height. ***explain each step or a rating will not be given***
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
code in python write a code where the initial value is compounded by a multiplier, rounded...
code in python write a code where the initial value is compounded by a multiplier, rounded to the nearest tenth Output: Initial Value: 10 Multiplier: 1.4 Number of compounds: 10 Your Values are: 10 , 14, 19.6 , 27.4, 38.4, 53.8, 75.3, 105.4, 147.6, 206.6
Use python programming to write this code and provide a screen short for the code. 2....
Use python programming to write this code and provide a screen short for the code. 2. Write a function that takes one argument (a string) and returns a string consisting of the single character from that string with the largest value. Your function should contain a for loop. You can assume that the input to your function will always be a valid string consisting of at least one character. You can assume that the string will consist only of lower-case...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT