废话没有 直接上代码
import copy
import operator
import random
import math
class Genome:
def __init__(self):
self.fitness = 0
self.gene_sequence = []
# 随机生成祖先群落
def gen_ancestors(value_array):
ancestors = random.sample(value_array, 36)
return ancestors
# 基因交换
def gene_change(array1, array2, pos, len):
arr = array1[pos:len:1]
del array1[pos:len]
arr2 = array2[pos:len:1]
del array2[pos:len]
arr.reverse()
arr2.reverse()
for i in arr:
array2.insert(pos, i)
for i in arr2:
array1.insert(pos, i)
return array1, array2
# 基因突变
def gene_variation(array, pos):
if array[pos] == 1:
array[pos] = 0
else:
array[pos] = 1
return array
# 基因适合度值判断
def survival_of_the_fittest(simple_array, new_gene_array):
result_map = {}
genomes = []
for gene in new_gene_array:
sum_of_squared_differences = 0
index = 0
for sige_gene in gene:
sum_of_squared_differences += (sige_gene - simple_array[index]) * (sige_gene - simple_array[index])
index += 1
# print()
genome = Genome()
genome.fitness = format(math.sqrt(sum_of_squared_differences), '.4f')
genome.gene_sequence = gene
genomes.append(genome)
def get_fitness(genome):
return genome.fitness
genomes.sort(key=get_fitness)
# for genome in genomes:
# print(genome.fitness)
# print(genome.gene_sequence)
return copy.deepcopy(genomes[0:5])
# 数据展示
def reverse_flattening_display(raw_data, descendant_simulation):
print("\n=========================原始数据展示========================")
index = 0
for i in raw_data:
if index % 6 == 0:
print()
print(i, end='')
index += 1
pass
print("\n=========================模拟数据展示========================")
index = 0
for i in descendant_simulation:
if index % 6 == 0:
print()
print(i, end='')
index += 1
if __name__ == '__main__':
default_map = [[0, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0]]
value_array = []
for values in default_map:
for value in values:
value_array.append(value)
gen_ancestors_array = []
for i in range(5):
gen_ancestors_array.append(gen_ancestors(copy.deepcopy(value_array)))
generation = 0
while True:
generation += 1
first = random.randint(0, len(gen_ancestors_array) - 1)
sec = -1
while sec == first or sec == -1:
sec = random.randint(0, len(gen_ancestors_array) - 1)
arr1 = gen_ancestors_array[sec]
arr2 = gen_ancestors_array[first]
exanged_array = []
exange_pos = random.randint(0, len(arr1) - 1)
# print("基因交换位置: " + str(exange_pos))
variation_pos = random.randint(0, len(arr1) - 1)
# print("基因变异位置: " + str(variation_pos))
arr3, arr4 = gene_change(copy.deepcopy(arr1), copy.deepcopy(arr2), exange_pos
,
5)
exanged_array.append(arr1)
exanged_array.append(arr2)
exanged_array.append(arr3)
exanged_array.append(arr4)
input_arr = gen_ancestors_array[random.randint(0, len(gen_ancestors_array) - 1)]
arr_mutated = gene_variation(copy.deepcopy(input_arr),
variation_pos)
exanged_array.append(arr_mutated)
# print("原图数据 = ", value_array)
# print("==================================================")
# for i in gen_ancestors_array:
# print("祖先数据 = ", i)
# print("==================================================")
# for i in exanged_array:
# print("子孙数据 = ", i)
new_gene_array = []
new_gene_array += copy.deepcopy(gen_ancestors_array)
new_gene_array += copy.deepcopy(exanged_array)
# for i in new_gene_array:
# print("后代全部基因 = ", i)
superior_genes = survival_of_the_fittest(copy.deepcopy(value_array), copy.deepcopy(new_gene_array))
reverse_flattening_display(value_array, superior_genes[0].gene_sequence)
if operator.eq(value_array, superior_genes[0].gene_sequence):
print("循环结束,共经历 " + str(generation) + " 代")
break
gen_ancestors_array.clear()
superior_genes.reverse()
for genes in superior_genes:
gen_ancestors_array.append(genes.gene_sequence)
# time.sleep(2)
运行结果
=========================原始数据展示========================
011110
000100
011110
000100
011110
000000
=========================模拟数据展示========================
011110
000100
011110
000100
011110
000000循环结束,共经历 179 代