MATLAB: How to initialize an iterative vectors in optimization

how to initialize an iterative vectors in optimization?

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.

Best Answer

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.