How to describe a sequence of numbers as a variable using Genetic algorithm in MATLAB?
1 view (last 30 days)
Show older comments
How can we define “sequence of numbers” as a variable using genetic algorithm in Matlab ? For example:
X = [1 2 3 4 5 6];
So in this case, X is not a real number, instead is a set of real numbers. If we apply permutations to x, like perms(x), it will give 720 Possible arrangements. It means x can vary in 720 different sequences. Each sequence gives different results. Now how can I apply GA to get the optimal sequence, if my variable is a sequence of numbers instead of a real number.
Thanks in advance.
0 Comments
Answers (1)
John D'Errico
on 25 Oct 2022
Edited: John D'Errico
on 25 Oct 2022
You can't simply tell GA to use such a permutation sequence. Although I suppose you could set up a nonlinear constraint that would exclude all sequences that were not permutations. That would make GA inefficient though, since it would keep on trying non-permutation sequences, only to then realize most of the test points were not feasible.
Simpler is to tell GA to use only ONE variable, that takes on 720 possible integer levels, each of which corresponds to one of the indicated permutations. Now you need only generate all 720 possible permutations. I would probably do it using a function, then set that variable as a persistent one. For example...
permN(0,10); % initialize the perms
Now we can use this function to quickly return the n'th permutation from the set of all such permutations. Subsequent calls pass in only one argument.
permN(4)
permN(137)
function P = permN(n,N)
persistent permslist
if nargin > 1
% set up the list of all permutations
permslist = perms(1:N);
end
if (n > 0) & (n <= size(permslist,1))
P = permslist(n,:);
else
P = [];
end
end
Will this fail for larger values of N? OF COURSE! Surely you don't expect it to work for the permutations of 100 numbers? At least not with a finite amount of time and system memory at your command? But it will certainly work for only a small set like the one you describe.
Finally, CAN you solve the problem for larger permutation sets? Well, yes, using factoradic numbers. But that would take more work to describe here for a problem that I don't think you have.
See Also
Categories
Find more on Genetic Algorithm 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!