From: <sla...@us...> - 2008-08-15 22:47:17
|
Revision: 5244 http://octave.svn.sourceforge.net/octave/?rev=5244&view=rev Author: slackydeb Date: 2008-08-15 22:47:24 +0000 (Fri, 15 Aug 2008) Log Message: ----------- cleaned ga function tests; modified the invocation of the creation function in the __ga_initial_population__ function to prevent bugs (a modified PopulationSize can be misunderstood by the creation function); hardcode some expectations removing asserts Modified Paths: -------------- trunk/octave-forge/main/ga/DESCRIPTION trunk/octave-forge/main/ga/inst/__ga_initial_population__.m trunk/octave-forge/main/ga/inst/__ga_mutationfcn__.m trunk/octave-forge/main/ga/inst/ga.m trunk/octave-forge/main/ga/inst/gacreationuniform.m Modified: trunk/octave-forge/main/ga/DESCRIPTION =================================================================== --- trunk/octave-forge/main/ga/DESCRIPTION 2008-08-15 12:54:44 UTC (rev 5243) +++ trunk/octave-forge/main/ga/DESCRIPTION 2008-08-15 22:47:24 UTC (rev 5244) @@ -1,12 +1,12 @@ Name: ga -Version: 0.9.0 -Date: 2008-08-14 +Version: 0.9.1 +Date: 2008-08-16 Author: Luca Favatella <sla...@gm...> Maintainer: Luca Favatella <sla...@gm...> Title: Genetic Algorithm and Direct Search Description: Genetic optimization code Categories: Optimization -Depends: octave (>= 2.9.7), miscellaneous (>= 1.0.6), communications (>= 1.0.0) +Depends: octave (>= 2.9.7), communications (>= 1.0.0) Autoload: yes License: GPL version 2 or later Url: http://octave.sf.net Modified: trunk/octave-forge/main/ga/inst/__ga_initial_population__.m =================================================================== --- trunk/octave-forge/main/ga/inst/__ga_initial_population__.m 2008-08-15 12:54:44 UTC (rev 5243) +++ trunk/octave-forge/main/ga/inst/__ga_initial_population__.m 2008-08-15 22:47:24 UTC (rev 5244) @@ -21,43 +21,38 @@ ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 3.1 +## Version: 3.2 #TODO consider PopulationSize as a #vector for multiple subpopolations function Population = \ __ga_initial_population__ (GenomeLength, FitnessFcn, options) - [nr, nc] = size (options.InitialPopulation); - if (nc == 0) - Population = options.CreationFcn (GenomeLength, FitnessFcn, options); + [nr nc] = size (options.InitialPopulation); + if ((nr == 0) || (nc == 0)) + Population(1:options.PopulationSize, 1:GenomeLength) = \ + options.CreationFcn (GenomeLength, FitnessFcn, options); elseif (nc == GenomeLength) - ## it is impossible to have a matrix with 0 rows and a positive - ## number of columns - ## - ## so, here nr > 0 + ## nr > 0 if (nr < options.PopulationSize) - OptionsWithModifiedPopulationSize = \ - setfield (options, - "PopulationSize", - options.PopulationSize - nr); - CreatedPartialPopulation = \ - options.CreationFcn (GenomeLength, - FitnessFcn, - OptionsWithModifiedPopulationSize); - Population = \ - vertcat (options.InitialPopulation(1:nr, - 1:GenomeLength), - CreatedPartialPopulation(1:(options.PopulationSize - nr), - 1:GenomeLength)); + + ## create a complete new population, and then select only needed + ## individuals (creating only a partial population is difficult) + CreatedPopulation(1:options.PopulationSize, 1:GenomeLength) = \ + options.CreationFcn (GenomeLength, FitnessFcn, options); + Population(1:options.PopulationSize, 1:GenomeLength) = \ + vertcat (options.InitialPopulation(1:nr, 1:GenomeLength), + CreatedPopulation(1:(options.PopulationSize - nr), + 1:GenomeLength)); elseif (nr == options.PopulationSize) - Population = options.InitialPopulation; + Population(1:options.PopulationSize, 1:GenomeLength) = \ + options.InitialPopulation; else ## nr > options.PopulationSize error ("nonempty 'InitialPopulation' must have no more than \ 'PopulationSize' rows"); endif - else + else ## (nc != 0) && (nc != GenomeLength) error ("nonempty 'InitialPopulation' must have 'GenomeLength' \ columns"); endif Modified: trunk/octave-forge/main/ga/inst/__ga_mutationfcn__.m =================================================================== --- trunk/octave-forge/main/ga/inst/__ga_mutationfcn__.m 2008-08-15 12:54:44 UTC (rev 5243) +++ trunk/octave-forge/main/ga/inst/__ga_mutationfcn__.m 2008-08-15 22:47:24 UTC (rev 5244) @@ -14,7 +14,7 @@ ## along with this program; If not, see <http://www.gnu.org/licenses/>. ## Author: Luca Favatella <sla...@gm...> -## Version: 1.1 +## Version: 1.2.1 function mutationChildren = \ __ga_mutationfcn__ (parents, options, nvars, FitnessFcn, @@ -22,21 +22,15 @@ thisPopulation) ## preconditions - [nr_parents nc_parents] = size (parents); - assert (nr_parents, 1); ## DEBUG #TODO move controls on #options.PopInitRange in a more general #function [nrPopInitRange, ncPopInitRange] = size (options.PopInitRange); assert (nrPopInitRange, 2); ## DEBUG assert ((ncPopInitRange == 1) || (ncPopInitRange == nvars)); ## DEBUG - assert (columns (thisPopulation), nvars); ## DEBUG - mutationChildren = \ - options.MutationFcn{1, 1} (parents, options, nvars, FitnessFcn, + mutationChildren(1:(columns (parents)), 1:nvars) = \ + options.MutationFcn{1, 1} (parents(1, :), options, nvars, FitnessFcn, state, thisScore, - thisPopulation); - - ## postconditions - assert (size (mutationChildren), [nc_parents, nvars]); ## DEBUG + thisPopulation(:, 1:nvars)); endfunction \ No newline at end of file Modified: trunk/octave-forge/main/ga/inst/ga.m =================================================================== --- trunk/octave-forge/main/ga/inst/ga.m 2008-08-15 12:54:44 UTC (rev 5243) +++ trunk/octave-forge/main/ga/inst/ga.m 2008-08-15 22:47:24 UTC (rev 5244) @@ -71,7 +71,7 @@ ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 5.10 +## Version: 5.19.1 function [x fval exitflag output population scores] = \ ga (fitnessfcn_or_problem, @@ -114,26 +114,33 @@ endif endfunction -%!xtest assert (ga (@(x) x ** 2, 1, [], [], [], [], [], [], [], gaoptimset ('EliteCount', 1, 'FitnessLimit', 0.001, 'Generations', 10, 'PopInitRange', [-1; 1])), 0, sqrt(0.001)) +%!# nvars == 2 +%!# min != zeros (1, nvars) -%!function retval = test_pseudo_rastriginsfcn (x) -%! retval = (x ** 2) - (cos (2 * pi * x)) + 1; +%!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-6) -%!xtest assert (ga (@test_pseudo_rastriginsfcn, 1, [], [], [], [], [], [], [], gaoptimset ('EliteCount', 1, 'FitnessLimit', 0.001, 'Generations', 25, 'PopInitRange', [-5; 5])), 0, sqrt(0.001)) -%!xtest assert (ga (@rastriginsfcn, 2), [0, 0], 1e-6) +%!# nvars == 1 +%!# min == zeros (1, nvars) -%!function retval = test_bias_rastriginsfcn (t) -%! min = [1, 0]; -%! x = t - min; -%! retval = 20 + (x(1) ** 2) + (x(2) ** 2) - 10 * (cos (2 * pi * x(1)) + cos (2 * pi * x(2))); +%!test assert (ga (@(x) x ** 2, 1), 0, 1e-3) -%!xtest assert (ga (@test_bias_rastriginsfcn, 2, [], [], [], [], [], [], [], gaoptimset ('FitnessLimit', 0.001, 'PopInitRange', [-2; 2], 'PopulationSize', 100)), [1, 0], sqrt(0.001)) +%!test assert (ga (@(x) (x ** 2) - (cos (2 * pi * x)) + 1, 1), 0, 1e-3) -%!function retval = test_4_variables (x) -%! retval = 0; -%! retval += 20 + (x(1) ** 2) + (x(2) ** 2) - 10 * (cos (2 * pi * x(1)) + cos (2 * pi * x(2))); -%! retval += (x(3) ** 2) - (cos (2 * pi * x(3))) + 1; -%! retval += x(4) ** 2; -%!xtest assert (ga (@test_4_variables, 4, [], [], [], [], [], [], [], gaoptimset ('FitnessLimit', 0.001, 'PopInitRange', [-1; 1])), [0, 0, 0, 0], sqrt(0.001)) \ No newline at end of file +%!# nvars == 2 +%!# min == zeros (1, nvars) + +%!xtest assert (ga (@rastriginsfcn, 2), [0, 0], 1e-3) + +%!xtest assert (ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("FitnessLimit", 1e-7, "Generations", 1000))), zeros (1, 2), 1e-6) + +%!xtest assert (ga (struct ("fitnessfcn", @rastriginsfcn, "nvars", 2, "options", gaoptimset ("FitnessLimit", 1e-7, "PopulationSize", 200))), zeros (1, 2), 1e-6) + + +%!# nvars == 4 +%!# min == zeros (1, nvars) + +%!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-6) \ No newline at end of file Modified: trunk/octave-forge/main/ga/inst/gacreationuniform.m =================================================================== --- trunk/octave-forge/main/ga/inst/gacreationuniform.m 2008-08-15 12:54:44 UTC (rev 5243) +++ trunk/octave-forge/main/ga/inst/gacreationuniform.m 2008-08-15 22:47:24 UTC (rev 5244) @@ -37,37 +37,28 @@ ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 4.5 +## Version: 4.7.2 function Population = gacreationuniform (GenomeLength, FitnessFcn, options) - [nr, nc] = size (options.PopInitRange); - #if ((nr != 2) - # ((nc != 1) && (nc != GenomeLength))) - # error ("'PopInitRange' must be 2-by-1 or 2-by-GenomeLength"); - #endif - ## obtain a 2-by-GenomeLength LocalPopInitRange - LocalPopInitRange = options.PopInitRange; - if (nc == 1) - LocalPopInitRange = LocalPopInitRange * ones (1, GenomeLength); - endif + switch (columns (options.PopInitRange)) + case 1 + LocalPopInitRange(1:2, 1:GenomeLength) = \ + options.PopInitRange(1:2, 1) * ones (1, GenomeLength); + case GenomeLength + LocalPopInitRange(1:2, 1:GenomeLength) = \ + options.PopInitRange(1:2, 1:GenomeLength); + endswitch - LB = LocalPopInitRange(1, 1:GenomeLength); - UB = LocalPopInitRange(2, 1:GenomeLength); + LB(1, 1:GenomeLength) = LocalPopInitRange(1, 1:GenomeLength); + UB(1, 1:GenomeLength) = LocalPopInitRange(2, 1:GenomeLength); ## pseudocode ## ## Population = Delta * RandomPopulationBetween0And1 + Offset - Population = \ + Population(1:options.PopulationSize, 1:GenomeLength) = \ ((ones (options.PopulationSize, 1) * (UB - LB)) .* \ rand (options.PopulationSize, GenomeLength)) + \ (ones (options.PopulationSize, 1) * LB); -endfunction - -%!test -%! GenomeLength = 2; -%! FitnessFcn = @rastriginsfcn; -%! options = gaoptimset (); -%! Population = gacreationuniform (GenomeLength, FitnessFcn, options); -%! assert (size (Population), [options.PopulationSize, GenomeLength]); \ No newline at end of file +endfunction \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |