function out=testfunction2(X) % Implements the fitness function for the knapsack problem. % INPUT: Population (number of the population x 12) %These are the values and weights from the problem (in the order given in %the table) val=[12 5 10 11 50 15 6 4 5 25 20 30]; wgt=[2 4 7 5 3 3 2 2 2 3 11 1]; %Find the size of the array X [numpop,lenx]=size(X); out=zeros(numpop,1); ValuX=sum(X.*repmat(val,numpop,1),2); %Total value WghtX=sum(X.*repmat(wgt,numpop,1),2); %Total weight for j=1:numpop if WghtX(j)<=20 % We have a 20 kg weight limit (otherwise, the out(j)=ValuX(j); % output is 0) end end