From bba32ae7088c269c95fe34903e6e4390fca90c6b Mon Sep 17 00:00:00 2001 From: parimatrix Date: Thu, 17 Oct 2019 10:46:16 +0530 Subject: [PATCH] Minor Fixes --- geneticAlgo/crossover.py | 17 +++++++++-------- geneticAlgo/genetic_algo_initial_popu.py | 4 +--- geneticAlgo/mutate.py | 9 +++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/geneticAlgo/crossover.py b/geneticAlgo/crossover.py index 43c0b5c..b250232 100644 --- a/geneticAlgo/crossover.py +++ b/geneticAlgo/crossover.py @@ -1,4 +1,5 @@ import random +from copy import deepcopy def breed(chromosome1, chromosome2): """ @@ -9,11 +10,7 @@ def breed(chromosome1, chromosome2): k = random.random() # print (k) if(k>=0.5): - temp = chromosome1[i] - chromosome1[i] = chromosome2[i] - chromosome2[i] = temp - # print (chromosome1) - # print (chromosome2) + chromosome1[i], chromosome2[i] = chromosome2[i], chromosome1[i] def crossover( matingPool, origChromosomes, noOfElite): @@ -32,19 +29,23 @@ def crossover( matingPool, origChromosomes, noOfElite): newChromosome = [] # Copy the elite chromosomes and persist them to next generation for i in range(0, noOfElite): - newChromosome.append(origChromosomes[matingPool[i]]) + newChromosome.append(deepcopy(origChromosomes[matingPool[i]])) LowerPtrForCrossover = noOfElite UpperPtrForCrossover = sizeOfMatingPool-1 while (LowerPtrForCrossover Size of initial population pool """ - population = [] - for i in range(1, initialPopuSize+1): - population.append(createChromosome(maxDishes)) + population = [createChromosome(maxDishes) for i in range(initialPopuSize)] return population diff --git a/geneticAlgo/mutate.py b/geneticAlgo/mutate.py index 9f318d0..35ac9fc 100644 --- a/geneticAlgo/mutate.py +++ b/geneticAlgo/mutate.py @@ -1,4 +1,5 @@ import random +from copy import deepcopy # from genetic_algo_gene import Dish @@ -32,7 +33,7 @@ def mutate(individual, mutationRate): individual[i].qty = tempArrQty[i-startPtr] return individual -def mutatePopulation ( population, mutationRate): +def mutatePopulation ( population, noOfElite, mutationRate): """ This function performs mutation on entire population @@ -43,9 +44,9 @@ def mutatePopulation ( population, mutationRate): """ popuLen = len(population) newPopu = [] - for i in range(0,popuLen): - mutantIndi = mutate(population[i],mutationRate) - newPopu.append(mutantIndi) + newPopu.extend([deepcopy(individual) for individual in population[:noOfElite]]) + newPopu.extend([mutate(individual, mutationRate) for individual in population[noOfElite:popuLen]]) + return newPopu # mutate function test