From: <sla...@us...> - 2008-08-05 12:24:37
|
Revision: 5215 http://octave.svn.sourceforge.net/octave/?rev=5215&view=rev Author: slackydeb Date: 2008-08-05 12:24:46 +0000 (Tue, 05 Aug 2008) Log Message: ----------- semplified the code Modified Paths: -------------- trunk/octave-forge/main/ga/DESCRIPTION trunk/octave-forge/main/ga/inst/ga.m trunk/octave-forge/main/ga/inst/gaoptimset.m Modified: trunk/octave-forge/main/ga/DESCRIPTION =================================================================== --- trunk/octave-forge/main/ga/DESCRIPTION 2008-08-05 10:23:22 UTC (rev 5214) +++ trunk/octave-forge/main/ga/DESCRIPTION 2008-08-05 12:24:46 UTC (rev 5215) @@ -1,6 +1,6 @@ Name: ga -Version: 0.4.3 -Date: 2008-08-03 +Version: 0.4.4 +Date: 2008-08-05 Author: Luca Favatella <sla...@gm...> Maintainer: Luca Favatella <sla...@gm...> Title: Genetic Algorithm and Direct Search Modified: trunk/octave-forge/main/ga/inst/ga.m =================================================================== --- trunk/octave-forge/main/ga/inst/ga.m 2008-08-05 10:23:22 UTC (rev 5214) +++ trunk/octave-forge/main/ga/inst/ga.m 2008-08-05 12:24:46 UTC (rev 5215) @@ -74,7 +74,7 @@ ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 5.8 +## Version: 5.8.1 function [x, fval, exitflag, output, population, scores] = \ ga (fitnessfcn_or_problem, @@ -117,16 +117,18 @@ endif endfunction -%!assert (ga (@rastriginsfcn, 2), [0, 0], 1e-6) +%!function retval = test_parabola (x) +%! retval = x ** 2; -%!function retval = test_4_variabili (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; +%!assert (ga (@test_parabola, 1, [], [], [], [], [], [], [], gaoptimset ('CrossoverFcn', @crossoversinglepoint, 'EliteCount', 1, 'FitnessLimit', 0.001, 'Generations', 10, 'PopInitRange', [-1; 1])), 0, sqrt(0.001)) -%!assert (ga (@test_4_variabili, 4, [], [], [], [], [], [], [], gaoptimset ('FitnessLimit', 0.001, 'PopInitRange', [-1; 1])), [0, 0, 0, 0], sqrt(0.001)) +%!function retval = test_f_con_inf_minimi_locali (x) +%! retval = (x ** 2) - (cos (2 * pi * x)) + 1; +%!assert (ga (@test_f_con_inf_minimi_locali, 1, [], [], [], [], [], [], [], gaoptimset ('CrossoverFcn', @crossoversinglepoint, 'EliteCount', 1, 'FitnessLimit', 0.001, 'Generations', 25, 'PopInitRange', [-5; 5])), 0, sqrt(0.001)) + +%!assert (ga (@rastriginsfcn, 2), [0, 0], 1e-6) + %!function retval = test_rastriginsfcn_traslato (t) %! min = [1, 0]; %! x = t - min; @@ -134,12 +136,10 @@ %!assert (ga (@test_rastriginsfcn_traslato, 2, [], [], [], [], [], [], [], gaoptimset ('FitnessLimit', 0.001, 'PopInitRange', [-2; 2], 'PopulationSize', 100)), [1, 0], sqrt(0.001)) -%!function retval = test_f_con_inf_minimi_locali (x) -%! retval = (x ** 2) - (cos (2 * pi * x)) + 1; +%!function retval = test_4_variabili (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; -%!assert (ga (@test_f_con_inf_minimi_locali, 1, [], [], [], [], [], [], [], gaoptimset ('CrossoverFcn', @crossoversinglepoint, 'EliteCount', 1, 'FitnessLimit', 0.001, 'Generations', 25, 'PopInitRange', [-5; 5])), 0, sqrt(0.001)) - -%!function retval = test_parabola (x) -%! retval = x ** 2; - -%!assert (ga (@test_parabola, 1, [], [], [], [], [], [], [], gaoptimset ('CrossoverFcn', @crossoversinglepoint, 'EliteCount', 1, 'FitnessLimit', 0.001, 'Generations', 10, 'PopInitRange', [-1; 1])), 0, sqrt(0.001)) \ No newline at end of file +%!assert (ga (@test_4_variabili, 4, [], [], [], [], [], [], [], gaoptimset ('FitnessLimit', 0.001, 'PopInitRange', [-1; 1])), [0, 0, 0, 0], sqrt(0.001)) \ No newline at end of file Modified: trunk/octave-forge/main/ga/inst/gaoptimset.m =================================================================== --- trunk/octave-forge/main/ga/inst/gaoptimset.m 2008-08-05 10:23:22 UTC (rev 5214) +++ trunk/octave-forge/main/ga/inst/gaoptimset.m 2008-08-05 12:24:46 UTC (rev 5215) @@ -54,7 +54,7 @@ ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 4.0 +## Version: 4.3 function options = gaoptimset (varargin) if ((nargout != 1) || @@ -62,77 +62,20 @@ print_usage (); else - ## structure with all default fields - default_options = __gaoptimset_default_options__ (); + ## initialize the return variable to a structure with all parameters + ## set to their default value + options = __gaoptimset_default_options__ (); - ## set options as specified - i = 1; - while (length (varargin) >= (i + 1)) - switch (varargin{i}) - case "CreationFcn" - options.CreationFcn = varargin{i + 1}; - case "CrossoverFcn" - options.CrossoverFcn = varargin{i + 1}; - case "CrossoverFraction" - options.CrossoverFraction = varargin{i + 1}; - case "EliteCount" - options.EliteCount = varargin{i + 1}; - case "FitnessLimit" - options.FitnessLimit = varargin{i + 1}; - case "Generations" - options.Generations = varargin{i + 1}; - case "MutationFcn" - options.MutationFcn = varargin{i + 1}; - case "PopInitRange" - options.PopInitRange = varargin{i + 1}; - case "PopulationSize" - options.PopulationSize = varargin{i + 1}; - case "SelectionFcn" - options.SelectionFcn = varargin{i + 1}; - endswitch + ## set custom options + for i = 1:2:length (varargin) + param = varargin{i}; + value = varargin{i + 1}; + if (! isfield (options, param)) + error ("wrong parameter"); + else + options = setfield (options, param, value); + endif i = i + 2; - endwhile - - ## set not specified options at default values - if ((! exist ("options", "var")) || - (! isfield (options, 'CreationFcn'))) - options.CreationFcn = default_options.CreationFcn; - endif - if ((! exist ("options", "var")) || - (! isfield (options, 'CrossoverFcn'))) - options.CrossoverFcn = default_options.CrossoverFcn; - endif - if ((! exist ("options", "var")) || - (! isfield (options, 'CrossoverFraction'))) - options.CrossoverFraction = default_options.CrossoverFraction; - endif - if ((! exist ("options", "var")) || - (! isfield (options, 'EliteCount'))) - options.EliteCount = default_options.EliteCount; - endif - if ((! exist ("options", "var")) || - (! isfield (options, 'FitnessLimit'))) - options.FitnessLimit = default_options.FitnessLimit; - endif - if ((! exist ("options", "var")) || - (! isfield (options, 'Generations'))) - options.Generations = default_options.Generations; - endif - if ((! exist ("options", "var")) || - (! isfield (options, 'MutationFcn'))) - options.MutationFcn = default_options.MutationFcn; - endif - if ((! exist ("options", "var")) || - (! isfield (options, 'PopInitRange'))) - options.PopInitRange = default_options.PopInitRange; - endif - if ((! exist ("options", "var")) || - (! isfield (options, 'PopulationSize'))) - options.PopulationSize = default_options.PopulationSize; - endif - if ((! exist ("options", "var")) || - (! isfield (options, 'SelectionFcn'))) - options.SelectionFcn = default_options.SelectionFcn; - endif + endfor endif 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. |