用遗传算法求解香蕉函数最小值的python代码
时间: 2023-09-10 09:07:31 浏览: 176
以下是使用遗传算法求解香蕉函数最小值的 Python 代码示例:
```python
import random
# 定义香蕉函数
def banana_function(x, y):
return (1 - x)**2 + 100 * (y - x**2)**2
# 定义个体类
class Individual:
def __init__(self, x, y):
self.x = x
self.y = y
self.fitness = banana_function(x, y)
# 定义遗传算法类
class GeneticAlgorithm:
def __init__(self, population_size, mutation_rate, crossover_rate, elitism_rate, tournament_size):
self.population_size = population_size
self.mutation_rate = mutation_rate
self.crossover_rate = crossover_rate
self.elitism_rate = elitism_rate
self.tournament_size = tournament_size
# 初始化种群
def initialize_population(self):
population = []
for i in range(self.population_size):
x = random.uniform(-10, 10)
y = random.uniform(-10, 10)
individual = Individual(x, y)
population.append(individual)
return population
# 选择操作
def selection(self, population):
selected_population = []
for i in range(self.population_size):
tournament = random.sample(population, self.tournament_size)
winner = min(tournament, key=lambda x: x.fitness)
selected_population.append(winner)
return selected_population
# 交叉操作
def crossover(self, population):
offspring_population = []
for i in range(int(self.population_size * self.crossover_rate)):
parent1, parent2 = random.sample(population, 2)
offspring_x = (parent1.x + parent2.x) / 2
offspring_y = (parent1.y + parent2.y) / 2
offspring = Individual(offspring_x, offspring_y)
offspring_population.append(offspring)
return offspring_population
# 变异操作
def mutation(self, population):
mutated_population = []
for i in range(int(self.population_size * self.mutation_rate)):
individual = random.choice(population)
mutated_x = individual.x + random.uniform(-0.1, 0.1)
mutated_y = individual.y + random.uniform(-0.1, 0.1)
mutated_individual = Individual(mutated_x, mutated_y)
mutated_population.append(mutated_individual)
return mutated_population
# 精英保留操作
def elitism(self, population):
sorted_population = sorted(population, key=lambda x: x.fitness)
elitism_size = int(self.population_size * self.elitism_rate)
elitism_population = sorted_population[:elitism_size]
return elitism_population
# 进化操作
def evolve(self, population):
selected_population = self.selection(population)
offspring_population = self.crossover(selected_population)
mutated_population = self.mutation(offspring_population)
new_population = self.elitism(population) + selected_population + mutated_population
return new_population
# 求解函数最小值
def solve(self):
population = self.initialize_population()
best_individual = min(population, key=lambda x: x.fitness)
for i in range(100):
population = self.evolve(population)
current_best_individual = min(population, key=lambda x: x.fitness)
if current_best_individual.fitness < best_individual.fitness:
best_individual = current_best_individual
return best_individual.x, best_individual.y
# 测试代码
if __name__ == '__main__':
ga = GeneticAlgorithm(population_size=100, mutation_rate=0.2, crossover_rate=0.5, elitism_rate=0.1, tournament_size=5)
x, y = ga.solve()
print(f"The minimum value of the banana function is {banana_function(x, y)} at ({x}, {y})")
```
在这个示例中,我们使用了一个包含 100 个个体的种群,进行了 100 次迭代,每次迭代使用了 5 轮锦标赛选择,以及 20% 的个体进行变异,50% 的个体进行交叉,和 10% 的精英保留策略。最后输出找到的香蕉函数最小值和对应的 $(x, y)$ 坐标。
阅读全文