1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.SGA {
|
---|
35 | /// <summary>
|
---|
36 | /// A standard genetic algorithm.
|
---|
37 | /// </summary>
|
---|
38 | [Item("SGA", "A standard genetic algorithm.")]
|
---|
39 | [Creatable("Algorithms")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class SGA : EngineAlgorithm {
|
---|
42 | #region Problem Properties
|
---|
43 | public override Type ProblemType {
|
---|
44 | get { return typeof(ISingleObjectiveProblem); }
|
---|
45 | }
|
---|
46 | public new ISingleObjectiveProblem Problem {
|
---|
47 | get { return (ISingleObjectiveProblem)base.Problem; }
|
---|
48 | set { base.Problem = value; }
|
---|
49 | }
|
---|
50 | #endregion
|
---|
51 |
|
---|
52 | #region Parameter Properties
|
---|
53 | private ValueParameter<IntValue> SeedParameter {
|
---|
54 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
55 | }
|
---|
56 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
57 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
58 | }
|
---|
59 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
60 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
61 | }
|
---|
62 | private ConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
63 | get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
64 | }
|
---|
65 | private ConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
66 | get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
67 | }
|
---|
68 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
69 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
70 | }
|
---|
71 | private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
72 | get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
73 | }
|
---|
74 | private ValueParameter<IntValue> ElitesParameter {
|
---|
75 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
76 | }
|
---|
77 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
78 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
79 | }
|
---|
80 | #endregion
|
---|
81 |
|
---|
82 | #region Properties
|
---|
83 | public IntValue Seed {
|
---|
84 | get { return SeedParameter.Value; }
|
---|
85 | set { SeedParameter.Value = value; }
|
---|
86 | }
|
---|
87 | public BoolValue SetSeedRandomly {
|
---|
88 | get { return SetSeedRandomlyParameter.Value; }
|
---|
89 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
90 | }
|
---|
91 | public IntValue PopulationSize {
|
---|
92 | get { return PopulationSizeParameter.Value; }
|
---|
93 | set { PopulationSizeParameter.Value = value; }
|
---|
94 | }
|
---|
95 | public ISelector Selector {
|
---|
96 | get { return SelectorParameter.Value; }
|
---|
97 | set { SelectorParameter.Value = value; }
|
---|
98 | }
|
---|
99 | public ICrossover Crossover {
|
---|
100 | get { return CrossoverParameter.Value; }
|
---|
101 | set { CrossoverParameter.Value = value; }
|
---|
102 | }
|
---|
103 | public PercentValue MutationProbability {
|
---|
104 | get { return MutationProbabilityParameter.Value; }
|
---|
105 | set { MutationProbabilityParameter.Value = value; }
|
---|
106 | }
|
---|
107 | public IManipulator Mutator {
|
---|
108 | get { return MutatorParameter.Value; }
|
---|
109 | set { MutatorParameter.Value = value; }
|
---|
110 | }
|
---|
111 | public IntValue Elites {
|
---|
112 | get { return ElitesParameter.Value; }
|
---|
113 | set { ElitesParameter.Value = value; }
|
---|
114 | }
|
---|
115 | public IntValue MaximumGenerations {
|
---|
116 | get { return MaximumGenerationsParameter.Value; }
|
---|
117 | set { MaximumGenerationsParameter.Value = value; }
|
---|
118 | }
|
---|
119 | private RandomCreator RandomCreator {
|
---|
120 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
121 | }
|
---|
122 | private SolutionsCreator SolutionsCreator {
|
---|
123 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
124 | }
|
---|
125 | private SGAMainLoop SGAMainLoop {
|
---|
126 | get { return (SGAMainLoop)SolutionsCreator.Successor; }
|
---|
127 | }
|
---|
128 | private List<ISelector> selectors;
|
---|
129 | private IEnumerable<ISelector> Selectors {
|
---|
130 | get { return selectors; }
|
---|
131 | }
|
---|
132 | #endregion
|
---|
133 |
|
---|
134 | public SGA()
|
---|
135 | : base() {
|
---|
136 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
137 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
138 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
139 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
140 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
141 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
142 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
143 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
144 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
145 |
|
---|
146 | RandomCreator randomCreator = new RandomCreator();
|
---|
147 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
148 | SGAMainLoop sgaMainLoop = new SGAMainLoop();
|
---|
149 | OperatorGraph.InitialOperator = randomCreator;
|
---|
150 |
|
---|
151 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
152 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
153 | randomCreator.SeedParameter.Value = null;
|
---|
154 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
155 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
156 | randomCreator.Successor = solutionsCreator;
|
---|
157 |
|
---|
158 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
159 | solutionsCreator.Successor = sgaMainLoop;
|
---|
160 |
|
---|
161 | sgaMainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
162 | sgaMainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
163 | sgaMainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
164 | sgaMainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
165 | sgaMainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
166 | sgaMainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
167 | sgaMainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
168 | sgaMainLoop.ResultsParameter.ActualName = "Results";
|
---|
169 |
|
---|
170 | Initialze();
|
---|
171 | }
|
---|
172 | [StorableConstructor]
|
---|
173 | private SGA(bool deserializing) : base() { }
|
---|
174 |
|
---|
175 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
176 | SGA clone = (SGA)base.Clone(cloner);
|
---|
177 | clone.Initialze();
|
---|
178 | return clone;
|
---|
179 | }
|
---|
180 |
|
---|
181 | #region Events
|
---|
182 | protected override void OnProblemChanged() {
|
---|
183 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
184 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
185 | ParameterizeStochasticOperator(Problem.Visualizer);
|
---|
186 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
187 | ParameterizeSolutionsCreator();
|
---|
188 | ParameterizeSGAMainLoop();
|
---|
189 | ParameterizeSelectors();
|
---|
190 | UpdateCrossovers();
|
---|
191 | UpdateMutators();
|
---|
192 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
193 | base.OnProblemChanged();
|
---|
194 | }
|
---|
195 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
196 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
197 | ParameterizeSolutionsCreator();
|
---|
198 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
199 | }
|
---|
200 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
201 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
202 | ParameterizeSolutionsCreator();
|
---|
203 | ParameterizeSGAMainLoop();
|
---|
204 | ParameterizeSelectors();
|
---|
205 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
206 | base.Problem_EvaluatorChanged(sender, e);
|
---|
207 | }
|
---|
208 | protected override void Problem_VisualizerChanged(object sender, EventArgs e) {
|
---|
209 | ParameterizeStochasticOperator(Problem.Visualizer);
|
---|
210 | ParameterizeSGAMainLoop();
|
---|
211 | base.Problem_VisualizerChanged(sender, e);
|
---|
212 | }
|
---|
213 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
214 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
215 | UpdateCrossovers();
|
---|
216 | UpdateMutators();
|
---|
217 | base.Problem_OperatorsChanged(sender, e);
|
---|
218 | }
|
---|
219 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
220 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
221 | ParameterizeSelectors();
|
---|
222 | }
|
---|
223 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
224 | ParameterizeSelectors();
|
---|
225 | }
|
---|
226 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
227 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
228 | ParameterizeSelectors();
|
---|
229 | }
|
---|
230 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
231 | ParameterizeSelectors();
|
---|
232 | }
|
---|
233 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
234 | ParameterizeSGAMainLoop();
|
---|
235 | ParameterizeSelectors();
|
---|
236 | }
|
---|
237 | #endregion
|
---|
238 |
|
---|
239 | #region Helpers
|
---|
240 | [StorableHook(HookType.AfterDeserialization)]
|
---|
241 | private void Initialze() {
|
---|
242 | InitializeSelectors();
|
---|
243 | UpdateSelectors();
|
---|
244 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
245 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
246 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
247 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
248 | if (Problem != null)
|
---|
249 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
250 | }
|
---|
251 |
|
---|
252 | private void ParameterizeSolutionsCreator() {
|
---|
253 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
254 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
255 | }
|
---|
256 | private void ParameterizeSGAMainLoop() {
|
---|
257 | SGAMainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
258 | SGAMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
259 | SGAMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
260 | SGAMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
261 | SGAMainLoop.VisualizerParameter.ActualName = Problem.VisualizerParameter.Name;
|
---|
262 | SGAMainLoop.VisualizationParameter.ActualName = Problem.Visualizer.VisualizationParameter.ActualName;
|
---|
263 | }
|
---|
264 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
265 | if (op is IStochasticOperator)
|
---|
266 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
267 | }
|
---|
268 | private void InitializeSelectors() {
|
---|
269 | selectors = new List<ISelector>();
|
---|
270 | if (ApplicationManager.Manager != null) {
|
---|
271 | selectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
|
---|
272 | ParameterizeSelectors();
|
---|
273 | }
|
---|
274 | }
|
---|
275 | private void ParameterizeSelectors() {
|
---|
276 | foreach (ISelector selector in Selectors) {
|
---|
277 | selector.CopySelected = new BoolValue(true);
|
---|
278 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
|
---|
279 | ParameterizeStochasticOperator(selector);
|
---|
280 | }
|
---|
281 | if (Problem != null) {
|
---|
282 | foreach (ISingleObjectiveSelector selector in Selectors.OfType<ISingleObjectiveSelector>()) {
|
---|
283 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
284 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 | private void UpdateSelectors() {
|
---|
289 | if (ApplicationManager.Manager != null) {
|
---|
290 | ISelector oldSelector = SelectorParameter.Value;
|
---|
291 | SelectorParameter.ValidValues.Clear();
|
---|
292 | foreach (ISelector selector in Selectors.OrderBy(x => x.Name))
|
---|
293 | SelectorParameter.ValidValues.Add(selector);
|
---|
294 | if (oldSelector != null) {
|
---|
295 | ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
296 | if (selector != null) SelectorParameter.Value = selector;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | }
|
---|
300 | private void UpdateCrossovers() {
|
---|
301 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
302 | CrossoverParameter.ValidValues.Clear();
|
---|
303 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
304 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
305 | if (oldCrossover != null) {
|
---|
306 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
307 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | private void UpdateMutators() {
|
---|
311 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
312 | MutatorParameter.ValidValues.Clear();
|
---|
313 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
314 | MutatorParameter.ValidValues.Add(mutator);
|
---|
315 | if (oldMutator != null) {
|
---|
316 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
317 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
318 | }
|
---|
319 | }
|
---|
320 | #endregion
|
---|
321 | }
|
---|
322 | }
|
---|