Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.GP.StructureIdentification/StandardGP.cs @ 1251

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

Improved !StandardGP and !OffspringSelectionGP variants for classification and time-series. #224 (Simple frontend for GP for non-expert users (similar to HeurisicLab.SGA))

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