From 575078ab0d25740563bf279a6dbff5c5368bbe8e Mon Sep 17 00:00:00 2001 From: prasys Date: Tue, 8 Oct 2019 10:11:45 +1300 Subject: [PATCH 1/2] Fixed Generation and few other things --- Example_GeneticAlgorithm.py | 20 ++++++-------------- __pycache__/ga.cpython-37.pyc | Bin 0 -> 1451 bytes 2 files changed, 6 insertions(+), 14 deletions(-) create mode 100644 __pycache__/ga.cpython-37.pyc diff --git a/Example_GeneticAlgorithm.py b/Example_GeneticAlgorithm.py index 1f02c1a7..06ecb3f3 100644 --- a/Example_GeneticAlgorithm.py +++ b/Example_GeneticAlgorithm.py @@ -1,6 +1,6 @@ import numpy import ga - +import matplotlib.pyplot """ The y=target is to maximize this equation ASAP: y = w1x1+w2x2+w3x3+w4x4+w5x5+6wx6 @@ -20,26 +20,19 @@ Mating pool size Population size """ -sol_per_pop = 8 -num_parents_mating = 4 +sol_per_pop = 16 # matching pool size +num_parents_mating = 2 # population size , or the number of parents mating # Defining the population size. pop_size = (sol_per_pop,num_weights) # The population will have sol_per_pop chromosome where each chromosome has num_weights genes. +print("pop size is",pop_size) #Creating the initial population. new_population = numpy.random.uniform(low=-4.0, high=4.0, size=pop_size) print(new_population) -""" -new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1] -new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1] -new_population[2, :] = [-1, 2, 2, -3, 2, 0.9] -new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4] -new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0] -new_population[5, :] = [-2, 3, -7, 6, 3, 3] -""" best_outputs = [] -num_generations = 1000 +num_generations = 5000 for generation in range(num_generations): print("Generation : ", generation) # Measuring the fitness of each chromosome in the population. @@ -81,8 +74,7 @@ print("Best solution : ", new_population[best_match_idx, :]) print("Best solution fitness : ", fitness[best_match_idx]) - -import matplotlib.pyplot +#Graph goes here matplotlib.pyplot.plot(best_outputs) matplotlib.pyplot.xlabel("Iteration") matplotlib.pyplot.ylabel("Fitness") diff --git a/__pycache__/ga.cpython-37.pyc b/__pycache__/ga.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f96d5122e21de477f83b5ca77806416065af1818 GIT binary patch literal 1451 zcmZvcO>Y}T7{}*j*LEByaZ{^lL_tV!F^5`IJs=c8fJ$(vtV)0)Rz%CnGq$(&?m9EO z#i`aO$_@AcoSGw_g6}a`PB|cPMndp^#xDwpSIRC^yrF~w&rVoE6al0{qvDnu((Lp9M#wX1ei`z4P8#Xo2L&Nb$+;Yd(} zLTd%6pP<@1FeQ7+YhJS+)>Yz1u@tqyJUP7P=EKZ@sxqCIcJTPC;giIm zckrpsb(xMnnoSCmmecIXWjQVK!6dnl*By+KS&XIQ^~$t=OCP}GAve2dN0g${Ky9M> zPs`zy>=my?&7KOykJ(sU@uggRQOa6;#~-ot&z1p31T}{ZYktL_(TK1Pl{g08V+Nlg z0`l+07;!D@U=y7%C4ntGVHa@!+sU`_8ry|oye$s#?4#ZjZ*p^loL9!b7;lyO0^OeRTBkMl zY{lXmu%#T7=jjfYP_E*4*)mw15_g|{xD0WGgIA%FxC;1{ToPj_)ho6^^j8u12DLcC z?2Gr$*u~r6$-pzbq1p*;pRqrF-8dhv#cO&=xe_U4EB$xp^HueeT(BR6;j~7yRnC(1;rg(+M&D__R*4LRB;U0i9+`XR;+)jaIwW F{Rc?MQceH> literal 0 HcmV?d00001 From 8eb61db831c05349967f90105152ad50827917cc Mon Sep 17 00:00:00 2001 From: prasys Date: Wed, 9 Oct 2019 11:20:49 +1300 Subject: [PATCH 2/2] Update Example_GeneticAlgorithm.py --- Example_GeneticAlgorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Example_GeneticAlgorithm.py b/Example_GeneticAlgorithm.py index 06ecb3f3..968c8146 100644 --- a/Example_GeneticAlgorithm.py +++ b/Example_GeneticAlgorithm.py @@ -10,7 +10,7 @@ """ # Inputs of the equation. -equation_inputs = [4,-2,3.5,5,-11,-4.7] +equation_inputs = [0.153,0.068,0.008,0.053,0.073,0.039,0.047,0.005,0.011] # Number of the weights we are looking to optimize. num_weights = len(equation_inputs)