]> Creatis software - CreaPhase.git/blob - octave_packages/ga-0.10.0/__ga_initial_population__.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / ga-0.10.0 / __ga_initial_population__.m
1 ## Copyright (C) 2008, 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{Population} =} __ga_initial_population__ (@var{GenomeLength}, @var{FitnessFcn}, @var{options})
18 ## Create an initial population.
19 ##
20 ## @seealso{__ga_problem__}
21 ## @end deftypefn
22
23 ## Author: Luca Favatella <slackydeb@gmail.com>
24 ## Version: 3.3
25
26                                 #TODO consider PopulationSize as a
27                                 #vector for multiple subpopolations
28
29 function Population = \
30       __ga_initial_population__ (GenomeLength, FitnessFcn, options)
31   if (isempty (options.InitialPopulation))
32     Population(1:options.PopulationSize, 1:GenomeLength) = \
33         options.CreationFcn (GenomeLength, FitnessFcn, options);
34   else
35     if (columns (options.InitialPopulation) != GenomeLength) ## columns (InitialPopulation) > 0
36       error ("nonempty 'InitialPopulation' must have 'GenomeLength' columns");
37     else ## rows (InitialPopulation) > 0
38       nrIP = rows (options.InitialPopulation);
39       if (nrIP > options.PopulationSize)
40         error ("nonempty 'InitialPopulation' must have no more than \
41             'PopulationSize' rows");
42       elseif (nrIP == options.PopulationSize)
43         Population(1:options.PopulationSize, 1:GenomeLength) = \
44             options.InitialPopulation;
45       else # rows (InitialPopulation) < PopulationSize
46
47         ## create a complete new population, and then select only needed
48         ## individuals (creating only a partial population is difficult)
49         CreatedPopulation(1:options.PopulationSize, 1:GenomeLength) = \
50             options.CreationFcn (GenomeLength, FitnessFcn, options);
51         Population(1:options.PopulationSize, 1:GenomeLength) = vertcat \
52             (options.InitialPopulation(1:nrIP, 1:GenomeLength),
53              CreatedPopulation(1:(options.PopulationSize - nrIP),
54                                1:GenomeLength));
55       endif
56     endif
57   endif
58 endfunction
59
60
61 %!test
62 %! GenomeLength = 2;
63 %! options = gaoptimset ();
64 %! Population = __ga_initial_population__ (GenomeLength, @rastriginsfcn, options);
65 %! assert (size (Population), [options.PopulationSize, GenomeLength]);
66
67 %!test
68 %! GenomeLength = 2;
69 %! options = gaoptimset ("InitialPopulation", [1, 2; 3, 4; 5, 6]);
70 %! Population = __ga_initial_population__ (GenomeLength, @rastriginsfcn, options);
71 %! assert (size (Population), [options.PopulationSize, GenomeLength]);
72
73
74 ## nonempty 'InitialPopulation' must have 'GenomeLength' columns
75
76 %!error
77 %! GenomeLength = 2;
78 %! options = gaoptimset ("InitialPopulation", [1; 3; 5]);
79 %! __ga_initial_population__ (GenomeLength, @rastriginsfcn, options);
80
81 %!error
82 %! GenomeLength = 2;
83 %! options = gaoptimset ("InitialPopulation", [1, 1, 1; 3, 3, 3; 5, 5, 5]);
84 %! __ga_initial_population__ (GenomeLength, @rastriginsfcn, options);
85
86
87 ## nonempty 'InitialPopulation' must have no more than 'PopulationSize' rows
88
89 %!error
90 %! GenomeLength = 2;
91 %! options = gaoptimset ("InitialPopulation", [1, 2; 3, 4; 5, 6], "PopulationSize", 2);
92 %! __ga_initial_population__ (GenomeLength, @rastriginsfcn, options);