]> Creatis software - CreaPhase.git/blob - octave_packages/ga-0.10.0/__ga_problem__.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / ga-0.10.0 / __ga_problem__.m
1 ## Copyright (C) 2008 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 ## Author: Luca Favatella <slackydeb@gmail.com>
17 ## Version: 5.9
18
19 function [x fval exitflag output population scores] = __ga_problem__ (problem)
20
21   ## first instruction
22   state.StartTime = time ();
23
24   ## second instruction
25   output = struct ("randstate", rand ("state"),
26                    "randnstate", randn ("state"));
27
28   ## instructions not to be executed at each generation
29   state.Population(1:problem.options.PopulationSize, 1:problem.nvars) = \
30       __ga_initial_population__ (problem.nvars,
31                                  problem.fitnessfcn,
32                                  problem.options);
33   state.Generation = 0;
34   private_state = __ga_problem_private_state__ (problem.options);
35   state.Selection = __ga_problem_state_selection__ (private_state,
36                                                     problem.options);
37
38   ## instructions to be executed at each generation
39   state = __ga_problem_update_state_at_each_generation__ (state, problem,
40                                                           private_state);
41
42   NextPopulation = zeros (problem.options.PopulationSize, problem.nvars);
43   while (! __ga_stop__ (problem, state)) ## fix a generation
44
45     ## elite
46     if (private_state.ReproductionCount.elite > 0)
47       [trash IndexSortedScores] = sort (state.Score);
48       NextPopulation(state.Selection.elite, 1:problem.nvars) = \
49           state.Population \
50           (IndexSortedScores(1:private_state.ReproductionCount.elite, 1),
51            1:problem.nvars);
52     endif
53
54     ## selection for crossover and mutation
55     parents(1, 1:private_state.nParents) = __ga_selectionfcn__ \
56         (state.Expectation, private_state.nParents, problem.options);
57
58     ## crossover
59     if (private_state.ReproductionCount.crossover > 0)
60       NextPopulation(state.Selection.crossover, 1:problem.nvars) = \
61           __ga_crossoverfcn__ \
62           (parents(1, private_state.parentsSelection.crossover),
63            problem.options, problem.nvars, problem.fitnessfcn,
64            false, ## unused
65            state.Population);
66     endif
67
68     ## mutation
69     if (private_state.ReproductionCount.mutation > 0)
70       NextPopulation(state.Selection.mutation, 1:problem.nvars) = \
71           __ga_mutationfcn__ \
72           (parents(1, private_state.parentsSelection.mutation),
73            problem.options, problem.nvars, problem.fitnessfcn,
74            state, state.Score,
75            state.Population);
76     endif
77
78     ## update state structure
79     state.Population(1:problem.options.PopulationSize,
80                      1:problem.nvars) = NextPopulation;
81     state.Generation++;
82     state = __ga_problem_update_state_at_each_generation__ (state, problem,
83                                                             private_state);
84   endwhile
85
86   [x fval exitflag output population scores] = \
87       __ga_problem_return_variables__ (state, problem);
88 endfunction
89
90                                 #state structure fields
91                                 #DONE state.Population
92                                 #DONE state.Score
93                                 #DONE state.Generation
94                                 #DONE state.StartTime
95                                 #state.StopFlag
96                                 #DONE state.Selection
97                                 #DONE state.Expectation
98                                 #DONE state.Best
99                                 #state.LastImprovement
100                                 #state.LastImprovementTime
101                                 #state.NonlinIneq
102                                 #state.NonlinEq