Defining Objective Function in Genetic Algorithm
显示 更早的评论
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
采纳的回答
更多回答(1 个)
Geoff Hayes
2018-5-22
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!