python遗传算法代码
时间: 2023-09-01 16:12:37 浏览: 79
以下是一个简单的python遗传算法代码,用于优化一个简单的函数:
```python
import random
# 定义问题
def objective_function(x):
return x**2
# 定义个体
def create_individual(bounds):
return [random.uniform(bounds[i][0], bounds[i][1]) for i in range(len(bounds))]
# 定义种群
def create_population(size, bounds):
return [create_individual(bounds) for _ in range(size)]
# 定义选择函数
def selection(population, scores):
return population[random.randint(0,len(population)-1)]
# 定义交叉函数
def crossover(parent1, parent2, crossover_rate):
child = []
for i in range(len(parent1)):
if random.random() < crossover_rate:
child.append(parent1[i])
else:
child.append(parent2[i])
return child
# 定义变异函数
def mutate(individual, bounds, mutation_rate):
for i in range(len(individual)):
if random.random() < mutation_rate:
individual[i] = random.uniform(bounds[i][0], bounds[i][1])
return individual
# 定义遗传算法
def genetic_algorithm(objective_function, bounds, population_size, iterations, crossover_rate, mutation_rate):
population = create_population(population_size, bounds)
for i in range(iterations):
scores = [objective_function(individual) for individual in population]
best_index = scores.index(min(scores))
best_individual = population[best_index]
print('Iteration {}: Best Score = {}, Best Individual = {}'.format(i+1, scores[best_index], best_individual))
new_population = [best_individual]
for _ in range(population_size-1):
parent1 = selection(population, scores)
parent2 = selection(population, scores)
child = crossover(parent1, parent2, crossover_rate)
child = mutate(child, bounds, mutation_rate)
new_population.append(child)
population = new_population
return population[best_index]
# 运行遗传算法
bounds = [(-5, 5)]
best = genetic_algorithm(objective_function, bounds, 50, 100, 0.8, 0.1)
print('Best Solution:', best)
```
该代码定义了一个简单的问题和一些用于遗传算法的函数。遗传算法本身被定义为`genetic_algorithm`函数。该函数的输入包括代表问题的目标函数,搜索空间的边界,种群大小,迭代次数,交叉概率和变异概率。该函数执行以下步骤:
1. 创建一个种群并评估每个个体的适应度
2. 选择最佳个体并打印结果
3. 生成新种群,其中包含最佳个体以及使用交叉和变异操作产生的新个体
4. 重复步骤2-3,直到达到指定的迭代次数
运行该代码会输出每一代的最佳适应度和相应的个体。在本例中,该代码正在寻找使目标函数$x^2$最小化的$x$值。输出应显示$x=0$是最佳解决方案。
相关推荐
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)