Skip to content
← Back to Community
I was trying to run the code but I have no idea what went wrong
Profile icon
seadmkan

rm(list=ls())

######### Benchmark Function #########
beville_ring_problem<-function(xx){
out<-((0.07075)(pi)(xx[2]^2-xx[1]^2)*(xx[3]))
return(out)}

beville_ring_constraint<-function(xx){
xx<-c(12,10,0.2,0.204)
y<-rep(NA,7)
pmax = 5400
cmax = 0.2
S = 200
E = 30e6
q = 0.3
H = 2
dmax = 12.01

K <- xx[2]/xx[1]
u <- ((6)/(pi)(log(K)))(((K-1)/(K))^2)
z <- ((6)/(pi)(log(K)))(K-1)/(ln(K))-1 #change ln to log
w <- (6)/(pilog(K))((K-1)/2) # Check this. log(K) is missing in this equation.

if (a>=1.4) {
fa<-1}
else if (a==1.5){
fa<-0.85}
else if (a==1.6){
fa<-0.77}
else if (a==1.7){
fa<-0.71}
else if (a==1.8){
fa<-0.66}
else if (a==1.9){
fa<-0.63}
else if (a==2.0){
fa<-0.6}
else if (a==2.1){
fa<-0.58}
else if (a==2.2){
fa<-0.56}
else if (a==2.3){
fa<-0.55}
else if (a==2.4){
fa<-0.53}
else if (a==2.5){
fa<-0.52}
else if (a==2.6){
fa<-0.51}
else if (a==2.7){
fa<-0.51}
else (a<=2.5){
fa<-0.5}
c1 <- f(a)*a

y[1] <- S-((4)(E)(cmax))/((1)-((q)^2))(u)(xx[2]^2)((z)(xx[4]-(cmax/2))+(w)(xx[3]))
y[2] <- ((4)
(E)(cmax))/((1)-((q)^2))((u)(xx[2]^2))((xx[4]-(cmax/2))(xx[4]-cmax)(xx[3])+xx[3]^3)
y[3] <- c1-cmax
y[4] <- H-xx[3]-xx[4]
y[5] <- dmax-xx[2]
y[6] <- xx[2]-xx[1]
y[7] <- 0.3-xx[4]/xx[2]-xx[1] # check this logic

return(y)}

############# Run from here ####################

optimum _value<-rep(NA,100)
for(t in 1:100){

############## Bees Algorithm start here #################

func=multiple_disc_clutch_brake_problem# Function, domains & global optimun (Run function before run)
domain1=c(60,90,1,600,2) # domain for more than 2 dimension (Insert domain values here)
domain2=c(80,110,3,1000,9)
#domain1=c(-100,-100) # domain for 2 dimension
#domain2=c(100,100)

n=10 #Bees Algorithm parameters
ee=2 #Number of elite sites
nep=10 # Recruited bees for elite site/s
ngh=1# neighbourhood size
m=5 # Number of best sites (m>ee)
nsp=5# recruited bees for remaining best sites
it=750
stlim=10

neigh.size<-c(rep((ngh1),n))
count<-c(rep(0,n))
variables<-length(domain1)
Funct_eval=integer(1)
abandon<-matrix(rep(NA,m
(variables+1)),ncol=(variables+1))
domains<-cbind(domain1,domain2)
Neval.iter=integer(1)
Global_par=double(variables)

matrix.rand<-matrix(rep(NA,n*variables),ncol=n)

for(j in 1:n){

repeat{ for(i in 1:variables) { if(i==3){ matrix.rand[i,j]<-round(runif(1,domains[i,1],domains[i,2])/0.5)*0.5 }else if(i==4){ matrix.rand[i,j]<-round(runif(1,domains[i,1],domains[i,2])/10)*10 }else{ matrix.rand[i,j]<-round(runif(1,domains[i,1],domains[i,2])) } } #put round before run if aa<- multiple_disc_clutch_brake_constraint(matrix.rand[,j]) if(aa[1]>=0 && aa[2]>=0 && aa[3]>=0 && aa[4]>=0 && aa[5]>=0 && aa[6]>=0 && aa[7]>=0 && aa[8]>=0){ # add one more constraint & change the inequality symbol break } }

}

fitness1<-rep(NA,n)

for(j in 1:n){
fitness1[j]<-func(matrix.rand[,j])}

fit.sample<-cbind(t(matrix.rand),fitness1,count, neigh.size)

Glo_opt<-min(fit.sample[,variables+1])
Global_par<-fit.sample[which(fit.sample[,(variables+1)]==min(fit.sample[,(variables+1)])),-((variables+1):(variables+3))]

Funct_eval<-length(fitness1)
w=0
while(Funct_eval < 1001 ){

w<-w+1 if(w==1){fit.sample<-fit.sample}else{fit.sample<-new.population} sorted<-fit.sample[order(fit.sample[,variables+1]),] ### ELITE SITE, "E" ELITE BEES elite.bees<-sorted[1:ee,-((variables+1):(variables+3))] elite.bees.neig<-matrix(rep(NA,ee*nep*variables),ncol=variables) #create a vector for elite bees which will be created after this line elite.fitt<-rep(NA,ee*nep) for(j in 1:ee){ for(i in 1:nep){ repeat{ for(k in 1:variables){ if(ee==1){ lower_limit<-max(domain1[k],elite.bees[k]-(sorted[j,(variables+3)]*(0.5))) upper_limit<-min(domain2[k],elite.bees[k]+(sorted[j,(variables+3)]*(0.5))) if(k==3){ elite.bees.neig[i,k]<-round(runif(1,lower_limit,upper_limit)/0.5)*0.5 }else if(k==4){ elite.bees.neig[i,k]<-round(runif(1,lower_limit,upper_limit)/10)*10 }else{ elite.bees.neig[i,j]<-round(runif(1,lower_limit,upper_limit)) } }else{ lower_limit<-max(domain1[k],elite.bees[j,k]-(sorted[j,(variables+3)]*(0.5))) ###### upper_limit<-min(domain2[k],elite.bees[j,k]+(sorted[j,(variables+3)]*(0.5))) if(k==3){ elite.bees.neig[(((j-1)*nep)+i),k]<-round(runif(1,lower_limit,upper_limit)/0.5)*0.5 }else if(k==4){ elite.bees.neig[(((j-1)*nep)+i),k]<-round(runif(1,lower_limit,upper_limit)/10)*10 }else{ elite.bees.neig[(((j-1)*nep)+i),k]<-round(runif(1,lower_limit,upper_limit)) } } } bb<- multiple_disc_clutch_brake_constraint(elite.bees.neig[(((j-1)*nep)+i),]) if(bb[1]>=0 && bb[2]>=0 && bb[3]>=0 && bb[4]>=0 && bb[5]>=0 && bb[6]>=0 && bb[7]>=0 && bb[8]>=0){ break } } }} for(fitt in 1:(ee*nep) ) { elite.fitt[fitt]<-func(elite.bees.neig[fitt,]) }#evaluate the fitness of elite.bees.neig Funct_eval<-Funct_eval+length(elite.fitt) elite.bees.neig_fit<-cbind(elite.bees.neig,elite.fitt)#bind the fitness with its variables bees.elite.fitness<-sorted[1:ee,-((variables+2):(variables+3))] fittest_elite_search<-matrix(rep(NA,ee*(variables+1)),ncol=(variables+1) ) if(ee==1){ neig_search_elite<-rbind(bees.elite.fitness,elite.bees.neig_fit ) #bind it with the previous elite to compare index<-which(neig_search_elite[,(variables+1)]==min(neig_search_elite[,(variables+1)])) if(length(index)>1){ fittest_elite_search<-neig_search_elite[index[1],] }else{ fittest_elite_search<-neig_search_elite[index,] } }else{ for(k in 1:ee){ neig_search_elite<-rbind(bees.elite.fitness[k,],elite.bees.neig_fit[((k-1)*nep+1):(k*nep),] ) index<-which(neig_search_elite[,(variables+1)]==min(neig_search_elite[,(variables+1)])) if(length(index)>1){ fittest_elite_search[k,]<-neig_search_elite[index[1],] }else{ fittest_elite_search[k,]<-neig_search_elite[index,] } } } if(ee==1){ if(fittest_elite_search[variables+1]>=sorted[1,(variables+1)]){ sorted[1,(variables+3)]<-sorted[1,(variables+3)]*0.8 sorted[1,(variables+2)]<-sorted[1,(variables+2)]+1 }else{ sorted[1,(variables+3)]<-ngh sorted[1,(variables+2)]<-0} fittest_elite_search<-c(fittest_elite_search,sorted[1,(variables+2):(variables+3)]) }else{ for(i in 1:ee){ if(fittest_elite_search[i,(variables+1)]>=sorted[i,(variables+1)]){ sorted[i,(variables+3)]<-sorted[i,(variables+3)]*0.8 sorted[i,(variables+2)]<-sorted[i,(variables+2)]+1 }else{ sorted[i,(variables+3)]<-ngh sorted[i,(variables+2)]<-0} } fittest_elite_search<-cbind(fittest_elite_search,sorted[(1:ee),(variables+2):(variables+3)]) } if(ee==1){ if(fittest_elite_search[variables+2]>=stlim){ Funct_eval<-Funct_eval+1 if(is.na(abandon[1,(variables+1)])){ abandon[1,]<-fittest_elite_search[1:(variables+1)] }else{ if(fittest_elite_search[variables+1]<abandon[1,(variables+1)]){ abandon[1,]<-fittest_elite_search[1:(variables+1)] }else{ abandon[1,]<-abandon[1,] } } for(i in 1:variables) { if(i==3){ fittest_elite_search[i]<-round(runif(1,domains[i,1],domains[i,2])/0.5)*0.5 }else if(i==4){ fittest_elite_search[i]<-round(runif(1,domains[i,1],domains[i,2])/10)*10 }else{ fittest_elite_search[i]<-round(runif(1,domains[i,1],domains[i,2])) } } ####### fittest_elite_search[variables+1]<-func(fittest_elite_search[1:variables]) fittest_elite_search[variables+2]<-0 fittest_elite_search[variables+3]<-ngh }else{fittest_elite_search<-fittest_elite_search} }else{ for(i in 1:ee){ if(fittest_elite_search[i,variables+2]>=stlim){ #capture the abandoned sites Funct_eval<-Funct_eval+1 if(is.na(abandon[i,(variables+1)])){ abandon[i,]<-fittest_elite_search[i,(1:(variables+1))] }else{ if(fittest_elite_search[i,(variables+1)]<abandon[i,(variables+1)]){ abandon[i,]<-fittest_elite_search[i,(1:(variables+1))] }else{ abandon[i,]<-abandon[i,] } } repeat{ for(j in 1:variables) { if(j==3){ fittest_elite_search[i,j]<-round(runif(1,domains[j,1],domains[j,2])/0.5)*0.5 }else if(j==4){ fittest_elite_search[i,j]<-round(runif(1,domains[j,1],domains[j,2])/10)*10 }else{ fittest_elite_search[i,j]<-round(runif(1,domains[j,1],domains[j,2])) } } ########### cc<- multiple_disc_clutch_brake_constraint(fittest_elite_search[i,1:variables]) if(cc[1]>=0 && cc[2]>=0 && cc[3]>=0 && cc[4]>=0 && cc[5]>=0 && cc[6]>=0 && cc[7]>=0 && cc[8]>=0){ break } } fittest_elite_search[i,variables+1]<-func(fittest_elite_search[i,1:variables]) fittest_elite_search[i,variables+2]<-0 fittest_elite_search[i,variables+3]<-ngh }else{fittest_elite_search[i,]<-fittest_elite_search[i,]} } } ###### LOCAL SEARCH FOR THE M-E BEES best.bees<-sorted[(ee+1):m,-((variables+1):(variables+3))] ##create a vector for best bees which will be created after this line best.bees.neig<-matrix(rep(NA,(m-ee)*nsp*variables),ncol=variables) for(j in 1:(m-ee)){ for(i in 1:nsp){ repeat{ for(k in 1:variables){ if((m-ee)==1){ lower_limit<-max(domain1[k],best.bees[k]-(sorted[(ee+j),(variables+3)]*(0.5))) upper_limit<-min(domain2[k],best.bees[k]+(sorted[(ee+j),(variables+3)]*(0.5))) best.bees.neig[i,k]<-runif(1,lower_limit,upper_limit)########## }else{ lower_limit<-max(domain1[k],best.bees[j,k]-(sorted[(ee+j),(variables+3)]*(0.5))) upper_limit<-min(domain2[k],best.bees[j,k]+(sorted[(ee+j),(variables+3)]*(0.5))) if(k==3){ best.bees.neig[(((j-1)*nsp)+i),k]<-round(runif(1,lower_limit,upper_limit)/0.5)*0.5 }else if(k==4){ best.bees.neig[(((j-1)*nsp)+i),k]<-round(runif(1,lower_limit,upper_limit)/10)*10 }else{ best.bees.neig[(((j-1)*nsp)+i),k]<-round(runif(1,lower_limit,upper_limit)) } } } dd<- multiple_disc_clutch_brake_constraint( best.bees.neig[(((j-1)*nsp)+i),]) if(dd[1]>=0 && dd[2]>=0 && dd[3]>=0 && dd[4]>=0 && dd[5]>=0 && dd[6]>=0 && dd[7]>=0 && dd[8]>=0){ break } } }} best.fitt.m_ee<-rep(NA,(m-ee)*nsp) for(fitt in 1:((m-ee)*nsp)){ best.fitt.m_ee[fitt]<-func(best.bees.neig[fitt,]) }#Evaluate the fitness function Funct_eval<-Funct_eval+length(best.fitt.m_ee) best.bees.neig_fit<-cbind(best.bees.neig,best.fitt.m_ee) #Bind the variables with its fitness bees.best.fitness<-sorted[(ee+1):m,-((variables+2):(variables+3))] fittest_best_search<-matrix(rep(NA,(m-ee)*(variables+1)),ncol=(variables+1)) if((m-ee)==1){ neig_search_best<-rbind(bees.best.fitness,best.bees.neig_fit ) index<-which(neig_search_best[,(variables+1)]==min(neig_search_best[,(variables+1)])) if(length(index)>1){ fittest_best_search<-neig_search_best[index[1],] }else{ fittest_best_search<-neig_search_best[index,] } }else{ for(k in 1:(m-ee)){ neig_search_best<-rbind(bees.best.fitness[k,],best.bees.neig_fit[((k-1)*nsp+1):(k*nsp),] ) index<-which(neig_search_best[,(variables+1)]==min(neig_search_best[,(variables+1)])) if(length(index)>1){ fittest_best_search[k,]<-neig_search_best[index[1],] }else{ fittest_best_search[k,]<-neig_search_best[index,] } } } if((m-ee)==1){ if(fittest_best_search[variables+1]>=sorted[(ee+1),(variables+1)]){ sorted[(ee+1),(variables+3)]<-sorted[(ee+1),(variables+3)]*0.8 sorted[(ee+1),(variables+2)]<-sorted[(ee+1),(variables+2)]+1 }else{ sorted[(ee+1),(variables+3)]<-ngh sorted[(ee+1),(variables+2)]<-0} fittest_best_search<-c(fittest_best_search,sorted[(ee+1),(variables+2):(variables+3)]) }else{ for(i in 1:(m-ee)){ if(fittest_best_search[i,(variables+1)]>=sorted[(i+ee),(variables+1)]){ sorted[(i+ee),(variables+3)]<-sorted[(i+ee),(variables+3)]*0.8 sorted[(i+ee),(variables+2)]<-sorted[(i+ee),(variables+2)]+1 }else{ sorted[(i+ee),(variables+3)]<-ngh sorted[(i+ee),(variables+2)]<-0} } fittest_best_search<-cbind(fittest_best_search,sorted[((ee+1):m),(variables+2):(variables+3)]) } if((m-ee)==1){ if(fittest_best_search[variables+2]>=stlim){ Funct_eval<-Funct_eval+1 if(is.na(abandon[ee+1,variables+1])){ abandon_sites[ee+1,]<-fittest_best_search[1:(variables+1)] }else{ if(fittest_best_search[variables+1]<abandon[ee+1,(variables+1)]){ abandon[ee+1,]<-fittest_best_search[1:(variables+1)] }else{ abandon[ee+1,]<-abandon[ee+1,] } } for(i in 1:variables) { fittest_best_search[i]<-runif(1,domains[i,1],domains[i,2]) }########## fittest_best_search[variables+1]<-func(fittest_best_search[1:variables]) fittest_best_search[variables+2]<-0 fittest_best_search[variables+3]<-ngh }else{fittest_best_search<-fittest_best_search} }else{ for(i in 1:(m-ee)){ if(fittest_best_search[i,variables+2]>=stlim){ Funct_eval<-Funct_eval+1 if(is.na(abandon[ee+i,(variables+1)])){ abandon[(ee+i),]<-fittest_best_search[i,1:(variables+1)] }else{ if(fittest_best_search[(i),(variables+1)]<abandon[(ee+i),(variables+1)]){ abandon[(ee+i),]<-fittest_best_search[(i),1:(variables+1)] }else{ abandon[(ee+i),]<-abandon[(ee+i),] } } repeat{ for(j in 1:variables) { if(j==3){ fittest_best_search[i,j]<-round(runif(1,domains[j,1],domains[j,2])/0.5)*0.5 }else if(j==4){ fittest_best_search[i,j]<-round(runif(1,domains[j,1],domains[j,2])/10)*10 }else{ fittest_best_search[i,j]<-round(runif(1,domains[j,1],domains[j,2])) } } ff<- multiple_disc_clutch_brake_constraint(fittest_best_search[i,(1:variables)]) if(ff[1]>=0 && ff[2]>=0 && ff[3]>=0 && ff[4]>=0 && ff[5]>=0 && ff[6]>=0 && ff[7]>=0 && ff[8]>=0){ break } } fittest_best_search[i,variables+1]<-func(fittest_best_search[i,1:variables]) fittest_best_search[i,variables+2]<-(fittest_best_search[i,variables+2])*0 fittest_best_search[i,variables+3]<-ngh }else{fittest_best_search[i,]<-fittest_best_search[i,]} } } ### GLOBAL SEARCH FOR N-M BEES global.search<-matrix(rep(NA,(n-m)*variables),nrow=variables) for(j in 1:(n-m)){ repeat{ for(i in 1:variables) { if(i==3){ global.search[i,j]<-round(runif(1,domains[i,1],domains[i,2])/0.5)*0.5 }else if(i==4){ global.search[i,j]<-round(runif(1,domains[i,1],domains[i,2])/10)*10 }else{ global.search[i,j]<-round(runif(1,domains[i,1],domains[i,2])) } } ########## gg<- multiple_disc_clutch_brake_constraint(global.search[,j]) if(gg[1]>=0 && gg[2]>=0 && gg[3]>=0 && gg[4]>=0 && gg[5]>=0 && gg[6]>=0 && gg[7]>=0 && gg[8]>=0){ break } } } global.search<-t(global.search) elite.fitt.global<-rep(NA,n-m) for(fitt in 1:(n-m)){ elite.fitt.global[fitt]<-func(global.search[fitt,]) } Funct_eval<-Funct_eval+length(elite.fitt.global) if(w==1){Neval.iter1<-Funct_eval}else{Neval.iter1<-c(Neval.iter1,Funct_eval)} fitt.global.pop<-cbind(global.search,elite.fitt.global) rand_ngh<-(rep((ngh*1),(n-m))) rand_count<-(rep(0,(n-m))) global.pop.new<-cbind(fitt.global.pop,rand_count,rand_ngh) new.population<-rbind(fittest_elite_search,fittest_best_search,global.pop.new) population_best<-rbind(new.population,c(Global_par,Glo_opt,0,ngh)) index.pop<-which(population_best[,(variables+1)]==min(population_best[,(variables+1)])) if(length(index.pop)>1){ iter.fittest1<-population_best[index.pop[1],-((variables+2):(variables+3))] }else{ iter.fittest1<-population_best[index.pop,-((variables+2):(variables+3))]} Global_par<-iter.fittest1[1:variables] Glo_opt<-func(Global_par) if(w==1){iter.fittest<-Glo_opt}else{iter.fittest<-c(iter.fittest,Glo_opt)}

}

Total.fitness<-cbind(iter.fittest,Neval.iter1)
plot(Total.fitness[,2],Total.fitness[,1],,type="l", xlab = "Numbers of function evaluation", ylab="Fitness")
grid ()
title("figure1")

optimum _value[t]<-Glo_opt

}

median(accuracy_vect)
write.table(accuracy_vect, "D:/MY DATA/SEM 9/FYP 2/Results/SYMMETRIACL/NGH=1 (SYMMETRIACL)/sba_fitnessvalue_Multiple_Clutch.txt", sep="\t") # set the location where it need to be saved
write.table(speed_vect, "D:/MY DATA/SEM 9/FYP 2/Results/SYMMETRIACL/NGH=1 (SYMMETRIACL)/sba_functionevaluation_Multiple_Clutch.txt", sep="\t") # set the location where it need to be saved

Voters
Profile icon
seadmkan
Comments
hotnewtop
Profile icon
Coder100

line 53, you forgot an if:

else if (a<-2.5) {
Profile icon
seadmkan

thank you I see it now. but still, I got an error when I run the code @Coder100

Profile icon
TANMAYBAGADIA

@Coder100 have u mastered almost all languages?