1 ## Copyright (C) 2008 Luca Favatella <slackydeb@gmail.com>
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.
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.
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/>.
16 ## Author: Luca Favatella <slackydeb@gmail.com>
19 function [x fval exitflag output population scores] = __ga_problem__ (problem)
22 state.StartTime = time ();
25 output = struct ("randstate", rand ("state"),
26 "randnstate", randn ("state"));
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,
34 private_state = __ga_problem_private_state__ (problem.options);
35 state.Selection = __ga_problem_state_selection__ (private_state,
38 ## instructions to be executed at each generation
39 state = __ga_problem_update_state_at_each_generation__ (state, problem,
42 NextPopulation = zeros (problem.options.PopulationSize, problem.nvars);
43 while (! __ga_stop__ (problem, state)) ## fix a generation
46 if (private_state.ReproductionCount.elite > 0)
47 [trash IndexSortedScores] = sort (state.Score);
48 NextPopulation(state.Selection.elite, 1:problem.nvars) = \
50 (IndexSortedScores(1:private_state.ReproductionCount.elite, 1),
54 ## selection for crossover and mutation
55 parents(1, 1:private_state.nParents) = __ga_selectionfcn__ \
56 (state.Expectation, private_state.nParents, problem.options);
59 if (private_state.ReproductionCount.crossover > 0)
60 NextPopulation(state.Selection.crossover, 1:problem.nvars) = \
62 (parents(1, private_state.parentsSelection.crossover),
63 problem.options, problem.nvars, problem.fitnessfcn,
69 if (private_state.ReproductionCount.mutation > 0)
70 NextPopulation(state.Selection.mutation, 1:problem.nvars) = \
72 (parents(1, private_state.parentsSelection.mutation),
73 problem.options, problem.nvars, problem.fitnessfcn,
78 ## update state structure
79 state.Population(1:problem.options.PopulationSize,
80 1:problem.nvars) = NextPopulation;
82 state = __ga_problem_update_state_at_each_generation__ (state, problem,
86 [x fval exitflag output population scores] = \
87 __ga_problem_return_variables__ (state, problem);
90 #state structure fields
91 #DONE state.Population
93 #DONE state.Generation
97 #DONE state.Expectation
99 #state.LastImprovement
100 #state.LastImprovementTime