遗传算法解决最小化ackley函数的python代码,精英保留一位,采用10位二进制编码,变异率百分之五,种群规模50,迭代次数50代
时间: 2024-01-11 12:02:27 浏览: 116
python实现遗传算法求解Ackley函数.zip
5星 · 资源好评率100%
好的,下面是遗传算法解决最小化Ackley函数的Python代码,遵循您的要求:
```python
import random
import math
# Ackley函数的实现
def ackley(x, y):
a = 20
b = 0.2
c = 2 * math.pi
term1 = -a * math.exp(-b * math.sqrt(0.5 * (x**2 + y**2)))
term2 = -math.exp(0.5 * (math.cos(c*x) + math.cos(c*y)))
return term1 + term2 + a + math.exp(1)
# 初始化种群
def init_population(pop_size, chrom_size):
population = []
for i in range(pop_size):
chrom = []
for j in range(chrom_size):
chrom.append(random.randint(0, 1))
population.append(chrom)
return population
# 计算适应度
def cal_fitness(chrom):
x = -32.768 + (32.768 - (-32.768)) / (2**10 - 1) * (chrom[0]*2**9 + chrom[1]*2**8 + chrom[2]*2**7 + chrom[3]*2**6 + chrom[4]*2**5 + chrom[5]*2**4 + chrom[6]*2**3 + chrom[7]*2**2 + chrom[8]*2**1 + chrom[9]*2**0)
y = -32.768 + (32.768 - (-32.768)) / (2**10 - 1) * (chrom[10]*2**9 + chrom[11]*2**8 + chrom[12]*2**7 + chrom[13]*2**6 + chrom[14]*2**5 + chrom[15]*2**4 + chrom[16]*2**3 + chrom[17]*2**2 + chrom[18]*2**1 + chrom[19]*2**0)
return 1 / (ackley(x, y) + 1)
# 选择操作
def selection(population, fitness):
new_population = []
elite = population[fitness.index(max(fitness))]
new_population.append(elite)
for i in range(len(population) - 1):
chrom1 = population[roulette(fitness)]
chrom2 = population[roulette(fitness)]
child = crossover(chrom1, chrom2)
new_population.append(child)
return new_population
# 轮盘赌选择
def roulette(fitness):
total_fitness = sum(fitness)
select_prob = [f/total_fitness for f in fitness]
select_prob_sum = [sum(select_prob[:i+1]) for i in range(len(select_prob))]
r = random.random()
for i in range(len(select_prob_sum)):
if r < select_prob_sum[i]:
return i
# 交叉操作
def crossover(chrom1, chrom2):
pos = random.randint(1, len(chrom1) - 1)
child = chrom1[:pos] + chrom2[pos:]
return child
# 变异操作
def mutation(chrom, mutation_rate):
for i in range(len(chrom)):
if random.random() < mutation_rate:
chrom[i] = 1 - chrom[i]
return chrom
# 遗传算法主程序
def genetic_algorithm(pop_size, chrom_size, elite_num, mutation_rate, max_iter):
population = init_population(pop_size, chrom_size)
fitness = [cal_fitness(chrom) for chrom in population]
best_fitness = max(fitness)
best_chrom = population[fitness.index(best_fitness)]
for i in range(max_iter):
# 选择
population = selection(population, fitness)
# 变异
for j in range(1, len(population)):
population[j] = mutation(population[j], mutation_rate)
# 计算适应度
fitness = [cal_fitness(chrom) for chrom in population]
# 精英保留
elite_index = sorted(range(len(fitness)), key=lambda k: fitness[k], reverse=True)[:elite_num]
elite_population = [population[i] for i in elite_index]
population = elite_population + [chrom for i, chrom in enumerate(population) if i not in elite_index]
# 记录最优解
best_fitness = max(best_fitness, max(fitness))
best_chrom = population[fitness.index(max(fitness))]
return best_chrom, best_fitness
# 运行遗传算法
best_chrom, best_fitness = genetic_algorithm(pop_size=50, chrom_size=10, elite_num=1, mutation_rate=0.05, max_iter=50)
# 输出结果
x = -32.768 + (32.768 - (-32.768)) / (2**10 - 1) * (best_chrom[0]*2**9 + best_chrom[1]*2**8 + best_chrom[2]*2**7 + best_chrom[3]*2**6 + best_chrom[4]*2**5 + best_chrom[5]*2**4 + best_chrom[6]*2**3 + best_chrom[7]*2**2 + best_chrom[8]*2**1 + best_chrom[9]*2**0)
y = -32.768 + (32.768 - (-32.768)) / (2**10 - 1) * (best_chrom[10]*2**9 + best_chrom[11]*2**8 + best_chrom[12]*2**7 + best_chrom[13]*2**6 + best_chrom[14]*2**5 + best_chrom[15]*2**4 + best_chrom[16]*2**3 + best_chrom[17]*2**2 + best_chrom[18]*2**1 + best_chrom[19]*2**0)
print("最优解:", (x, y))
print("最优解的适应度值:", best_fitness)
```
希望这份代码可以帮到您。
阅读全文