├── README.md └── Algorithm ├── __pycache__ ├── GWO.cpython-37.pyc ├── solution.cpython-37.pyc └── benchmarks.cpython-37.pyc ├── solution.py ├── optimizer.py ├── GWO.py └── benchmarks.py /README.md: -------------------------------------------------------------------------------- 1 | # GWO 2 | Grey wolf Optimization 3 | -------------------------------------------------------------------------------- /Algorithm/__pycache__/GWO.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MRamzi10050/GWO/HEAD/Algorithm/__pycache__/GWO.cpython-37.pyc -------------------------------------------------------------------------------- /Algorithm/__pycache__/solution.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MRamzi10050/GWO/HEAD/Algorithm/__pycache__/solution.cpython-37.pyc -------------------------------------------------------------------------------- /Algorithm/__pycache__/benchmarks.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MRamzi10050/GWO/HEAD/Algorithm/__pycache__/benchmarks.cpython-37.pyc -------------------------------------------------------------------------------- /Algorithm/solution.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | class solution: 5 | def __init__(self): 6 | self.best = 0 7 | self.bestIndividual=[] 8 | self.convergence = [] 9 | self.optimizer="" 10 | self.objfname="" 11 | self.startTime=0 12 | self.endTime=0 13 | self.executionTime=0 14 | self.lb=0 15 | self.ub=0 16 | self.dim=0 17 | self.popnum=0 18 | self.maxiers=0 19 | -------------------------------------------------------------------------------- /Algorithm/optimizer.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | import GWO as gwo 5 | import benchmarks 6 | import csv 7 | import numpy 8 | import time 9 | 10 | 11 | def selector(algo,func_details,popSize,Iter): 12 | function_name=func_details[0] 13 | lb=func_details[1] 14 | ub=func_details[2] 15 | dim=func_details[3] 16 | 17 | 18 | if(algo==2): 19 | x=gwo.GWO(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter) 20 | 21 | return x 22 | 23 | 24 | # Select optimizers 25 | 26 | GWO = True 27 | 28 | 29 | 30 | # Select benchmark function 31 | F1=True 32 | '''F2=True 33 | F3=True 34 | F4=True 35 | F5=True 36 | F6=True 37 | F7=True 38 | F8=True 39 | F9=True 40 | F10=True 41 | F11=True 42 | F12=True 43 | F13=True 44 | F14=True 45 | F15=True 46 | F16=True 47 | F17=True 48 | F18=True 49 | F19=True 50 | ''' 51 | 52 | 53 | optimizer=[GWO] 54 | benchmarkfunc=[F1] 55 | 56 | # Select number of repetitions for each experiment. 57 | # To obtain meaningful statistical results, usually 30 independent runs 58 | # are executed for each algorithm. 59 | NumOfRuns=1 60 | 61 | # Select general parameters for all optimizers (population size, number of iterations) 62 | PopulationSize = 5 63 | Iterations= 5 64 | 65 | #Export results ? 66 | Export=True 67 | 68 | 69 | #ExportToFile="YourResultsAreHere.csv" 70 | #Automaticly generated name by date and time 71 | ExportToFile="experiment"+time.strftime("%Y-%m-%d-%H-%M-%S")+".csv" 72 | 73 | # Check if it works at least once 74 | Flag=False 75 | 76 | # CSV Header for for the cinvergence 77 | CnvgHeader=[] 78 | 79 | for l in range(0,Iterations): 80 | CnvgHeader.append("Iter"+str(l+1)) 81 | 82 | 83 | for i in range (0, len(optimizer)): 84 | for j in range (0, len(benchmarkfunc)): 85 | if((optimizer[i]==True) and (benchmarkfunc[j]==True)): # start experiment if an optimizer and an objective function is selected 86 | for k in range (0,NumOfRuns): 87 | 88 | func_details=benchmarks.getFunctionDetails(j) 89 | x=selector(2,func_details,PopulationSize,Iterations) 90 | if(Export==True): 91 | with open(ExportToFile, 'a',newline='\n') as out: 92 | writer = csv.writer(out,delimiter=',') 93 | if (Flag==False): # just one time to write the header of the CSV file 94 | header= numpy.concatenate([["Optimizer","objfname","startTime","EndTime","ExecutionTime"],CnvgHeader]) 95 | writer.writerow(header) 96 | a=numpy.concatenate([[x.optimizer,x.objfname,x.startTime,x.endTime,x.executionTime],x.convergence]) 97 | writer.writerow(a) 98 | out.close() 99 | Flag=True # at least one experiment 100 | 101 | if (Flag==False): # Faild to run at least one experiment 102 | print("No Optomizer or Cost function is selected. Check lists of available optimizers and cost functions") 103 | 104 | 105 | -------------------------------------------------------------------------------- /Algorithm/GWO.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | import random 5 | import numpy 6 | import math 7 | from solution import solution 8 | import time 9 | 10 | 11 | 12 | 13 | def GWO(objf,lb,ub,dim,SearchAgents_no,Max_iter): 14 | 15 | 16 | #Max_iter=1000 17 | #lb=-100 18 | #ub=100 19 | #dim=30 20 | #SearchAgents_no=5 21 | 22 | # initialize alpha, beta, and delta_pos 23 | Alpha_pos=numpy.zeros(dim) 24 | Alpha_score=float("inf") 25 | 26 | Beta_pos=numpy.zeros(dim) 27 | Beta_score=float("inf") 28 | 29 | Delta_pos=numpy.zeros(dim) 30 | Delta_score=float("inf") 31 | 32 | if not isinstance(lb, list): 33 | lb = [lb] * dim 34 | if not isinstance(ub, list): 35 | ub = [ub] * dim 36 | 37 | #Initialize the positions of search agents 38 | Positions = numpy.zeros((SearchAgents_no, dim)) 39 | for i in range(dim): 40 | Positions[:, i] = numpy.random.uniform(0,1, SearchAgents_no) * (ub[i] - lb[i]) + lb[i] 41 | 42 | Convergence_curve=numpy.zeros(Max_iter) 43 | s=solution() 44 | 45 | # Loop counter 46 | print("GWO is optimizing \""+objf.__name__+"\"") 47 | 48 | timerStart=time.time() 49 | s.startTime=time.strftime("%Y-%m-%d-%H-%M-%S") 50 | # Main loop 51 | for l in range(0,Max_iter): 52 | for i in range(0,SearchAgents_no): 53 | 54 | # Return back the search agents that go beyond the boundaries of the search space 55 | for j in range(dim): 56 | Positions[i,j]=numpy.clip(Positions[i,j], lb[j], ub[j]) 57 | 58 | # Calculate objective function for each search agent 59 | fitness=objf(Positions[i,:]) 60 | 61 | # Update Alpha, Beta, and Delta 62 | if fitnessAlpha_score and fitnessAlpha_score and fitness>Beta_score and fitnessa)+k*((-x-a)**m)*(x<(-a)); 16 | return y 17 | 18 | def F1(x): 19 | s=numpy.sum(x**2); 20 | return s 21 | ''' 22 | def F2(x): 23 | o=sum(abs(x))+prod(abs(x)); 24 | return o; 25 | 26 | def F3(x): 27 | dim=len(x)+1; 28 | o=0; 29 | for i in range(1,dim): 30 | o=o+(numpy.sum(x[0:i]))**2; 31 | return o; 32 | 33 | def F4(x): 34 | o=max(abs(x)); 35 | return o; 36 | 37 | def F5(x): 38 | dim=len(x); 39 | o=numpy.sum(100*(x[1:dim]-(x[0:dim-1]**2))**2+(x[0:dim-1]-1)**2); 40 | return o; 41 | 42 | def F6(x): 43 | o=numpy.sum(abs((x+.5))**2); 44 | return o; 45 | 46 | def F7(x): 47 | dim=len(x); 48 | 49 | w=[i for i in range(len(x))] 50 | for i in range(0,dim): 51 | w[i]=i+1; 52 | o=numpy.sum(w*(x**4))+numpy.random.uniform(0,1); 53 | return o; 54 | 55 | def F8(x): 56 | o=sum(-x*(numpy.sin(numpy.sqrt(abs(x))))); 57 | return o; 58 | 59 | def F9(x): 60 | dim=len(x); 61 | o=numpy.sum(x**2-10*numpy.cos(2*math.pi*x))+10*dim; 62 | return o; 63 | 64 | 65 | def F10(x): 66 | dim=len(x); 67 | o=-20*numpy.exp(-.2*numpy.sqrt(numpy.sum(x**2)/dim))-numpy.exp(numpy.sum(numpy.cos(2*math.pi*x))/dim)+20+numpy.exp(1); 68 | return o; 69 | 70 | def F11(x): 71 | dim=len(x); 72 | w=[i for i in range(len(x))] 73 | w=[i+1 for i in w]; 74 | o=numpy.sum(x**2)/4000-prod(numpy.cos(x/numpy.sqrt(w)))+1; 75 | return o; 76 | 77 | def F12(x): 78 | dim=len(x); 79 | o=(math.pi/dim)*(10*((numpy.sin(math.pi*(1+(x[0]+1)/4)))**2)+numpy.sum((((x[1:dim-1]+1)/4)**2)*(1+10*((numpy.sin(math.pi*(1+(x[1:dim-1]+1)/4))))**2))+((x[dim-1]+1)/4)**2)+numpy.sum(Ufun(x,10,100,4)); 80 | return o; 81 | 82 | def F13(x): 83 | dim=len(x); 84 | o=.1*((numpy.sin(3*math.pi*x[1]))**2+sum((x[0:dim-2]-1)**2*(1+(numpy.sin(3*math.pi*x[1:dim-1]))**2))+ 85 | ((x[dim-1]-1)**2)*(1+(numpy.sin(2*math.pi*x[dim-1]))**2))+numpy.sum(Ufun(x,5,100,4)); 86 | return o; 87 | 88 | def F14(x): 89 | aS=[[-32,-16,0,16,32,-32,-16,0,16,32,-32,-16,0,16,32,-32,-16,0,16,32,-32,-16,0,16,32],[-32,-32,-32,-32,-32,-16,-16,-16,-16,-16,0,0,0,0,0,16,16,16,16,16,32,32,32,32,32]]; 90 | aS=numpy.asarray(aS); 91 | bS = numpy.zeros(25) 92 | v=numpy.matrix(x) 93 | for i in range(0,25): 94 | H=v-aS[:,i]; 95 | bS[i]=numpy.sum((numpy.power(H,6))); 96 | w=[i for i in range(25)] 97 | for i in range(0,24): 98 | w[i]=i+1; 99 | o=((1./500)+numpy.sum(1./(w+bS)))**(-1); 100 | return o; 101 | 102 | def F15(L): 103 | aK=[.1957,.1947,.1735,.16,.0844,.0627,.0456,.0342,.0323,.0235,.0246]; 104 | bK=[.25,.5,1,2,4,6,8,10,12,14,16]; 105 | aK=numpy.asarray(aK); 106 | bK=numpy.asarray(bK); 107 | bK = 1/bK; 108 | fit=numpy.sum((aK-((L[0]*(bK**2+L[1]*bK))/(bK**2+L[2]*bK+L[3])))**2); 109 | return fit 110 | 111 | def F16(L): 112 | o=4*(L[0]**2)-2.1*(L[0]**4)+(L[0]**6)/3+L[0]*L[1]-4*(L[1]**2)+4*(L[1]**4); 113 | return o 114 | 115 | def F17(L): 116 | o=(L[1]-(L[0]**2)*5.1/(4*(numpy.pi**2))+5/numpy.pi*L[0]-6)**2+10*(1-1/(8*numpy.pi))*numpy.cos(L[0])+10; 117 | return o 118 | 119 | def F18(L): 120 | o=(1+(L[0]+L[1]+1)**2*(19-14*L[0]+3*(L[0]**2)-14*L[1]+6*L[0]*L[1]+3*L[1]**2))*(30+(2*L[0]-3*L[1])**2*(18-32*L[0]+12*(L[0]**2)+48*L[1]-36*L[0]*L[1]+27*(L[1]**2))); 121 | return o 122 | # map the inputs to the function blocks 123 | def F19(L): 124 | aH=[[3,10,30],[.1,10,35],[3,10,30],[.1,10,35]]; 125 | aH=numpy.asarray(aH); 126 | cH=[1,1.2,3,3.2]; 127 | cH=numpy.asarray(cH); 128 | pH=[[.3689,.117,.2673],[.4699,.4387,.747],[.1091,.8732,.5547],[.03815,.5743,.8828]]; 129 | pH=numpy.asarray(pH); 130 | o=0; 131 | for i in range(0,4): 132 | o=o-cH[i]*numpy.exp(-(numpy.sum(aH[i,:]*((L-pH[i,:])**2)))); 133 | return o 134 | 135 | 136 | def F20(L): 137 | aH=[[10,3,17,3.5,1.7,8],[.05,10,17,.1,8,14],[3,3.5,1.7,10,17,8],[17,8,.05,10,.1,14]]; 138 | aH=numpy.asarray(aH); 139 | cH=[1,1.2,3,3.2]; 140 | cH=numpy.asarray(cH); 141 | pH=[[.1312,.1696,.5569,.0124,.8283,.5886],[.2329,.4135,.8307,.3736,.1004,.9991],[.2348,.1415,.3522,.2883,.3047,.6650],[.4047,.8828,.8732,.5743,.1091,.0381]]; 142 | pH=numpy.asarray(pH); 143 | o=0; 144 | for i in range(0,4): 145 | o=o-cH[i]*numpy.exp(-(numpy.sum(aH[i,:]*((L-pH[i,:])**2)))); 146 | return o 147 | 148 | def F21(L): 149 | aSH=[[4,4,4,4],[1,1,1,1],[8,8,8,8],[6,6,6,6],[3,7,3,7],[2,9,2,9],[5,5,3,3],[8,1,8,1],[6,2,6,2],[7,3.6,7,3.6]]; 150 | cSH=[.1,.2,.2,.4,.4,.6,.3,.7,.5,.5]; 151 | aSH=numpy.asarray(aSH); 152 | cSH=numpy.asarray(cSH); 153 | fit=0; 154 | for i in range(0,4): 155 | v=numpy.matrix(L-aSH[i,:]) 156 | fit=fit-((v)*(v.T)+cSH[i])**(-1); 157 | o=fit.item(0); 158 | return o 159 | 160 | def F22(L): 161 | aSH=[[4,4,4,4],[1,1,1,1],[8,8,8,8],[6,6,6,6],[3,7,3,7],[2,9,2,9],[5,5,3,3],[8,1,8,1],[6,2,6,2],[7,3.6,7,3.6]]; 162 | cSH=[.1,.2,.2,.4,.4,.6,.3,.7,.5,.5]; 163 | aSH=numpy.asarray(aSH); 164 | cSH=numpy.asarray(cSH); 165 | fit=0; 166 | for i in range(0,6): 167 | v=numpy.matrix(L-aSH[i,:]) 168 | fit=fit-((v)*(v.T)+cSH[i])**(-1); 169 | o=fit.item(0); 170 | return o 171 | 172 | def F23(L): 173 | aSH=[[4,4,4,4],[1,1,1,1],[8,8,8,8],[6,6,6,6],[3,7,3,7],[2,9,2,9],[5,5,3,3],[8,1,8,1],[6,2,6,2],[7,3.6,7,3.6]]; 174 | cSH=[.1,.2,.2,.4,.4,.6,.3,.7,.5,.5]; 175 | aSH=numpy.asarray(aSH); 176 | cSH=numpy.asarray(cSH); 177 | fit=0; 178 | for i in range(0,9): 179 | v=numpy.matrix(L-aSH[i,:]) 180 | fit=fit-((v)*(v.T)+cSH[i])**(-1); 181 | o=fit.item(0); 182 | return o 183 | ''' 184 | def getFunctionDetails(a): 185 | 186 | # [name, lb, ub, dim] 187 | param = { 0: ["F1",-100,100,30], 188 | 1 : ["F2",-10,10,30], 189 | 2 : ["F3",-100,100,30], 190 | 3 : ["F4",-100,100,30] , 191 | 4 : ["F5",-30,30,30], 192 | 5 : ["F6",-100,100,30], 193 | 6 : ["F7",-1.28,1.28,30], 194 | 7 : ["F8",-500,500,30], 195 | 8 : ["F9",-5.12,5.12,30], 196 | 9 : ["F10",-32,32,30], 197 | 10 : ["F11",-600,600,30] , 198 | 11 : ["F12",-50,50,30], 199 | 12 : ["F13",-50,50,30], 200 | 13 : ["F14",-65.536,65.536,2], 201 | 14 : ["F15",-5,5,4], 202 | 15 : ["F16",-5,5,2], 203 | 16 : ["F17",-5,15,2], 204 | 17 : ["F18",-2,2,2] , 205 | 18 : ["F19",0,1,3], 206 | 19 : ["F20",0,1,6], 207 | 20 : ["F21",0,10,4], 208 | 21 : ["F22",0,10,4], 209 | 22 : ["F23",0,10,4], 210 | } 211 | return param.get(a, "nothing") 212 | 213 | 214 | 215 | --------------------------------------------------------------------------------