3
3
import copy
4
4
import numpy as np
5
5
from numpy import log2
6
- # import matplotlib.pyplot as plt
6
+ import matplotlib .pyplot as plt
7
7
from CAPalloction import channelbandwidth , readFile ,classifyUser ,getPower ,interfere1 ,RateNow ,channelAllocate ,turnInToParticle ,chanNumOfEachUser ,ParticleInToMatrix
8
8
from SINR import distance
9
9
#----------------------PSO参数设置---------------------------------
@@ -37,9 +37,14 @@ def SINR(self, X):
37
37
"""定义一个求信噪比的函数:基站n将信道s分配给用户k,X表示一个粒子(功率等级的粒子),不是初始化由各个粒子组成的矩阵"""
38
38
'''interfere1(n, s, user, chanlist, bsx, bsy)函数参数'''
39
39
SINRlist = []##定义一个sinr列表,初始化为空,后续会在里面包含每一个用户的SINR值,其长度等于用户数量
40
- bsx = self .BSX + [0 ]#将宏基站的坐标加入到基站坐标bsx,bsy中
41
- bsy = self .BSY + [0 ]
40
+ bsx = self .BSX #将宏基站的坐标加入到基站坐标bsx,bsy中
41
+ print bsx
42
+ bsy = self .BSY
43
+ print bsy
42
44
XP = np .array (ParticleInToMatrix (X )) #将得到的粒子转为原来的功率等级矩阵(有没有必要转成ndarray有待考虑)
45
+ print len (X )
46
+ for i in xrange (len (XP )):
47
+ print len (XP [i ])
43
48
# channelofEacheruser = chanNumOfEachUser(self.BSchanAllocate)##每个用户的信道数量
44
49
for indexi in xrange (len (self .BSchanAllocate )):#对于同一个用户占用多个信道的情况后续处理,暂时当做每个不同信道的用户当做不同的用户,即便是同一个用户
45
50
if self .BSchanAllocate [indexi ].count (- 1 ) < 64 :##当前基站存在信道分配
@@ -58,7 +63,8 @@ def SINR(self, X):
58
63
p1 = XP [indexi ][indexj ]#得到对应信道分配的功率等级
59
64
inter = interfere1 (indexi , indexj , u , self .BSchanAllocate , bsx , bsy )
60
65
d = distance (u [0 ],u [1 ],currentBSX ,currentBSY )
61
- sinr = P * p1 * d ** (- 4 )/ (inter + P * (L ** (- 4 )))
66
+ # print type(inter)
67
+ sinr = P * p1 * d ** (- 4 )/ (inter + P * L ** (- 4 ))
62
68
SINRlist .append (sinr )## 暂时将所得到的值追加到SINRlist中去,至于一个用户占用多个信道的问题,暂时还没有想到别的办法,捎带考虑;这么做得到的结果是:这个列表的长度>=用户数量
63
69
64
70
else :
@@ -96,7 +102,7 @@ def userV(self,X):#计算每个用户的速率
96
102
if self .BSCover [i ][j ]== self .BSchanAllocate [ii ][jj ]:
97
103
rate += userV [ii ][jj ]
98
104
V .append (rate )#上面的if和else 必定会执行一个
99
- return V
105
+ return V #返回的是列表,下面求self.p_fit[i]得是一个值【注意注意】
100
106
101
107
#---------------------目标函数Sphere函数-----------------------------
102
108
def function (self ,x ): #p不是列表,是numpy.ndarray,列表是不能进行数值运算的
@@ -124,9 +130,10 @@ def init_Population(self):
124
130
for j in range (self .dim ):
125
131
self .V [i ][j ] = random .uniform (0 ,1 ) #初始化粒子的速度
126
132
self .pbest [i ] = self .X [i ] ##每个粒子的最佳位置
127
- tmp = self .function (self .X [i ])#调用目标函数,计算当前粒子的适应值,目标函数是处理每一个粒子的,在这个表达式中显而易见
133
+ # tmp = self.function(self.X[i])#调用目标函数,计算当前粒子的适应值,目标函数是处理每一个粒子的,在这个表达式中显而易见
134
+ tmp = self .userV (self .X [i ])#调用目标函数,计算当前粒子的适应值,目标函数是处理每一个粒子的,在这个表达式中显而易见
128
135
self .p_fit [i ] = tmp ##每个粒子最佳适应值
129
- if (tmp < self .fit ): #判断小于全局最佳适应值,将当前粒子的最佳适应值赋值给全局最佳适应值
136
+ if (tmp < self .fit ): #判断小于全局最佳适应值,将当前粒子的最佳适应值赋值给全局最佳适应值【适应度的值是一个(向量),所以这里在初始化中需要改】
130
137
self .fit = tmp #
131
138
self .gbest = self .X [i ]
132
139
@@ -145,7 +152,7 @@ def init_Population2(self):
145
152
if self .X [i ][j ]!= 0 :##在x[i]不为0的位置,随机初始化一个速度值
146
153
self .V [i ][j ] = random .uniform (0 ,1 ) #初始化粒子的速度
147
154
self .pbest [i ] = self .X [i ] ##每个粒子的最佳位置
148
- tmp = self .function (self .X [i ])#调用目标函数,计算当前粒子的适应值,目标函数是处理每一个粒子的,在这个表达式中显而易见
155
+ tmp = self .userV (self .X [i ])#调用目标函数,计算当前粒子的适应值,目标函数是处理每一个粒子的,在这个表达式中显而易见
149
156
self .p_fit [i ] = tmp ##每个粒子最佳适应值
150
157
if (tmp < self .fit ): #判断小于全局最佳适应值,将当前粒子的最佳适应值赋值给全局最佳适应值
151
158
self .fit = tmp #
@@ -196,14 +203,14 @@ def printXV(self):
196
203
fitness = my_pso .iterator ()
197
204
my_pso .printXV ()
198
205
#----------------------画 图--------------------------
199
- # plt.figure(1)
200
- # plt.title("Figure1")
201
- # plt.xlabel("iterators", size=14)
202
- # plt.ylabel("fitness", size=14)
203
- # t = np.array([t for t in range(0,max_iter)]) ##200 为max_iter
204
- # fitness = np.array(fitness)
205
- # plt.plot(t,fitness, color='b',linewidth=3)
206
- # plt.show()
206
+ plt .figure (1 )
207
+ plt .title ("Figure1" )
208
+ plt .xlabel ("iterators" , size = 14 )
209
+ plt .ylabel ("fitness" , size = 14 )
210
+ t = np .array ([t for t in range (0 ,max_iter )]) ##200 为max_iter
211
+ fitness = np .array (fitness )
212
+ plt .plot (t ,fitness , color = 'b' ,linewidth = 3 )
213
+ plt .show ()
207
214
208
215
209
216
0 commit comments