差分进化算法matlab源码(单目标sphere函数)

  • 代码是自己写的,如果对代码中有疑问或者不正确的地方,请评论留言,谢谢。

  •  差分进化算法的matlab源码,这里只对单目标连续问题做研究,离散问题目前没有涉及,下面是标准差分进化算法的matlab源码,里面的函数是单目标的sphere函数。这份代码只是更好的理解差分进化算法(DE)的原理和迭代的过程。

问题:单目标sphere函数

 

运行效果:

源码里关于参数的设置是看过一些sci改进的DE论文中提到的对参数合理的设置。

matlab源码(单目标sphere函数):

%标准差分进化算法matlab源码

clear;      %删除工作区中的所有变量
close all;  %关闭所有图形窗口
clc;        %清除命令窗口的内容

%控制变量的设置,这里要求的是单目标的sphere函数
Np=160;     %Np在(5D,10D)之间
F=0.1;      %F的设定在0.0和1.0附近
Cr=0.5;     %Cr的设定在(0.4,0.9)之间
Algebra=100;
Dimension=30;
Generation=1;
scope_min=-100;  %测试函数的解空间
scope_max=100;

%初始化种群变量
variation=zeros(Np,Dimension);
trial=zeros(Np,Dimension);

%进行初始化操作并且评估种群的适应值
initial=rand(Np,Dimension)*(scope_max-scope_min)+scope_min;
for i=1:Np
    adapt_value_1(i)=sum(initial(i,:).^2);
end
trace(Generation)=min(adapt_value_1); %记录代数和每代最优解之间的关系

%进行变异->交叉->选择的迭代
while(Generation<=Algebra)
    for j=1:Np
        dx=randperm(Np);
        if dx(1)==j,dx(1)=dx(4);
        elseif dx(2)==j,dx(2)=dx(4);
        elseif dx(3)==j,dx(2)=dx(4);
        end
        variation(j,:)=initial(dx(1),:)+F*(initial(dx(2),:)-initial(dx(3),:));
        %对变异种群的个体进行是否在解空间中做判断
        for m=1:Dimension
            if variation(j,m)<scope_min || variation(j,m)>scope_max
                variation(j,m)=rand*(scope_max-scope_min)+scope_min;
            end
        end
        for n=1:Dimension
            cr=rand(1);
            if cr<Cr || dx(5)==n
                trial(j,n)=variation(j,n);
            else
                trial(j,n)=initial(j,n);
            end
        end
        adapt_value_2(j)=sum(trial(j,:).^2);
    end
    for q=1:Np
        if adapt_value_2(q)<=adapt_value_1(q)
            initial(q,:)=trial(q,:);
        end
        adapt_value_1(q)=sum(initial(q,:).^2);
    end
    Generation=Generation+1;
    trace(Generation)=min(adapt_value_1);
end
figure(1);
plot(trace);
xlabel('进化代数');
ylabel('适应值');
  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Sphere函数是一个常用的基准测试函数,其公式为: f(x) = x1^2 + x2^2 + ... + xn^2 其中x为n维向量。差分进化算法(Differential Evolution, DE)是一种常用的优化算法,它通过对候选解进行向量差分来产生新的解,并根据一定的策略选择更优的解作为下一代种群的基础。 下面是使用差分进化算法求解Sphere函数的Python代码示例: ```python import numpy as np def sphere(x): return np.sum(np.square(x)) def de(fobj, bounds, mut=0.8, crossp=0.7, popsize=20, its=1000): dimensions = len(bounds) pop = np.random.rand(popsize, dimensions) min_b, max_b = np.asarray(bounds).T diff = np.fabs(min_b - max_b) pop_denorm = min_b + pop * diff fitness = np.asarray([fobj(ind) for ind in pop_denorm]) best_idx = np.argmin(fitness) best = pop_denorm[best_idx] for i in range(its): for j in range(popsize): idxs = [idx for idx in range(popsize) if idx != j] a, b, c = pop[np.random.choice(idxs, 3, replace=False)] mutant = np.clip(a + mut * (b - c), 0, 1) cross_points = np.random.rand(dimensions) < crossp if not np.any(cross_points): cross_points[np.random.randint(0, dimensions)] = True trial = np.where(cross_points, mutant, pop[j]) trial_denorm = min_b + trial * diff f = fobj(trial_denorm) if f < fitness[j]: fitness[j] = f pop[j] = trial if f < fitness[best_idx]: best_idx = j best = trial_denorm yield best, fitness[best_idx] bounds = [(0, 10)] * 10 # 10维变量范围在0到10之间 for solution, fitness in de(sphere, bounds): print(solution, fitness) ``` 代码中使用了生成随机种群、向量差分、选择优秀解等经典的差分进化算法操作,通过迭代不断优化种群,最终得到了Sphere函数的最优解。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值