-
Notifications
You must be signed in to change notification settings - Fork 2
/
MOPSO.py
261 lines (197 loc) · 7.22 KB
/
MOPSO.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import numpy as np
from numpy import matlib
import matplotlib.pyplot as plt
import random as random
import math
def deleteOneRepositoryMember(rep , gamma):
gridindices = [item.gridIndex for item in rep]
OCells = np.unique(gridindices) # ocupied cells
N = np.zeros(len(OCells))
for k in range(len(OCells)):
N[k] = gridindices.count(OCells[k])
# selection probablity
p = [math.exp(gamma*item) for item in N]
p = np.array(p)/sum(p)
# select cell index
sci = roulettewheelSelection(p)
SelectedCell = OCells[sci]
#selected Cell members
selectedCellmembers = [item for item in gridindices if item == SelectedCell]
selectedmemberindex = np.random.randint(0,len(selectedCellmembers))
#selectedmember = selectedCellmembers[selectedmemberindex]
# delete memeber
#rep[selectedmemberindex] = []
rep = np.delete(rep, selectedmemberindex)
return rep.tolist()
def SelectLeader(rep , beta):
gridindices = [item.gridIndex for item in rep]
OCells = np.unique(gridindices) # ocupied cells
N = np.zeros(len(OCells))
for k in range(len(OCells)):
N[k] = gridindices.count(OCells[k])
# selection probablity
p = [math.exp(-beta*item) for item in N]
p = np.array(p)/sum(p)
# select cell index
sci = roulettewheelSelection(p)
SelectedCell = OCells[sci]
#selected Cell members
selectedCellmembers = [item for item in gridindices if item == SelectedCell]
selectedmemberindex = np.random.randint(0,len(selectedCellmembers))
# selectedmember = selectedCellmembers[selectedmemberindex]
return rep[selectedmemberindex]
def roulettewheelSelection(p):
r = random.random()
cumsum = np.cumsum(p)
y = (cumsum<r)
x= [i for i in y if i==True]
return len(x)
def FindGridIndex(particle, grid):
nObj = len(particle.cost)
NGrid = len(grid[0].LowerBounds)
particle.gridSubIndex = np.zeros((1,nObj))[0]
for j in range(nObj):
index_in_Dim = len( [item for item in grid[j].UpperBounds if particle.cost[j]>item])
particle.gridSubIndex[j] = index_in_Dim
particle.gridIndex = particle.gridSubIndex[0]
for j in range(1,nObj):
particle.gridIndex = particle.gridIndex
particle.gridIndex = NGrid*particle.gridIndex
particle.gridIndex = particle.gridIndex + particle.gridSubIndex[j]
return particle
def CreateGrid(pop,nGrid,alpha,nobj):
costs = [item.cost for item in pop]
Cmin = np.min(costs,axis=0)
Cmax = np.max(costs,axis=0)
deltaC = Cmax - Cmin
Cmin = Cmin - alpha*deltaC
Cmax = Cmax + alpha*deltaC
grid = [GridDim() for p in range(nobj)]
for i in range(nobj):
dimValues = np.linspace(Cmin[i],Cmax[i],nGrid+1).tolist()
grid[i].LowerBounds = [-float('inf')] + dimValues
grid[i].UpperBounds = dimValues + [float('inf')]
return grid
def Dominates(x,y):
x=np.array(x)
y=np.array(y)
x_dominate_y = all(x<=y) and any(x<y)
return x_dominate_y
def DetermineDomination(pop):
pop_len= len(pop)
for i in range(pop_len):
pop[i].IsDominated = False
for i in range(pop_len-1):
for j in range(i+1,pop_len):
if Dominates(pop[i].cost,pop[j].cost):
pop[j].IsDominated = True
if Dominates(pop[j].cost,pop[i].cost):
pop[i].IsDominated = True
return pop
# problem definition
def MOP2(x):
x = np.array(x)
n= len(x)
z1 = 1 - math.exp(-sum((x-1/math.sqrt(n))**2))
z2 = 1 - math.exp(-sum((x+1/math.sqrt(n))**2))
return [z1,z2]
costfunction = lambda x: MOP2(x)
nVar = 5 # number of decision vars
varMin = -4
varMax = 4
maxIt = 100
nPop = 200 # population size
nRep = 50 # size of repository
w = 0.5 # inertia wieght
c1 = 2 # personal learning coefficient
c2 = 2 # global learning coefficient
wdamping = 0.99
# ################ constriction coefficients
# phi1 = 2.05
# phi2 = 2.05
# phi = phi1+phi2
# chi = 2/(phi - 2 + np.sqrt(phi**2 - 4*phi))
# w = chi # inertia wieght
# c1 = chi*phi1 # personal learning coefficient
# c2 = chi*phi2 # global learning coefficient
# wdamping = 1
# #################
beta = 1 # leader selection pressure
gamma = 1 # deletion selection pressure
NoGrid = 5
alpha=0.1 # nerkhe tavarrom grid
# initialization
class Particle:
position = []
cost = []
velocity = []
best_position = []
best_cost = []
IsDominated = []
gridIndex = []
gridSubIndex = []
# for each objective a grid items is division of values of objective cost
class GridDim:
LowerBounds = []
UpperBounds = []
#Particles = np.matlib.repmat(Particle,nPop,1)
Particles = [Particle() for p in range(nPop)]
for i in range(nPop):
Particles[i].position = np.random.uniform(varMin,varMax,nVar)
Particles[i].velocity = np.zeros(nVar)
Particles[i].cost = costfunction(Particles[i].position)
# update best personal Best
Particles[i].best_position = Particles[i].position
Particles[i].best_cost = Particles[i].cost
Particles[i].IsDominated = False
Particles = DetermineDomination(Particles)
Repos = [item for item in Particles if item.IsDominated == False ]
nObj =len( Repos[0].cost)
grid = CreateGrid(Repos,NoGrid,alpha=0.1,nobj=nObj)
for r in range(len(Repos)):
Repos[r] = FindGridIndex(Repos[0],grid)
# MOPSO main loop
for it in range(maxIt):
for i in range(nPop):
leader = SelectLeader(Repos,beta)
# update velocity
Particles[i].velocity = w*Particles[i].velocity \
+ c1*np.random.rand(1,nVar)[0]*(Particles[i].best_position - Particles[i].position) \
+ c2*np.random.rand(1,nVar)[0]*(leader.position - Particles[i].position)
# update position
Particles[i].position = Particles[i].position + Particles[i].velocity
# evaluation
Particles[i].cost = costfunction(Particles[i].position)
if Dominates(Particles[i].cost,Particles[i].best_cost):
Particles[i].best_position = Particles[i].position
Particles[i].best_cost = Particles[i].cost
else:
if np.random.rand() > 0.5:
Particles[i].best_position = Particles[i].position
Particles[i].best_cost = Particles[i].cost
Repos = Repos + Particles
Repos = DetermineDomination(Repos)
Repos = [item for item in Repos if item.IsDominated == False ]
grid = CreateGrid(Repos,NoGrid,alpha=0.1,nobj=nObj)
for r in range(len(Repos)):
Repos[r] = FindGridIndex(Repos[r],grid)
# check if repository is full
if len(Repos) > nRep :
extra = len(Repos) - nRep
for e in range(extra):
Repos = deleteOneRepositoryMember(Repos,gamma)
########## show figure ##########
plt.clf()
particlesCost = np.reshape( [item.cost for item in Particles ],newshape=(nPop,2))
repositoryCost = [item.cost for item in Repos]
repositoryCost = np.reshape( repositoryCost, newshape=(len(repositoryCost),2))
plt.plot(particlesCost[:,0], particlesCost[:,1], 'o' ,mfc='none')
plt.plot(repositoryCost[:,0], repositoryCost[:,1], 'r*')
plt.draw()
plt.pause(0.00000000001)
w=w*wdamping
# print(repositoryCost)
# print("ok")
# print(particlesCost)
########## show figure ##########
plt.show()