Defining Objective Function in Genetic Algorithm
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
Geoff Hayes
le 22 Mai 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.
Catégories
En savoir plus sur Genetic Algorithm dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!