Mercurial > forge
changeset 9710:7df2e3ca6195 octave-forge
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
author | slackydeb |
---|---|
date | Thu, 15 Mar 2012 02:23:29 +0000 |
parents | 374d67bbed1f |
children | d221e75701b0 |
files | main/ga/inst/ga.m main/ga/inst/ga_demo.m |
diffstat | 2 files changed, 42 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- a/main/ga/inst/ga.m Thu Mar 15 02:23:15 2012 +0000 +++ b/main/ga/inst/ga.m Thu Mar 15 02:23:29 2012 +0000 @@ -71,7 +71,7 @@ ## @end deftypefn ## Author: Luca Favatella <slackydeb@gmail.com> -## Version: 5.21.1 +## Version: 6.0.0 function [x fval exitflag output population scores] = \ ga (fitnessfcn_or_problem, @@ -115,44 +115,20 @@ endfunction -## nvars == 2 and min != zeros (1, nvars) +## number of arguments + +## type 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) +%!test x = ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("FitnessLimit", 1e-7, "Generations", 1000))) + +%!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))) ## nvars == 1 and min == zeros (1, nvars) -%!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 == 2 and min == zeros (1, nvars) - -## 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) +%!test assert (ga (@(x) x ** 2, 1), 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) +%!test assert (ga (@(x) (x ** 2) - (cos (2 * pi * x)) + 1, 1), 0, 1e-3); ## InitialPopulation and InitialScores options @@ -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
--- a/main/ga/inst/ga_demo.m Thu Mar 15 02:23:15 2012 +0000 +++ b/main/ga/inst/ga_demo.m Thu Mar 15 02:23:29 2012 +0000 @@ -21,10 +21,42 @@ ## Author: Luca Favatella <slackydeb@gmail.com> ## 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