python 遗传算法代码
时间: 2023-09-16 10:15:48 浏览: 48
当然,我可以为您提供一个简单的Python遗传算法代码示例。以下是一个基本的遗传算法实现:
```python
import random
# 定义问题:求解最大化函数 f(x) = x^2
def fitness_function(x):
return x ** 2
# 初始化种群
def init_population(population_size, chromosome_length):
population = []
for _ in range(population_size):
chromosome = [random.randint(0, 1) for _ in range(chromosome_length)]
population.append(chromosome)
return population
# 评估适应度
def evaluate_fitness(population):
fitness_scores = []
for chromosome in population:
x = chromosome_to_value(chromosome)
fitness_score = fitness_function(x)
fitness_scores.append(fitness_score)
return fitness_scores
# 将染色体解码为数值
def chromosome_to_value(chromosome):
value = 0
for bit in chromosome:
value = (value << 1) | bit
return value
# 选择
def selection(population, fitness_scores):
selected_population = []
total_fitness = sum(fitness_scores)
probabilities = [score / total_fitness for score in fitness_scores]
for _ in range(len(population)):
selected_chromosome = random.choices(population, probabilities)[0]
selected_population.append(selected_chromosome)
return selected_population
# 交叉
def crossover(parent1, parent2):
crossover_point = random.randint(1, len(parent1) - 1)
child1 = parent1[:crossover_point] + parent2[crossover_point:]
child2 = parent2[:crossover_point] + parent1[crossover_point:]
return child1, child2
# 变异
def mutation(chromosome, mutation_rate):
mutated_chromosome = chromosome.copy()
for i in range(len(mutated_chromosome)):
if random.random() < mutation_rate:
mutated_chromosome[i] = 1 - mutated_chromosome[i]
return mutated_chromosome
# 遗传算法主函数
def genetic_algorithm(population_size, chromosome_length, mutation_rate, generations):
population = init_population(population_size, chromosome_length)
for _ in range(generations):
fitness_scores = evaluate_fitness(population)
selected_population = selection(population, fitness_scores)
new_population = []
while len(new_population) < population_size:
parent1, parent2 = random.sample(selected_population, 2)
child1, child2 = crossover(parent1, parent2)
child1 = mutation(child1, mutation_rate)
child2 = mutation(child2, mutation_rate)
new_population.append(child1)
new_population.append(child2)
population = new_population
best_chromosome = max(population, key=lambda chromosome: fitness_function(chromosome_to_value(chromosome)))
best_solution = chromosome_to_value(best_chromosome)
return best_solution
# 使用遗传算法求解最大化函数 f(x) = x^2
population_size = 100
chromosome_length = 10
mutation_rate = 0.01
generations = 100
best_solution = genetic_algorithm(population_size, chromosome_length, mutation_rate, generations)
print(f"Best solution: {best_solution}")
```
这个例子实现了一个简单的二进制遗传算法,用于求解最大化函数 f(x) = x^2。其中,种群中的每个个体被表示为一个二进制染色体,通过选择、交叉和变异等操作逐步优化,最终得到适应度最高的个体作为解。您可以根据自己的实际问题进行相应的修改和扩展。希望对您有帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)