From: <sla...@us...> - 2012-03-15 02:23:35
|
Revision: 9896 http://octave.svn.sourceforge.net/octave/?rev=9896&view=rev Author: slackydeb Date: 2012-03-15 02:23:29 +0000 (Thu, 15 Mar 2012) Log Message: ----------- ga: restructure unit tests, fixing bug #3287917 The related Debian bug is #622929. Convert some unit tests to proper unit tests. Move other unit tests to ga_demo (they will be converted to demos in the future), as that code is more suitable for demos than for unit tests (the result of the ga function is not deterministic by design). http://sourceforge.net/tracker/?func=detail&aid=3287917&group_id=2888&atid=102888 http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=622929 Modified Paths: -------------- trunk/octave-forge/main/ga/inst/ga.m trunk/octave-forge/main/ga/inst/ga_demo.m Modified: trunk/octave-forge/main/ga/inst/ga.m =================================================================== --- trunk/octave-forge/main/ga/inst/ga.m 2012-03-15 02:23:15 UTC (rev 9895) +++ trunk/octave-forge/main/ga/inst/ga.m 2012-03-15 02:23:29 UTC (rev 9896) @@ -71,7 +71,7 @@ ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 5.21.1 +## Version: 6.0.0 function [x fval exitflag output population scores] = \ ga (fitnessfcn_or_problem, @@ -115,46 +115,22 @@ endfunction -## nvars == 2 and min != zeros (1, nvars) +## number of arguments -## TODO: Move this code into a future demo, as it shows that ga -## optimizes also functions whose minimum is not in zero -%!# TODO: get this test working with tol = 1e-6 -%!xtest -%! min = [-1, 2]; -%! assert (ga (struct ("fitnessfcn", @(x) rastriginsfcn (x - min), "nvars", 2, "options", gaoptimset ("FitnessLimit", 1e-7, "Generations", 1000, "PopInitRange", [-5; 5], "PopulationSize", 200))), min, 1e-5) +## type of arguments +%!test x = ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("FitnessLimit", 1e-7, "Generations", 1000))) -## nvars == 1 and min == zeros (1, nvars) +%!test x = ga (struct ("fitnessfcn", @(x) rastriginsfcn (x(1:2)) + ((x(3) ** 2) - (cos (2 * pi * x(3))) + 1) + (x(4) ** 2), "nvars", 4, "options", gaoptimset ("EliteCount", 5, "FitnessLimit", 1e-7, "PopInitRange", [-2; 2], "PopulationSize", 200))) -%!test assert (ga (@(x) x ** 2, 1), 0, 1e-3) -%!test assert (ga (@(x) (x ** 2) - (cos (2 * pi * x)) + 1, 1), 0, 1e-3) +## nvars == 1 and min == zeros (1, nvars) +%!test assert (ga (@(x) x ** 2, 1), 0, 1e-3); -## nvars == 2 and min == zeros (1, nvars) +%!test assert (ga (@(x) (x ** 2) - (cos (2 * pi * x)) + 1, 1), 0, 1e-3); -## TODO: Move this code into a future demo, as it shows that ga -## optimizes also functions with nvars > 0 -%!xtest assert (ga (@rastriginsfcn, 2), [0, 0], 1e-3) -## TODO: Convert this test into a test on type of arguments -%!# TODO: get this test working with tol = 1e-6 -%!xtest assert (ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("FitnessLimit", 1e-7, "Generations", 1000))), zeros (1, 2), 1e-4) - -## TODO: This test is like the previous one ("PopulationSize" vs. -## "Generations"): remove it? -%!# TODO: get this test working with tol = 1e-6 -%!xtest assert (ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("FitnessLimit", 1e-7, "PopulationSize", 200))), zeros (1, 2), 1e-4) - - -## nvars == 4 and min == zeros (1, nvars) - -## TODO: Convert this test into a test on type of arguments -%!# TODO: get this test working with tol = 1e-3 -%!xtest assert (ga (struct ("fitnessfcn", @(x) rastriginsfcn (x(1:2)) + ((x(3) ** 2) - (cos (2 * pi * x(3))) + 1) + (x(4) ** 2), "nvars", 4, "options", gaoptimset ("EliteCount", 5, "FitnessLimit", 1e-7, "PopInitRange", [-2; 2], "PopulationSize", 200))), zeros (1, 4), 1e-2) - - ## InitialPopulation and InitialScores options %!error ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("InitialPopulation", [0, 0; 0, 0; 0, 0; 0, 0], "PopulationSize", 3))); @@ -173,18 +149,3 @@ %!test ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("Generations", 10, "Vectorized", "on"))); %!xtest ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("Generations", 10, "UseParallel", "always"))); - -## TODO: move to demo as this is really a xtest -%!test ## Vectorized option speeds up execution -%! -%! tic (); -%! ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("Generations", 10, "PopulationSize", 200))); -%! elapsed_time = toc (); -%! -%! tic (); -%! ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("Generations", 10, "PopulationSize", 200, "Vectorized", "on"))); -%! elapsed_time_with_vectorized = toc (); -%! -%! assert (elapsed_time > elapsed_time_with_vectorized); - - #TODO: test that UseParallel speeds up execution \ No newline at end of file Modified: trunk/octave-forge/main/ga/inst/ga_demo.m =================================================================== --- trunk/octave-forge/main/ga/inst/ga_demo.m 2012-03-15 02:23:15 UTC (rev 9895) +++ trunk/octave-forge/main/ga/inst/ga_demo.m 2012-03-15 02:23:29 UTC (rev 9896) @@ -21,10 +21,42 @@ ## Author: Luca Favatella <sla...@gm...> ## Created: March 2012 -## Version: 0.0.1 +## Version: 0.0.2 demo ga_demo %!demo %! % TODO + + +## This code is a simple example of the usage of ga + # TODO: convert to demo +%!xtest assert (ga (@rastriginsfcn, 2), [0, 0], 1e-3) + + +## This code shows that ga optimizes also functions whose minimum is not +## in zero + # TODO: convert to demo +%!xtest +%! min = [-1, 2]; +%! assert (ga (struct ("fitnessfcn", @(x) rastriginsfcn (x - min), "nvars", 2, "options", gaoptimset ("FitnessLimit", 1e-7, "Generations", 1000, "PopInitRange", [-5; 5], "PopulationSize", 200))), min, 1e-5) + + +## This code shows that the "Vectorize" option usually speeds up execution + # TODO: convert to demo +%!test +%! +%! tic (); +%! ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("Generations", 10, "PopulationSize", 200))); +%! elapsed_time = toc (); +%! +%! tic (); +%! ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("Generations", 10, "PopulationSize", 200, "Vectorized", "on"))); +%! elapsed_time_with_vectorized = toc (); +%! +%! assert (elapsed_time > elapsed_time_with_vectorized); + +## The "UseParallel" option should speed up execution + # TODO: write demo (after implementing + # UseParallel) - low priority This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |