forked from 7ossam81/EvoloPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS.py
162 lines (97 loc) · 3.43 KB
/
CS.py
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
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-
"""
Created on Tue May 24 13:13:28 2016
@author: Hossam Faris
"""
import math
import numpy
import random
import time
from solution import solution
def get_cuckoos(nest,best,lb,ub,n,dim):
# perform Levy flights
tempnest=numpy.zeros((n,dim))
tempnest=numpy.array(nest)
beta=3/2;
sigma=(math.gamma(1+beta)*math.sin(math.pi*beta/2)/(math.gamma((1+beta)/2)*beta*2**((beta-1)/2)))**(1/beta);
s=numpy.zeros(dim)
for j in range (0,n):
s=nest[j,:]
u=numpy.random.randn(len(s))*sigma
v=numpy.random.randn(len(s))
step=u/abs(v)**(1/beta)
stepsize=0.01*(step*(s-best))
s=s+stepsize*numpy.random.randn(len(s))
tempnest[j,:]=numpy.clip(s, lb, ub)
return tempnest
def get_best_nest(nest,newnest,fitness,n,dim,objf):
# Evaluating all new solutions
tempnest=numpy.zeros((n,dim))
tempnest=numpy.copy(nest)
for j in range(0,n):
#for j=1:size(nest,1),
fnew=objf(newnest[j,:])
if fnew<=fitness[j]:
fitness[j]=fnew
tempnest[j,:]=newnest[j,:]
# Find the current best
fmin = min(fitness)
K=numpy.argmin(fitness)
bestlocal=tempnest[K,:]
return fmin,bestlocal,tempnest,fitness
# Replace some nests by constructing new solutions/nests
def empty_nests(nest,pa,n,dim):
# Discovered or not
tempnest=numpy.zeros((n,dim))
K=numpy.random.uniform(0,1,(n,dim))>pa
stepsize=random.random()*(nest[numpy.random.permutation(n),:]-nest[numpy.random.permutation(n),:])
tempnest=nest+stepsize*K
return tempnest
##########################################################################
def CS(objf,lb,ub,dim,n,N_IterTotal):
#lb=-1
#ub=1
#n=50
#N_IterTotal=1000
#dim=30
# Discovery rate of alien eggs/solutions
pa=0.25
nd=dim
# Lb=[lb]*nd
# Ub=[ub]*nd
convergence=[]
# RInitialize nests randomely
nest=numpy.random.rand(n,dim)*(ub-lb)+lb
new_nest=numpy.zeros((n,dim))
new_nest=numpy.copy(nest)
bestnest=[0]*dim;
fitness=numpy.zeros(n)
fitness.fill(float("inf"))
s=solution()
print("CS is optimizing \""+objf.__name__+"\"")
timerStart=time.time()
s.startTime=time.strftime("%Y-%m-%d-%H-%M-%S")
fmin,bestnest,nest,fitness =get_best_nest(nest,new_nest,fitness,n,dim,objf)
convergence = [];
# Main loop counter
for iter in range (0,N_IterTotal):
# Generate new solutions (but keep the current best)
new_nest=get_cuckoos(nest,bestnest,lb,ub,n,dim)
# Evaluate new solutions and find best
fnew,best,nest,fitness=get_best_nest(nest,new_nest,fitness,n,dim,objf)
new_nest=empty_nests(new_nest,pa,n,dim) ;
# Evaluate new solutions and find best
fnew,best,nest,fitness=get_best_nest(nest,new_nest,fitness,n,dim,objf)
if fnew<fmin:
fmin=fnew
bestnest=best
if (iter%10==0):
print(['At iteration '+ str(iter)+ ' the best fitness is '+ str(fmin)]);
convergence.append(fmin)
timerEnd=time.time()
s.endTime=time.strftime("%Y-%m-%d-%H-%M-%S")
s.executionTime=timerEnd-timerStart
s.convergence=convergence
s.optimizer="CS"
s.objfname=objf.__name__
return s