How to describe a sequence of numbers as a variable using Genetic algorithm in MATLAB?

1 view (last 30 days)
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.

Answers (1)

John D'Errico
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)
ans = 1×10
10 9 8 7 6 5 4 2 1 3
permN(137)
ans = 1×10
10 9 8 7 5 6 2 1 4 3
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.
  1 Comment
Shehryar Ishaque
Shehryar Ishaque on 26 Oct 2022
Thank you very much for your response.
Actually, i dont want to use permutations. As permutations will give me all possible arrangements of the numbers, instead i want to use GA that can create different arrangements itself. All of them will be among the possible permutations ofcourse, but it will not solve all the possible arrangments, instead it will solve only arrangements that improves the solution and will give me the optimal one. Also you told that permutations can not be used for a large set of numbers, that is true, thats why i want to use GA. Permutations can not be applied for a set having greater than 11 numbers. But in my case, I have to use it for a set of numbers that may exceed 15 numbers.
I think my question was not clear, but still i got one positive from your answer and that is "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." Alright, so if i consider ONE variable that defines the sequence of the numbers. So in my case i have to use two variables. ONE variable will select the number of integers in a set and the other will define the sequence of the integers with in the set. Now,
1) If i gave one sequence initially, how GA will modify it itself for every iteration. How can i set it up?
2) My objective function is affected by the number of integers in a set as well as sequence of the integers, but the equation of the objective function does not invole the variables (both the number of integers and the sequence of the integers) directly. So how can i handle this?

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!