Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Algorithms/3.2/OffspringSelectionGP.cs @ 2395

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

Implemented #762 (ValidationQuality improvment progress as additional stopping criterion for default StructId Engines)

File size: 10.9 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.Algorithms {
33  public class OffspringSelectionGP : AlgorithmBase, IEditable {
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 OffspringSelectionGP()
57      : base() {
58      PopulationSize = 1000;
59      Parents = 20;
60      MaxGenerations = 10000; // something large to make sure we either stop because of max-evaluated soltions or selection pressure limit
61      MaxEvaluatedSolutions = 5000000;
62      SelectionPressureLimit = 400;
63      ComparisonFactor = 1.0;
64      SuccessRatioLimit = 1.0;
65    }
66
67    protected override VariableInjector CreateGlobalInjector() {
68      VariableInjector injector = base.CreateGlobalInjector();
69      injector.AddVariable(new HeuristicLab.Core.Variable("MaxEvaluatedSolutions", new IntData()));
70      injector.AddVariable(new HeuristicLab.Core.Variable("ComparisonFactor", new DoubleData()));
71      injector.AddVariable(new HeuristicLab.Core.Variable("SelectionPressureLimit", new DoubleData()));
72      injector.AddVariable(new HeuristicLab.Core.Variable("SuccessRatioLimit", new DoubleData()));
73      return injector;
74    }
75
76    protected override IOperator CreateSelectionOperator() {
77      CombinedOperator selector = new CombinedOperator();
78      selector.Name = "Selector";
79      SequentialProcessor seq = new SequentialProcessor();
80      seq.Name = "Selector";
81      EmptyOperator emptyOp = new EmptyOperator();
82      ProportionalSelector femaleSelector = new ProportionalSelector();
83      femaleSelector.GetVariableInfo("Selected").ActualName = "Parents";
84      femaleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
85
86      RandomSelector maleSelector = new RandomSelector();
87      maleSelector.GetVariableInfo("Selected").ActualName = "Parents";
88      maleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
89      SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
90      RightChildReducer rightChildReducer = new RightChildReducer();
91      SubScopesMixer mixer = new SubScopesMixer();
92
93      seqSubScopesProc.AddSubOperator(femaleSelector);
94      seqSubScopesProc.AddSubOperator(emptyOp);
95
96      seq.AddSubOperator(maleSelector);
97      seq.AddSubOperator(seqSubScopesProc);
98      seq.AddSubOperator(rightChildReducer);
99      seq.AddSubOperator(mixer);
100
101      selector.OperatorGraph.AddOperator(seq);
102      selector.OperatorGraph.InitialOperator = seq;
103      return selector;
104    }
105
106    protected override IOperator CreateChildCreater() {
107      CombinedOperator childCreater = new CombinedOperator();
108      childCreater.Name = "Create children";
109      SequentialProcessor main = new SequentialProcessor();
110      SequentialProcessor seq = new SequentialProcessor();
111      SequentialProcessor offspringSelectionSeq = new SequentialProcessor();
112      OperatorExtractor selector = new OperatorExtractor();
113      selector.Name = "Selector (extr.)";
114      selector.GetVariableInfo("Operator").ActualName = "Selector";
115      SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
116      EmptyOperator emptyOp = new EmptyOperator();
117      OffspringSelector offspringSelector = new OffspringSelector();
118      ChildrenInitializer childInitializer = new ChildrenInitializer();
119      UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
120      SequentialProcessor individualSeqProc = new SequentialProcessor();
121      OperatorExtractor crossover = new OperatorExtractor();
122      crossover.Name = "Crossover (extr.)";
123      crossover.GetVariableInfo("Operator").ActualName = "Crossover";
124      StochasticBranch cond = new StochasticBranch();
125      cond.GetVariableInfo("Probability").ActualName = "MutationRate";
126      OperatorExtractor manipulator = new OperatorExtractor();
127      manipulator.Name = "Manipulator (extr.)";
128      manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
129      OperatorExtractor evaluator = new OperatorExtractor();
130      evaluator.Name = "Evaluator (extr.)";
131      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
132      Counter evalCounter = new Counter();
133      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
134      WeightedOffspringFitnessComparer offspringFitnessComparer = new WeightedOffspringFitnessComparer();
135      SubScopesRemover parentScopesRemover = new SubScopesRemover();
136
137      Sorter sorter = new Sorter();
138      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
139      sorter.GetVariableInfo("Value").ActualName = "Quality";
140
141      main.AddSubOperator(seq);
142      seq.AddSubOperator(selector);
143      seq.AddSubOperator(seqSubScopesProc);
144      seqSubScopesProc.AddSubOperator(emptyOp);
145      seqSubScopesProc.AddSubOperator(offspringSelectionSeq);
146      seq.AddSubOperator(offspringSelector);
147      offspringSelector.AddSubOperator(seq);
148
149      offspringSelectionSeq.AddSubOperator(childInitializer);
150      offspringSelectionSeq.AddSubOperator(individualProc);
151      offspringSelectionSeq.AddSubOperator(sorter);
152
153      individualProc.AddSubOperator(individualSeqProc);
154      individualSeqProc.AddSubOperator(crossover);
155      individualSeqProc.AddSubOperator(cond);
156      cond.AddSubOperator(manipulator);
157      individualSeqProc.AddSubOperator(evaluator);
158      individualSeqProc.AddSubOperator(evalCounter);
159      individualSeqProc.AddSubOperator(offspringFitnessComparer);
160      individualSeqProc.AddSubOperator(parentScopesRemover);
161
162      SequentialSubScopesProcessor seqSubScopesProc2 = new SequentialSubScopesProcessor();
163      main.AddSubOperator(seqSubScopesProc2);
164      seqSubScopesProc2.AddSubOperator(emptyOp);
165      seqSubScopesProc2.AddSubOperator(sorter);
166
167      childCreater.OperatorGraph.AddOperator(main);
168      childCreater.OperatorGraph.InitialOperator = main;
169      return childCreater;
170    }
171
172    protected override IOperator CreateTerminationCondition() {
173      CombinedOperator terminationCritertion = new CombinedOperator();
174      terminationCritertion.Name = "TerminationCondition";
175      GreaterThanComparator selPresComparator = new GreaterThanComparator();
176      selPresComparator.GetVariableInfo("LeftSide").ActualName = "SelectionPressure";
177      selPresComparator.GetVariableInfo("RightSide").ActualName = "SelectionPressureLimit";
178      selPresComparator.GetVariableInfo("Result").ActualName = "TerminationCriterion";
179
180      IOperator baseAndSelPresTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), selPresComparator);
181
182      GreaterThanComparator evalSolutionsComparer = new GreaterThanComparator();
183      evalSolutionsComparer.GetVariableInfo("LeftSide").ActualName = "EvaluatedSolutions";
184      evalSolutionsComparer.GetVariableInfo("RightSide").ActualName = "MaxEvaluatedSolutions";
185      evalSolutionsComparer.GetVariableInfo("Result").ActualName = "TerminationCriterion";
186
187      IOperator combinedTerminationCritertion = AlgorithmBase.CombineTerminationCriterions(baseAndSelPresTerminationCriterion, evalSolutionsComparer);
188
189      terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCritertion);
190      terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCritertion;
191      return terminationCritertion;
192    }
193
194    protected override IOperator CreateLoggingOperator() {
195      CombinedOperator loggingOperator = new CombinedOperator();
196      loggingOperator.Name = "Logging";
197      SequentialProcessor seq = new SequentialProcessor();
198
199      DataCollector collector = new DataCollector();
200      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
201      names.Add(new StringData("BestQuality"));
202      names.Add(new StringData("AverageQuality"));
203      names.Add(new StringData("WorstQuality"));
204      names.Add(new StringData("EvaluatedSolutions"));
205      names.Add(new StringData("SelectionPressure"));
206      QualityLogger qualityLogger = new QualityLogger();
207      seq.AddSubOperator(collector);
208      seq.AddSubOperator(qualityLogger);
209
210      loggingOperator.OperatorGraph.AddOperator(seq);
211      loggingOperator.OperatorGraph.InitialOperator = seq;
212      return loggingOperator;
213    }
214
215    public virtual IEditor CreateEditor() {
216      return new OffspringSelectionGpEditor(this);
217    }
218
219    public override IView CreateView() {
220      return new OffspringSelectionGpEditor(this);
221    }
222
223    public override object Clone(IDictionary<Guid, object> clonedObjects) {
224      OffspringSelectionGP clone = (OffspringSelectionGP)base.Clone(clonedObjects);
225      clone.SelectionPressureLimit = SelectionPressureLimit;
226      clone.SuccessRatioLimit = SuccessRatioLimit;
227      clone.ComparisonFactor = ComparisonFactor;
228      return clone;
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.