Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/OffspringSelectionGP.cs @ 1454

Last change on this file since 1454 was 1347, checked in by gkronber, 15 years ago

Fixed hard-coded OffspringGP implementations to work with new crossover architecture. #521 (Hard-coded GP implementations do not work with new crossover operator architecture)

File size: 12.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using System.Diagnostics;
29using HeuristicLab.DataAnalysis;
30using HeuristicLab.Operators;
31using HeuristicLab.Random;
32using HeuristicLab.Selection;
33using HeuristicLab.Logging;
34using HeuristicLab.Data;
35using HeuristicLab.Operators.Programmable;
36using HeuristicLab.Selection.OffspringSelection;
37using HeuristicLab.Evolutionary;
38
39namespace HeuristicLab.GP.StructureIdentification {
40  public class OffspringSelectionGP : StandardGP {
41
42    public virtual int MaxEvaluatedSolutions {
43      get { return GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data; }
44      set { GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data = value; }
45    }
46
47    public virtual double SelectionPressureLimit {
48      get { return GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data; }
49      set { GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data = value; }
50    }
51
52    public virtual double ComparisonFactor {
53      get { return GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data; }
54      set { GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data = value; }
55    }
56
57    public virtual double SuccessRatioLimit {
58      get { return GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data; }
59      set { GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data = value; }
60    }
61
62    public override int MaxGenerations {
63      get { throw new NotSupportedException(); }
64      set { /* ignore */ }
65    }
66
67    public override int TournamentSize {
68      get { throw new NotSupportedException(); }
69      set { /* ignore */ }
70    }
71
72    public OffspringSelectionGP()
73      : base() {
74      PopulationSize = 1000;
75      Parents = 20;
76      MaxEvaluatedSolutions = 1000000;
77      SelectionPressureLimit = 300;
78      ComparisonFactor = 1.0;
79      SuccessRatioLimit = 1.0;
80    }
81
82    protected internal override IOperator CreateGlobalInjector() {
83      VariableInjector injector = (VariableInjector)base.CreateGlobalInjector();
84      injector.RemoveVariable("TournamentSize");
85      injector.RemoveVariable("MaxGenerations");
86      injector.AddVariable(new HeuristicLab.Core.Variable("MaxEvaluatedSolutions", new IntData()));
87      injector.AddVariable(new HeuristicLab.Core.Variable("ComparisonFactor", new DoubleData()));
88      injector.AddVariable(new HeuristicLab.Core.Variable("SelectionPressureLimit", new DoubleData()));
89      injector.AddVariable(new HeuristicLab.Core.Variable("SuccessRatioLimit", new DoubleData()));
90      return injector;
91    }
92
93    protected internal override IOperator CreateSelector() {
94      CombinedOperator selector = new CombinedOperator();
95      selector.Name = "Selector";
96      SequentialProcessor seq = new SequentialProcessor();
97      seq.Name = "Selector";
98      EmptyOperator emptyOp = new EmptyOperator();
99      ProportionalSelector femaleSelector = new ProportionalSelector();
100      femaleSelector.GetVariableInfo("Selected").ActualName = "Parents";
101      femaleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
102
103      RandomSelector maleSelector = new RandomSelector();
104      maleSelector.GetVariableInfo("Selected").ActualName = "Parents";
105      maleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
106      SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
107      RightChildReducer rightChildReducer = new RightChildReducer();
108      SubScopesMixer mixer = new SubScopesMixer();
109
110      seqSubScopesProc.AddSubOperator(femaleSelector);
111      seqSubScopesProc.AddSubOperator(emptyOp);
112
113      seq.AddSubOperator(maleSelector);
114      seq.AddSubOperator(seqSubScopesProc);
115      seq.AddSubOperator(rightChildReducer);
116      seq.AddSubOperator(mixer);
117
118      selector.OperatorGraph.AddOperator(seq);
119      selector.OperatorGraph.InitialOperator = seq;
120      return selector;
121    }
122
123    protected internal override IOperator CreateChildCreater() {
124      CombinedOperator childCreater = new CombinedOperator();
125      childCreater.Name = "Create children";
126      SequentialProcessor main = new SequentialProcessor();
127      SequentialProcessor seq = new SequentialProcessor();
128      SequentialProcessor offspringSelectionSeq = new SequentialProcessor();
129      OperatorExtractor selector = new OperatorExtractor();
130      selector.Name = "Selector (extr.)";
131      selector.GetVariableInfo("Operator").ActualName = "Selector";
132      SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
133      EmptyOperator emptyOp = new EmptyOperator();
134      OffspringSelector offspringSelector = new OffspringSelector();
135      ChildrenInitializer childInitializer = new ChildrenInitializer();
136      UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
137      SequentialProcessor individualSeqProc = new SequentialProcessor();
138      OperatorExtractor crossover = new OperatorExtractor();
139      crossover.Name = "Crossover (extr.)";
140      crossover.GetVariableInfo("Operator").ActualName = "Crossover";
141      StochasticBranch cond = new StochasticBranch();
142      cond.GetVariableInfo("Probability").ActualName = "MutationRate";
143      OperatorExtractor manipulator = new OperatorExtractor();
144      manipulator.Name = "Manipulator (extr.)";
145      manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
146      OperatorExtractor evaluator = new OperatorExtractor();
147      evaluator.Name = "Evaluator (extr.)";
148      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
149      Counter evalCounter = new Counter();
150      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
151      WeightedOffspringFitnessComparer offspringFitnessComparer = new WeightedOffspringFitnessComparer();
152      SubScopesRemover parentScopesRemover = new SubScopesRemover();
153
154      Sorter sorter = new Sorter();
155      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
156      sorter.GetVariableInfo("Value").ActualName = "Quality";
157
158      UniformSequentialSubScopesProcessor validationEvaluator = new UniformSequentialSubScopesProcessor();
159      MeanSquaredErrorEvaluator validationQualityEvaluator = new MeanSquaredErrorEvaluator();
160      validationQualityEvaluator.Name = "ValidationMeanSquaredErrorEvaluator";
161      validationQualityEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
162      validationQualityEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
163      validationQualityEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
164
165      main.AddSubOperator(seq);
166      seq.AddSubOperator(selector);
167      seq.AddSubOperator(seqSubScopesProc);
168      seqSubScopesProc.AddSubOperator(emptyOp);
169      seqSubScopesProc.AddSubOperator(offspringSelectionSeq);
170      seq.AddSubOperator(offspringSelector);
171      offspringSelector.AddSubOperator(seq);
172
173      offspringSelectionSeq.AddSubOperator(childInitializer);
174      offspringSelectionSeq.AddSubOperator(individualProc);
175      offspringSelectionSeq.AddSubOperator(sorter);
176
177      individualProc.AddSubOperator(individualSeqProc);
178      individualSeqProc.AddSubOperator(crossover);
179      individualSeqProc.AddSubOperator(cond);
180      cond.AddSubOperator(manipulator);
181      individualSeqProc.AddSubOperator(evaluator);
182      individualSeqProc.AddSubOperator(evalCounter);
183      individualSeqProc.AddSubOperator(offspringFitnessComparer);
184      individualSeqProc.AddSubOperator(parentScopesRemover);
185
186      SequentialSubScopesProcessor seqSubScopesProc2 = new SequentialSubScopesProcessor();
187      main.AddSubOperator(seqSubScopesProc2);
188      seqSubScopesProc2.AddSubOperator(emptyOp);
189
190      SequentialProcessor newGenProc = new SequentialProcessor();
191      newGenProc.AddSubOperator(sorter);
192      newGenProc.AddSubOperator(validationEvaluator);
193      seqSubScopesProc2.AddSubOperator(newGenProc);
194
195      validationEvaluator.AddSubOperator(validationQualityEvaluator);
196
197      childCreater.OperatorGraph.AddOperator(main);
198      childCreater.OperatorGraph.InitialOperator = main;
199      return childCreater;
200    }
201
202    protected internal override IOperator CreateLoopCondition(IOperator loop) {
203      SequentialProcessor seq = new SequentialProcessor();
204      seq.Name = "Loop Condition";
205      LessThanComparator generationsComparator = new LessThanComparator();
206      generationsComparator.GetVariableInfo("LeftSide").ActualName = "EvaluatedSolutions";
207      generationsComparator.GetVariableInfo("RightSide").ActualName = "MaxEvaluatedSolutions";
208      generationsComparator.GetVariableInfo("Result").ActualName = "EvaluatedSolutionsCondition";
209
210      LessThanComparator selPresComparator = new LessThanComparator();
211      selPresComparator.GetVariableInfo("LeftSide").ActualName = "SelectionPressure";
212      selPresComparator.GetVariableInfo("RightSide").ActualName = "SelectionPressureLimit";
213      selPresComparator.GetVariableInfo("Result").ActualName = "SelectionPressureCondition";
214
215      ConditionalBranch generationsCond = new ConditionalBranch();
216      generationsCond.GetVariableInfo("Condition").ActualName = "EvaluatedSolutionsCondition";
217
218      ConditionalBranch selPresCond = new ConditionalBranch();
219      selPresCond.GetVariableInfo("Condition").ActualName = "SelectionPressureCondition";
220
221      seq.AddSubOperator(generationsComparator);
222      seq.AddSubOperator(selPresComparator);
223      seq.AddSubOperator(generationsCond);
224      generationsCond.AddSubOperator(selPresCond);
225      selPresCond.AddSubOperator(loop);
226
227      return seq;
228    }
229
230    protected internal override IOperator CreateLoggingOperator() {
231      CombinedOperator loggingOperator = new CombinedOperator();
232      loggingOperator.Name = "Logging";
233      SequentialProcessor seq = new SequentialProcessor();
234
235      DataCollector collector = new DataCollector();
236      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
237      names.Add(new StringData("BestQuality"));
238      names.Add(new StringData("AverageQuality"));
239      names.Add(new StringData("WorstQuality"));
240      names.Add(new StringData("BestValidationQuality"));
241      names.Add(new StringData("AverageValidationQuality"));
242      names.Add(new StringData("WorstValidationQuality"));
243      names.Add(new StringData("EvaluatedSolutions"));
244      names.Add(new StringData("SelectionPressure"));
245      QualityLogger qualityLogger = new QualityLogger();
246      QualityLogger validationQualityLogger = new QualityLogger();
247      validationQualityLogger.Name = "ValidationQualityLogger";
248      validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
249      validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
250
251      seq.AddSubOperator(collector);
252      seq.AddSubOperator(qualityLogger);
253      seq.AddSubOperator(validationQualityLogger);
254
255      loggingOperator.OperatorGraph.AddOperator(seq);
256      loggingOperator.OperatorGraph.InitialOperator = seq;
257      return loggingOperator;
258    }
259
260
261    public override IEditor CreateEditor() {
262      return new OffspringSelectionGpEditor(this);
263    }
264
265    public override IView CreateView() {
266      return new OffspringSelectionGpEditor(this);
267    }
268
269    public override object Clone(IDictionary<Guid, object> clonedObjects) {
270      OffspringSelectionGP clone = (OffspringSelectionGP)base.Clone(clonedObjects);
271      clone.SelectionPressureLimit = SelectionPressureLimit;
272      clone.SuccessRatioLimit = SuccessRatioLimit;
273      clone.ComparisonFactor = ComparisonFactor;
274      return clone;
275    }
276  }
277}
Note: See TracBrowser for help on using the repository browser.