Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1393 was 1347, checked in by gkronber, 16 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
RevLine 
[1156]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;
[1287]36using HeuristicLab.Selection.OffspringSelection;
[1347]37using HeuristicLab.Evolutionary;
[1156]38
39namespace HeuristicLab.GP.StructureIdentification {
[1287]40  public class OffspringSelectionGP : StandardGP {
[1156]41
[1287]42    public virtual int MaxEvaluatedSolutions {
43      get { return GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data; }
44      set { GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data = value; }
[1156]45    }
46
[1287]47    public virtual double SelectionPressureLimit {
48      get { return GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data; }
49      set { GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data = value; }
[1156]50    }
51
[1287]52    public virtual double ComparisonFactor {
53      get { return GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data; }
54      set { GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data = value; }
[1156]55    }
56
[1287]57    public virtual double SuccessRatioLimit {
58      get { return GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data; }
59      set { GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data = value; }
[1156]60    }
61
[1287]62    public override int MaxGenerations {
63      get { throw new NotSupportedException(); }
64      set { /* ignore */ }
[1156]65    }
66
[1287]67    public override int TournamentSize {
68      get { throw new NotSupportedException(); }
69      set { /* ignore */ }
[1156]70    }
71
[1287]72    public OffspringSelectionGP()
73      : base() {
[1156]74      PopulationSize = 1000;
[1287]75      Parents = 20;
76      MaxEvaluatedSolutions = 1000000;
77      SelectionPressureLimit = 300;
78      ComparisonFactor = 1.0;
79      SuccessRatioLimit = 1.0;
[1156]80    }
81
[1287]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()));
[1156]90      return injector;
91    }
92
[1287]93    protected internal override IOperator CreateSelector() {
94      CombinedOperator selector = new CombinedOperator();
95      selector.Name = "Selector";
[1156]96      SequentialProcessor seq = new SequentialProcessor();
[1287]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;
[1156]102
[1287]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();
[1156]109
[1287]110      seqSubScopesProc.AddSubOperator(femaleSelector);
111      seqSubScopesProc.AddSubOperator(emptyOp);
[1156]112
[1287]113      seq.AddSubOperator(maleSelector);
114      seq.AddSubOperator(seqSubScopesProc);
115      seq.AddSubOperator(rightChildReducer);
116      seq.AddSubOperator(mixer);
[1156]117
[1287]118      selector.OperatorGraph.AddOperator(seq);
119      selector.OperatorGraph.InitialOperator = seq;
120      return selector;
[1156]121    }
122
[1287]123    protected internal override IOperator CreateChildCreater() {
[1156]124      CombinedOperator childCreater = new CombinedOperator();
[1287]125      childCreater.Name = "Create children";
[1347]126      SequentialProcessor main = new SequentialProcessor();
[1156]127      SequentialProcessor seq = new SequentialProcessor();
[1287]128      SequentialProcessor offspringSelectionSeq = new SequentialProcessor();
[1156]129      OperatorExtractor selector = new OperatorExtractor();
130      selector.Name = "Selector (extr.)";
131      selector.GetVariableInfo("Operator").ActualName = "Selector";
[1287]132      SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
133      EmptyOperator emptyOp = new EmptyOperator();
134      OffspringSelector offspringSelector = new OffspringSelector();
[1347]135      ChildrenInitializer childInitializer = new ChildrenInitializer();
136      UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
137      SequentialProcessor individualSeqProc = new SequentialProcessor();
[1156]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";
[1347]151      WeightedOffspringFitnessComparer offspringFitnessComparer = new WeightedOffspringFitnessComparer();
152      SubScopesRemover parentScopesRemover = new SubScopesRemover();
[1156]153
154      Sorter sorter = new Sorter();
155      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
156      sorter.GetVariableInfo("Value").ActualName = "Quality";
157
[1287]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";
[1156]164
[1347]165      main.AddSubOperator(seq);
166      seq.AddSubOperator(selector);
167      seq.AddSubOperator(seqSubScopesProc);
[1287]168      seqSubScopesProc.AddSubOperator(emptyOp);
[1347]169      seqSubScopesProc.AddSubOperator(offspringSelectionSeq);
170      seq.AddSubOperator(offspringSelector);
171      offspringSelector.AddSubOperator(seq);
[1287]172
[1347]173      offspringSelectionSeq.AddSubOperator(childInitializer);
174      offspringSelectionSeq.AddSubOperator(individualProc);
175      offspringSelectionSeq.AddSubOperator(sorter);
[1287]176
[1156]177      individualProc.AddSubOperator(individualSeqProc);
[1347]178      individualSeqProc.AddSubOperator(crossover);
[1156]179      individualSeqProc.AddSubOperator(cond);
180      cond.AddSubOperator(manipulator);
181      individualSeqProc.AddSubOperator(evaluator);
182      individualSeqProc.AddSubOperator(evalCounter);
[1347]183      individualSeqProc.AddSubOperator(offspringFitnessComparer);
184      individualSeqProc.AddSubOperator(parentScopesRemover);
[1156]185
[1287]186      SequentialSubScopesProcessor seqSubScopesProc2 = new SequentialSubScopesProcessor();
[1347]187      main.AddSubOperator(seqSubScopesProc2);
[1287]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
[1347]197      childCreater.OperatorGraph.AddOperator(main);
198      childCreater.OperatorGraph.InitialOperator = main;
[1156]199      return childCreater;
200    }
201
[1287]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;
[1156]228    }
229
[1287]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
[1156]265    public override IView CreateView() {
266      return new OffspringSelectionGpEditor(this);
267    }
268
269    public override object Clone(IDictionary<Guid, object> clonedObjects) {
[1287]270      OffspringSelectionGP clone = (OffspringSelectionGP)base.Clone(clonedObjects);
271      clone.SelectionPressureLimit = SelectionPressureLimit;
272      clone.SuccessRatioLimit = SuccessRatioLimit;
273      clone.ComparisonFactor = ComparisonFactor;
[1156]274      return clone;
275    }
276  }
277}
Note: See TracBrowser for help on using the repository browser.