python遗传算法_【机器学习】遗传算法(Genetic Algorithm)的Python实现
时间: 2023-07-07 08:26:51 浏览: 170
好的,我了解您的问题了。以下是一个简单的Python实现遗传算法的示例代码:
```python
import random
# 定义目标函数
def fitness_function(x):
return x**2
# 定义种群数量
POPULATION_SIZE = 100
# 定义染色体长度
CHROMOSOME_LENGTH = 20
# 定义交叉概率
CROSSOVER_RATE = 0.8
# 定义变异概率
MUTATION_RATE = 0.1
# 定义种群
population = []
# 初始化种群
for i in range(POPULATION_SIZE):
chromosome = []
for j in range(CHROMOSOME_LENGTH):
chromosome.append(random.randint(0, 1))
population.append(chromosome)
# 迭代次数
GENERATIONS = 500
# 开始迭代
for generation in range(GENERATIONS):
# 计算适应度
fitness_values = []
for chromosome in population:
x = int(''.join(map(str, chromosome)), 2)
fitness_values.append(fitness_function(x))
# 找到最优解
best_fitness = max(fitness_values)
best_chromosome = population[fitness_values.index(best_fitness)]
# 输出结果
print("Generation:", generation, "Best fitness:", best_fitness, "Best chromosome:", best_chromosome)
# 选择
parents = []
for i in range(POPULATION_SIZE):
parent1 = population[random.randint(0, POPULATION_SIZE-1)]
parent2 = population[random.randint(0, POPULATION_SIZE-1)]
if fitness_function(int(''.join(map(str, parent1)), 2)) > fitness_function(int(''.join(map(str, parent2)), 2)):
parents.append(parent1)
else:
parents.append(parent2)
# 交叉
offspring = []
for i in range(0, POPULATION_SIZE, 2):
parent1 = parents[i]
parent2 = parents[i+1]
if random.random() < CROSSOVER_RATE:
crossover_point = random.randint(1, CHROMOSOME_LENGTH-1)
offspring1 = parent1[:crossover_point] + parent2[crossover_point:]
offspring2 = parent2[:crossover_point] + parent1[crossover_point:]
offspring.append(offspring1)
offspring.append(offspring2)
else:
offspring.append(parent1)
offspring.append(parent2)
# 变异
for i in range(POPULATION_SIZE):
chromosome = offspring[i]
for j in range(CHROMOSOME_LENGTH):
if random.random() < MUTATION_RATE:
chromosome[j] = 1 - chromosome[j]
# 更新种群
population = offspring
```
这个示例实现了一个简单的遗传算法来求解目标函数 $f(x)=x^2$ 的最大值,其中 $x$ 是一个20位的二进制数。在每个迭代中,选择、交叉和变异操作被执行以生成新的种群。最终找到最优解并输出结果。
请注意,这只是一个简单的示例,实际应用中需要根据具体问题进行适当的修改和调整。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20210720083447.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)