Skip to content

Commit 64e854c

Browse files
committed
5.23
5.23
1 parent 70f45bc commit 64e854c

File tree

5 files changed

+199
-94
lines changed

5 files changed

+199
-94
lines changed

BS.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
0 -289.999236834 -0.66530881188
2-
1 274.978162406 62.5380700055
3-
2 22.6439418514 -198.713995223
4-
3 210.03325169 -294.838995359
5-
4 158.537234913 161.065033903
6-
5 -90.7831431639 345.26427692
7-
6 -218.953286408 -262.723159183
8-
7 336.066756529 -131.833740584
9-
8 -156.013262528 103.09637198
10-
9 50.1396508788 278.522917207
1+
0 -150.861422948 240.6176034
2+
1 314.692239477 166.447572565
3+
2 146.303977361 -199.007904889
4+
3 -103.580324669 -160.4746595
5+
4 312.969240283 -237.695718591
6+
5 136.929309505 367.306907366
7+
6 -18.5919368682 156.902325934
8+
7 -245.822048567 -106.749802989
9+
8 -272.405400993 143.733425165
10+
9 151.08445599 61.1758707184

CAPalloction.py

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,73 @@ def getPower(chanlist):
371371
#print "ptotal = %s"%ptotal
372372
wp = uniform(0.001, (ptotal-s)*0.8) ##在0.001和剩余功率之间随机产生一个功率值
373373
p[i][j] = wp*(1.0/channelnum)##不至于随机数极差太大
374-
return p
374+
return p
375+
376+
#----------------------PSO参数设置---------------------------------
377+
class PSO():
378+
379+
def __init__(self,pN,dim,max_iter):
380+
'''粒子群初始化信息'''
381+
self.w = 0.8
382+
self.c1 = 2
383+
self.c2 = 2
384+
self.r1= 0.6
385+
self.r2=0.3
386+
self.pN = pN #粒子数量
387+
self.dim = dim #搜索维度
388+
self.max_iter = max_iter #迭代次数
389+
self.X = np.zeros((self.pN,self.dim)) #所有粒子的位置和速度
390+
self.V = np.zeros((self.pN,self.dim))
391+
self.pbest = np.zeros((self.pN,self.dim)) #个体经历的最佳位置和全局最佳位置
392+
self.gbest = np.zeros((1,self.dim))
393+
self.p_fit = np.zeros(self.pN) #每个个体的历史最佳适应值
394+
self.fit = 1e10 #全局最佳适应值
395+
396+
#---------------------目标函数Sphere函数-----------------------------
397+
def function(self,x): #x是列表
398+
'''
399+
首要解决的问题是 目标函数的的表达式
400+
'''
401+
# sum = 0
402+
# length = len(x)
403+
# x = x**2
404+
# for i in range(length):
405+
# sum += x[i]
406+
# return sum
407+
pass
408+
#---------------------初始化种群----------------------------------
409+
def init_Population(self):
410+
for i in range(self.pN):
411+
for j in range(self.dim):
412+
self.X[i][j] = random.uniform(0,1) #初始化种群的位置
413+
self.V[i][j] = random.uniform(0,1) #初始化种群的速度
414+
self.pbest[i] = self.X[i]
415+
tmp = self.function(self.X[i])
416+
self.p_fit[i] = tmp
417+
if(tmp < self.fit):
418+
self.fit = tmp
419+
self.gbest = self.X[i]
420+
421+
#----------------------更新粒子位置----------------------------------
422+
def iterator(self):
423+
fitness = []
424+
for t in range(self.max_iter):
425+
for i in range(self.pN): #更新gbest\pbest
426+
temp = self.function(self.X[i])
427+
if(temp<self.p_fit[i]): #更新个体最优
428+
self.p_fit[i] = temp
429+
self.pbest[i] = self.X[i]
430+
if(self.p_fit[i] < self.fit): #更新全局最优
431+
self.gbest = self.X[i]
432+
self.fit = self.p_fit[i]
433+
for i in range(self.pN):
434+
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])
435+
436+
self.X[i] = self.X[i] + self.V[i]
437+
fitness.append(self.fit)
438+
print(self.fit) #输出最优值
439+
return fitness
440+
375441

376442
#------------------------------主 函 数 ---------------------------------------
377443
if __name__=="__main__":
@@ -382,27 +448,37 @@ def getPower(chanlist):
382448
####将宏基站的坐标加入到基站的坐标列表中
383449
BSX = BSX+[0.0]
384450
BSY = BSY+[0.0]
385-
# ##将用户按照基站的覆盖范围分类之后,将宏基站的坐标加入到基站坐标列表中去
386-
# # s = 0
387-
# # for i in BSCover:
388-
# # print i
389-
# # s += len(i)
390-
# # print "len(i)=%d"%len(i)
391-
# # print "sum user:%d"%s
392-
#
451+
#---------------------------得到An_k_s----------------------------
393452
An_k_s=[[0 for i in xrange(channelnum)] for j in xrange(TotalNum)]
394453
BSchanAllocate = channelAllocate(BSCover,BSX,BSY)
395454
for i in xrange(len(BSchanAllocate)):
396455
print BSchanAllocate[i]
397456
for j in xrange(len(BSchanAllocate[i])):
398457
if BSchanAllocate[i][j]!=-1:
399458
An_k_s[i][j]=1
400-
# print "\n"
401-
# for i in xrange(len(An_k_s)):
402-
# print An_k_s[i]
403-
459+
404460
##既然信道分配已经确定了,那么平均功率所组成的一个粒子可以算作一个初始化粒子,然后针对这些已经分配信道的的用户的信道功率多做几次(20次)功率随机分配,就会产生许多不同的初始化
405461
print "\n"
462+
temp = []
406463
p = getPower(BSchanAllocate)
407464
for i in p:
408-
print i
465+
temp= i + temp
466+
print "len(temp)="+str(len(temp))
467+
468+
#----------------------粒子群程序执行-----------------------
469+
my_pso = PSO(pN=30,dim=5,max_iter=100)
470+
my_pso.init_Population()
471+
fitness = my_pso.iterator()
472+
473+
#----------------------画 图--------------------------
474+
plt.figure(1)
475+
plt.title("Figure1")
476+
plt.xlabel("iterators", size=14)
477+
plt.ylabel("fitness", size=14)
478+
t = np.array([t for t in range(0,100)])
479+
fitness = np.array(fitness)
480+
plt.plot(t,fitness, color='b',linewidth=3)
481+
plt.show()
482+
483+
484+

pso.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ def __init__(self,pN,dim,max_iter):
4040

4141
#---------------------目标函数Sphere函数-----------------------------
4242
def function(self,x): #x是列表
43-
# sum = 0
44-
# length = len(x)
45-
# x = x**2
46-
# for i in range(length):
47-
# sum += x[i]
48-
# return sum
49-
pass
43+
'''
44+
首要解决的问题是 目标函数的的表达式
45+
'''
46+
sum = 0
47+
length = len(x)
48+
x = x**2
49+
for i in range(length):
50+
sum += x[i]
51+
return sum
52+
# pass
5053
#---------------------初始化种群----------------------------------
5154
def init_Population(self):
5255
for i in range(self.pN):
@@ -80,8 +83,9 @@ def iterator(self):
8083
print(self.fit) #输出最优值
8184
return fitness
8285

83-
#----------------------程序执行-----------------------
84-
my_pso = PSO(pN=30,dim=5,max_iter=100)
86+
#----------------------程序执行-----------------------
87+
max_iter = 200
88+
my_pso = PSO(pN=30,dim=5,max_iter=max_iter)
8589
my_pso.init_Population()
8690
fitness = my_pso.iterator()
8791

@@ -90,7 +94,7 @@ def iterator(self):
9094
plt.title("Figure1")
9195
plt.xlabel("iterators", size=14)
9296
plt.ylabel("fitness", size=14)
93-
t = np.array([t for t in range(0,100)])
97+
t = np.array([t for t in range(0,max_iter)]) ##200 为max_iter
9498
fitness = np.array(fitness)
9599
plt.plot(t,fitness, color='b',linewidth=3)
96100
plt.show()

test.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,40 @@
1111

1212

1313

14-
14+
# -*- coding:utf-8 -*-
15+
# class TreeNode:
16+
# def __init__(self, x):
17+
# self.val = x
18+
# self.left = None
19+
# self.right = None
20+
class Solution:
21+
# 返回构造的TreeNode根节点
22+
23+
#def reConstructBinaryTree(self, pre, tin):
24+
# write code here
25+
#if len(pre)==0:return None
26+
#elif len(pre)==1;return TreeNode(pre[0])
27+
#else:
28+
# gen = TreeNode(pre[0])
29+
#t = tin.index(pre[0])#t指向根的下标
30+
#gen.left = reConstructBinaryTree(self,pre[1:1+len(tin[:tin.index(pre[0])])],tin[:tin.index(pre[0])])
31+
#gen.right = reConstructBinaryTree(self,pre[1+len(tin[:tin.index(pre[0])]):],tin[tin.index(pre[0])+1:])
32+
33+
# gen.left = self.reConstructBinaryTree(pre[1:tin.index(pre[0])+1],tin[:tin.index(pre[0])])            
34+
# gen.right = self.reConstructBinaryTree(pre[tin.index(pre[0])+1:],tin[tin.index(pre[0])+1:] )
35+
#return gen
36+
37+
def reConstructBinaryTree(self, pre, tin):
38+
# write code here
39+
if len(pre) == 0:
40+
return None
41+
if len(pre) == 1:
42+
return TreeNode(pre[0])
43+
else:
44+
flag = TreeNode(pre[0])
45+
flag.left = self.reConstructBinaryTree(pre[1:tin.index(pre[0])+1],tin[:tin.index(pre[0])])
46+
flag.right = self.reConstructBinaryTree(pre[tin.index(pre[0])+1:],tin[tin.index(pre[0])+1:] )
47+
return flag
1548

1649

1750

user.txt

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,51 @@
1-
0 382.131891814 -20.9907639377
2-
1 -91.3915011608 456.596380669
3-
2 -295.134344266 -124.760426412
4-
3 -196.507830244 -337.888150177
5-
4 192.267679695 443.409889939
6-
5 119.821233417 205.50761286
7-
6 -396.227816568 0.777736038077
8-
7 -167.682863566 -275.16288796
9-
8 8.43475828818 100.880481238
10-
9 -280.901111151 338.039306825
11-
10 -286.676018918 -97.4459000945
12-
11 -456.781902988 155.725011959
13-
12 189.808302794 350.651749736
14-
13 108.392007234 -409.666450505
15-
14 295.938973747 10.2240042379
16-
15 485.449708379 33.2069999836
17-
16 121.022689706 -112.104345189
18-
17 -12.1859243169 481.228276862
19-
18 370.013346625 -108.618328252
20-
19 -480.469866972 27.2305124708
21-
20 -360.313560932 -308.219736502
22-
21 1.48892320914 197.358800285
23-
22 -114.948488279 211.388965246
24-
23 166.742817441 -207.904827547
25-
24 -18.7070683205 389.49290082
26-
25 -150.859744937 448.676540347
27-
26 186.044717992 -160.495687563
28-
27 -86.7370107531 69.5001530593
29-
28 -90.8893292597 -449.559987977
30-
29 -149.691969121 235.191608789
31-
30 145.654862446 343.672828069
32-
31 -441.289378666 165.728591157
33-
32 184.824593266 13.8693654015
34-
33 -36.4479728004 -426.053556114
35-
34 186.591642689 -373.296077721
36-
35 372.035782304 48.3711334956
37-
36 9.84776034378 -253.714322052
38-
37 -130.200687758 210.81709566
39-
38 28.2124313482 -222.062466407
40-
39 -175.582490489 109.66454596
41-
40 -327.57789298 -169.548149213
42-
41 487.951785393 3.3765834399
43-
42 30.4273134897 -395.828022006
44-
43 -307.332510759 -309.886947149
45-
44 9.67936876124 -477.764773133
46-
45 -207.663939068 -212.098583864
47-
46 -413.988263452 195.900348362
48-
47 -372.171172529 323.260253162
49-
48 275.166983831 103.457148573
50-
49 311.545021899 222.860819266
51-
50 104.755593171 -157.872007362
52-
51 192.739224632 288.887439074
53-
52 -42.0184126249 439.955118153
54-
53 -409.87653607 136.485326674
55-
54 414.333375891 66.6759758219
56-
55 344.099303145 -334.239702796
57-
56 -134.560760113 294.18208515
58-
57 136.89489922 409.322643741
59-
58 -245.677559914 -179.227943217
1+
0 45.6490968419 365.866058592
2+
1 -295.246400343 -236.240435468
3+
2 -170.91623197 -313.992044093
4+
3 -258.729890439 293.923698277
5+
4 -229.989647858 371.643477523
6+
5 -19.3414750977 153.369815721
7+
6 167.503966972 4.26647112288
8+
7 235.238916058 -48.4712031171
9+
8 221.117248212 399.076455409
10+
9 77.9471170521 -174.723611153
11+
10 335.535726653 -317.116806757
12+
11 -425.597764492 149.229665353
13+
12 47.1087261636 -20.9242190714
14+
13 84.8994736777 -288.919595941
15+
14 -460.573172755 172.736331264
16+
15 427.915855348 -39.4786801799
17+
16 -43.1461707641 -163.765884764
18+
17 -171.288498705 -326.806184987
19+
18 332.587587466 257.920274156
20+
19 -96.2487007271 -143.578189984
21+
20 468.991206729 152.227297699
22+
21 -27.6432989784 6.4394198997
23+
22 -285.694408273 -119.563930455
24+
23 237.323510634 -436.609745035
25+
24 4.2334209012 -460.752470972
26+
25 118.493632967 153.588644227
27+
26 -91.0742349714 174.063814293
28+
27 -205.389844498 135.111733396
29+
28 -200.833621493 -124.244069577
30+
29 430.388914804 137.137807075
31+
30 -49.7622931872 211.824755823
32+
31 372.86458503 329.143703721
33+
32 20.317430866 -203.473214562
34+
33 -90.4482511628 189.772136777
35+
34 -11.189518381 232.399788531
36+
35 211.212357651 -409.549839279
37+
36 211.060837856 -121.349180735
38+
37 94.3992267887 -165.918556111
39+
38 270.167362194 -250.652012676
40+
39 160.218431251 411.19340761
41+
40 -29.3809257738 -411.433366
42+
41 304.063328284 112.33567549
43+
42 77.9929650653 219.279013253
44+
43 55.2310539679 171.688001401
45+
44 473.868969531 144.817128693
46+
45 -147.524279121 -15.6990137524
47+
46 -356.574630315 198.700819158
48+
47 -74.0760897155 -325.980911019
49+
48 205.884303151 -166.332543122
50+
49 63.029521645 -320.326115084
51+
50 -147.187200581 230.979815776

0 commit comments

Comments
 (0)