Python code for COOT Optimization
import from numpy import random
import time
import numpy as np
rng = np.random.default_rng()
import math
import sys
from numpy import linalg as LA
import numpy as np
import math
from numpy import infSupporting functions
def fun(X):
output = sum(np.square(X))
return output
# This function is to initialize the Vulture population.
def initial(pop, dim, ub, lb):
X = np.zeros([pop, dim])
for i in range(pop):
for j in range(dim):
X[i, j] = random.random()*(ub[j] - lb[j]) + lb[j]
return X
# Calculate fitness values for each Vulture
def CaculateFitness1(X,fun):
fitness = fun(X)
return fitness
# Boundary detection function.
def BorderCheck1(X,lb,ub,dim):
for j in range(dim):
#print(X)
if X[j]<lb[j]:
X[j] = ub[j]
elif X[j]>ub[j]:
X[j] = lb[j]
return X
COOT Algorithm
def COOT(N,dim,lb,ub,Max_iter,fun):
NLeader=math.ceil(0.1*N)
Ncoot=N-NLeader
Convergence_curve = np.zeros([300,1])
fitness = np.zeros([pop, 1])
gBest=np.zeros([1,dim])
temp = np.zeros([1,dim])
gBestScore=inf
CootPos=initial(Ncoot, dim, lb,ub)
CootFitness=np.zeros([1,Ncoot])
#Initialize the locations of Leaders
LeaderPos=initial(NLeader, dim, lb,ub)
LeaderFit=np.zeros([1,NLeader])
for i in range(Ncoot):
CootFitness[0,i] = CaculateFitness1(CootPos[i, :], fun)
if gBestScore > CootFitness[0,i]:
gBestScore = CootFitness[0,i]
gBest = CootPos[i, :]
for i in range(NLeader):
LeaderFit[0,i] = CaculateFitness1(LeaderPos[i, :], fun)
if gBestScore > LeaderFit[0,i]:
gBestScore = LeaderFit[0,i]
gBest = LeaderPos[i, :]
Convergence_curve[0] = gBestScore
for t in range(Max_iter):
B=2-t*(1/Max_iter)
A=1-t*(1/Max_iter)
for i in range(Ncoot):
if random.rand()<0.5:
R=-1+2*random.rand()
R1=random.rand()
else:
R=-1+2*random.rand(1,dim)
R1=random.rand(1,dim)
k=math.fmod(i,NLeader)
k=math.floor(k)
if random.rand()<0.5:
r1=2*np.multiply(R1,np.cos(2*math.pi*R)) #|| i==1
CootPos[i,:]=np.multiply(r1,(LeaderPos[k,:]-CootPos[i,:]))+LeaderPos[k,:]
CootPos[i,:] = BorderCheck1(CootPos[i,:], lb, ub, dim)
else:
if random.rand()<0.5 and not i==1:
CootPos[i,:]=(CootPos[i,:]+CootPos[i-1,:])/2
CootPos[i,:] = BorderCheck1(CootPos[i,:], lb, ub, dim)
else:
Q=(random.rand(1,dim)*(ub-lb))+lb
CootPos[i,:]=CootPos[i,:]+A*np.multiply(R1,(Q[1,:]-CootPos[i,:]))
CootPos[i,:] = BorderCheck1(CootPos[i,:], lb, ub, dim)
for i1 in range(Ncoot):
CootFitness[0,i1] = CaculateFitness1(CootPos[i1, :], fun)
k=math.fmod(i,NLeader)
k=math.floor(k)
if CootFitness[0,i1] < LeaderFit[0,k]:
temp = LeaderPos[k, :]
tempfit =LeaderFit[0,k]
LeaderPos[k, :] = CootPos[i1, :]
LeaderFit[0,k] = CootFitness[0,i1]
CootPos[i1, :] = temp
CootFitness[0,i1] = tempfit
for i2 in range(NLeader):
if random.rand()<0.5:
R=-1+2*random.rand()
R3=random.rand()
else:
R=-1+2*random.rand(1,dim)
R3=random.rand(1,dim)
if random.rand()<0.5:
r2=B*np.multiply(R3,np.cos(2*math.pi*R))
temp1=np.multiply(r2,(gBest-LeaderPos[i2,:]))+gBest
#print('temp1',temp)
else:
r2=B*np.multiply(R3,np.cos(2*math.pi*R))
temp1=np.multiply(r2,(gBest-LeaderPos[i2,:]))-gBest
temp2 = temp1[0]
if temp2.size == 1:
temp = temp1
else:
temp = temp2
temp = BorderCheck1(temp, lb, ub, dim)
tempfit = CaculateFitness1(temp, fun)
if gBestScore > tempfit:
LeaderFit[0,i2] = gBestScore
LeaderPos[i2,:] = gBest
gBestScore = tempfit
gBest = temp
#print('t',t)
Convergence_curve[t] = gBestScore
#print('Convergence_curve',Convergence_curve[t])
return Convergence_curve,gBestScore,gBest
Main code
rng = np.random.default_rng()
time_start = time.time()
pop = 20 # Population size n
MaxIter = 300 # Maximum number of iterations.
dim = 10 # The dimension.
fl=-100 # The lower bound of the search interval.
ul=100 # The upper bound of the search interval.
lb = fl*np.ones([dim, 1])
ub = ul*np.ones([dim, 1])
Convergence_curve,gBestScore,gBest = COOT(pop, dim, lb, ub, MaxIter, fun)
print('gBestScore',gBestScore)
View convergence curve
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot( Convergence_curve,color='dodgerblue', marker='o', markeredgecolor='dodgerblue', markerfacecolor='dodgerblue')
ax.set_xlabel('Number of Iterations',fontsize=15)
ax.set_ylabel('Fitness',fontsize=15)
ax.set_title('COOT Optimization')
plt.savefig('image.jpg', format='jpg')
plt.show()
Comments
Post a Comment