基础遗传算法代码
时间: 2023-07-07 12:09:29 浏览: 125
以下是一个基础的遗传算法代码示例,用于解决优化问题:
```python
import random
import numpy as np
# 定义适应度函数,这里以简单的函数 f(x)=x^2 为例
def fitness_func(x):
return x ** 2
# 定义初始化种群函数
def init_population(pop_size, chrom_length):
population = np.zeros((pop_size, chrom_length), dtype=int)
for i in range(pop_size):
for j in range(chrom_length):
population[i][j] = random.randint(0, 1)
return population
# 定义选择操作函数
def selection(population, fitness_values):
select_prob = fitness_values / np.sum(fitness_values)
cum_prob = np.cumsum(select_prob)
new_population = np.zeros_like(population)
for i in range(len(population)):
r = random.random()
for j in range(len(cum_prob)):
if r <= cum_prob[j]:
new_population[i] = population[j]
break
return new_population
# 定义交叉操作函数
def crossover(parents, pc):
offspring = np.zeros_like(parents)
for i in range(0, len(parents), 2):
if i+1 < len(parents):
if random.random() < pc:
crossover_point = random.randint(1, len(parents[i])-1)
offspring[i][:crossover_point] = parents[i][:crossover_point]
offspring[i][crossover_point:] = parents[i+1][crossover_point:]
offspring[i+1][:crossover_point] = parents[i+1][:crossover_point]
offspring[i+1][crossover_point:] = parents[i][crossover_point:]
else:
offspring[i] = parents[i]
offspring[i+1] = parents[i+1]
else:
offspring[i] = parents[i]
return offspring
# 定义变异操作函数
def mutation(offspring, pm):
for i in range(len(offspring)):
for j in range(len(offspring[i])):
if random.random() < pm:
offspring[i][j] = 1 if offspring[i][j] == 0 else 0
return offspring
# 定义主函数
def main():
# 定义参数
pop_size = 10
chrom_length = 8
max_gen = 100
pc = 0.7
pm = 0.001
# 初始化种群
population = init_population(pop_size, chrom_length)
# 开始迭代
for gen in range(max_gen):
# 计算适应度值
fitness_values = np.array([fitness_func(chrom) for chrom in population])
# 选择操作
new_population = selection(population, fitness_values)
# 交叉操作
new_population = crossover(new_population, pc)
# 变异操作
new_population = mutation(new_population, pm)
# 更新种群
population = new_population
# 找到最优解
best_fitness = -1
for chrom in population:
fitness = fitness_func(chrom)
if fitness > best_fitness:
best_fitness = fitness
best_chrom = chrom
print('最优解为:', best_chrom, ',对应的适应度值为:', best_fitness)
if __name__ == '__main__':
main()
```
注意,这里的适应度函数是一个简单的二次函数 $f(x) = x^2$,在实际中需要根据具体问题设计适应度函数。此外,种群的染色体长度、交叉率、变异率等参数也需要根据具体问题进行调整。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)