遗传算法python实例
时间: 2023-06-28 11:05:54 浏览: 39
以下是一个简单的遗传算法Python实例:
```python
import random
# 目标函数
def objective_function(x):
return x ** 2
# 创建种群
def create_population(population_size, chromosome_size):
population = []
for i in range(population_size):
chromosome = []
for j in range(chromosome_size):
chromosome.append(random.randint(0, 1))
population.append(chromosome)
return population
# 计算适应度
def calculate_fitness(chromosome):
x = 0
for i in range(len(chromosome)):
x += chromosome[i] * (2 ** i)
return objective_function(x)
# 选择
def selection(population):
fitness_list = [calculate_fitness(chromosome) for chromosome in population]
total_fitness = sum(fitness_list)
probabilities = [fitness / total_fitness for fitness in fitness_list]
cumulative_probabilities = [sum(probabilities[:i+1]) for i in range(len(probabilities))]
selected_population = []
for i in range(len(population)):
r = random.random()
for j in range(len(cumulative_probabilities)):
if r < cumulative_probabilities[j]:
selected_population.append(population[j])
break
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):
for i in range(len(chromosome)):
if random.random() < mutation_rate:
chromosome[i] = 1 - chromosome[i]
return chromosome
# 遗传算法主程序
def genetic_algorithm(population_size, chromosome_size, generations, mutation_rate):
population = create_population(population_size, chromosome_size)
for i in range(generations):
selected_population = selection(population)
new_population = []
for j in range(population_size // 2):
parent1 = random.choice(selected_population)
parent2 = random.choice(selected_population)
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=calculate_fitness)
x = 0
for i in range(len(best_chromosome)):
x += best_chromosome[i] * (2 ** i)
return x, objective_function(x)
# 测试
x, y = genetic_algorithm(population_size=100, chromosome_size=10, generations=100, mutation_rate=0.1)
print("最优解: x = %.2f, y = %.2f" % (x, y))
```
这个例子中,我们想要求解函数 $f(x) = x^2$ 的最大值,其中 $x$ 的取值范围为 0 到 $2^{chromosome\_size}-1$。遗传算法的参数设置如下:
- 种群大小:100
- 染色体长度:10
- 迭代次数:100
- 变异率:10%
在主程序中,我们首先创建种群,然后进行 100 次迭代。在每次迭代中,我们进行选择、交叉和变异操作,然后生成新的种群。最终,我们找到适应度最高的染色体,计算出对应的 $x$ 值和 $y$ 值,即为所求的最优解。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![slx](https://img-home.csdnimg.cn/images/20210720083646.png)