forked from 7ossam81/EvoloPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer.py
More file actions
151 lines (126 loc) · 4.17 KB
/
optimizer.py
File metadata and controls
151 lines (126 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
"""
Created on Tue May 17 15:50:25 2016
@author: hossam
"""
import PSO as pso
import MVO as mvo
import GWO as gwo
import MFO as mfo
import CS as cs
import BAT as bat
import WOA as woa
import FFA as ffa
import SSA as ssa
import GA as ga
import HHO as hho
import SCA as sca
import JAYA as jaya
import benchmarks
import csv
import numpy
import time
def selector(algo,func_details,popSize,Iter):
function_name=func_details[0]
lb=func_details[1]
ub=func_details[2]
dim=func_details[3]
if(algo==0):
x=pso.PSO(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==1):
x=mvo.MVO(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==2):
x=gwo.GWO(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==3):
x=mfo.MFO(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==4):
x=cs.CS(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==5):
x=bat.BAT(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==6):
x=woa.WOA(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==7):
x=ffa.FFA(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==8):
x=ssa.SSA(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==9):
x=ga.GA(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==10):
x=hho.HHO(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==11):
x=sca.SCA(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
if(algo==12):
x=jaya.JAYA(getattr(benchmarks, function_name),lb,ub,dim,popSize,Iter)
return x
# Select optimizers
PSO= False
MVO= False
GWO = False
MFO = False
CS = False
BAT = False
WOA = False
FFA = False
SSA = False
GA = False
HHO = False
SCA = False
JAYA = True
# Select benchmark function
F1=True
F2=True
F3=True
F4=True
F5=True
F6=True
F7=True
F8=True
F9=True
F10=True
F11=True
F12=True
F13=True
F14=True
F15=True
F16=True
F17=True
F18=True
F19=True
optimizer=[PSO, MVO, GWO, MFO, CS, BAT, WOA, FFA, SSA, GA, HHO, SCA, JAYA]
benchmarkfunc=[F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19]
# Select number of repetitions for each experiment.
# To obtain meaningful statistical results, usually 30 independent runs
# are executed for each algorithm.
NumOfRuns=1
# Select general parameters for all optimizers (population size, number of iterations)
PopulationSize = 500
Iterations= 50
#Export results ?
Export=True
#ExportToFile="YourResultsAreHere.csv"
#Automaticly generated name by date and time
ExportToFile="experiment"+time.strftime("%Y-%m-%d-%H-%M-%S")+".csv"
# Check if it works at least once
Flag=False
# CSV Header for for the cinvergence
CnvgHeader=[]
for l in range(0,Iterations):
CnvgHeader.append("Iter"+str(l+1))
for i in range (0, len(optimizer)):
for j in range (0, len(benchmarkfunc)):
if((optimizer[i]==True) and (benchmarkfunc[j]==True)): # start experiment if an optimizer and an objective function is selected
for k in range (0,NumOfRuns):
func_details=benchmarks.getFunctionDetails(j)
x=selector(i,func_details,PopulationSize,Iterations)
if(Export==True):
with open(ExportToFile, 'a',newline='\n') as out:
writer = csv.writer(out,delimiter=',')
if (Flag==False): # just one time to write the header of the CSV file
header= numpy.concatenate([["Optimizer","objfname","startTime","EndTime","ExecutionTime"],CnvgHeader])
writer.writerow(header)
a=numpy.concatenate([[x.optimizer,x.objfname,x.startTime,x.endTime,x.executionTime],x.convergence])
writer.writerow(a)
out.close()
Flag=True # at least one experiment
if (Flag==False): # Faild to run at least one experiment
print("No Optomizer or Cost function is selected. Check lists of available optimizers and cost functions")