changeset 9845:3b7136683b6f octave-forge

ga: add unit tests about vectorized evaluation of fitness function
author slackydeb
date Sat, 24 Mar 2012 16:33:44 +0000
parents 94a15827f127
children 6b76ad220e06
files main/ga/inst/ga.m
diffstat 1 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/main/ga/inst/ga.m	Sat Mar 24 16:33:31 2012 +0000
+++ b/main/ga/inst/ga.m	Sat Mar 24 16:33:44 2012 +0000
@@ -294,6 +294,35 @@
 %!                           "InitialScores",     zeros (ip + 1, 1));
 %! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, bad_options);
 
+## error with vectorized evaluation of objective function. Vectorized
+## objective functions are better because can be evaluated both as
+## serial and vectorized.
+%!shared nvars
+%! nvars = 2;
+%!function [C, Ceq] = nonlcon (x)
+%!  C = [];
+%!  Ceq = [];
+%!function f = ff (nvars)
+%!  f = @(x) sum (x(:, 1:nvars) .** 2, 2);
+%!function f_not_vectorized = ff_not_vectorized (nvars)
+%!  f_not_vectorized = @(x) sum (x(1:nvars) .** 2);
+%!test # A non-vectorized objective function works when no vectorization is required
+%! f = ff_not_vectorized (nvars);
+%! options = gaoptimset ("Vectorized", "off");
+%! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options);
+%!error # A non-vectorized objective function does not work when vectorization is required
+%! f = ff_not_vectorized (nvars);
+%! options = gaoptimset ("Vectorized", "on");
+%! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options);
+%!test # A vectorized objective function works when no vectorization is required
+%! f = ff (nvars);
+%! options = gaoptimset ("Vectorized", "off");
+%! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options);
+%!test # A vectorized objective function works when vectorization is required
+%! f = ff (nvars);
+%! options = gaoptimset ("Vectorized", "on");
+%! x = ga (f, nvars, [], [], [], [], [], [], @nonlcon, options);
+
 ## error with conflicting optimization parameters: parallel and
 ## vectorized evaluation of objective function
 %!shared f, nvars