How to initialize an iterative vectors in optimization?
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
Daniel Mbadjoun
on 14 Jun 2019
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
Daniel Mbadjoun
on 15 Jun 2019
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.
Rik
on 18 Jun 2019
Very important comment in my problem
Daniel Mbadjoun
on 18 Jun 2019
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
Accepted Answer
More Answers (0)
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!