The ga-function (genetic algorithm) gives me totally random and wrong solutions?

1 view (last 30 days)
Hello! I have a super easy puzzle that I would like to solve, and I have tried multiple solvers and found the ga to be the easiest to use, but it just gives me totally random and wrong results.
Here is my objective function/problem: min f = x(1)*x(3)+x(2)*x(4)*100;
lower and upper bounds för x_n are:
ub = [1 1 1 1];
lb = [0.4 0 0 0];
x(3) and x(4) have to be equal to 0 or 1, therefore intcon = [3 4];
my constraints are: x(1)*x(3)+x(2)*x(4)-load=0 where the load can vary between 0 and 2.
The problem is that the ga function just randomly give me different solutions which can be correct or wrong depending on how many times I hit "run". How can I overcome this problem? Or have anyone a better way to solve this problem?
Thank you very much!
Here is my code:
function ga_fraga
ub = [1 1 1 1];
lb = [0.4 0 0 0];
intcon = [3 4];
[xval fval]=ga(@myfun,length(lb),[],[],[],[],lb,ub,@nonlcon,intcon);
xval(1:2).*xval(3:4)
fval
end
function [c,ceq] = nonlcon(x)
load=0.6;
c = [];
ceq = [];
c(1) = x(1)*x(3)+x(2)*x(4)-load;
c(2) = -c(1);
end
function f = myfun(x)
f = x(1)*x(3)+x(2)*x(4)*100;
end

Answers (1)

Nayan Rawat
Nayan Rawat on 12 Jul 2019
Use fmincon

Community Treasure Hunt

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

Start Hunting!