Skip to content

Commit 10204fa

Browse files
committed
7.10
7.10
1 parent b7a8507 commit 10204fa

File tree

3 files changed

+103
-29
lines changed

3 files changed

+103
-29
lines changed

CAPalloction.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def interfere1(n, s, user, chanlist, bsx, bsy):
169169
interf += p*(d**(-4))
170170

171171
return interf
172+
else:
173+
print "Function interfere1:len(chanlist)!=len(BS)"
172174

173175
"""
174176
BSCover 一行代表一个基站 下的所有用户
@@ -394,7 +396,7 @@ def ParticleInToMatrix(p):#函数的作用是:将一个粒子群的粒子转换
394396
'''p表示一个功率等级的粒子'''
395397
PRankmatrix = []#功率等级矩阵
396398
for i in xrange(len(p)/64):
397-
PRankmatrix.append(p[64*i:63+64*i])
399+
PRankmatrix.append(p[64*i:64+64*i])##右边界取不到
398400
return PRankmatrix
399401

400402
#-------------------------------------统 计 用 户 信 道 数 量 函 数 -----------------------------------------
@@ -414,8 +416,8 @@ def chanNumOfEachUser(chanAllocate):
414416
def judgeTwoListEqual(ndarray1,ndarray2):
415417
'''判断两个array是否相等'''
416418
return (ndarray1 ==ndarray2).all()#判断两个矩阵相等
419+
# return (ndarray1>ndarray2).all()#判断前一个矩阵大于后一个矩阵
417420

418-
419421
#------------------------------主 函 数 ---------------------------------------
420422
if __name__=="__main__":
421423

@@ -431,11 +433,11 @@ def judgeTwoListEqual(ndarray1,ndarray2):
431433
for i in xrange(5):
432434
BSchanAllocate = channelAllocate(BSCover,BSchanAllocate,BSX,BSY)#2017年6月23日11:10:04修改(实际上没做任何修改),原因:之前是想在原来的存在的BSchanAllocate基础上通过多次信道的重新分配达到一个相对稳定或者较好的的状态
433435

434-
for i in xrange(len(BSchanAllocate)):
435-
print BSchanAllocate[i]
436-
for j in xrange(len(BSchanAllocate[i])):
437-
if BSchanAllocate[i][j]!=-1:
438-
An_k_s[i][j]=1
436+
# for i in xrange(len(BSchanAllocate)):
437+
# print BSchanAllocate[i]
438+
# for j in xrange(len(BSchanAllocate[i])):
439+
# if BSchanAllocate[i][j]!=-1:
440+
# An_k_s[i][j]=1
439441

440442
##既然信道分配已经确定了,那么平均功率所组成的一个粒子可以算作一个初始化粒子,然后针对这些已经分配信道的的用户的信道功率多做几次(20次)功率随机分配,就会产生许多不同的初始化
441443

@@ -445,9 +447,17 @@ def judgeTwoListEqual(ndarray1,ndarray2):
445447
# P.append(p)
446448
# for j in P:
447449
# print j
448-
p = getPower(BSchanAllocate)
449-
for i in xrange(len(p)):
450-
print p[i]
451-
if i==len(p)-1:
452-
print sum(p[i])
453-
450+
# p = getPower(BSchanAllocate)
451+
# for i in xrange(len(p)):
452+
# print p[i]
453+
# if i==len(p)-1:
454+
# print sum(p[i])
455+
x = turnInToParticle(BSchanAllocate)
456+
y = ParticleInToMatrix(x)##出问题了
457+
print len(x)
458+
for i in xrange(len(y)):
459+
print len(y[i])
460+
461+
462+
463+

pso.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import copy
44
import numpy as np
55
from numpy import log2
6-
# import matplotlib.pyplot as plt
6+
import matplotlib.pyplot as plt
77
from CAPalloction import channelbandwidth, readFile,classifyUser,getPower,interfere1,RateNow,channelAllocate,turnInToParticle,chanNumOfEachUser,ParticleInToMatrix
88
from SINR import distance
99
#----------------------PSO参数设置---------------------------------
@@ -37,9 +37,14 @@ def SINR(self, X):
3737
"""定义一个求信噪比的函数:基站n将信道s分配给用户k,X表示一个粒子(功率等级的粒子),不是初始化由各个粒子组成的矩阵"""
3838
'''interfere1(n, s, user, chanlist, bsx, bsy)函数参数'''
3939
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
4244
XP = np.array(ParticleInToMatrix(X)) #将得到的粒子转为原来的功率等级矩阵(有没有必要转成ndarray有待考虑)
45+
print len(X)
46+
for i in xrange(len(XP)):
47+
print len(XP[i])
4348
# channelofEacheruser = chanNumOfEachUser(self.BSchanAllocate)##每个用户的信道数量
4449
for indexi in xrange(len(self.BSchanAllocate)):#对于同一个用户占用多个信道的情况后续处理,暂时当做每个不同信道的用户当做不同的用户,即便是同一个用户
4550
if self.BSchanAllocate[indexi].count(-1) < 64:##当前基站存在信道分配
@@ -58,7 +63,8 @@ def SINR(self, X):
5863
p1 = XP[indexi][indexj]#得到对应信道分配的功率等级
5964
inter = interfere1(indexi, indexj, u, self.BSchanAllocate, bsx, bsy)
6065
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))
6268
SINRlist.append(sinr)## 暂时将所得到的值追加到SINRlist中去,至于一个用户占用多个信道的问题,暂时还没有想到别的办法,捎带考虑;这么做得到的结果是:这个列表的长度>=用户数量
6369

6470
else:
@@ -96,7 +102,7 @@ def userV(self,X):#计算每个用户的速率
96102
if self.BSCover[i][j]==self.BSchanAllocate[ii][jj]:
97103
rate += userV[ii][jj]
98104
V.append(rate)#上面的if和else 必定会执行一个
99-
return V
105+
return V#返回的是列表,下面求self.p_fit[i]得是一个值【注意注意】
100106

101107
#---------------------目标函数Sphere函数-----------------------------
102108
def function(self,x): #p不是列表,是numpy.ndarray,列表是不能进行数值运算的
@@ -124,9 +130,10 @@ def init_Population(self):
124130
for j in range(self.dim):
125131
self.V[i][j] = random.uniform(0,1) #初始化粒子的速度
126132
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])#调用目标函数,计算当前粒子的适应值,目标函数是处理每一个粒子的,在这个表达式中显而易见
128135
self.p_fit[i] = tmp ##每个粒子最佳适应值
129-
if(tmp < self.fit): #判断小于全局最佳适应值,将当前粒子的最佳适应值赋值给全局最佳适应值
136+
if(tmp < self.fit): #判断小于全局最佳适应值,将当前粒子的最佳适应值赋值给全局最佳适应值【适应度的值是一个(向量),所以这里在初始化中需要改】
130137
self.fit = tmp #
131138
self.gbest = self.X[i]
132139

@@ -145,7 +152,7 @@ def init_Population2(self):
145152
if self.X[i][j]!=0:##在x[i]不为0的位置,随机初始化一个速度值
146153
self.V[i][j] = random.uniform(0,1) #初始化粒子的速度
147154
self.pbest[i] = self.X[i] ##每个粒子的最佳位置
148-
tmp = self.function(self.X[i])#调用目标函数,计算当前粒子的适应值,目标函数是处理每一个粒子的,在这个表达式中显而易见
155+
tmp = self.userV(self.X[i])#调用目标函数,计算当前粒子的适应值,目标函数是处理每一个粒子的,在这个表达式中显而易见
149156
self.p_fit[i] = tmp ##每个粒子最佳适应值
150157
if(tmp < self.fit): #判断小于全局最佳适应值,将当前粒子的最佳适应值赋值给全局最佳适应值
151158
self.fit = tmp #
@@ -196,14 +203,14 @@ def printXV(self):
196203
fitness = my_pso.iterator()
197204
my_pso.printXV()
198205
#----------------------画 图--------------------------
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()
207214

208215

209216

tuling.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*-coding:utf-8 -*-
2+
import itchat,time,re
3+
from itchat.content import *
4+
import urllib,urllib2
5+
import json
6+
7+
def sendMessage(msg):
8+
info = msg['Text'].encode('UTF-8')
9+
url = 'http://www.tuling123.com/openapi/api'##接口url
10+
data = {u'key':"fb6a17ef837e4012a125f3aeddc9f47c","info":info,u"loc":'',"userid":''}
11+
data = urllib.urlencode(data)
12+
url2 = urllib2.Request(url,data)
13+
response = urllib2.urlopen(url2)
14+
apicontent = response.read()
15+
s = json.loads(apicontent,encoding='utf-8')
16+
print "s==",s
17+
if s['code']==100000:
18+
itchat.send(s['text'],msg['FromUserName'])
19+
20+
@itchat.msg_register([TEXT])
21+
def text_reply(msg):
22+
msgType = msg['MsgType']
23+
if msgType==1:##文字消息
24+
sendMessage(msg)
25+
# info = msg['Text'].encode('UTF-8')
26+
# url = 'http://www.tuling123.com/openapi/api'##接口url
27+
# data = {u'key':"fb6a17ef837e4012a125f3aeddc9f47c","info":info,u"loc":'',"userid":''}
28+
# data = urllib.urlencode(data)
29+
# url2 = urllib2.Request(url,data)
30+
# response = urllib2.urlopen(url2)
31+
# apicontent = response.read()
32+
# s = json.loads(apicontent,encoding='utf-8')
33+
# print "s==",s
34+
# if s['code']==100000:
35+
# itchat.send(s['text'],msg['FromUserName'])
36+
elif msgType == 3:##图片消息
37+
info = '不要给我发图片消息'.encode('UTF-8')
38+
# url = 'http://www.tuling123.com/openapi/api'##接口url
39+
# data = {u'key':"fb6a17ef837e4012a125f3aeddc9f47c","info":info,u"loc":'',"userid":''}
40+
# data = urllib.urlencode(data)
41+
# url2 = urllib2.Request(url,data)
42+
# response = urllib2.urlopen(url2)
43+
# apicontent = response.read()
44+
# s = json.loads(apicontent,encoding='utf-8')
45+
s = {'text':'不要给我发图片消息'.encode("UTF-8")}
46+
if s['code']==100000:
47+
itchat.send(info,msg['FromUserName'])
48+
49+
if __name__=="__main__":
50+
itchat.auto_login(enableCmdQR = 2,hotReload = True)
51+
itchat.run(debug = True)
52+
53+
54+
55+
56+
57+

0 commit comments

Comments
 (0)