From: <sla...@us...> - 2008-08-08 16:10:38
|
Revision: 5231 http://octave.svn.sourceforge.net/octave/?rev=5231&view=rev Author: slackydeb Date: 2008-08-08 16:10:40 +0000 (Fri, 08 Aug 2008) Log Message: ----------- improve creation functions and tests Modified Paths: -------------- trunk/octave-forge/main/ga/DESCRIPTION trunk/octave-forge/main/ga/inst/__ga_problem__.m trunk/octave-forge/main/ga/inst/__ga_set_initial_population__.m trunk/octave-forge/main/ga/inst/crossoverscattered.m trunk/octave-forge/main/ga/inst/crossoversinglepoint.m Modified: trunk/octave-forge/main/ga/DESCRIPTION =================================================================== --- trunk/octave-forge/main/ga/DESCRIPTION 2008-08-08 11:56:59 UTC (rev 5230) +++ trunk/octave-forge/main/ga/DESCRIPTION 2008-08-08 16:10:40 UTC (rev 5231) @@ -1,5 +1,5 @@ Name: ga -Version: 0.5.3 +Version: 0.5.4 Date: 2008-08-08 Author: Luca Favatella <sla...@gm...> Maintainer: Luca Favatella <sla...@gm...> Modified: trunk/octave-forge/main/ga/inst/__ga_problem__.m =================================================================== --- trunk/octave-forge/main/ga/inst/__ga_problem__.m 2008-08-08 11:56:59 UTC (rev 5230) +++ trunk/octave-forge/main/ga/inst/__ga_problem__.m 2008-08-08 16:10:40 UTC (rev 5231) @@ -17,11 +17,15 @@ ## 02110-1301, USA. ## Author: Luca Favatella <sla...@gm...> -## Version: 4.4 +## Version: 4.4.2 function [x fval exitflag output population scores] = __ga_problem__ (problem) individui_migliori = []; - popolazione = __ga_set_initial_population__ (problem); + popolazione = __ga_set_initial_population__ (problem.nvars, + problem.fitnessfcn, + problem.options); + #TODO + #consider InitialScores for state structure %% in this while, generation is fixed generazione = 1; ## TODO initial generation should be 0 (for state structure) Modified: trunk/octave-forge/main/ga/inst/__ga_set_initial_population__.m =================================================================== --- trunk/octave-forge/main/ga/inst/__ga_set_initial_population__.m 2008-08-08 11:56:59 UTC (rev 5230) +++ trunk/octave-forge/main/ga/inst/__ga_set_initial_population__.m 2008-08-08 16:10:40 UTC (rev 5231) @@ -17,55 +17,63 @@ ## 02110-1301, USA. ## -*- texinfo -*- -## @deftypefn{Function File} {@var{Population} =} __ga_set_initial_population__ (@var{problem}) +## @deftypefn{Function File} {@var{Population} =} __ga_set_initial_population__ (@var{GenomeLength}, @var{FitnessFcn}, @var{options}) ## Create an initial population. ## ## @seealso{__ga_problem__} ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 1.1.1 +## Version: 2.0.10 -function Population = __ga_set_initial_population__ (problem) +function Population = \ + __ga_set_initial_population__ (GenomeLength, FitnessFcn, options) #TODO - # - #consider InitialScores - # #consider PopulationSize as a vector for #multiple subpopolations - - [nr, nc] = size (problem.options.InitialPopulation); - + [nr, nc] = size (options.InitialPopulation); if (nc == 0) - Population = problem.options.CreationFcn (problem.nvars, - problem.fitnessfcn, - problem.options); - elseif (nc == problem.nvars) + Population = 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 - if (nr < problem.options.PopulationSize) + if (nr < options.PopulationSize) OptionsWithModifiedPopulationSize = \ - setfield (problem.options, + setfield (options, "PopulationSize", - problem.options.PopulationSize - nr); + options.PopulationSize - nr); CreatedPartialPopulation = \ - problem.options.CreationFcn (problem.nvars, - problem.fitnessfcn, - OptionsWithModifiedPopulationSize); + options.CreationFcn (GenomeLength, + FitnessFcn, + OptionsWithModifiedPopulationSize); Population = \ - vertcat (problem.options.InitialPopulation, - CreatedPartialPopulation); - elseif (nr == problem.options.PopulationSize) - Population = problem.options.InitialPopulation; - else ## nr > problem.options.PopulationSize + vertcat (options.InitialPopulation(1:nr, + 1:GenomeLength), + CreatedPartialPopulation(1:(options.PopulationSize - nr), + 1:GenomeLength)); + elseif (nr == options.PopulationSize) + Population = options.InitialPopulation; + else ## nr > options.PopulationSize error ("nonempty 'InitialPopulation' must have no more than \ 'PopulationSize' rows"); endif else - error ("nonempty 'InitialPopulation' must have 'number of \ - variables' columns"); + error ("nonempty 'InitialPopulation' must have 'GenomeLength' \ + columns"); endif -endfunction \ No newline at end of file +endfunction + +%!test +%! GenomeLength = 2; +%! options = gaoptimset (); +%! Population = __ga_set_initial_population__ (GenomeLength, @rastriginsfcn, options); +%! assert (size (Population), [options.PopulationSize, GenomeLength]); + +%!test +%! GenomeLength = 2; +%! options = gaoptimset ("InitialPopulation", [1, 2; 3, 4; 5, 6]); +%! Population = __ga_set_initial_population__ (GenomeLength, @rastriginsfcn, options); +%! assert (size (Population), [options.PopulationSize, GenomeLength]); \ No newline at end of file Modified: trunk/octave-forge/main/ga/inst/crossoverscattered.m =================================================================== --- trunk/octave-forge/main/ga/inst/crossoverscattered.m 2008-08-08 11:56:59 UTC (rev 5230) +++ trunk/octave-forge/main/ga/inst/crossoverscattered.m 2008-08-08 16:10:40 UTC (rev 5231) @@ -24,7 +24,7 @@ ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 5.1.1 +## Version: 5.1.3 function xoverKids = \ crossoverscattered (parents, @@ -44,12 +44,13 @@ xoverKids = child1; endfunction -%!test -%! parents = [0 4.3 51 -6; 3 -34 5 64.212]; +%!shared nvars, xoverKids +%! parents = [3.2 -34 51 64.21; 3.2 -34 51 64.21]; %! options = gaoptimset (); %! nvars = 4; %! FitnessFcn = false; ## this parameter is unused in the current implementation %! unused = false; %! thisPopulation = false; ## this parameter is unused in the current implementation %! xoverKids = crossoverscattered (parents, options, nvars, FitnessFcn, unused, thisPopulation); -%! assert (size (xoverKids), [1, nvars]); \ No newline at end of file +%!assert (size (xoverKids), [1, nvars]); +%!assert (xoverKids(1, 1:nvars), [3.2 -34 51 64.21]); \ No newline at end of file Modified: trunk/octave-forge/main/ga/inst/crossoversinglepoint.m =================================================================== --- trunk/octave-forge/main/ga/inst/crossoversinglepoint.m 2008-08-08 11:56:59 UTC (rev 5230) +++ trunk/octave-forge/main/ga/inst/crossoversinglepoint.m 2008-08-08 16:10:40 UTC (rev 5231) @@ -24,7 +24,7 @@ ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 5.1.1 +## Version: 5.1.2 function xoverKids = \ crossoversinglepoint (parents, @@ -45,12 +45,13 @@ xoverKids = child1; endfunction -%!test -%! parents = [0 4.3 51 -6; 3 -34 5 64.212]; +%!shared nvars, xoverKids +%! parents = [3.2 -34 51 64.21; 3.2 -34 51 64.21]; %! options = gaoptimset (); %! nvars = 4; %! FitnessFcn = false; ## this parameter is unused in the current implementation %! unused = false; %! thisPopulation = false; ## this parameter is unused in the current implementation -%! xoverKids = crossoversinglepoint (parents, options, nvars, FitnessFcn, unused, thisPopulation); -%! assert (size (xoverKids), [1, nvars]); \ No newline at end of file +%! xoverKids = crossoverscattered (parents, options, nvars, FitnessFcn, unused, thisPopulation); +%!assert (size (xoverKids), [1, nvars]); +%!assert (xoverKids(1, 1:nvars), [3.2 -34 51 64.21]); \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |