Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on lose coupling of CEDMA and GP/SVR with plugin HeuristicLab.Modeling as common bridge. #635

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