Code Address: HTTPS://GITHUB.COM/GUOJUN007/BINARY_SGA
Population initialization:
BINARY_SGA/population_init/population_init.py
#Population initialization FunctiondefPopulation_init (population, N, V, nbits):#the number of arguments V is not used delpopulation[:] forIinchxrange (N): Tempindividual=[] forJinchNbits:tempval=[] forKinchXrange (j): Tempval.append (random.randint (0,1) ) tempindividual.append (tempval) population.append (tempindividual)
Select action: (Roulette selection)
BINARY_SGA/selection/selection.py
#!/usr/bin/env python#Encoding:utf-8ImportCopyImportRandom#Roulette Selection Methoddefselection (population, xbin): s=sum (xbin) Temp=[k*1.0/s forKinchXBin] Temp2=[] S2=0 forKinchTemp:s2=s2+k temp2.append (S2) Temp3=[] for_inchxrange (len (population)): R=random.random () forIinchxrange (len (TEMP2)):ifr<=Temp2[i]: temp3.append (i) BreakTemp4=[] Temp5=[] forIinchtemp3:temp4.append (Copy.deepcopy (population[i)) Temp5.append (Xbin[i]) population[:]=temp4 xbin[:]=temp5
Cross operation: (single point intersection)
BINARY_SGA/crossover/crossover.py
# Binary coding, single point crossover def Crossover (population, Alfa, nbits): for in Xrange (Len (population), 2): for in xrange (len (nbits)): R =random.random () if r<alfa: P=random.randint (1, nbits[j]-1 ) population[i][j][p:], population[i+1][j][p:]=population[i+1][j][p:], Population[i][j][p:]
Mutation Operation:
binary_sga/mutation/mutation.py
def mutation (population, Belta, nbits): for inch xrange (len (population)): for inch xrange (len (nbits)): for inch xrange (Nbits[j]): r=random.random () if r<belta: Population[i][j][k]^=1
Binary individual decoding operation:
BINARY_SGA/decode/decode.py
#population individual decodingdefDecode (population, population_real, Minbinval, Maxbinval, nbits):delpopulation_real[:]defIner (vallist): L=Len (vallist) s=0 forIinchvallist:s=s+i* (2** (L-1)) L=l-1returns forIinchpopulation:temp=[] forJinchI:temp.append (Iner (j)) forJinchxrange (len (temp)): Temp[j]=temp[j]* (Maxbinval[j]-minbinval[j]) *1.0/(2** (NBITS[J))-1) +Minbinval[j] Population_real.append (temp)
Test function Section:
BINARY_SGA/function/object_fun.py
#duality problem, converted to maximum value#Two-dimensional rastrigin test functiondefObject_fun (p):ImportMath x=p[0] y=p[1] Object_value=20.0+x**2+y**2-10.0* (Math.Cos (2*math.pi*x) +math.cos (2*math.pi*y))return100.0-Object_value"""#求最大值, no conversion required # Two-dimensional Schaffer test function def object_fun (p): Import math x=p[0] y=p[1] object_value =0.5-((Math.sin (Math. sqrt (x**2+y**2)) **2-0.5)/(1+0.001* (x**2+y**2)) **2 return Object_value"""
Main function Section:
BINARY_SGA/sga.py
n=200V=2nbits= (17, 17) Maxbinval= (-5,-5) Minbinval= (5, 5) Population=[]population_real=[]alfa=0.9Belta=0.05#List of target function valuesxbin=[]defPer_run (): Population_init (population, N, V, nbits) forIinchXrange (200): Decode (population, population_real, Minbinval, Maxbinval, nbits) eval_fun (Population_real, XBin) Selection (population, xbin) crossover (population, Alfa, nbits) mutation (population, Belta, nbits) deco De (population, population_real, Minbinval, Maxbinval, nbits) eval_fun (Population_real, XBin)return100.0-max (XBin)
n is the number of individual populations.
V is the number of independent variables.
Nbits the length of each independent variable binary encoding.
Maxbinval the upper limit of each argument.
Minbinval the lower bounds of each argument.
Population individual binary code list population.
Population individual real number decoding list population_real.
alfa=0.9 crossover probability. belta=0.05 mutation probability.
Standard genetic algorithm (binary coded Python implementation)