]> Creatis software - CreaPhase.git/blob - octave_packages/ga-0.10.0/gaoptimset.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / ga-0.10.0 / gaoptimset.m
1 ## Copyright (C) 2008, 2009, 2010 Luca Favatella <slackydeb@gmail.com>
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn{Function File} {@var{options} =} gaoptimset
18 ## @deftypefnx{Function File} {@var{options} =} gaoptimset ('@var{param1}', @var{value1}, '@var{param2}', @var{value2}, @dots{})
19 ## Create genetic algorithm options structure.
20 ##
21 ## @strong{Inputs}
22 ## @table @var
23 ## @item param
24 ## Parameter to set. Unspecified parameters are set to their default
25 ## values; specifying no parameters is allowed.
26 ## @item value
27 ## Value of @var{param}.
28 ## @end table
29 ##
30 ## @strong{Outputs}
31 ## @table @var
32 ## @item options
33 ## Structure containing the options, or parameters, for the genetic algorithm.
34 ## @end table
35 ##
36 ## @strong{Options}
37 ## @table @code
38 ## @item CreationFcn
39 ## @item CrossoverFcn
40 ## @item CrossoverFraction
41 ## @item EliteCount
42 ## @item FitnessLimit
43 ## @item FitnessScalingFcn
44 ## @item Generations
45 ## @item InitialPopulation
46 ##       Can be partial.
47 ## @item InitialScores
48 ##       column vector | [] (default) . Can be partial.
49 ## @item MutationFcn
50 ## @item PopInitRange
51 ## @item PopulationSize
52 ## @item SelectionFcn
53 ## @item TimeLimit
54 ## @item UseParallel
55 ##       "always" | "never" (default) . Parallel evaluation of objective function. TODO: parallel evaluation of nonlinear constraints
56 ## @item Vectorized
57 ##       "on" | "off" (default) . Vectorized evaluation of objective function. TODO: vectorized evaluation of nonlinear constraints
58 ## @end table
59 ##
60 ## @seealso{ga}
61 ## @end deftypefn
62
63 ## Author: Luca Favatella <slackydeb@gmail.com>
64 ## Version: 4.4.7
65
66 function options = gaoptimset (varargin)
67   if ((nargout != 1) ||
68       (mod (length (varargin), 2) == 1))
69     print_usage ();
70   else
71
72     ## initialize the return variable to a structure with all parameters
73     ## set to their default value
74     options = __gaoptimset_default_options__ ();
75
76     ## set custom options
77     for i = 1:2:length (varargin)
78       param = varargin{i};
79       value = varargin{i + 1};
80       if (! isfield (options, param))
81         error ("wrong parameter");
82       else
83         options = setfield (options, param, value);
84       endif
85     endfor
86   endif
87 endfunction
88
89
90 ## number of input arguments
91 %!error options = gaoptimset ("odd number of arguments")
92 %!error options = gaoptimset ("Generations", 123, "odd number of arguments")
93
94 ## number of output arguments
95 %!error gaoptimset ("Generations", 123)
96 %!error [a, b] = gaoptimset ("Generations", 123)
97
98 ## type of arguments
99 # TODO
100 %!#error options = gaoptimset ("Vectorized", "bad value") # TODO: fix
101 %!#error options = gaoptimset ("UseParallel", "bad value") # TODO: fix
102
103 # TODO: structure/add tests below
104
105 %!assert (getfield (gaoptimset ("Generations", 123), "Generations"), 123)
106
107 %!test
108 %! options = gaoptimset ("EliteCount", 1,
109 %!                       "FitnessLimit", 1e-7,
110 %!                       "Generations", 1000,
111 %!                       "PopInitRange", [-5; 5],
112 %!                       "PopulationSize", 200);
113 %!
114 %! ## "CrossoverFraction" is not specified, so gaoptimset should put the default value: testing this too
115 %! assert ([(getfield (options, "CrossoverFraction"));
116 %!          (getfield (options, "EliteCount"));
117 %!          (getfield (options, "FitnessLimit"));
118 %!          (getfield (options, "Generations"));
119 %!          (getfield (options, "PopInitRange"));
120 %!          (getfield (options, "PopulationSize"))],
121 %!         [0.8; 1; 1e-7; 1000; [-5; 5]; 200])