赞
踩
- import random
- import numpy as np
- import matplotlib.pyplot as plt
-
- '''个人认为参数设置非常的合理,但是效果却是最不好的'''
- def init_population(n):
- '''生成一个种群'''
- population = []
- for i in range(100):
- cs = [i for i in range(1,n+1)]
- random.shuffle(cs)
- population.append(cs)
- return population
-
- def init_v(n):
- '''生成一个初始速度的列表,对应一个种群'''
- v = []
- for i in range(100):
- in1 = []
- for j in range(n): #n维
- x = random.random()
- in1.append(x)
- v.append(in1)
- return v
-
- def Map(lis):
- '''这是一个映射函数,将一个列表变成全排列'''
- lis_dup = lis[:]
- lis.sort()
- #使用两个列表,对其合理的进行排序
- location = []
- for i in lis_dup:
- index = lis.index(i) + 1
- location.append(index)
- return location
-
- def ff(population,n,v1,v2):
- '''传入一个种群,返回不同个体对应函数值的列表'''
- y_s = []
- for i in population:
- location = Map(i)
- cost_sum = 0
- for j in range(n):
- for k in range(n):
- loca1 = location.index(j+1)
- loca2 = location.index(k+1)
- cost = v2[j][k]*v1[loca1][loca2]
- cost_sum = cost_sum + cost
- y_s.append(cost_sum)
- index = y_s.index(min(y_s))
- best = population[index] #best为种群中表现最好的个体
- return y_s,best
-
-
-
- def ff_solo(i,n,v1,v2):
- '''传入一个个体,得到这个个体的函数值'''
- location = Map(i)
- cost_sum = 0
- for j in range(n):
- for k in range(n):
- loca1 = location.index(j+1)
- loca2 = location.index(k+1)
- cost = v2[j][k]*v1[loca1][loca2]
- cost_sum = cost_sum + cost
- return cost_sum
-
- def trans_v(v,population,p,g):
- '''速度改变函数'''
- vs = []
- for i in range(len(v)):
- r1 = random.random()
- r2 = random.random()
- j = 10*np.array(v[i]) + 5*r1*(np.array(p[i]) - np.array(population[i])) + 5*r2*(np.array(g) - np.array(population[i]))
- j = list(j)
- vs.append(j)
- return vs
-
- def trans_popu(population,v,n,v1,v2):
- '''种群改变函数'''
- population_new = np.array(population) + np.array(v)
-
- for i in range(len(population)):
- if ff_solo(list(population_new[i]), n, v1, v2) < ff_solo(population[i], n, v1, v2):
- population[i] = list(population_new[i])
- return population
-
-
- def trans_p(population,p,n,v1,v2):
- '''p也为一个种群,记录粒子到访的最好位置'''
- for i in range(len(population)):
- if ff_solo(population[i], n, v1, v2) < ff_solo(p[i], n, v1, v2):
- p[i] = population[i]
- return p
- def read():
- with open('D:/学习文件/大三上/科研课堂/qap-problems/QAP12.dat','r',encoding='utf-8') as f:
- comments = f.read().splitlines()
- n = eval(comments[0])
- v11 = comments[2:2+n]
- v22 = comments[3+n:3+n+n]
- v1 = []
- v2 = []
- for i in v11:
- int_list = list(map(int, i.split()))
- v1.append(int_list)
- for i in v22:
- int_list = list(map(int, i.split()))
- v2.append(int_list)
- return v1,v2,n
-
- def main():
- v1,v2,n = read()
- population = init_population(n)
- v = init_v(n)
-
- y_s_before,g = ff(population,n,v1,v2) #一开始就定义一个全局最优位置
-
- p = population
- ans = []
- for i in range(1200):
- y_s,best = ff(population,n,v1,v2)
- #下面这个判断是对全局最优进行判断
- if ff_solo(best, n, v1, v2) < ff_solo(g, n, v1, v2):
- g = best
-
- #下面更新个体最优
- trans_p(population,p,n,v1,v2)
-
-
- print(min(y_s))
- ans.append(min(y_s))
-
-
- #print(len(v),len(population),len(p))
- v = trans_v(v, population, p, best)
- population = trans_popu(population,v,n,v1,v2)
-
- plt.plot(ans)
-
- main()
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。