Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented operator to calculate variable impacts based on the log of variable frequencies over the whole GP run and integrated operators into default SGP and OSGP algorithms. #853 (Operator to calculate variable impacts as integral over the relative frequencies of variable references over the whole GP run and the whole population)

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