【分布估计算法】EDA学习(附带样例代码)

简介

通过一个概率模型描述候选解在空间得分布,采用统计学习手段从群体宏观的角度建立一个描述解分布得概率模型,然后对概率模型随机采样产生新的种群,如此反复进行,实现种群得进化,直到终止条件。(建模-采用-建模-采样-循环至最优)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nOoe0Pu0-1657272222349)(0.png)]

也就是两者都是在一定的种群数量下,一个是对个体进行优化,一个是对群体进行优化,以一种带有“全局操控”性的操作模式替换掉遗传算法中对“积木块”具有破坏作用的遗传算子(选择算子、交配算子和变异算子),这就是所描述的分布估计算法。

算法思路

Randomly generated the initial population P(0);
t = 0;
While not met the termination condition do
Begin
 Select a set of promising individuals D(t) form the current population P(t);
 Estimate the probability distribution of the selected set D(t);
 Generate a set of new individuals N(t) according to the estimate;
 Create a new population P(t+1) by replacing some individuals of P(t) by N(t);
 t = t+1;
end
  1. 随机产生M个个体作为初始种群;
  2. 然后计算M个个体的适应值,如果符合终止条件,算法结束,否则继续进行;
  3. 选择最优的N个个体用来更新概率向量p(x), N <= M
  4. 由新的概率模型采样M次,得到新一代群体,返回 2

更新过程:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eXbqYkPu-1657272222350)(1.jpg)]

实例

采用UMDA来求解一个四维的OneMax问题。在这个例子中,我们用一个简单的概率向量p=(p1,p2,p3,p4)来表示描述种群分布的概率模型,其中pi表示xi取1的概率,(1—pi)则为xi取0的概率。

  1. 产生初始种群。为了使初始种群在定义域内符合均匀分布,我们定义初始化概率向量模型p=(0.5,0.5,0.5,0.5),然后根据p产生规模为10的初始种群,最后根据F(x)=x1+x2+x3+x4,计算出初始种群的适应度,最终的结果如下表所示。
nox1x2x3x4f
111013
210001
301001
401113
511013
601102
710102
800112
910001
1010012
  1. 按照种群的适应度从高到低进行排序。假设Se=5,则从种群中选出适应度较高的5个个体用来更新概率向量模型p。更新概率模型时令pi = ni/Se,这里ni 为在选出的较优个体中xi =1的个体数。最终选出的个体如下表所示,从而得到新的概率模型为p=(3/5,4/5,3/5,3/5) = (0.6,0.8,0.6,0.6)。
nox1x2x3x4f
111013
401113
511013
601102
710102
  1. 根据更新后的概率模型p产生新的样本,并计算这些新样本的适应度。最终得到新一代的种群如下表所示。
nox1x2x3x4f
111002
201001
301113
411114
501113
600101
711103
811114
911013
1011002

​ 通过以上三步,分布估计算法完成了第一代的进化过程。接着重复第二步和第三步完成下一代的进化,最终得到的种群平均适应度和概率模型如下表所示。我们可以看出,随着演化的进行,种群的整体质量不断提高,概率向量逐渐逼近全局最优解。

gapProbability vectorscore
1(0.5, 0.5, 0.5, 0.5)2.2
2(0.6, 0.8 0.6, 0.6)2.6
3(0.8 1.0, 0.6, 0.8)2.9
4(1.0, 1.0, 1.0, 0.8)3.8
5(1.0, 1.0, 1.0, 1.0)4.0

demo代码

import random

N = 10  # 种群数量
Se = 5  # 适应度较好种群选取数量
p = [0.5, 0.5, 0.5, 0.5]  # 初始化概率向量模型
print("初始化概率向量", p)
# 循化直至最优值,也可以改成一定次数后模型变化不大
gap = 1
while sum(p) < 4:
    populations = []  # 种群

    # 生成种群
    for i in range(N):
        temp = []
        for j in p:
            if random.random() < j:
                temp.append(1)
            else:
                temp.append(0)
        temp.append(sum(temp))  # 评价指标
        populations.append(temp)


    populations = sorted(populations, key=lambda x: x[4], reverse=True)  # 排序,种群适应度又高到低
    print("第{}轮种群(适应度排序后):".format(gap), populations)
    # 适应度计算
    temp_p = [0, 0, 0, 0]
    for i in range(Se):
        for j in range(4):
            temp_p[j] += populations[i][j]

    # 计算新的概率模型
    p = [x / Se for x in temp_p]

    print("第{}轮概率向量更新为:".format(gap), p)
    gap += 1
print("寻优完毕。")

参考链接

一文搞懂什么是分布估计算法

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个完整的Python实现EDA算法代码示例: ``` python import numpy as np from scipy.stats import truncnorm # 定义初始化种群函数 def initialize_population(pop_size, n_vars, bounds): pop = [] for i in range(pop_size): individual = [] for j in range(n_vars): a, b = bounds[j] r = truncnorm.rvs(a, b) individual.append(r) pop.append(individual) return np.array(pop) # 定义适应度函数 def fitness_func(individual): # 这里假设适应度函数为个体各变量值的平方和 return -np.sum(np.square(individual)) # 定义选择父代函数 def select_parents(population, fitness, n_parents): fitness = fitness / np.sum(fitness) parents_idx = np.random.choice(np.arange(len(population)), size=n_parents, replace=False, p=fitness) return population[parents_idx] # 定义生成新个体函数 def generate_offspring(parents, n_offspring): n_parents, n_vars = parents.shape offspring = [] for i in range(n_offspring): p1 = parents[np.random.randint(n_parents)] p2 = parents[np.random.randint(n_parents)] child = [] for j in range(n_vars): if np.random.rand() < 0.5: child.append(p1[j]) else: child.append(p2[j]) offspring.append(child) return np.array(offspring) # 定义运行EDA算法函数 def eda(pop_size, n_vars, bounds, fitness_func, n_generations): population = initialize_population(pop_size, n_vars, bounds) for i in range(n_generations): fitness = evaluate(population, fitness_func) parents = select_parents(population, fitness, pop_size // 2) offspring = generate_offspring(parents, pop_size) population = np.vstack((population, offspring)) fitness = evaluate(population, fitness_func) elite_idx = np.argsort(fitness)[-pop_size:] population = population[elite_idx] return population[np.argmax(evaluate(population, fitness_func))] # 定义计算适应度函数 def evaluate(population, fitness_func): return np.array([fitness_func(individual) for individual in population]) # 测试运行EDA算法 pop_size = 100 # 种群大小 n_vars = 10 # 变量数量 bounds = [(-2, 2)] * n_vars # 变量范围 n_generations = 100 # 迭代次数 best_individual = eda(pop_size, n_vars, bounds, fitness_func, n_generations) print("最优解:", best_individual) print("最优适应度:", -fitness_func(best_individual)) ``` 上述代码中,假设适应度函数为个体各变量值的平方和,可根据实际问题进行修改。在测试运行EDA算法时,可以根据具体问题设置种群大小、变量数量、变量范围和迭代次数等参数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值