You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to initialize an iterative vectors in optimization?
10 views (last 30 days)
Show older comments
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 Comments
Jan
on 14 Jun 2019
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
on 14 Jun 2019
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
on 15 Jun 2019
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
on 16 Jun 2019
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
on 18 Jun 2019
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
on 18 Jun 2019
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
on 18 Jun 2019
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
Accepted Answer
Daniel Mbadjoun
on 17 Jun 2019
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.
More Answers (0)
See Also
Categories
Find more on Agents in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)