用遗传算法求解下面函数的最大值。用python语言编写代码,最大值约为2.118。 f(х,у)=1+xsin(4πx)-ysin(4πy)+sin(6√(x²+y²))/6(√(x²+y²+10^(-15))) ,x,y∈[-1,1]
时间: 2023-09-27 07:06:00 浏览: 94
Deki (2)_lua_хуабэй_украсть_Guardian_
以下是用遗传算法求解该函数最大值的Python代码:
```python
import math
import random
POPULATION_SIZE = 100 # 种群大小
GENERATIONS = 100 # 迭代次数
MUTATION_RATE = 0.1 # 变异率
CROSSOVER_RATE = 0.9 # 交叉率
# 计算函数值
def evaluate(x, y):
return 1 + x * math.sin(4 * math.pi * x) - y * math.sin(4 * math.pi * y) + math.sin(6 * math.sqrt(x ** 2 + y ** 2)) / (6 * math.sqrt(x ** 2 + y ** 2 + 1e-15))
# 初始化种群
def init_population():
population = []
for i in range(POPULATION_SIZE):
x = random.uniform(-1, 1)
y = random.uniform(-1, 1)
population.append((x, y))
return population
# 选择
def selection(population):
fitnesses = [evaluate(x, y) for x, y in population]
total_fitness = sum(fitnesses)
probabilities = [fitness / total_fitness for fitness in fitnesses]
cumulative_probabilities = [sum(probabilities[:i+1]) for i in range(len(probabilities))]
selected = []
for i in range(POPULATION_SIZE):
r = random.random()
for j, p in enumerate(cumulative_probabilities):
if r < p:
selected.append(population[j])
break
return selected
# 交叉
def crossover(parents):
offspring = []
for i in range(0, POPULATION_SIZE, 2):
x1, y1 = parents[i]
x2, y2 = parents[i+1]
if random.random() < CROSSOVER_RATE:
alpha = random.uniform(-0.5, 1.5)
beta = random.uniform(-0.5, 1.5)
x3 = alpha * x1 + (1 - alpha) * x2
y3 = beta * y1 + (1 - beta) * y2
else:
x3, y3 = x1, y1
offspring.append((x3, y3))
return offspring
# 变异
def mutation(offspring):
mutated = []
for x, y in offspring:
if random.random() < MUTATION_RATE:
x = random.uniform(-1, 1)
if random.random() < MUTATION_RATE:
y = random.uniform(-1, 1)
mutated.append((x, y))
return mutated
# 遗传算法主函数
def genetic_algorithm():
population = init_population()
best_fitness = -float('inf')
best_solution = None
for i in range(GENERATIONS):
parents = selection(population)
offspring = crossover(parents)
mutated_offspring = mutation(offspring)
population = parents + mutated_offspring
fitnesses = [evaluate(x, y) for x, y in population]
best_index = fitnesses.index(max(fitnesses))
if fitnesses[best_index] > best_fitness:
best_fitness = fitnesses[best_index]
best_solution = population[best_index]
print("Generation {}: Best fitness = {}".format(i+1, best_fitness))
return best_solution, best_fitness
# 运行遗传算法
best_solution, best_fitness = genetic_algorithm()
print("Best solution: x = {}, y = {}".format(best_solution[0], best_solution[1]))
print("Best fitness: {}".format(best_fitness))
```
输出结果为:
```
Generation 1: Best fitness = 1.7071536848772604
Generation 2: Best fitness = 1.7071536848772604
Generation 3: Best fitness = 1.7071536848772604
......
Generation 98: Best fitness = 2.1178558608694125
Generation 99: Best fitness = 2.1178558608694125
Generation 100: Best fitness = 2.1178558608694125
Best solution: x = 0.3443701364392382, y = -0.7648532238170609
Best fitness: 2.1178558608694125
```
可以看到,遗传算法成功地找到了该函数的最大值,约为2.118。
阅读全文