Trying to generate a population of 50 randomly permutated numbers but program doesn't stop running
Show older comments
I am trying to generate a population of 50 sets of 20 randomly sequenced aircraft. The only condition is that each aircraft can only shift 2 positions forward or backward from their initial positions. My code doesn't produce errors but it runs forever even when I just want to produce 1 generation with the above conditions.
%create initial population by randomizing positions of aircraft for 50
%aircraft
randcounter=1;
%let x be the number of aircraft landing in the time frame
x=20;
while randcounter<=50;
temp= randperm(x);
n=1; %n must start with 1 because when checking condition we must start checking from temp(1) later on
o=0;
%function to check if the random permutation is feasible (ie each
%position is shifted not more than 2 places).
%n is used to check that all 20 numbers have been checked.
%o is used to count if all have passed. if finally after all 20 have
%been checked and o is less than 20, means this permutation fails.
while n<=x;
if ((temp(n)<=(n+2))&& (temp(n)>=(n-2)))
n=n+1;
o=o+1;
else
n=n+1;
end
end
if o==20; %the permutation passes
initialpop(randcounter)=temp;
randcounter= randcounter+1;
else
randcounter=randcounter;
end
temp=[];
end
1 Comment
dpb
on 23 Dec 2013
I'd guess the restrictions aren't ever satisfied (and likely will take an inordinate amount of time as you've discovered to ever be so by using only rejection/regeneration).
To check that is so, monitor the value of the counter o; it never reaches 20, does it?
I suspect you'll have to think the restrictions through more carefully and do reordering within the checking process rather than simply rejecting the sample to ever get the sequence that is suitably ordered given your stated limitations of positions.
Or perhaps you should generate an initial order then resample from that some subset number to reorder within the initial sequence by the allowable [0,1,2] positions either way instead. What, specifically, would be dependent on what the process is that you're actually trying to model as to how the reordering is trying to simulate.
Accepted Answer
More Answers (1)
Tim
on 23 Dec 2013
0 votes
Categories
Find more on Random Number Generation 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!