def CreatCase2(path, PicoNum, UserNum, Num): ''' 新的创建样例方法,根据同样的用户分配在边缘分布3-6个基站,不用集中分布了——————————————17.11.05 批量创建测试用例, 路径,pico基站数,用户数,创建个数 如此创建的结果,用户分布按照密集型分布,基站分布分为两种,边缘或处于用户密集处 所以两种基站分布方式场景用户的位置是一样的,保证计算结果在同一场景下进行 ''' for i in range(Num): tun1 = LayPicoBS(MacroRange, PicoRange, PicoNum, 2) # 没什么用,为了给tun3填参数 tun3 = LayUsers(MacroRange, PicoRange, UserNum, 0, tun1, 0.5) #均匀分布用户位置 tun3 = LayUsersReal(MacroRange, PicoRange, UserNum, 0, tun1, 0.5) for j in range(7): #循环4次,分别创建基站数为3,4,5,6个的边缘分布样例 # PicoNumNow = PicoNum + j #当前的pico基站数量 PicoNumNow = j + 1 PathR = CreatTxt(path, PicoNumNow, UserNum, 'R', i) #创建边缘分布用例名称 print(PathR) tun2 = LayPicoBS(MacroRange, PicoRange, PicoNumNow, 0) #边缘分布基站位置 DataR = [tun2[0], tun2[1], tun3[0], tun3[1]] #边缘分布数据 RnWLists.writeTxt(PathR, DataR, 1) #写入随机分布位置
def CreatCase(path, PicoNum, UserNum, Num): ''' 批量创建测试用例, 路径,pico基站数,用户数,创建个数 如此创建的结果,用户分布按照密集型分布,基站分布分为两种,边缘或处于用户密集处 所以两种基站分布方式场景用户的位置是一样的,保证计算结果在同一场景下进行 ''' for i in range(Num): PathR = CreatTxt(path, PicoNum, UserNum, 'R', i) #边缘分布用例 PathN = CreatTxt(path, PicoNum, UserNum, 'N', i) #集中分布用例 print(PathR) tun1 = LayPicoBS(MacroRange, PicoRange, PicoNum, 2) #集中分布基站位置 tun2 = LayPicoBS(MacroRange, PicoRange, PicoNum, 0) #边缘分布挤占位置 tun3 = LayUsers(MacroRange, PicoRange, UserNum, 1, tun1, 0.5) #集中分布用户位置 DataR = [tun2[0], tun2[1], tun3[0], tun3[1]] #边缘分布数据 DataN = [tun1[0], tun1[1], tun3[0], tun3[1]] #集中分布数据 RnWLists.writeTxt(PathR, DataR, 1) RnWLists.writeTxt(PathN, DataN, 1)
def ga_convergence(m, g, CasePath, ConvergenceSavePath): data = RnWLists.readTxt(CasePath, 1) DM = GetDM(data[0]) picoNumber = len(DM) - 1 userNumber = len(DM[0]) begin = time.clock() BSList = solve.SetBS(macroNumber, picoNumber, infofmbs, infofpbs, DM) print("执行遗传算法初始化") Result = solve.ga_convergence(m=m, BSList=BSList, g=g, userNumber=userNumber) RnWLists.writeTxt(ConvergenceSavePath, Result, 1) print("执行遗传算法完成") end = time.clock() use = int(end - begin) minutes = int(use / 60) secends = use % 60 print("程序执行用时%d分%d秒" % (minutes, secends))
def MakeParamas(m, g, CaseRootPath, N, flag=1, CutNumber=60, PicoNum=3, UserNum=70, Times=8): begin = time.clock() ParamasList = [] #参数列表 Class = 'R' CasePath = create_txt(CaseRootPath, PicoNum, UserNum, Class, N) for i in range(Times): data = RnWLists.readTxt(CasePath, 1) #读取数据 if flag == 0: data[0][0] = [] #这两个是用来清空pico坐标,形成对照组的 data[0][1] = [] CaseClass = 'O' Stes = cutData(data[0], CutNumber) #切取部分用户数 DM = GetDM(Stes) #计算DM矩阵 BSList = solve.SetBS(1, PicoNum, infofmbs, infofpbs, DM) Parama = [m, BSList, g, CutNumber] ParamasList.append(Parama) #加入参数列表 end = time.clock() use = int(end - begin) minutes = int(use / 60) secends = use % 60 # print("构造参数列表用时%d分%d秒" %(minutes, secends)) return ParamasList
def ga_pso(m, BSList, g, userNumber, LowestRate=1.4, maxchannal=3, Qmax=1, Alpha=10, B=20, ConvergenceFlag=0): ''' 单目标优化遗传算法,采用Zoutendijk优化 ''' if ConvergenceFlag == 1: res = [] # 记录收敛性 Population = [] for i in range(m): # 产生m个个体形成种群 indi = Individual(BSList, userNumber, maxchannal, Qmax, Alpha, B) # def __init__(self, BS, userNum, maxc, Qmax, Alpha, B): indi.CalculateTotalRate # 计算一下速率,为gener赋值 indi.Revise() indi.CalculateAll() indi.LowestRate = LowestRate * userNumber Population.append(indi) GeneLength = len(Population[0].genec[0]) best = copy.deepcopy(Population[0]) best.power = 26 if ConvergenceFlag == 1: res.append(best.rp) print("开始执行遗传算法+PSO") for i in range(g): # print("开始第%d次迭代" % (i)) NewPopulation = [] for j in range(int(len(Population) / 2)): k = random.randint(0, len(Population) - 1) X = Population.pop(k) k = random.randint(0, len(Population) - 1) Y = Population.pop(k) # 随机选择XY交叉 n = random.randint(1, int(GeneLength / 10)) # 交叉次数为基因长度除以10,取整 Children = cross_two_point(X, Y, n) # 其中包括父代两个个体和子代两个个体一共四个[x,y,xc,yc] # Children[0].Mutation(6) Children[0].MutationSingleTarget(6) Children[0].Revise() # Children[1].Mutation(6) #对父代变异 Children[1].MutationSingleTarget(6) Children[1].Revise() NewPopulation += Children if len(NewPopulation) < m: print("错误,当前NewPopulation大小为%d,目标种群大小为%d" % (len(NewPopulation), m)) for j in range(len(NewPopulation)): NewPopulation[j].Revise() # 修复个体 NewPopulation[j].CalculateAll() Population = choose(NewPopulation, m) NewPopulation = [] for j in range(int(len(Population) / 2)): k = random.randint(0, len(Population) - 1) X = Population.pop(k) k = random.randint(0, len(Population) - 1) Y = Population.pop(k) # 随机选择XY交叉 n = random.randint(1, int(GeneLength / 10)) # 交叉次数为基因长度除以10,取整 Children = Crossover2SingleTarget( X, Y, n, TargetFlag=1) # 其中包括父代两个个体和子代两个个体一共四个[x,y,xc,yc] # Children[0].Mutation(6) Children[0].MutationSingleTarget(6, TargetFlag=1) Children[0].Revise() # Children[1].Mutation(6) #对父代变异 Children[1].MutationSingleTarget(6, TargetFlag=1) Children[1].Revise() NewPopulation += Children for j in range(len(NewPopulation)): NewPopulation[j].Revise() # 修复个体 NewPopulation[j].CalculateAll() Population = choose(NewPopulation, m) # PopulationZoutendijk(Population) for j in range(len(Population)): if Population[j].rate > Population[j].LowestRate and Population[ j].power < best.power: best = copy.deepcopy(Population[j]) if ConvergenceFlag == 1 and i % 5 == 0: res.append(best.rp) indiSavePath = './data/indi/result_ga_pso.txt' print("开始写入个体") RnWLists.writeTxt(indiSavePath, best.genec, flag=0) RnWLists.writeTxt(indiSavePath, best.genep, flag=1) if ConvergenceFlag == 1: return res else: return best
def run_ga(m, g, CaseRootPath, ResultRootPath, flag=1, ProcessNumber=8, begin=20, end=60, PicoNumber=3, CaseNumber=0, times=8): TotalTimerBegin = time.clock() if end < begin: print('错误,end不能小于begin') exit(0) if flag == 0: Clas = 'O' elif flag == 1: Clas = 'R' elif flag == 2: Clas = 'Z' else: Clas = 'N' pool = multiprocessing.Pool(ProcessNumber) for i in range(begin, end + 1, 10): #按照用户数量循环计算,以及间隔数量 TimerBegin = time.clock() print("本次循环中有%d个用户" % (i)) Paramas = MakeParamas(m, g, CaseRootPath, CaseNumber, flag=flag, CutNumber=i, PicoNum=PicoNumber, Times=times) CasePath = create_txt(CaseRootPath, PicoNum=PicoNumber, UserNum=i, Class=Clas, N=CaseNumber) Result = pool.map(solve_ga, Paramas) #并行计算 print("result 长度为") print(len(Result)) SavePath = create_txt(ResultRootPath, PicoNumber, i, Clas, CaseNumber) #生成保存位置 RnWLists.writeTxt(SavePath, Result, 1) #循环存入数据 print("写入数据中,%d次重复数据" % (times)) print("当前样例为, %s" % (CasePath)) print("写入位置为, %s" % (SavePath)) TimerEnd = time.clock() use = int(TimerEnd - TimerBegin) minutes = int(use / 60) secends = use % 60 print("%d个用户,用时%d分%d秒" % (i, minutes, secends)) #SavePath = create_txt(ResultRootPath, 'save', 3, 60, Clas, CaseNumber) #RnWLists.writeTxt(SavePath, result, 1) #将结果写入 TotalTimerEnd = time.clock() use = int(TotalTimerEnd - TotalTimerBegin) minutes = int(use / 60) secends = use % 60 print("当前计算结束,用时%d分%d秒,结果写入%s" % (minutes, secends, SavePath))