Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2318 was 2222, checked in by gkronber, 15 years ago

Merged changes from GP-refactoring branch back into the trunk #713.

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