condition in while loop

10 views (last 30 days)
Mudasir Ahmed
Mudasir Ahmed on 5 Oct 2014
Commented: Geoff Hayes on 6 Oct 2014
while ((ch1(1,1)~=60)&(ch1(1,2)~=40)) ||
((ch2(1,1)~=60)&(ch2(1,2)~=40)) ||
((ch3(1,1)~=60)&(ch3(1,2)~=40)) ||
((ch4(1,1)~=60)&(ch4(1,2)~=40)))
body
end
hi,
i want to run a loop until any one of the condition ch1(60,40),ch2(60,40),ch3(60,40),ch4(60,40) met. is above syntax is correct or not. i run the above line but didn't get proper response which is desired.
kindly help me. i will be highly thankful to you :) thanks in advance
  4 Comments
Geoff Hayes
Geoff Hayes on 6 Oct 2014
Mudasir - from your previous question at ga & target tracking, the (60,40) is the position of the target, the ch1, ch2, ch3, and ch4 are presumably the chromosomes of your population (of only size four?). And each chromosome has two genes (or variables). Are you trying to force a genetic algorithm to run until one of the chromosomes has converged on the target position? That won't necessarily (or even probably) happen given the small population size and the fact that the solutions may all converge prematurely to another location.
Mudasir Ahmed
Mudasir Ahmed on 6 Oct 2014
thanks sir,
sir i have written a complete code and also it is working fine with for loop(iterations>50). now i want to make it conditional means whenever any one of the chromosome reach at target stop the loop. i have tried but failed to do this. sir kindly give me your personal email id or any other plate form on which i can discuss in detail with you and send you a complete code or even i can paste code here but it is bit long comprise of 200+ lines, i think only few changes are required and for that i need you guidance sir. thanks in advance

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Oct 2014
Mudasir - you can try the following if you want to break out of the loop if any of your chromosomes obtain a certain value (or get close enough to it). It doesn't limit you to four chromosomes (which is much too small of a population size for a genetic algorithm)
numChromosomes = 42;
chromosomes = zeros(numChromosomes,2);
tol = 0.0001;
while all(sqrt((chromosomes(:,1)-60).^2 + (chromosomes(:,2)-40).^2)>tol)
% do stuff
end
The above creates an mx2 matrix of chromosomes which will (presumably) be updated by the algorithm. The condition in the while loop uses the usual distance function
all(sqrt((chromosomes(:,1)-60).^2 + (chromosomes(:,2)-40).^2)>tol)
We compute the distance for each chromosome from the target position of (60,40). We then compare each to a tolerance tol which is a small number. If all of these distances are greater than tol then we keep iterating. If at least one is less than or equal to tol, then the condition is broken and we exit the while loop.
You do have to be careful as you may become stuck in an infinite loop as your GA population (the chromosomes) may converge prematurely to another solution (position) and so will never achieve the target position (despite the mutation or any other method you invoke against your chromosomes).

More Answers (2)

per isakson
per isakson on 6 Oct 2014
while ((ch1(1,1)~=60)&&(ch1(1,2)~=40)) || ...
((ch2(1,1)~=60)&&(ch2(1,2)~=40)) || ...
((ch3(1,1)~=60)&&(ch3(1,2)~=40)) || ...
((ch4(1,1)~=60)&&(ch4(1,2)~=40))
body
end

Mudasir Ahmed
Mudasir Ahmed on 6 Oct 2014
target=[60,40] % target location
ch1=[0,100] ch2=[15,85] ch3=[85,05] ch4=[95,15]
thanks sir, it is working, but iterations varies for every trial, means in first attempt good solution comes in 300 iteration after that it varies from 72,641,260....means for every trial, number of require iterations varies(require large number of iterations compare to PSO). i think it will be counter by tacking more chromosome instead of using 4. is there are another techniques to improve response other then increasing number of chromosome.
My 2nd question is that sir, robot is placed at random position e.g robot1=[10,20] and their task is to reach at target, but we have to move robot to the target in step by step manner. but as we take 4 chromosome, so how we can assign a location to robot for every iteration. (i think in every iteration value of best chromosome will be transferred to robot.)
sir my 3rd question is , as GA is not suitable or provide desired response compare to other optimization technique like PSO then what is the advantage of using it.
thanks in advance sir :)
  1 Comment
Geoff Hayes
Geoff Hayes on 6 Oct 2014
Mudasir - as your answer isn't an answer to your original question (but three new questions!) then you should delete this answer and start a new thread, or continue the conversation at a previous post of yours, GAs and target tracking, so that readers of this thread do not become confused.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!