MATLAB Answers

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

2 views (last 30 days)
jar on 23 Nov 2013
Answered: Nayan Rawat on 12 Jul 2019
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);
function [c,ceq] = nonlcon(x)
c = [];
ceq = [];
c(1) = x(1)*x(3)+x(2)*x(4)-load;
c(2) = -c(1);
function f = myfun(x)
f = x(1)*x(3)+x(2)*x(4)*100;


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by