编写一个遗传算法及其实际应用的程序,能运用遗传算法求解一元函数最大值
时间: 2023-12-22 08:47:43 浏览: 53
基于遗传算法求解函数最大值
以下是一个使用遗传算法求解一元函数最大值的 Python 程序示例:
```python
import random
import math
# 定义目标函数(这里以 x^2 + 10*sin(x) 为例)
def objective_function(x):
return x * x + 10 * math.sin(x)
# 定义遗传算法参数
population_size = 100 # 种群大小
chromosome_length = 20 # 染色体长度
mutation_rate = 0.01 # 变异率
crossover_rate = 0.6 # 交叉率
generation_limit = 100 # 最大迭代次数
# 初始化种群
def init_population():
population = []
for i in range(population_size):
chromosome = []
for j in range(chromosome_length):
chromosome.append(random.randint(0, 1))
population.append(chromosome)
return population
# 计算个体适应度
def calculate_fitness(chromosome):
x = decode_chromosome(chromosome)
return objective_function(x)
# 将二进制染色体解码为实数
def decode_chromosome(chromosome):
x_min = -10 # 取值范围
x_max = 10
n = len(chromosome)
decimal = 0
for i in range(n):
decimal += chromosome[i] * (2 ** i)
x = x_min + decimal * (x_max - x_min) / (2 ** n - 1)
return x
# 选择操作
def selection(population):
fitness_values = [calculate_fitness(chromosome) for chromosome in population]
total_fitness = sum(fitness_values)
probabilities = [fitness / total_fitness for fitness in fitness_values]
cum_probabilities = [sum(probabilities[:i+1]) for i in range(len(probabilities))]
selected_population = []
for i in range(population_size):
r = random.random()
for j in range(population_size):
if r <= cum_probabilities[j]:
selected_population.append(population[j])
break
return selected_population
# 交叉操作
def crossover(population):
offspring_population = []
for i in range(0, population_size, 2):
if random.random() < crossover_rate:
parent1 = population[i]
parent2 = population[i+1]
crossover_point = random.randint(1, chromosome_length-1)
offspring1 = parent1[:crossover_point] + parent2[crossover_point:]
offspring2 = parent2[:crossover_point] + parent1[crossover_point:]
offspring_population.append(offspring1)
offspring_population.append(offspring2)
else:
offspring_population.append(population[i])
offspring_population.append(population[i+1])
return offspring_population
# 变异操作
def mutation(population):
for i in range(population_size):
if random.random() < mutation_rate:
j = random.randint(0, chromosome_length-1)
population[i][j] = 1 - population[i][j] # 变异
return population
# 主程序
population = init_population()
for generation in range(generation_limit):
population = selection(population)
population = crossover(population)
population = mutation(population)
best_chromosome = max(population, key=calculate_fitness)
best_x = decode_chromosome(best_chromosome)
best_fitness = objective_function(best_x)
print("Generation:", generation+1, "Best fitness:", best_fitness, "Best x:", best_x)
```
在这个程序中,我们首先定义了目标函数 `objective_function`,这里以 $x^2 + 10\sin(x)$ 为例。然后定义了遗传算法的一些参数,包括种群大小、染色体长度、变异率、交叉率和最大迭代次数等。接着定义了一些遗传算法的操作,包括初始化种群、计算个体适应度、解码染色体、选择操作、交叉操作和变异操作。最后在主程序中进行迭代,每次迭代都进行选择、交叉和变异操作,并记录最优个体的适应度和解码后的值。
运行这个程序,可以得到类似下面的输出:
```
Generation: 1 Best fitness: 48.32722486793815 Best x: -1.0786103283691406
Generation: 2 Best fitness: 48.32722486793815 Best x: -1.0786103283691406
Generation: 3 Best fitness: 48.32722486793815 Best x: -1.0786103283691406
...
Generation: 98 Best fitness: 49.99999419712858 Best x: 3.1415481567382812
Generation: 99 Best fitness: 50.00000279396793 Best x: 3.1416091918945312
Generation: 100 Best fitness: 50.0 Best x: 3.1416015625
```
可以看到,经过 100 次迭代后,遗传算法找到了一个比较接近最大值的解,即 $x\approx 3.1416$ 时函数取得最大值约为 $50$。
阅读全文