# 遗传算法（2）

1、基因编码

(000000100100100)2=292，对应0.179，(111000100110110)2=-12598，对应-7.690。

2、设计初始群体

1

000000100101001

101010101010101

2

011000100101100

001100110011001

3

001000100100101

101010101010101

4

000110100100100

110011001100110

5

100000100100101

101010101010101

6

101000100100100

111100001111000

7

101010100110100

101010101010101

8

100110101101000

000011110000111

3、适应度函数

4、产生下一代

1

000000100101001

101010101010101

1X+2X

XA: 000000100101100

YA: 101010110011001

2

011000100101100

001100110011001

1Y+2Y

XB: 011000100101001

YB: 001100101010101

1染色体的X基因的前7位和2染色体的X基因的后8位将结合；

1染色体的Y基因的前7位和2染色体的Y基因的后8位将结合；

2染色体的X基因的前7位和1染色体的X基因的后8位将结合；

2染色体的Y基因的前7位和1染色体的Y基因的后8位将结合。

1、断开点的位置

2、基因变异的位置

5、迭代计算

#coding=utf-8

import random

import math

import numpy as np

#染色体长度

CHROMOSOME_SIZE = 15

#判断退出

```def is_finished(last_three):
s =sorted(last_three)
if s[0]and s[2] - s[0] < 0.01 * s[0]:
return True
else:
return False```

#初始染色体样态

```def init():
chromosome_state1 = ['000000100101001', '101010101010101']
chromosome_state2 = ['011000100101100', '001100110011001']
chromosome_state3 = ['001000100100101', '101010101010101']
chromosome_state4 = ['000110100100100', '110011001100110']
chromosome_state5 = ['100000100100101', '101010101010101']
chromosome_state6 = ['101000100100100', '111100001111000']
chromosome_state7= ['101010100110100', '101010101010101']
chromosome_state8 = ['100110101101000', '000011110000111']
chromosome_states = [chromosome_state1,
chromosome_state2,
chromosome_state3,
chromosome_state4,
chromosome_state5,
chromosome_state6,
chromosome_state7,
chromosome_state8]
returnchromosome_states```

#计算适应度

```def fitness(chromosome_states):
fitnesses= []
forchromosome_state in chromosome_states:
ifchromosome_state[0][0] == '1':
x= 10 * (-float(int(chromosome_state[0][1:], 2) - 1)/16384)
else:
x= 10 * (float(int(chromosome_state[0], 2) + 1)/16384)
ifchromosome_state[1][0] == '1':
y= 10 * (-float(int(chromosome_state[1][1:], 2) - 1)/16384)
else:
y= 10 * (float(int(chromosome_state[1], 2) + 1)/16384)
z = y* math.sin(x) + x * math.cos(y)
fitnesses.append(z)
returnfitnesses```

#筛选

```def filter(chromosome_states, fitnesses):
#top 8 对应的索引值
chromosome_states_new = []
top1_fitness_index = 0
for i innp.argsort(fitnesses)[::-1][:8].tolist():
chromosome_states_new.append(chromosome_states[i])
top1_fitness_index = i
returnchromosome_states_new, top1_fitness_index```

#产生下一代

```def crossover(chromosome_states):
chromosome_states_new = []
whilechromosome_states:
chromosome_state = chromosome_states.pop(0)
for vin chromosome_states:
pos = random.choice(range(8, CHROMOSOME_SIZE - 1))
chromosome_states_new.append([chromosome_state[0][:pos] + v[0][pos:],chromosome_state[1][:pos] + v[1][pos:]])
chromosome_states_new.append([v[0][:pos] + chromosome_state[1][pos:],v[0][:pos] + chromosome_state[1][pos:]])
returnchromosome_states_new```

#基因突变

```def mutation(chromosome_states):
n =int(5.0 / 100 * len(chromosome_states))
while n> 0:
n -=1
chromosome_state = random.choice(chromosome_states)
index= chromosome_states.index(chromosome_state)
pos =random.choice(range(len(chromosome_state)))
x =chromosome_state[0][:pos] + str(int(not int(chromosome_state[0][pos]))) +chromosome_state[0][pos+1:]
y =chromosome_state[1][:pos] + str(int(not int(chromosome_state[1][pos]))) +chromosome_state[1][pos+1:]
chromosome_states[index] = [x, y]
if __name__ == '__main__':
chromosome_states = init()
last_three= [0] * 3
last_num= 0
n = 100
while n> 0:
n -=1
chromosome_states = crossover(chromosome_states)
mutation(chromosome_states)
fitnesses = fitness(chromosome_states)
chromosome_states, top1_fitness_index = filter(chromosome_states,fitnesses)
last_three[last_num] = fitnesses[top1_fitness_index]
ifis_finished(last_three):
break
iflast_num >= 2:
last_num = 0
else:
last_num += 1```

#x: 7.69897460938 y:7.69897460938 z:8.79528923825

#x: -8.35693359375 y:-8.35693359375 z:11.3501994249

231 篇文章47 人订阅

0 条评论

## 相关文章

### AI 技术讲座精选：数学不好，也可以学习人工智能（六）——巧用数学符号

【AI100 导读】欢迎阅读《数学不好，也可以学好人工智能》系列的第六篇文章。如果你错过了之前的五部分，一定记得把它们找出来看一下！这篇文章作者会帮你学习数学符...

3728

3887

47110

3205

1715

1411

### 初学数据挖掘——相似性度量(二)

上一篇中介绍了四个算法，并用四个算法分别计算了两个人的相似度。这篇就来讲讲相似性算法在实际当中怎么用。第一：将指定的人与其他人作相似性比较，并从高到低进行排...

2316

5921

2072

2866