Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGP.cs @ 4783

Last change on this file since 4783 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 15.1 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;
36
37namespace HeuristicLab.GP.StructureIdentification {
38  public class StandardGP : AlgorithmBase, IEditable {
39
40    public virtual int MaxGenerations {
41      get { return GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data; }
42      set { GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data = value; }
43    }
44
45    public virtual int TournamentSize {
46      get { return GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data; }
47      set { GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data = value; }
48    }
49
50    public double FullTreeShakingFactor {
51      get { return GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data; }
52      set { GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data = value; }
53    }
54
55    public double OnePointShakingFactor {
56      get { return GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data; }
57      set { GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data = value; }
58    }
59
60    public int MinInitialTreeSize {
61      get { return GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data; }
62      set { GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data = value; }
63    }
64
65    public override int MaxTreeSize {
66      get {
67        return base.MaxTreeSize;
68      }
69      set {
70        base.MaxTreeSize = value;
71        MinInitialTreeSize = value / 2;
72      }
73    }
74
75    public override int PopulationSize {
76      get {
77        return base.PopulationSize;
78      }
79      set {
80        base.PopulationSize = value;
81        Parents = 2 * value;
82      }
83    }
84
85    public StandardGP()
86      : base() {
87      PopulationSize = 10000;
88      MaxGenerations = 100;
89      TournamentSize = 7;
90      MutationRate = 0.15;
91      Elites = 1;
92      MaxTreeSize = 100;
93      MaxTreeHeight = 10;
94      FullTreeShakingFactor = 0.1;
95      OnePointShakingFactor = 1.0;
96      PunishmentFactor = 10.0;
97      UseEstimatedTargetValue = false;
98      SetSeedRandomly = true;
99    }
100
101    protected internal override IOperator CreateProblemInjector() {
102      return new ProblemInjector();
103    }
104
105    protected internal override IOperator CreateSelector() {
106      TournamentSelector selector = new TournamentSelector();
107      selector.Name = "Selector";
108      selector.GetVariableInfo("Selected").ActualName = "Parents";
109      selector.GetVariableInfo("GroupSize").Local = false;
110      selector.RemoveVariable("GroupSize");
111      selector.GetVariableInfo("GroupSize").ActualName = "TournamentSize";
112      return selector;
113    }
114
115    protected internal override IOperator CreateGlobalInjector() {
116      VariableInjector globalInjector = (VariableInjector)base.CreateGlobalInjector();
117      globalInjector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", new IntData()));
118      globalInjector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", new IntData()));
119      globalInjector.AddVariable(new HeuristicLab.Core.Variable("FullTreeShakingFactor", new DoubleData()));
120      globalInjector.AddVariable(new HeuristicLab.Core.Variable("OnePointShakingFactor", new DoubleData()));
121      globalInjector.AddVariable(new HeuristicLab.Core.Variable("MinInitialTreeSize", new IntData()));
122      return globalInjector;
123    }
124
125    protected internal override IOperator CreateCrossover() {
126      StandardCrossOver crossover = new StandardCrossOver();
127      crossover.Name = "Crossover";
128      crossover.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
129      return crossover;
130    }
131
132    protected internal override IOperator CreateTreeCreator() {
133      ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator();
134      treeCreator.Name = "Tree generator";
135      treeCreator.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
136      treeCreator.GetVariableInfo("MinTreeSize").ActualName = "MinInitialTreeSize";
137      return treeCreator;
138    }
139
140    protected internal override IOperator CreateFunctionLibraryInjector() {
141      return new FunctionLibraryInjector();
142    }
143
144    protected internal override IOperator CreateManipulator() {
145      CombinedOperator manipulator = new CombinedOperator();
146      manipulator.Name = "Manipulator";
147      StochasticMultiBranch multibranch = new StochasticMultiBranch();
148      FullTreeShaker fullTreeShaker = new FullTreeShaker();
149      fullTreeShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
150      fullTreeShaker.GetVariableInfo("ShakingFactor").ActualName = "FullTreeShakingFactor";
151
152      OnePointShaker onepointShaker = new OnePointShaker();
153      onepointShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
154      onepointShaker.GetVariableInfo("ShakingFactor").ActualName = "OnePointShakingFactor";
155      ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation();
156      changeNodeTypeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
157      CutOutNodeManipulation cutOutNodeManipulation = new CutOutNodeManipulation();
158      cutOutNodeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
159      DeleteSubTreeManipulation deleteSubTreeManipulation = new DeleteSubTreeManipulation();
160      deleteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
161      SubstituteSubTreeManipulation substituteSubTreeManipulation = new SubstituteSubTreeManipulation();
162      substituteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
163
164      IOperator[] manipulators = new IOperator[] {
165        onepointShaker, fullTreeShaker,
166        changeNodeTypeManipulation,
167        cutOutNodeManipulation,
168        deleteSubTreeManipulation,
169        substituteSubTreeManipulation};
170
171      DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]);
172      for (int i = 0; i < manipulators.Length; i++) {
173        probabilities.Data[i] = 1.0;
174        multibranch.AddSubOperator(manipulators[i]);
175      }
176      multibranch.GetVariableInfo("Probabilities").Local = true;
177      multibranch.AddVariable(new HeuristicLab.Core.Variable("Probabilities", probabilities));
178
179      manipulator.OperatorGraph.AddOperator(multibranch);
180      manipulator.OperatorGraph.InitialOperator = multibranch;
181      return manipulator;
182    }
183
184    protected internal override IOperator CreateBestSolutionProcessor() {
185      SequentialProcessor bestSolutionProcessor = new SequentialProcessor();
186      MeanSquaredErrorEvaluator testMseEvaluator = new MeanSquaredErrorEvaluator();
187      testMseEvaluator.Name = "TestMeanSquaredErrorEvaluator";
188      testMseEvaluator.GetVariableInfo("MSE").ActualName = "TestQuality";
189      testMseEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
190      testMseEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
191      MeanAbsolutePercentageErrorEvaluator trainingMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
192      trainingMapeEvaluator.Name = "TrainingMapeEvaluator";
193      trainingMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TrainingMAPE";
194      trainingMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
195      trainingMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
196      MeanAbsolutePercentageErrorEvaluator validationMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
197      validationMapeEvaluator.Name = "ValidationMapeEvaluator";
198      validationMapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE";
199      validationMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
200      validationMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
201      MeanAbsolutePercentageErrorEvaluator testMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
202      testMapeEvaluator.Name = "TestMapeEvaluator";
203      testMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TestMAPE";
204      testMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
205      testMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
206      MeanAbsolutePercentageOfRangeErrorEvaluator trainingMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
207      trainingMapreEvaluator.Name = "TrainingMapreEvaluator";
208      trainingMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TrainingMAPRE";
209      trainingMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
210      trainingMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
211      MeanAbsolutePercentageOfRangeErrorEvaluator validationMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
212      validationMapreEvaluator.Name = "ValidationMapreEvaluator";
213      validationMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "ValidationMAPRE";
214      validationMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
215      validationMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
216      MeanAbsolutePercentageOfRangeErrorEvaluator testMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
217      testMapreEvaluator.Name = "TestMapreEvaluator";
218      testMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TestMAPRE";
219      testMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
220      testMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
221      CoefficientOfDeterminationEvaluator trainingR2Evaluator = new CoefficientOfDeterminationEvaluator();
222      trainingR2Evaluator.Name = "TrainingR2Evaluator";
223      trainingR2Evaluator.GetVariableInfo("R2").ActualName = "TrainingR2";
224      trainingR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
225      trainingR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
226      CoefficientOfDeterminationEvaluator validationR2Evaluator = new CoefficientOfDeterminationEvaluator();
227      validationR2Evaluator.Name = "ValidationR2Evaluator";
228      validationR2Evaluator.GetVariableInfo("R2").ActualName = "ValidationR2";
229      validationR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
230      validationR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
231      CoefficientOfDeterminationEvaluator testR2Evaluator = new CoefficientOfDeterminationEvaluator();
232      testR2Evaluator.Name = "TestR2Evaluator";
233      testR2Evaluator.GetVariableInfo("R2").ActualName = "TestR2";
234      testR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
235      testR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
236      ProgrammableOperator progOperator = new ProgrammableOperator();
237      progOperator.RemoveVariableInfo("Result");
238      progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In));
239      progOperator.Code = @"
240int evalSolutions = EvaluatedSolutions.Data;
241scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions)));
242";
243      bestSolutionProcessor.AddSubOperator(testMseEvaluator);
244      bestSolutionProcessor.AddSubOperator(trainingMapeEvaluator);
245      bestSolutionProcessor.AddSubOperator(validationMapeEvaluator);
246      bestSolutionProcessor.AddSubOperator(testMapeEvaluator);
247      bestSolutionProcessor.AddSubOperator(trainingMapreEvaluator);
248      bestSolutionProcessor.AddSubOperator(validationMapreEvaluator);
249      bestSolutionProcessor.AddSubOperator(testMapreEvaluator);
250      bestSolutionProcessor.AddSubOperator(trainingR2Evaluator);
251      bestSolutionProcessor.AddSubOperator(validationR2Evaluator);
252      bestSolutionProcessor.AddSubOperator(testR2Evaluator);
253      bestSolutionProcessor.AddSubOperator(progOperator);
254      return bestSolutionProcessor;
255    }
256
257    protected internal override IOperator CreateLoggingOperator() {
258      CombinedOperator loggingOperator = new CombinedOperator();
259      loggingOperator.Name = "Logging";
260      SequentialProcessor seq = new SequentialProcessor();
261
262      DataCollector collector = new DataCollector();
263      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
264      names.Add(new StringData("BestQuality"));
265      names.Add(new StringData("AverageQuality"));
266      names.Add(new StringData("WorstQuality"));
267      names.Add(new StringData("BestValidationQuality"));
268      names.Add(new StringData("AverageValidationQuality"));
269      names.Add(new StringData("WorstValidationQuality"));
270      LinechartInjector lineChartInjector = new LinechartInjector();
271      lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
272      lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 6;
273      QualityLogger qualityLogger = new QualityLogger();
274      QualityLogger validationQualityLogger = new QualityLogger();
275      validationQualityLogger.Name = "ValidationQualityLogger";
276      validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
277      validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
278
279      seq.AddSubOperator(collector);
280      seq.AddSubOperator(lineChartInjector);
281      seq.AddSubOperator(qualityLogger);
282      seq.AddSubOperator(validationQualityLogger);
283
284      loggingOperator.OperatorGraph.AddOperator(seq);
285      loggingOperator.OperatorGraph.InitialOperator = seq;
286      return loggingOperator;
287    }
288
289    public virtual IEditor CreateEditor() {
290      return new StandardGpEditor(this);
291    }
292
293    public override IView CreateView() {
294      return new StandardGpEditor(this);
295    }
296  }
297}
Note: See TracBrowser for help on using the repository browser.