function f=testfunction(x,funnum) % TESTFUNCTION - Test functions for optimization % These are the test functions that appear in Appendix I. % Set funnum to the function you want to use. % funnum=17 is the MOO function % % Each column of input x correspond to the input variables x_1, x_2, x_3, ... x_n. % Thus the output from two dimensional inputs is given via % % testfunction( [1,2], 13 ) % % From the book: % Practical Genetic Algorithms % by Haupt & Haupt. % % Epage: 250 % % Written by: % -- % John L. Weatherwax 2006-08-28 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- if( nargin<2 ) funnum=16; end switch funnum case 1, f = abs(x)+cos(x); case 2, f = abs(x)+sin(x); case 3, f = x(:,1).^2 + x(:,2).^2; case 4, f = 100*(x(:,2).^2 - x(:,1)).^2 + (1-x(:,1)).^2; case 5, f = sum( abs(x')-10*cos(sqrt(abs(10*x'))) )'; case 6, f = (x.^2 + x).*cos(x); case 7, f = x(:,1).*sin(4*x(:,1))+1.1*x(:,2).*sin(2*x(:,2)); case 8, f = x(:,2).*sin(4*x(:,1))+1.1*x(:,2).*sin(2*x(:,2)); case 9, f(:,1) = x(:,1).^4 + 2*x(:,2).^4 + randn(length(x(:,1)),1); case 10, f(:,1) = 20+sum(x'.^2 - 10*cos(2*pi*x'))'; case 11, f(:,1) = 1 + sum(abs(x').^2/4000)' - prod(cos(x'))'; case 12, f(:,1) = 0.5+(sin(sqrt(x(:,1).^2 + x(:,2).^2).^2)-0.5)./(1+0.1*(x(:,1).^2+x(:,2).^2)); case 13, aa = x(:,1).^2 + x(:,2).^2; bb = ((x(:,1)+0.5).^2 + x(:,2).^2).^0.1; f(:,1) = aa.^0.25.*sin(30*bb).^2 + abs(x(:,1))+abs(x(:,2)); case 14, f(:,1) = besselj(0,x(:,1).^2+x(:,2).^2)+abs(1-x(:,1))/10+abs(1-x(:,2))/10; case 15, f(:,1) = -exp(0.2*sqrt((x(:,1)-1).^2+(x(:,2)-1).^2)+(cos(2*x(:,1))+sin(2*x(:,1)))); case 16, f(:,1) = -x(:,1).*sin(sqrt(abs(x(:,1)-(x(:,2)+9))))-(x(:,2)+9).*sin(sqrt(abs(x(:,2)+0.5*x(:,1)+9))); case 17, x=x+1; f(:,1) = (x(:,1)+x(:,2).^2 + sqrt(x(:,3))+ 1.0./x(:,4))/8.5; f(:,2) = (1./x(:,1)+1./x(:,2)+x(:,3)+x(:,4))/6; otherwise error( 'unknown function' ); end