【优化算法】多目标水母搜索优化算法 (MOJS) 【含Matlab源码 248期】

举报
海神之光 发表于 2022/05/29 03:30:31 2022/05/29
【摘要】 一、获取代码方式 获取代码方式1: 完整代码已上传我的资源:【优化算法】多目标水母搜索优化算法 (MOJS) 【含Matlab源码 248期】 获取代码方式2: 通过订阅紫极神光博客付费专栏,凭支付凭...

一、获取代码方式

获取代码方式1:
完整代码已上传我的资源:【优化算法】多目标水母搜索优化算法 (MOJS) 【含Matlab源码 248期】

获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。

备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);

二、水母搜索优化算法简介

这项研究发展了一个新的元启发式算法,它是受水母在海洋中的行为启发,被称为人工水母搜索(JS)优化器。
水母搜索行为的模拟包括它们跟随洋流、它们在水母群中的运动(主动运动和被动运动)、在这些运动之间切换的时间控制机制,以及收敛到水母花的状态。新算法在基准函数和优化问题上得到了成功的测试。值得注意的是,JS只有两个控制参数,即群体规模和迭代数。因此,JS的使用非常简单,并且可能是解决优化问题的一个优秀的元启发式算法。

三、部分源代码

%% Main MOJS optimizer
function ARCH = MOJS(params,MultiObj)
% Parameters
Np      = params.Np;
Nr      = params.Nr;
MaxIt   = params.maxiter;
ngrid   = params.ngrid;
fun     = MultiObj.fun;
nVar    = MultiObj.nVar;
var_min = MultiObj.var_min(:);
var_max = MultiObj.var_max(:);
it=1;
% Initialization by Eq. 25
POS=initialchaos(7,Np,nVar,var_max',var_min');
POS_fit      = fun(POS);
ELI_POS      = POS;
ELI_POS_fit  = POS_fit;
DOMINATED    = checkDomination(POS_fit);
ARCH.pos     = POS(~DOMINATED,:);
ARCH.pos_fit = POS_fit(~DOMINATED,:);
ARCH         = updateGrid(ARCH,ngrid);

display(['Iteration #0 - Archive size: ' num2str(size(ARCH.pos,1))]);
%% Main MOJS loop
stopCondition = false;
while ~stopCondition
    % Select leader by Eq. 16
    h = selectLeader(ARCH);
    % Calculate time control by Eq. 15
    Ct=abs((1-it*((1)/MaxIt))*(2*rand-1));
    if Ct>=0.5
        Meanvl=mean(ELI_POS);
        for i=1:Np
            % The new position is determined by Eq.19 and Eq.20
            POS(i,:) = ELI_POS(i,:) + Levy(nVar).*(ARCH.pos(h,:) - 3*rand([1 nVar]).*Meanvl);
        end
    else
        for i=1:Np
            if rand<(1-Ct)
                % Jellyfish follow type B
                % Determine the direction by Eq. 24
                j=i;
                while j==i
                    j=randperm(Np,1);
                end
                Step = ELI_POS(i,:) - ELI_POS(j,:);
                if dominates(ELI_POS_fit(j,:),ELI_POS_fit(i,:))
                    Step = -Step;
                end
                % The new position is determined by Eq. 22
                POS(i,:) =ARCH.pos(h,:) + rand([1 nVar]).*Step;
            else
                % Jellyfish follow type A
                % The new position is determined by Eq. 21
                POS(i,:)=ARCH.pos(h,:)+Levy(nVar).*(ELI_POS(i,:)-ARCH.pos(h,:));
            end
        end
    end
    %% Update new position by opposition-based jumping using Eq. 26
    if rand <(it/MaxIt)
        [POS] = OPPOS(POS,var_max,var_min);
    end
    %% Check boundaries
    if rand>=0.5
        POS=checksimplebounds(POS,var_min',var_max');
    else
        POS = checkBoundaries(POS,var_max,var_min);
    end
    %% Evaluate the population
    POS_fit = fun(POS);
    pos_best = dominates(POS_fit, ELI_POS_fit);
    best_pos = ~dominates(ELI_POS_fit, POS_fit);
    best_pos(rand(Np,1)>=0.5) = 0;
    if(sum(pos_best)>1)
        ELI_POS_fit(pos_best,:) = POS_fit(pos_best,:);
        ELI_POS(pos_best,:) = POS(pos_best,:);
    end
    if(sum(best_pos)>1)
        ELI_POS_fit(best_pos,:) = POS_fit(best_pos,:);
        ELI_POS(best_pos,:) = POS(best_pos,:);
    end
    %% Update the archive 
    if size(ARCH.pos,1)==1
        ARCH.pos= POS;
        ARCH.pos_fit= POS_fit;
        ARCH = updateArchive(ARCH,ELI_POS,ELI_POS_fit,ngrid);
    else
        ARCH = updateArchive(ARCH,ELI_POS,ELI_POS_fit,ngrid);
        if size(ARCH.pos,1)==1
            ARCH.pos= ELI_POS;
            ARCH.pos_fit= ELI_POS_fit;
        end
    end
    if(size(ARCH.pos,1)>Nr)
        % Delete the worst members from archive by Eq. 18
        ARCH = deleteFromArchive(ARCH,size(ARCH.pos,1)-Nr,ngrid);
    end
    display(['Iteration #' num2str(it) ' - Archive size: ' num2str(size(ARCH.pos,1))]);
    it=it+1;
    if(it>MaxIt), stopCondition = true; end
end
%% Plotting paretofront
if(size(ARCH.pos_fit,2)==2)
    plot(ARCH.pos_fit(:,1),ARCH.pos_fit(:,2),'or'); hold on;
    grid on; xlabel('f1'); ylabel('f2');
end
if(size(ARCH.pos_fit,2)==3)
    plot3(ARCH.pos_fit(:,1),ARCH.pos_fit(:,2),ARCH.pos_fit(:,3),'or'); hold on;
    grid on; xlabel('f1'); ylabel('f2'); zlabel('f3');
end
end

%% This function calucates the leader performance by a roulette wheel selection
% based on the quality of each hypercube
function selected = selectLeader(ARCH)
% Roulette wheel
prob    = cumsum(ARCH.quality(:,2));     % Cumulated probs
sel_hyp = ARCH.quality(find(rand(1,1)*max(prob)<=prob,1,'first'),1); % Selected hypercube
% Select the index leader as a random selection inside that hypercube
idx      = 1:1:length(ARCH.grid_idx);
selected = idx(ARCH.grid_idx==sel_hyp);
selected = selected(randi(length(selected)));
end

%% This function returns 1 if x dominates y and 0 otherwise
function d = dominates(x,y)
d = all(x<=y,2) & any(x<y,2);
end

%% This function checks the domination inside the population.
function domi_vector = checkDomination(fitness)
Np = size(fitness,1);
if Np>2
    domi_vector = zeros(Np,1);
    all_perm = nchoosek(1:Np,2);    % Possible permutations
    all_perm = [all_perm; [all_perm(:,2) all_perm(:,1)]];
    
    d = dominates(fitness(all_perm(:,1),:),fitness(all_perm(:,2),:));
    dominated_particles = unique(all_perm(d==1,2));
    domi_vector(dominated_particles) = 1;
else
    domi_vector=ones(Np,1);
end
end

%% This function updates the archive given a new population 
function ARCH = updateArchive(ARCH,POS,POS_fit,ngrid)
% Domination between jellyfish
DOMINATED  = checkDomination(POS_fit);
ARCH.pos    = [ARCH.pos; POS(~DOMINATED,:)];
ARCH.pos_fit= [ARCH.pos_fit; POS_fit(~DOMINATED,:)];
% Domination between nondominated jellyfish and the last archive 
DOMINATED  = checkDomination(ARCH.pos_fit);
ARCH.pos_fit= ARCH.pos_fit(~DOMINATED,:);
ARCH.pos    = ARCH.pos(~DOMINATED,:);
% Updating the grid
ARCH        = updateGrid(ARCH,ngrid);
end

%% Function that updates the hypercube grid, the hypercube where belongs
function ARCH = updateGrid(ARCH,ngrid)
% Computing the  hypercube limitation
ndim = size(ARCH.pos_fit,2);
ARCH.hypercube_limits = zeros(ngrid+1,ndim);
for dim = 1:1:ndim
    ARCH.hypercube_limits(:,dim) = linspace(min(ARCH.pos_fit(:,dim)),max(ARCH.pos_fit(:,dim)),ngrid+1)';
end
% Computing where belongs each jellyfish
npar = size(ARCH.pos_fit,1);
ARCH.grid_idx = zeros(npar,1);
ARCH.grid_subidx = zeros(npar,ndim);
for n = 1:1:npar
    idnames = [];
    for d = 1:1:ndim
        ARCH.grid_subidx(n,d) = find(ARCH.pos_fit(n,d)<=ARCH.hypercube_limits(:,d)',1,'first')-1;
        if(ARCH.grid_subidx(n,d)==0), ARCH.grid_subidx(n,d) = 1; end
        idnames = [idnames ',' num2str(ARCH.grid_subidx(n,d))];
    end
    ARCH.grid_idx(n) = eval(['sub2ind(ngrid.*ones(1,ndim)' idnames ');']);
end
% Quality based on the number of jellyfish in each hypercube
ARCH.quality = zeros(ngrid,2);
ids = unique(ARCH.grid_idx);
for i = 1:length(ids)
    ARCH.quality(i,1) = ids(i);                       
    ARCH.quality(i,2) = 10/sum(ARCH.grid_idx==ids(i)); 
end
end

%% This function deletes an excess of jellyfish inside the archive using crowding distances
function ARCH = deleteFromArchive(ARCH,n_extra,ngrid)
% Compute the crowding distances
crowding = zeros(size(ARCH.pos,1),1);
for m = 1:1:size(ARCH.pos_fit,2)
    [m_fit,idx] = sort(ARCH.pos_fit(:,m),'ascend');
    m_up     = [m_fit(2:end); Inf];
    m_down   = [Inf; m_fit(1:end-1)];
    distance = (m_up-m_down)./(max(m_fit)-min(m_fit));
    [~,idx]  = sort(idx,'ascend');
    crowding = crowding + distance(idx);
end
crowding(isnan(crowding)) = Inf;
% This function deletes the extra jellyfish with the smallest crowding distances
[~,del_idx] = sort(crowding,'ascend');
del_idx = del_idx(1:n_extra);
ARCH.pos(del_idx,:) = [];
ARCH.pos_fit(del_idx,:) = [];
ARCH = updateGrid(ARCH,ngrid);
end

%% This function checks the boundary of jellyfish search space
function [POS] = checkBoundaries(POS,var_max,var_min)
% Useful matrices
Np = size(POS,1);
MAXLIM   = repmat(var_max(:)',Np,1);
MINLIM   = repmat(var_min(:)',Np,1);
POS(POS>MAXLIM) = MAXLIM(POS>MAXLIM);
POS(POS<MINLIM) = MINLIM(POS<MINLIM);
end
function POS=checksimplebounds(POS,Lb,Ub)
for i=1:size(POS,1)
    ns_tmp=POS(i,:);
    I=ns_tmp<Lb;
    while sum(I)~=0
        ns_tmp(I)=Ub(I)+(ns_tmp(I)-Lb(I));
        I=ns_tmp<Lb;
    end
    J=ns_tmp>Ub;
    while sum(J)~=0
        ns_tmp(J)=Lb(J)+(ns_tmp(J)-Ub(J));
        J=ns_tmp>Ub;
    end
    POS(i,:)=ns_tmp;
end
end

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235

四、运行结果

在这里插入图片描述

五、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016.
[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.

文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。

原文链接:qq912100926.blog.csdn.net/article/details/120274682

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。