이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to initialize an iterative vectors in optimization?
조회 수: 7 (최근 30일)
이전 댓글 표시
Hi,
I would like to start from this program which works well when it is a 1 * 2 initialization to a 2 * 2 initialization which I will then put the program which is wrong.
clear
clc
if nargin<1,
% Number of agents (or different solutions)
n=10;
end
% list of paramters
m_k=0;
lp1=0.7;
lp2=0.5;
bg=0;
w_k=0;
N_IterTotal=2000;
nd=2; %% Simple bounds of the search domain
%bea function
% [ x , y ]
Lb= [-4.5, -4.5];% Lower bounds
Ub= [4.5, 4.5];% Upper bounds
sum =0;
for t=1:1;
size(Lb);
% Random initial solutions
%%%%%disp('agent(i,:) bp_k(i)');
for i=1:n,
agent(i,:)=Lb+(Ub-Lb).*rand(size(Lb))
w_array(i,:)= agent(i,:);
bp_k(i,:)= agent(i,:);
disp(strcat(num2str(agent(i,:))));
end
This is wrong code that i want help.
clear
clc
if nargin<1,
% Number of agents (or different solutions)
n=10;
end
% list of paramters
m_k=0;
lp1=0.7;
lp2=0.5;
bg=0;
w_k=0;
N_IterTotal=2000;
nd=2; %% Simple bounds of the search domain
%bea function
% [ x , y, z ]
Lb=[36.5*ones(n,1),50*ones(n,1),0.73*ones(n,1)];%% Lower bounds
Ub=[41.5*ones(n,1),396*ones(n,1),0.94*ones(n,1)];% Upper bounds
sum =0;
for t=1:1;
size(Lb);
% Random initial solutions
%%%%%disp('agent(i,:) bp_k(i)');
for i=1:n,
agent(i,:)=Lb+(Ub-Lb).*rand(size(Lb))%%%%le problème réside ici sur la déclaration de cette ligne de commande
w_array(i,:)= agent(i,:);
bp_k(i,:)= agent(i,:);
disp(strcat(num2str(agent(i,:))));
end
Please help me to write well the command line agent(i,:)=Lb+(Ub-Lb).*rand(size(Lb)) to initialize it at vectors 2*2 with Lb and Ub.
댓글 수: 9
Jan
2019년 6월 14일
I do not understand, what you try to do. Does the code produce an error message? If so, please post a copy of it. It is easier to fix a problem than to guess, what the problem is.
Maybe all you need is to convert
agent(i,:)=Lb+(Ub-Lb).*rand(size(Lb))
to
agent(i, :, :) = Lb+(Ub-Lb).*rand(size(Lb))
% ^
Daniel Mbadjoun
2019년 6월 14일
Thank you for trying to help me.
I would like to start from the first program to generate the second which this time has the lower and upper vector and not scalar.
in the first case, we have Simple bounds of the search domain
Lb= [-4.5, -4.5];
Ub= [4.5, 4.5];
buffalo(i,:)=Lb+(Ub-Lb).*rand(size(Lb));
while in the second program, the bounds are in vector form hence the problem of declaring the command line
Lb=[36.5*ones(n,1),50*ones(n,1),0.73*ones(n,1)];
Ub=[41.5*ones(n,1),396*ones(n,1),0.94*ones(n,1)];
n=10;
m=3;
for i=1:n
for j=1:m
agent(i,:,:)=Lb(:,:)+(Ub(:,:)-Lb(:,:)).*rand(59,3)% How can i express this equality and i'm not sure this command line respect the procedure of the first program
end
end
%%%% Message error
Matrix dimensions must agree.
Error in three_var_Songloulou_sse_buffalo>simplebounds (line 189)
I=ns_tmp<Lb;
Error in three_var_Songloulou_sse_buffalo (line 114)
s=simplebounds(s,Lb,Ub);
You will see under the message error:
Matrix dimensions must agree.
Error in three_var_Songloulou_sse_buffalo>simplebounds (line 193)
J=ns_tmp>Ub;
Error in three_var_Songloulou_sse_buffalo (line 114)
s=simplebounds(s,Lb,Ub);
Daniel Mbadjoun
2019년 6월 15일
Hi Mr Jan,
i come back to you with few ameliorations by some new errors.
function [best_agent,fmin]=Ago_algorithmv6(n)
clear
clc
if nargin<1,
% Number of agents (or different solutions)
n=10;
end
%% Change this if you want to get better results
% list of paramters
m_k=0;
lp1=0.7;
lp2=0.5;
bg=0;
w_k=0;
N_IterTotal=2000;
nd=3; %% Simple bounds of the search domain
%beata function
% [ x , y , z ]
Lb=[36.5*ones(n,1),50*ones(n,1),0.73*ones(n,1)];
Ub=[41.5*ones(n,1),396*ones(n,1),0.94*ones(n,1)];
% tuning = 20;
sum =0;
% overall_best = fobj(Lb+(Ub-Lb).*rand(size(Lb)));
for t=1:3;
% Random initial solutions
disp('agent(i,:) bp_k(i)');
for i=1:n,
for j=1:n
agent(i,:,:)=Lb(:,:)+(Ub(:,:)-Lb(:,:)).*rand(10,3);%%%%%%%% Please check this line
w_array(i,:,:)= agent(i,:,:);
bp_k(i,:,:)= agent(i,:,:);
disp(strcat(num2str(agent(i,:,:))));
end
end
% Get the current best
fitness=10^10*ones(n,1);
[fmin,best_agent,agent,fitness]=get_best_agent(agent,agent,fitness);
N_iter=0;
%% Starting iterations
for iter=1:N_IterTotal,
for jj=1:n
s=agent(jj,:,:);
w=w_array(jj,:,:);
%disp(strcat(num2str(jj),' - agent before:[',num2str(s),']'));
% Step2. Update the agents exploitation using Equation (3.1)
s=s + lp1*(best_agent-w)*rand + lp2*(bp_k(jj,:,:)*rand - w);
disp(strcat(num2str(jj),' - agent after :[',num2str(s),']=',num2str(fobj(s))));
%Step2 Update the location of agents using (3.2):
w =((w+ s ))/rand;
% Apply simple bounds/limits
s=simplebounds(s,Lb,Ub);
w_array(jj,:,:)=w;
%disp(strcat('agent :[',num2str(s),']'));
% disp(strcat('w :[',num2str(w),']'));
% Evaluating all new solutions
if fobj(s)< fobj(agent(jj,:,:)) %fnew<fitness(jj),
agent(jj,:,:)=s;
end
if fobj(s)<fobj(bp_k(jj,:,:)),
%agent(jj,:,:)=s;
bp_k(jj,:,:)=s;%fobj(bp_k(jj,:, :));
end
% find global best
%fnew=fobj(s);
if fobj(s)<fobj(best_agent),
%fitness(jj)=fnew;
best_agent=s;
end
end
% Find the current agent
%[fmin,K]=min(fitness) ;
%best_agent=agent(K,:);
best_agent
fmin= fobj(best_agent)
end %% End of iterations
end
%% --------------- All subfunctions are list below ------------------
%% Find the current best agent
function [fmin,best,agent,fitness]=get_best_agent(agent,newagent,fitness)
% Evaluating all new solutions
for j=1:size(agent,1),
fnew=fobj(newagent(j,:));
if fnew<=fitness(j),
fitness(j)=fnew;
agent(j,:)=newagent(j,:);
end
end
% Find the current best
[fmin,K]=min(fitness) ;
best=agent(K,:);
% Application of simple constraints
function s=simplebounds(s,Lb,Ub)
% Apply the lower bound
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
% Apply the upper bounds
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
% Update this new move
s=ns_tmp;
%% You can replace the following by your own functions
% A d-dimensional objective function
function f=fobj(x)
filename = 'D_test2.xlsx';
sheet = 1;
xlRange = 'A2:E60';
Mm = xlsread(filename, sheet, xlRange);
%
% %Get the data
HB=(Mm(:,2)); %%% home ball
P=(Mm(:,3)); %%% poney
QTU=(Mm(:,1));%%quantity unit
Rg=(Mm(:,4));
k=0.00981;
m=(Mm(:,5));
x1=QTU(:);
f =sum((x1 - x(2)./(k.*x(1).*x(3))).^ 2);%stu
Please look how to correct this error
Array dimensions must match for binary array op.
Error in agent_code (line 66)
s=s + lp1*(best_agent-w)*rand + lp2*(bp_k(jj,:,:)*rand - w);
I tried in vain to send you the data file 'D_test2'
can you offer me another alternative to send it to you?
Jan
2019년 6월 16일
Some remarks:
The clear on top of a function is completely useless. Avoid such cargo-cult-programming, because it is confusing only.
I'd prefer to rewrite
Lb=[36.5*ones(n,1),50*ones(n,1),0.73*ones(n,1)];
as
Lb = repmat([36.4, 50, 0.73], n, 1);
but this is a question of taste only.
Using names of important biult-in functions for variables causes serious troubles frequently:
sum =0;
Avoid this source of errors.
Do you really want to display the string disp('agent(i,:) bp_k(i) ? Or is the intention to show the contents of the variables agent and bp_k.
agent(i,:,:)=Lb(:,:)+(Ub(:,:)-Lb(:,:)).*rand(10,3);
%%%%%%%% Please check this line
I've checked it, but what is the problem? You can simplify the code to:
agent(i,:,:) = Lb + (Ub - Lb) .* rand(10,3);
But this calculates exactly the same - or produce exactly the same error.
"I tried in vain to send you the data file 'D_test2' " - what does this mean? How do you want to send the file? Simply attach it here in the forum.
Your code is hard to read. Out-commented code lines are a DON'T for writing clean code. The readers have to guess, if they are wanted, tried, a different version or a hint, what is not working.
For the actual problem: You have to find out, what the error is. The array dimensions do not match, so simply check, what the dimensions are. Let Matlab stop at the error:
dbstop if error
Then check, what the sizes of the used arrays are and try to fix it.
Daniel Mbadjoun
2019년 6월 18일
Mr Rik, i try to explain my problem below. I have a serious problem to run my program and have expect results like write in each command line. Please ask me what you need to understand and i will try to give you response. Thanks for looking my request.
Rik
2019년 6월 18일
I haven't looked into this thread, I was just checking flagged content. I saw your comment posted as a flag, and I decided to move it to where it belongs: a comment. Since you posted an answer yourself (and now even accepted that answer), I assumed your question was solved anyway. If it isn't solved, I would suggest you move your answer to a comment if that is what it is. I don't feel like reading those walls of code to find out what is going wrong, it looks like Jan has provided you some pointers for solving this and/or improving the answerability of your question.
Daniel Mbadjoun
2019년 6월 18일
It is a mistake i have don't with the post in the answer. Please i need help to run well my code
buffalo(i,:,:)=Lb+(Ub-Lb).*rand(size(Lb));%%%% (1) I want to have the matrix of 59*3 for each iteration as this follow line command when we replace this by
buffalo(:,:,:)=Lb+(Ub-Lb).*rand(size(Lb)); %%% (2) I obtain matrix 59*3 only for one iteration as i expect when i write my program
%%%% With line (1), i obtain mismatch matrix between 'agent' and 'w', 's' and 'best_agent'. I want to obtain the same dimensions between 'agent' and 'w', 's' and 'best_agent'
%%%% With line (2), i obtain the final result but i only have during this simulation one iteration
%%%% How can i declare well the command line (1) to obtain each matrix 59*3 for each iteration and how declare 'w','s' to have for each iteration 59*3?
i expect you to he
function [best_agent,fmin]=Ago_algorithmv6(n)
clear
clc
if nargin<1,
% Number of agents (or different solutions)
n=10;
end
%% Change this if you want to get better results
% list of paramters
m_k=0;
lp1=0.7;
lp2=0.5;
bg=0;
w_k=0;
N_IterTotal=2000;
nd=59; %% Simple bounds of the search domain
%beata function
% [ x , y , z ]
Lb = repmat([36.5, 50, 0.73], nd, 1);
Ub = repmat([41.5, 396, 0.94], nd, 1);
% tuning = 20;
sum =0;
% overall_best = fobj(Lb+(Ub-Lb).*rand(size(Lb)));
for t=1:3;
% Random initial solutions
disp('agent(i,:) bp_k(i)');
for i=1:n,
for j=1:n
agent(:,:,:)=Lb(:,:)+(Ub(:,:)-Lb(:,:)).*rand(size(Lb));%%%%%%%% Please check this line
w_array(:,:,:)= agent(:,:,:);
bp_k(:,:,:)= agent(:,:,:);
disp(strcat(num2str(agent(:,:,:))));
end
end
% Get the current best
fitness=10^10*ones(n,1);
[fmin,best_agent,agent,fitness]=get_best_agent(agent,agent,fitness);
N_iter=0;
%% Starting iterations
for iter=1:N_IterTotal,
for jj=1:n
s=agent(jj,:,:);
w=w_array(jj,:,:);
%disp(strcat(num2str(jj),' - agent before:[',num2str(s),']'));
% Step2. Update the agents exploitation using Equation (3.1)
s=s + lp1*(best_agent-w)*rand + lp2*(bp_k(jj,:,:)*rand - w);
disp(strcat(num2str(jj),' - agent after :[',num2str(s),']=',num2str(fobj(s))));
%Step2 Update the location of agents using (3.2):
w =((w+ s ))/rand;
% Apply simple bounds/limits
s=simplebounds(s,Lb,Ub);
w_array(jj,:,:)=w;
%disp(strcat('agent :[',num2str(s),']'));
% disp(strcat('w :[',num2str(w),']'));
% Evaluating all new solutions
if fobj(s)< fobj(agent(jj,:,:)) %fnew<fitness(jj),
agent(jj,:,:)=s;
end
if fobj(s)<fobj(bp_k(jj,:,:)),
%agent(jj,:,:)=s;
bp_k(jj,:,:)=s;%fobj(bp_k(jj,:, :));
end
% find global best
%fnew=fobj(s);
if fobj(s)<fobj(best_agent),
%fitness(jj)=fnew;
best_agent=s;
end
end
% Find the current agent
%[fmin,K]=min(fitness) ;
%best_agent=agent(K,:);
best_agent
fmin= fobj(best_agent)
end %% End of iterations
end
%% --------------- All subfunctions are list below ------------------
%% Find the current best agent
function [fmin,best,agent,fitness]=get_best_agent(agent,newagent,fitness)
% Evaluating all new solutions
for j=1:size(agent,3),
fnew=fobj(newagent(j,:));
if fnew<=fitness(j),
fitness(j)=fnew;
agent(j,:)=newagent(j,:);
end
end
% Find the current best
[fmin,K]=min(fitness) ;
best=agent(K,:);
% Application of simple constraints
function s=simplebounds(s,Lb,Ub)
% Apply the lower bound
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
% Apply the upper bounds
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
% Update this new move
s=ns_tmp;
%% You can replace the following by your own functions
% A d-dimensional objective function
function f=fobj(x)
filename = 'D_test2.xlsx';
sheet = 1;
xlRange = 'A2:E60';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% i expect to have the same dimensions with this code below %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Mm = xlsread(filename, sheet, xlRange);
%
% %Get the data
HB=(Mm(:,2)); %%% home ball
P=(Mm(:,3)); %%% poney
QTU=(Mm(:,1));%%quantity unit
Rg=(Mm(:,4));
k=0.00981;
m=(Mm(:,5));
x1=QTU(:);
f =sum((x1 - x(2)./(k.*x(1).*x(3))).^ 2);%stu
dbstop if error
i expect to have the same dimensions with this code below
function [best_agent,fmin]=Ago_algorithmv6(n)
clear
clc
if nargin<1,
% Number of agents (or different solutions)
n=10;
end
%% Change this if you want to get better results
% list of paramters
m_k=0;
lp1=0.7;
lp2=0.5;
bg=0;
w_k=0;
N_IterTotal=2000;
nd=3; %% Simple bounds of the search domain
%beata function
% [ x , y , z ]
Lb = repmat([36.5, 50, 0.73], nd, 1);
Ub = repmat([41.5, 396, 0.94], nd, 1);
% tuning = 20;
sum =0;
% overall_best = fobj(Lb+(Ub-Lb).*rand(size(Lb)));
for t=1:1;
% Random initial solutions
disp('agent(i,:) bp_k(i)');
for i=1:n,
for j=1:n
agent(i,:,:)=Lb(:,:)+(Ub(:,:)-Lb(:,:)).*rand(size(Lb));%%%%%%%% I can't obtain for each iteration matrix 59*3 as i expect but blocks of 10*59*3
w_array(i,:,:)= agent(i,:,:);
bp_k(i,:,:)= agent(i,:,:);
%%%%%disp(strcat(num2str(agent(i,:,:))));
end
end
% Get the current best
fitness=10^10*ones(n,1);
[fmin,best_agent,agent,fitness]=get_best_agent(agent,agent,fitness);
N_iter=0;
%% Starting iterations
for iter=1:N_IterTotal,
for jj=1:n
s=agent(jj,:,:);%%%% I expect to have matrix 59*3 but i have matrix 1*177
w=w_array(jj,:,:);%%% I expect to have matrix 59*3 but i have matrix 1*177
%disp(strcat(num2str(jj),' - agent before:[',num2str(s),']'));
% Step2. Update the agents exploitation using Equation (3.1)
s=s + lp1*(best_agent-w)*rand + lp2*(bp_k(jj,:,:)*rand - w);%%%I expect to have matrix 59*3 but i have matrix 1*177
%%%%%disp(strcat(num2str(jj),' - agent after :[',num2str(s),']=',num2str(fobj(s))));
%Step2 Update the location of agents using (3.2):
w =((w+ s ))/rand;
% Apply simple bounds/limits
s=simplebounds(s,Lb,Ub);
w_array(jj,:,:)=w;%%%%I expect to have matrix 59*3 but i have matrix 1*177
%disp(strcat('agent :[',num2str(s),']'));
% disp(strcat('w :[',num2str(w),']'));
% Evaluating all new solutions
if fobj(s)< fobj(agent(jj,:,:)) %fnew<fitness(jj),
agent(jj,:,:)=s;
end
if fobj(s)<fobj(bp_k(jj,:,:)),
%agent(jj,:,:)=s;
bp_k(jj,:,:)=s;%fobj(bp_k(jj,:, :));
end
% find global best
%fnew=fobj(s);
if fobj(s)<fobj(best_agent),
%fitness(jj)=fnew;
best_agent=s;
end
end
% Find the current agent
%[fmin,K]=min(fitness) ;
%best_agent=agent(K,:);
best_agent
fmin= fobj(best_agent)
end %% End of iterations
end
%% --------------- All subfunctions are list below ------------------
%% Find the current best agent
function [fmin,best,agent,fitness]=get_best_agent(agent,newagent,fitness)
% Evaluating all new solutions
for j=1:size(agent,3),
fnew=fobj(newagent(j,:));
if fnew<=fitness(j),
fitness(j)=fnew;
agent(j,:)=newagent(j,:);
end
end
% Find the current best
[fmin,K]=min(fitness) ;
best=agent(K,:);
% Application of simple constraints
function s=simplebounds(s,Lb,Ub)
% Apply the lower bound
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
% Apply the upper bounds
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
% Update this new move
s=ns_tmp;
%% You can replace the following by your own functions
% A d-dimensional objective function
function f=fobj(x)
filename = 'D_test2.xlsx';
sheet = 1;
xlRange = 'A2:E60';
Mm = xlsread(filename, sheet, xlRange);
%
% %Get the data
HB=(Mm(:,2)); %%% home ball
P=(Mm(:,3)); %%% poney
QTU=(Mm(:,1));%%quantity unit
Rg=(Mm(:,4));
k=0.00981;
m=(Mm(:,5));
x1=QTU(:);
f =sum((x1 - x(2)./(k.*x(1).*x(3))).^ 2);%stu
%%% Please run the two code to understand my requests. You will see below the attach document for running this code.
Please look my code, i need help
채택된 답변
Daniel Mbadjoun
2019년 6월 17일
Thanks Mr Jan, your counsels help me to reduce some errors. But my real problem is increlentation of this command line which no mismatch of three columns of 'agent' with 'best_agent'
buffalo(i,:,:)=Lb+(Ub-Lb).*rand(size(Lb));%%%% (1) I want to have the matrix of 59*3 for each iteration as this follow line command when we replace this by
buffalo(:,:,:)=Lb+(Ub-Lb).*rand(size(Lb)); %%% (2) I obtain matrix 59*3 only for one iteration as i expect when i write my program
%%%% With line (1), i obtain mismatch matrix between 'agent' and 'w', 's' and 'best_agent'. I want to obtain the same dimensions between 'agent' and 'w', 's' and 'best_agent'
%%%% With line (2), i obtain the final result but i only have during this simulation one iteration
%%%% How can i declare well the command line (1) to obtain each matrix 59*3 for each iteration and how declare 'w','s' to have for each iteration 59*3?
You can try this code and have a good simulation but with only one iteration
function [best_agent,fmin]=Ago_algorithmv6(n)
clear
clc
if nargin<1,
% Number of agents (or different solutions)
n=10;
end
%% Change this if you want to get better results
% list of paramters
m_k=0;
lp1=0.7;
lp2=0.5;
bg=0;
w_k=0;
N_IterTotal=2000;
nd=59; %% Simple bounds of the search domain
%beata function
% [ x , y , z ]
Lb = repmat([36.5, 50, 0.73], nd, 1);
Ub = repmat([41.5, 396, 0.94], nd, 1);
% tuning = 20;
sum =0;
% overall_best = fobj(Lb+(Ub-Lb).*rand(size(Lb)));
for t=1:3;
% Random initial solutions
disp('agent(i,:) bp_k(i)');
for i=1:n,
for j=1:n
agent(:,:,:)=Lb(:,:)+(Ub(:,:)-Lb(:,:)).*rand(size(Lb));%%%%%%%% Please check this line
w_array(:,:,:)= agent(:,:,:);
bp_k(:,:,:)= agent(:,:,:);
disp(strcat(num2str(agent(:,:,:))));
end
end
% Get the current best
fitness=10^10*ones(n,1);
[fmin,best_agent,agent,fitness]=get_best_agent(agent,agent,fitness);
N_iter=0;
%% Starting iterations
for iter=1:N_IterTotal,
for jj=1:n
s=agent(jj,:,:);
w=w_array(jj,:,:);
%disp(strcat(num2str(jj),' - agent before:[',num2str(s),']'));
% Step2. Update the agents exploitation using Equation (3.1)
s=s + lp1*(best_agent-w)*rand + lp2*(bp_k(jj,:,:)*rand - w);
disp(strcat(num2str(jj),' - agent after :[',num2str(s),']=',num2str(fobj(s))));
%Step2 Update the location of agents using (3.2):
w =((w+ s ))/rand;
% Apply simple bounds/limits
s=simplebounds(s,Lb,Ub);
w_array(jj,:,:)=w;
%disp(strcat('agent :[',num2str(s),']'));
% disp(strcat('w :[',num2str(w),']'));
% Evaluating all new solutions
if fobj(s)< fobj(agent(jj,:,:)) %fnew<fitness(jj),
agent(jj,:,:)=s;
end
if fobj(s)<fobj(bp_k(jj,:,:)),
%agent(jj,:,:)=s;
bp_k(jj,:,:)=s;%fobj(bp_k(jj,:, :));
end
% find global best
%fnew=fobj(s);
if fobj(s)<fobj(best_agent),
%fitness(jj)=fnew;
best_agent=s;
end
end
% Find the current agent
%[fmin,K]=min(fitness) ;
%best_agent=agent(K,:);
best_agent
fmin= fobj(best_agent)
end %% End of iterations
end
%% --------------- All subfunctions are list below ------------------
%% Find the current best agent
function [fmin,best,agent,fitness]=get_best_agent(agent,newagent,fitness)
% Evaluating all new solutions
for j=1:size(agent,3),
fnew=fobj(newagent(j,:));
if fnew<=fitness(j),
fitness(j)=fnew;
agent(j,:)=newagent(j,:);
end
end
% Find the current best
[fmin,K]=min(fitness) ;
best=agent(K,:);
% Application of simple constraints
function s=simplebounds(s,Lb,Ub)
% Apply the lower bound
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
% Apply the upper bounds
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
% Update this new move
s=ns_tmp;
%% You can replace the following by your own functions
% A d-dimensional objective function
function f=fobj(x)
filename = 'D_test2.xlsx';
sheet = 1;
xlRange = 'A2:E60';
Mm = xlsread(filename, sheet, xlRange);
%
% %Get the data
HB=(Mm(:,2)); %%% home ball
P=(Mm(:,3)); %%% poney
QTU=(Mm(:,1));%%quantity unit
Rg=(Mm(:,4));
k=0.00981;
m=(Mm(:,5));
x1=QTU(:);
f =sum((x1 - x(2)./(k.*x(1).*x(3))).^ 2);%stu
dbstop if error
i expect to have the same dimensions with this code below
function [best_agent,fmin]=Ago_algorithmv6(n)
clear
clc
if nargin<1,
% Number of agents (or different solutions)
n=10;
end
%% Change this if you want to get better results
% list of paramters
m_k=0;
lp1=0.7;
lp2=0.5;
bg=0;
w_k=0;
N_IterTotal=2000;
nd=3; %% Simple bounds of the search domain
%beata function
% [ x , y , z ]
Lb = repmat([36.5, 50, 0.73], nd, 1);
Ub = repmat([41.5, 396, 0.94], nd, 1);
% tuning = 20;
sum =0;
% overall_best = fobj(Lb+(Ub-Lb).*rand(size(Lb)));
for t=1:1;
% Random initial solutions
disp('agent(i,:) bp_k(i)');
for i=1:n,
for j=1:n
agent(i,:,:)=Lb(:,:)+(Ub(:,:)-Lb(:,:)).*rand(size(Lb));%%%%%%%% I can't obtain for each iteration matrix 59*3 as i expect but blocks of 10*59*3
w_array(i,:,:)= agent(i,:,:);
bp_k(i,:,:)= agent(i,:,:);
%%%%%disp(strcat(num2str(agent(i,:,:))));
end
end
% Get the current best
fitness=10^10*ones(n,1);
[fmin,best_agent,agent,fitness]=get_best_agent(agent,agent,fitness);
N_iter=0;
%% Starting iterations
for iter=1:N_IterTotal,
for jj=1:n
s=agent(jj,:,:);%%%% I expect to have matrix 59*3 but i have matrix 1*177
w=w_array(jj,:,:);%%% I expect to have matrix 59*3 but i have matrix 1*177
%disp(strcat(num2str(jj),' - agent before:[',num2str(s),']'));
% Step2. Update the agents exploitation using Equation (3.1)
s=s + lp1*(best_agent-w)*rand + lp2*(bp_k(jj,:,:)*rand - w);%%%I expect to have matrix 59*3 but i have matrix 1*177
%%%%%disp(strcat(num2str(jj),' - agent after :[',num2str(s),']=',num2str(fobj(s))));
%Step2 Update the location of agents using (3.2):
w =((w+ s ))/rand;
% Apply simple bounds/limits
s=simplebounds(s,Lb,Ub);
w_array(jj,:,:)=w;%%%%I expect to have matrix 59*3 but i have matrix 1*177
%disp(strcat('agent :[',num2str(s),']'));
% disp(strcat('w :[',num2str(w),']'));
% Evaluating all new solutions
if fobj(s)< fobj(agent(jj,:,:)) %fnew<fitness(jj),
agent(jj,:,:)=s;
end
if fobj(s)<fobj(bp_k(jj,:,:)),
%agent(jj,:,:)=s;
bp_k(jj,:,:)=s;%fobj(bp_k(jj,:, :));
end
% find global best
%fnew=fobj(s);
if fobj(s)<fobj(best_agent),
%fitness(jj)=fnew;
best_agent=s;
end
end
% Find the current agent
%[fmin,K]=min(fitness) ;
%best_agent=agent(K,:);
best_agent
fmin= fobj(best_agent)
end %% End of iterations
end
%% --------------- All subfunctions are list below ------------------
%% Find the current best agent
function [fmin,best,agent,fitness]=get_best_agent(agent,newagent,fitness)
% Evaluating all new solutions
for j=1:size(agent,3),
fnew=fobj(newagent(j,:));
if fnew<=fitness(j),
fitness(j)=fnew;
agent(j,:)=newagent(j,:);
end
end
% Find the current best
[fmin,K]=min(fitness) ;
best=agent(K,:);
% Application of simple constraints
function s=simplebounds(s,Lb,Ub)
% Apply the lower bound
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
% Apply the upper bounds
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
% Update this new move
s=ns_tmp;
%% You can replace the following by your own functions
% A d-dimensional objective function
function f=fobj(x)
filename = 'D_test2.xlsx';
sheet = 1;
xlRange = 'A2:E60';
Mm = xlsread(filename, sheet, xlRange);
%
% %Get the data
HB=(Mm(:,2)); %%% home ball
P=(Mm(:,3)); %%% poney
QTU=(Mm(:,1));%%quantity unit
Rg=(Mm(:,4));
k=0.00981;
m=(Mm(:,5));
x1=QTU(:);
f =sum((x1 - x(2)./(k.*x(1).*x(3))).^ 2);%stu
Please run the two code to understand my requests. You will see below the attach document for running this code.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Agents에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
