Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/OffspringSelectionGP.cs @ 2212

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

GP Refactoring: #713

  • added project GP.Operators
  • moved operators from plugin GP to plugin GP.Operators
  • deleted unused constraints
  • removed dependency of GP plugins on Constraints plugin
  • moved StructID functions into directory Symbols
  • deleted unused class FunView
  • implemented add and remove functionality for the FunctionLibraryView
File size: 12.6 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 HeuristicLab.Core;
[2212]25using HeuristicLab.Data;
26using HeuristicLab.Evolutionary;
27using HeuristicLab.Logging;
[1156]28using HeuristicLab.Operators;
29using HeuristicLab.Selection;
[1287]30using HeuristicLab.Selection.OffspringSelection;
[1156]31
32namespace HeuristicLab.GP.StructureIdentification {
[1287]33  public class OffspringSelectionGP : StandardGP {
[1857]34    public override string Name { get { return "OffspringSelectionGP"; } }
[1156]35
[1287]36    public virtual int MaxEvaluatedSolutions {
37      get { return GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data; }
38      set { GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data = value; }
[1156]39    }
40
[1287]41    public virtual double SelectionPressureLimit {
42      get { return GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data; }
43      set { GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data = value; }
[1156]44    }
45
[1287]46    public virtual double ComparisonFactor {
47      get { return GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data; }
48      set { GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data = value; }
[1156]49    }
50
[1287]51    public virtual double SuccessRatioLimit {
52      get { return GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data; }
53      set { GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data = value; }
[1156]54    }
55
[1287]56    public override int MaxGenerations {
57      get { throw new NotSupportedException(); }
58      set { /* ignore */ }
[1156]59    }
60
[1287]61    public override int TournamentSize {
62      get { throw new NotSupportedException(); }
63      set { /* ignore */ }
[1156]64    }
65
[1287]66    public OffspringSelectionGP()
67      : base() {
[1156]68      PopulationSize = 1000;
[1287]69      Parents = 20;
[2130]70      MaxEvaluatedSolutions = 5000000;
71      SelectionPressureLimit = 400;
[1287]72      ComparisonFactor = 1.0;
73      SuccessRatioLimit = 1.0;
[1156]74    }
75
[1287]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()));
[1156]84      return injector;
85    }
86
[1287]87    protected internal override IOperator CreateSelector() {
88      CombinedOperator selector = new CombinedOperator();
89      selector.Name = "Selector";
[1156]90      SequentialProcessor seq = new SequentialProcessor();
[1287]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;
[1156]96
[1287]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();
[1156]103
[1287]104      seqSubScopesProc.AddSubOperator(femaleSelector);
105      seqSubScopesProc.AddSubOperator(emptyOp);
[1156]106
[1287]107      seq.AddSubOperator(maleSelector);
108      seq.AddSubOperator(seqSubScopesProc);
109      seq.AddSubOperator(rightChildReducer);
110      seq.AddSubOperator(mixer);
[1156]111
[1287]112      selector.OperatorGraph.AddOperator(seq);
113      selector.OperatorGraph.InitialOperator = seq;
114      return selector;
[1156]115    }
116
[1287]117    protected internal override IOperator CreateChildCreater() {
[1156]118      CombinedOperator childCreater = new CombinedOperator();
[1287]119      childCreater.Name = "Create children";
[1347]120      SequentialProcessor main = new SequentialProcessor();
[1156]121      SequentialProcessor seq = new SequentialProcessor();
[1287]122      SequentialProcessor offspringSelectionSeq = new SequentialProcessor();
[1156]123      OperatorExtractor selector = new OperatorExtractor();
124      selector.Name = "Selector (extr.)";
125      selector.GetVariableInfo("Operator").ActualName = "Selector";
[1287]126      SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
127      EmptyOperator emptyOp = new EmptyOperator();
128      OffspringSelector offspringSelector = new OffspringSelector();
[1347]129      ChildrenInitializer childInitializer = new ChildrenInitializer();
130      UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
131      SequentialProcessor individualSeqProc = new SequentialProcessor();
[1156]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";
[1347]145      WeightedOffspringFitnessComparer offspringFitnessComparer = new WeightedOffspringFitnessComparer();
146      SubScopesRemover parentScopesRemover = new SubScopesRemover();
[1156]147
148      Sorter sorter = new Sorter();
149      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
150      sorter.GetVariableInfo("Value").ActualName = "Quality";
151
[1287]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";
[1156]158
[1347]159      main.AddSubOperator(seq);
160      seq.AddSubOperator(selector);
161      seq.AddSubOperator(seqSubScopesProc);
[1287]162      seqSubScopesProc.AddSubOperator(emptyOp);
[1347]163      seqSubScopesProc.AddSubOperator(offspringSelectionSeq);
164      seq.AddSubOperator(offspringSelector);
165      offspringSelector.AddSubOperator(seq);
[1287]166
[1347]167      offspringSelectionSeq.AddSubOperator(childInitializer);
168      offspringSelectionSeq.AddSubOperator(individualProc);
169      offspringSelectionSeq.AddSubOperator(sorter);
[1287]170
[1156]171      individualProc.AddSubOperator(individualSeqProc);
[1347]172      individualSeqProc.AddSubOperator(crossover);
[1156]173      individualSeqProc.AddSubOperator(cond);
174      cond.AddSubOperator(manipulator);
175      individualSeqProc.AddSubOperator(evaluator);
176      individualSeqProc.AddSubOperator(evalCounter);
[1347]177      individualSeqProc.AddSubOperator(offspringFitnessComparer);
178      individualSeqProc.AddSubOperator(parentScopesRemover);
[1156]179
[1287]180      SequentialSubScopesProcessor seqSubScopesProc2 = new SequentialSubScopesProcessor();
[1347]181      main.AddSubOperator(seqSubScopesProc2);
[1287]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
[1347]191      childCreater.OperatorGraph.AddOperator(main);
192      childCreater.OperatorGraph.InitialOperator = main;
[1156]193      return childCreater;
194    }
195
[1287]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;
[1156]222    }
223
[1287]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
[1156]259    public override IView CreateView() {
260      return new OffspringSelectionGpEditor(this);
261    }
262
263    public override object Clone(IDictionary<Guid, object> clonedObjects) {
[1287]264      OffspringSelectionGP clone = (OffspringSelectionGP)base.Clone(clonedObjects);
265      clone.SelectionPressureLimit = SelectionPressureLimit;
266      clone.SuccessRatioLimit = SuccessRatioLimit;
267      clone.ComparisonFactor = ComparisonFactor;
[1156]268      return clone;
269    }
270  }
271}
Note: See TracBrowser for help on using the repository browser.