Defining Objective Function in Genetic Algorithm
Mostrar comentarios más antiguos
Trying to optimize the following function using Genetic Algorithm (A,B are arrays of length 10000 attached)
y= [x] * [A-B]
such that
- x is binary
- -B'*x <= blimit - sum(B)
load ('A_B_Arrays');
nvars = numel(A);
blimit = 1165.208;
Aineq = -B';
bineq = blimit - sum(B);
Aeq = [];
beq = [];
lb = zeros(1,N);
ub = ones(1,N);
nonlcon=[];
intcon = 1:nvars;
tic
x = ga(fun,nvars,Aineq,bineq,Aeq,beq,lb,ub,nonlcon,IntCon)
toc
MATLAB Documentation says that objective function should accept "a row vector of length nvars and return a scalar value". The length of vector x, in this case, depends on the length of [A-B]. How do I define the objective function with so many input variables?
function y = fun(x(1),x(2),x(3),...,x(nvars))
y=[x(1) x(2) ... x(nvars)] * [A-B];
end
Respuesta aceptada
Más respuestas (1)
Geoff Hayes
el 22 de Mayo de 2018
Neeraj - your fitness function would be simply
function y = fun(x)
y = x * [A-B];
end
where x is an array of variables being optimized.
1 comentario
Neeraj Rama
el 22 de Mayo de 2018
Categorías
Más información sobre Genetic Algorithm en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!