Skip to content

Commit 5f06b86

Browse files
committed
6-2
6-2
1 parent 7c99456 commit 5f06b86

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

.settings/org.eclipse.core.resources.prefs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ encoding/CAPalloction.py=utf-8
33
encoding/Drawpic.py=gbk
44
encoding/SINR.py=utf-8
55
encoding/brest.py=utf-8
6+
encoding/chanAllocate.py=utf-8
67
encoding/coverage.py=utf-8
78
encoding/flask_email.py=utf-8
89
encoding/pract.py=utf-8

pso.py

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,13 @@
44
import numpy as np
55
import matplotlib.pyplot as plt
66
from CAPalloction import readFile,classifyUser,getPower,channelAllocate,turnInToParticle
7-
8-
9-
10-
class Bird(object):##定义一个鸟类(粒子类)
11-
12-
def __init__(self, speed, position, fit, lBestPosition, lBestFit):
13-
'''
14-
初始化的信息:speed粒子的速率,position:粒子的位置,fit适应度,lBestPostion粒子经理的最佳位置,
15-
lBestFit粒子的最佳是硬度值
16-
'''
17-
self.speed = speed
18-
self.position = position
19-
self.fit = fit
20-
self.lBestFit = lBestFit
21-
self.lBestPosition = lBestPosition
227

238
#----------------------PSO参数设置---------------------------------
249
class PSO():
25-
2610
def __init__(self,pN,dim,max_iter):
2711
self.w = 0.8
28-
self.c1 = 2
29-
self.c2 = 2
12+
self.c1 = 1.4961#2
13+
self.c2 = 1.4961#2
3014
self.r1= 0.6
3115
self.r2=0.3
3216
self.channelnum = 64
@@ -35,15 +19,19 @@ def __init__(self,pN,dim,max_iter):
3519
self.max_iter = max_iter #迭代次数
3620
self.X = np.zeros((self.pN,self.dim)) #所有粒子的位置和速度
3721
self.V = np.zeros((self.pN,self.dim))
38-
self.pbest = np.zeros((self.pN,self.dim)) #个体经历的最佳位置和全局最佳位置
39-
self.gbest = np.zeros((1,self.dim))
22+
self.pbest = np.zeros((self.pN,self.dim)) #个体经历的最佳位置
23+
self.gbest = np.zeros((1,self.dim)) #全局最佳位置
4024
self.p_fit = np.zeros(self.pN) #每个个体的历史最佳适应值
4125
self.fit = 1e10 #全局最佳适应值
42-
26+
self.filename = ['user.txt','bs.txt']
27+
self.UserX,self.UserY,self.BSX,self.BSY = readFile(*self.filename)
28+
self.BSCover = classifyUser(100,self.UserX,self.UserY,self.BSX,self.BSY)
29+
self.BSchanAllocate=[[-1 for i in xrange(self.channelnum)] for j in xrange(len(self.BSCover))]#初始化信道分配
4330
#---------------------目标函数Sphere函数-----------------------------
4431
def function(self,x): #x是列表
4532
'''
4633
首要解决的问题是 目标函数的的表达式
34+
由于粒子由是不同信道的功率等级(比例)表示的,所以
4735
'''
4836
sum = 0
4937
length = len(x)
@@ -54,40 +42,31 @@ def function(self,x): #x是列表
5442

5543
#---------------------初始化种群----------------------------------
5644
def init_Population(self):
57-
filename = ['user.txt','bs.txt']
58-
UserX,UserY,BSX,BSY = readFile(*filename)
59-
BSCover = classifyUser(100,UserX,UserY,BSX,BSY)
60-
BSchanAllocate=[[-1 for i in xrange(self.channelnum)] for j in xrange(len(BSCover))]#初始化信道分配
6145
####将宏基站的坐标加入到基站的坐标列表中
62-
BSX = BSX+[0.0]
63-
BSY = BSY+[0.0]
46+
self.BSX = self.BSX+[0.0]
47+
self.BSY = self.BSY+[0.0]
6448
for i in xrange(5):
65-
BSchanAllocate = channelAllocate(BSCover,BSchanAllocate,BSX,BSY)
49+
self.BSchanAllocate = channelAllocate(self.BSCover,self.BSchanAllocate,self.BSX,self.BSY)
6650

6751
for i in xrange(self.pN):
68-
self.X[i] = turnInToParticle(getPower(BSchanAllocate))#初始化粒子位置
52+
self.X[i] = turnInToParticle(getPower(self.BSchanAllocate))#初始化粒子位置
6953
for j in range(self.dim):
70-
self.V[i][j] = random.uniform(0,1)
71-
self.pbest[i] = self.X[i]
72-
tmp = self.function(self.X[i])#目标函数
73-
self.p_fit[i] = tmp
74-
if(tmp < self.fit):
75-
self.fit = tmp
54+
self.V[i][j] = random.uniform(0,1) #初始化粒子的速度
55+
self.pbest[i] = self.X[i] ##每个粒子的最佳位置
56+
tmp = self.function(self.X[i])#调用目标函数,计算当前粒子的适应值,目标函数是处理每一个粒子的,在这个表达式中显而易见
57+
self.p_fit[i] = tmp ##每个粒子最佳适应值
58+
if(tmp < self.fit): #判断小于全局最佳适应值,将当前粒子的最佳适应值赋值给全局最佳适应值
59+
self.fit = tmp #
7660
self.gbest = self.X[i]
77-
78-
# for i in range(self.pN):
79-
# for j in range(self.dim):
80-
# self.X[i][j] = random.uniform(0,1) #初始化种群的位置
81-
# self.V[i][j] = random.uniform(0,1) #初始化种群的速度
82-
# self.pbest[i] = self.X[i]
83-
# tmp = self.function(self.X[i])
84-
# self.p_fit[i] = tmp
85-
# if(tmp < self.fit):
86-
# self.fit = tmp
87-
# self.gbest = self.X[i]
88-
#
89-
90-
61+
#--------------------------粒子的惯性权重-----------------------------
62+
def inertia_Weight(self,t):
63+
"""保证惯性权重开始:0.9全局搜索能力强,最后为0.1:局部搜索能力强"""
64+
k = (0.9-0.1)/(1-self.max_iter)
65+
b = (0.9*self.max_iter-0.1)/(self.max_iter-1)
66+
y = k*t + b
67+
return y
68+
69+
9170
#----------------------更新粒子位置----------------------------------
9271
def iterator(self):
9372
fitness = []
@@ -97,16 +76,18 @@ def iterator(self):
9776
if(temp<self.p_fit[i]): #更新个体最优
9877
self.p_fit[i] = temp
9978
self.pbest[i] = self.X[i]
100-
if(self.p_fit[i] < self.fit): #更新全局最优
79+
if(self.p_fit[i] < self.fit):#更新全局最优
10180
self.gbest = self.X[i]
102-
self.fit = self.p_fit[i]
81+
self.fit = self.p_fit[i]
82+
self.w = self.inertia_Weight(t)##调用类内部的函数,改变w的值,收敛速度明显加快
10383
for i in range(self.pN):
10484
self.V[i] = self.w*self.V[i] + self.c1*self.r1*(self.pbest[i] - self.X[i])+ self.c2*self.r2*(self.gbest - self.X[i])
10585

10686
self.X[i] = self.X[i] + self.V[i]
10787
fitness.append(self.fit)
10888
print(self.fit) #输出最优值
10989
return fitness
90+
#----------------------------------粒子群类构造结束-------------------------------------------------
11091
if __name__=="__main__":
11192
#----------------------程序执行-----------------------
11293
max_iter = 200

0 commit comments

Comments
 (0)