From 7a6571ccd4c37da6e0402c79131f5c20511a6ccc Mon Sep 17 00:00:00 2001 From: AKM Elias Date: Tue, 5 Dec 2017 23:46:49 +0600 Subject: [PATCH] Update genticAlgorithForReproduction.py code updated --- genticAlgorithForReproduction.py | 143 +++++++++++-------------------- 1 file changed, 51 insertions(+), 92 deletions(-) diff --git a/genticAlgorithForReproduction.py b/genticAlgorithForReproduction.py index b26f1f7..12afeb1 100644 --- a/genticAlgorithForReproduction.py +++ b/genticAlgorithForReproduction.py @@ -6,7 +6,7 @@ import math #population... -populations = [[1,0,0,1,0],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,0],[0,0,1,0,1]] +populations = [[1,0,0,1,0],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,0],[1,0,1,1,1]] #expected outecome or target generation expected_output = [1,1,0,1,0] #maximum bit not changed.. @@ -25,26 +25,39 @@ #main algo start... #we will need these list later.. listToCross = [] -listToCross2 = [] +listToCross2 = populations #homany time the expected generation found.. maincount = 0 #threshold_value assigninig or assuming..let assume 2 for this problem thv = 2 flag=0 + #fitness function..we will do this by comparing eith expected outecome to all the population given value = [] -j = 0 -for i in range(len(populations)): - cnt = 0 - for bit in range(len(populations[i])): - #print(populations[i][bit]) - if populations[i][bit]!=expected_output[bit]: - cnt+=1 - value.append(cnt) +def bestFit(population): + global value + value = [] + #print("bestfit") + j = 0 + for i in range(len(population)): + cnt = 0 + for bit in range(len(population[i])): + #print(populations[i][bit]) + if population[i][bit]!=expected_output[bit]: + cnt+=1 + value.append(cnt) #j+=1 - -#function for crossover.. -def crossover(i): + +#list assigning after best fit function.. +def assigningBestPopulations(population): + global value + for i in range(len(population)): + if value[i]<=2: + #print(value[i]) + listToCross.append(population[i]) + + +def crossover(): #print(i) global listToCross2 global mxbit @@ -52,76 +65,31 @@ def crossover(i): check = 0 flag=0 maincount = 0 - if i==0: + #crossover for every combination.. - for j in range(len(listToCross)-1): - cmp1 = j - for k in range(len(listToCross)-j-1): - cmp1+=1 - temp1 = [] - temp2 = [] - for vl in range(len(listToCross[cmp1])): - #bit can remain same upto maxbit .. - if vl0..these new populations will crossover among them.. - listToCross2 = listcr - #equations are smae here but combinations are different.. - else: - cmp2 = 0 - #print(len(listToCross2)) - for j in range(0,len(listToCross2),2): + for j in range(len(listToCross)-1): + cmp1 = j + for k in range(len(listToCross)-j-1): + cmp1+=1 temp1 = [] temp2 = [] - print(j) - for vl in range(len(listToCross2[j])): - + for vl in range(len(listToCross[cmp1])): + #bit can remain same upto maxbit .. if vl0..these new populations will crossover among them.. + listToCross2 = listcr + #equations are smae here but combinations are different.. + #print(listcr) - return flag,maincount - -#list assigning after best fit function.. -for i in range(len(populations)): - if value[i]<=2: - listToCross.append(populations[i]) - + return flag,maincount #here looping limit is 3 because it assumed that within 3 crossover target generation will be found.. - for i in range(3): - - flag,maincount = crossover(i) + bestFit(listToCross2) + assigningBestPopulations(listToCross2) + flag,maincount = crossover() if flag==1: - print("expected genertion found ",maincount,"time!!") + print("found ",maincount) break - - - +