필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
jar
jar 2013년 11월 23일
답변: Nayan Rawat 2019년 7월 12일
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

답변 (1개)

Nayan Rawat
Nayan Rawat 2019년 7월 12일
Use fmincon

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by