Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.GP.StructureIdentification/3.4/OffspringSelectionGP.cs @ 2044

Last change on this file since 2044 was 1914, checked in by epitzer, 16 years ago

Migration of DataAnalysis, GP, GP.StructureIdentification and Modeling to new Persistence-3.3 (#603)

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