Info

This question is closed. Reopen it to edit or answer.

Problems running genetic algoritm (wrong function? or something else)

1 view (last 30 days)
Hello
I wrote below function and tried to use "ga" to minimize it, however I got similax "x" values for both variants of "z" i.e.
z=x(1)*30+x(2)*30+x(3)*30+x(4)*25+x(5)*25+x(6)*25
and
z=x(1)*30+x(2)*30+x(3)*30+x(4)*25+x(5)*25+x(6)*25+
(round(x(1)/2000+0.4999999999))*5000+(round(x(2)/2000+0.4999999999))*5500+...
(round(x(3)/2000+0.4999999999))*6000+(round(x(4)/2000+0.4999999999))*6500+...
(round(x(5)/2000+0.4999999999))*7500+(round(x(6)/2000+0.4999999999))*8000;
But I think that if some "x" were zero, function value will be lower... Do anyone know what is wrong? Below is complete code:
function z = optis(x)
%UNTITLED Summary of this function goes here
z=x(1)*30+x(2)*30+x(3)*30+x(4)*25+x(5)*25+x(6)*25+...
(round(x(1)/2000+0.4999999999))*5000+(round(x(2)/2000+0.4999999999))*5500+...
(round(x(3)/2000+0.4999999999))*6000+(round(x(4)/2000+0.4999999999))*6500+...
(round(x(5)/2000+0.4999999999))*7500+(round(x(6)/2000+0.4999999999))*8000;
end
variables = {'x(1)','x(2)','x(3)','x(4)','x(5)','x(6)'};
N = length(variables);
% create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
lb = zeros(size(variables));
lb([x(1),x(2),x(3),x(4),x(5),x(6)]) = ...
[0,0,0,0,0,0];
A =zeros(5,6);
A(1,x(1)) = -1; A(1,x(4)) = -1; b(1) = -400;
A(2,x(2)) = -1; A(2,x(5)) = -1; b(2) = -500;
A(3,x(3)) = -1; A(3,x(6)) = -1; b(3) = -800;
A(4,[x(1) x(2) x(3)]) = [1 1 1]; b(4) = 1500;
A(5,[x(4) x(5) x(6)]) = [1 1 1]; b(5) = 700;
opts = gaoptimset('PlotFcns',@gaplotbestf);
rng(1,'twister')
IntCon = [1 2 3 4 5 6];
[x,fval,exitflag] = ga(@optis,6,A,b,[],[],lb,[],[],IntCon,opts)

Answers (0)

Community Treasure Hunt

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

Start Hunting!