function [J] = artifical_neural_net_genetic_fn(x, x_sample, f_sample) % % The function called by the general genetic algorithm code % % Inputs: % x is a matrix of size [ N_pop x N_vars ] (the genetic population) % x_sample: the "x" sampling % f_sample: = f(x_sample) % % 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. % %----- [ N_pop, N_vars ] = size(x); logsig = @(x) ( 1 ./ ( 1 + exp(-x) ) ); J = zeros(N_pop,1); for pi=1:N_pop, % Extract out the various NN weights: W = x(pi,:); W1 = W(1:5); % weights in the first hidden layer b1 = W(6:11); % bias of in the first hidden layer b_1,b_2,...,b_5, and b_6 W2 = W(12:16); % weights in the second hidden layer % Compute the output of the NN at each input x value: out = zeros(1,length(x_sample)); for ii=1:length(x_sample) xin = ( x_sample(ii) - 3 )/2.; % scale inputs so they are [ -1, +1 ] a1 = logsig( W1*xin + b1(1:5) ); a2 = logsig( a1(:)' * W2(:) + b1(6) ); out(ii) = a2; end % compute the norm between the truth and what we found when we used our NN: J(pi) = mean( (f_sample(:) - out(:)).^2 ); end