- 记录一下数模比赛自己写出来的用于解题的遗传算法,我门组这次华为杯数模竞赛做的是F题,是有关航空公司机组人员航行规划的问题。
- 我们根据本题设计了的基因序列是2310位的二进制数。我们将该题转化为了0,1问题,基因的前2205位代表机组飞行选择的具体方案,而后105位用于表示fitness,后面105位所含的1的个数越多,则说明该种方案越好。
import numpy as np
import heapq
import csv
DNA_SIZE = 2310
POP_SIZE = 1000
CROSS_RATE = 0.8
MUTATION_RATE = 0.03
N_GENERATIONS = 2000
def ExtractDNA(pop):
take_off = []
for i in range(len(pop)):
x = []
for j in range(2205, DNA_SIZE):
x.append(pop[i][j])
take_off.append(x)
return take_off
def get_fitness(pred):
take_off_value = []
for i in range(len(Extract_values)):
sum = 0
for j in range(105):
sum += Extract_values[i][j]
take_off_value.append(sum)
return take_off_value
def delete(pop):
for i in range(1000):
flag = False
for k in range(0, 104):
sum = 0
for j in range(k, 1155, 105):
sum += pop[i][j]
if sum >= pop[i, 2205 + k]:
break
if sum < pop[i, 2205 + k]:
pop = np.delete(pop, i, axis=0)
break
for i in range(1000):
break_flag = False
for j in range(21):
sum = 0
if break_flag == True:
break
for k in range(105):
if break_flag == True:
break
for s in range(105):
m = j * 105 + s
sum += pop[i][m] * int(Conflict_matrix[s][k])
if sum > 1:
break_flag = True
pop = np.delete(pop, i, axis=0)
break
return pop
def select(fitness):
fitness = np.array(fitness)
idx = heapq.nlargest(1000, range(len(fitness)),
fitness.take)
return pop[idx]
def crossover(parent, pop_copy):
size = len(pop_copy)
if np.random.rand() < CROSS_RATE:
i_ = np.random.randint(0, size, size=1)
a = np.random.randint(0, 21, size=1)
cross_point1 = a[0] * 105
cross_point2 = cross_point1 + 105
for i in range(cross_point1, cross_point2):
parent[i] = pop_copy[i_, i]
return parent
def mutate(child):
for i in range(2205):
if np.random.rand() < 0.05:
child[i] = 1 if child[i] == 0 else 0
for i in range(2205, 2310):
if np.random.rand() < 0.01:
if np.random.rand() < 0.3:
child[i] = 0
else:
child[i] = 1
return child
file = "/Users/yzyzzzz/Documents/2021研究生数学建模/2021年中国研究生数学建模竞赛赛题/2021年F题/冲突矩阵.csv"
with open(file, 'r', encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile)
OriginalData = list(csv_reader)
Conflict_matrix = OriginalData.copy()
pop = np.random.randint(2, size=(POP_SIZE, DNA_SIZE))
for _ in range(N_GENERATIONS):
pop_copy = pop.copy()
children = []
for x in range(len(pop)):
parent = pop[x]
child = crossover(parent, pop_copy)
child = mutate(child)
pop = np.vstack((pop, child))
pop = delete(pop)
Extract_values = ExtractDNA(pop)
fitness = get_fitness(Extract_values)
pop = select(fitness)
print("当前种群的适应度分别是:", fitness)
print("循环代数是:", _ + 1001)
fitness = np.array(fitness)
best_fitness = heapq.nlargest(3, range(len(fitness)), fitness.take)
print("最高的fitness是:", fitness[best_fitness])
print("最好的DNA坐标是:", best_fitness)
best_DNA = pop[best_fitness]
with open('/Users/yzyzzzz/Documents/2021研究生数学建模/2021年中国研究生数学建模竞赛赛题/2021年F题/最好的机组分配情况(1.A)2.csv', 'a',
encoding='utf-8', newline='') as csvfile2:
writer = csv.writer(csvfile2)
writer.writerows(best_DNA)
- 程序截图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6e3d22e5096dbd0578d09c1b7973eb42.jpeg)