How to add 'not equal' constraints in 'Genetic Algorithm'
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I think that this problem would be perfect to solve with the Genetic Algorithm in Matlab. Just set up the equation and let it try integer number until it has found the solution. However, I can't find a good way to include the information that any of the number can have the same integer value. The GA does not allow 'equality constraints'; only 'inequality constraints'. Which allows to say that something must be larger than something else.
The approach I tried then was to set the GA in a while loop that was fun until all the integers it found was different from each other. But this does not run properly, dont know why.
I will copy the code here. If anybody has a suggestion for 'not equal' constraints can be implemented directly in the GA, or how it can be worked out another way, I would be very happy.
Main function:
%
close all
clear all
clc
%
%Defines some random parameters
a1 = 1;
a2 = 1;
a3 = 1;
a4 = 1;
a5 = 1;
a6 = 1;
a7 = 1;
a8 = 1;
a9 = 1;
%Let the GA solver run until the while condition is met
while a1 ~= a2 && a1 ~= a3 && a1 ~= a4 && a1 ~= a5 && a1 ~= a6 && a1 ~= a7 && a1 ~= a8 && a1 ~= a9...
a2 ~= a3 && a2 ~= a4 && a2 ~= a5 && a2 ~= a6 && a2 ~= a7 && a2 ~= a8 && a2 ~= a9 &&...
a3 ~= a4 && a3 ~= a5 && a3 ~= a6 && a3 ~= a7 && a3 ~= a8 && a3 ~= a9 &&...
a4 ~= a5 && a4 ~= a6 && a4 ~= a7 && a4 ~= a8 && a4 ~= a9 &&...
a5 ~= a6 && a5 ~= a7 && a5 ~= a8 && a5 ~= a9 &&...
a6 ~= a7 && a6 ~= a8 && a6 ~= a9 &&...
a7 ~= a8 && a7 ~= a9 &&...
a8 ~= a9;
%
%%%GA solver%%%
%Defines minimizer function
funObj = @(guess)Vitetnam_objective(guess);
%
%Defines upper- and lower bounds
Lb = [0 0 0 0 0 0 0 0 0];
Ub = [9 9 9 9 9 9 9 9 9];
%Defines integer parameters
IntCon = [1 2 3 4 5 6 7 8 9];
%Sets options for GA.
options = gaoptimset('Display','iter',...
'TolFun',1e-3,'PopulationSize',1000,'StallGenLimit',2,'Generations',50);
%Calls the GA function
[result,fvall] = ga(funObj,9,[],[],[],[],Lb,Ub,[],IntCon,options);
%
%Recieves the result
a1 = result(1);
a2 = result(2);
a3 = result(3);
a4 = result(4);
a5 = result(5);
a6 = result(6);
a7 = result(7);
a8 = result(8);
a9 = result(9);
end
%Tests the result
Res=((((((((((((a1 + 13) * a2) / a3 ) + a4 ) + 12 ) * a5) - a6) - 11) + a7) * a8) / a9) - 10);
Minimizing function:
%
%Objective function to minimize
function E=Vitetnam_objective(guess)
a1 = guess(1);
a2 = guess(2);
a3 = guess(3);
a4 = guess(4);
a5 = guess(5);
a6 = guess(6);
a7 = guess(7);
a8 = guess(8);
a9 = guess(9);
%
Res=((((((((((((a1 + 13) * a2) / a3 ) + a4 ) + 12 ) * a5) - a6) - 11) + a7) * a8) / a9) - 10);
%
E = abs(66-Res);
1 Kommentar
Lucas Carneiro
am 20 Mär. 2017
Bearbeitet: Lucas Carneiro
am 18 Mär. 2018
I am dealing with a similar problem and I guess you can easily state as constraints:
mod(a1-a2)>=1 mod(a1-a3)>=1 ... and all possible permutations.
Since solutions are integer, this should be sufficient
Antworten (1)
Walter Roberson
am 24 Mai 2015
If I read the documentation properly you can use nloncon in combination with integer constraints. For any two values p and q, returning p==q has the effect of implementing a non-equality constraint: the solver tries to enforce that the returned value <= 0 and 0 is false for logical conditions so p==q is 0 (and accepted) for p~=q .
You could just code a whole bunch of these,
c(:,1) = x(:,1) == x(:,2);
c(:,2) = x(:,1) == x(:,3);
and so on. Or you could + a bunch together since the sum of 0's (the desired condition) is 0 and a single 1 anywhere would make the sum > 0 (not wanted)
c(:,1) = x(:,1) == x(:,2) + x(:,1) == x(:,3) + x(:,1) == x(:,4) + ...
and of course you can repmat():
c(:,1) = sum(repmat(x(:,1),1,size(x,2)-1) == x(:,2:end),2);
and you can loop:
nrow = size(x,1);
ncol = size(x,2);
c = zeros(nrow, ncol-1);
for K = 1 : ncol - 1
c(:,K) = sum(repmat(x(:,K),1,ncol-K) == x(:,K+1:end),2);
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!