Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.GP.StructureIdentification/3.3/AlgorithmBase.cs @ 2498

Last change on this file since 2498 was 1922, checked in by gkronber, 16 years ago

#650 (IAlgorithm and derived interfaces should provide properties to retrieve results):

  • Implemented properties to retrieve model quality
  • Changed CEDMA executor to retrieve results via properties
  • Removed obsolete class Execution in CEDMA (replaced by the interface IAlgorithm)
File size: 21.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;
36using HeuristicLab.Evolutionary;
37using HeuristicLab.Modeling;
38
39namespace HeuristicLab.GP.StructureIdentification {
40  public abstract class AlgorithmBase : ItemBase, IAlgorithm, IStochasticAlgorithm {
41    public virtual string Name { get { return "GP"; } }
42    public virtual string Description { get { return "TODO"; } }
43
44    public abstract Dataset Dataset { get; set; }
45    public abstract int TargetVariable { get; set; }
46
47    public virtual double MutationRate {
48      get { return GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data; }
49      set { GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data = value; }
50    }
51    public virtual int PopulationSize {
52      get { return GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data; }
53      set { GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data = value; }
54    }
55
56    public virtual bool SetSeedRandomly {
57      get { return GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data; }
58      set { GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data = value; }
59    }
60
61    public virtual int RandomSeed {
62      get { return GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data; }
63      set { GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data = value; }
64    }
65
66    public virtual IOperator ProblemInjector {
67      get { return algorithm.SubOperators[0]; }
68      set {
69        value.Name = "ProblemInjector";
70        algorithm.RemoveSubOperator(0);
71        algorithm.AddSubOperator(value, 0);
72      }
73    }
74
75    public virtual IModel Model {
76      get {
77        if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
78        IScope bestModelScope = engine.GlobalScope.GetVariableValue<IScope>("BestValidationSolution", false);
79        return CreateGPModel(bestModelScope);
80      }
81    }
82
83    public virtual int Elites {
84      get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; }
85      set { GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data = value; }
86    }
87
88    public virtual int MaxTreeSize {
89      get { return GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data; }
90      set { GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data = value; }
91    }
92
93    public virtual int MaxTreeHeight {
94      get { return GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data; }
95      set { GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data = value; }
96    }
97
98    public virtual int Parents {
99      get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; }
100      set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; }
101    }
102
103    public virtual double PunishmentFactor {
104      get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
105      set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
106    }
107
108    public virtual bool UseEstimatedTargetValue {
109      get { return GetVariableInjector().GetVariable("UseEstimatedTargetValue").GetValue<BoolData>().Data; }
110      set { GetVariableInjector().GetVariable("UseEstimatedTargetValue").GetValue<BoolData>().Data = value; }
111    }
112
113    private IOperator algorithm;
114
115    private SequentialEngine.SequentialEngine engine;
116    public IEngine Engine {
117      get { return engine; }
118      protected set { engine = (SequentialEngine.SequentialEngine)value; }
119    }
120
121    public AlgorithmBase() {
122      engine = new SequentialEngine.SequentialEngine();
123      CombinedOperator algo = CreateAlgorithm();
124      engine.OperatorGraph.AddOperator(algo);
125      engine.OperatorGraph.InitialOperator = algo;
126      SetSeedRandomly = true;
127      Elites = 1;
128      MutationRate = 0.15;
129      PopulationSize = 1000;
130      MaxTreeSize = 100;
131      MaxTreeHeight = 10;
132      Parents = 2000;
133      PunishmentFactor = 10;
134      UseEstimatedTargetValue = false;
135    }
136
137    protected internal virtual CombinedOperator CreateAlgorithm() {
138      CombinedOperator algo = new CombinedOperator();
139      algo.Name = "GP";
140      SequentialProcessor seq = new SequentialProcessor();
141      IOperator problemInjector = CreateProblemInjector();
142
143      RandomInjector randomInjector = new RandomInjector();
144      randomInjector.Name = "Random Injector";
145
146      IOperator globalInjector = CreateGlobalInjector();
147      IOperator initialization = CreateInitialization();
148      IOperator funLibInjector = CreateFunctionLibraryInjector();
149      IOperator treeEvaluatorInjector = new HL2TreeEvaluatorInjector();
150
151      IOperator mainLoop = CreateMainLoop();
152      mainLoop.Name = "Main loop";
153
154      IOperator treeCreator = CreateTreeCreator();
155
156      MeanSquaredErrorEvaluator evaluator = new MeanSquaredErrorEvaluator();
157      evaluator.GetVariableInfo("MSE").ActualName = "Quality";
158      evaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
159      evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
160      evaluator.Name = "Evaluator";
161
162      IOperator crossover = CreateCrossover();
163      IOperator manipulator = CreateManipulator();
164
165      IOperator selector = CreateSelector();
166      LeftReducer cleanUp = new LeftReducer();
167
168      seq.AddSubOperator(problemInjector);
169      seq.AddSubOperator(randomInjector);
170      seq.AddSubOperator(globalInjector);
171      seq.AddSubOperator(funLibInjector);
172      seq.AddSubOperator(treeEvaluatorInjector);
173      seq.AddSubOperator(initialization);
174      seq.AddSubOperator(mainLoop);
175      seq.AddSubOperator(cleanUp);
176
177      initialization.AddSubOperator(treeCreator);
178      initialization.AddSubOperator(evaluator);
179
180      mainLoop.AddSubOperator(selector);
181      mainLoop.AddSubOperator(crossover);
182      mainLoop.AddSubOperator(manipulator);
183      mainLoop.AddSubOperator(evaluator);
184      algo.OperatorGraph.AddOperator(seq);
185      algo.OperatorGraph.InitialOperator = seq;
186      this.algorithm = seq;
187      return algo;
188    }
189
190    protected internal virtual IOperator CreateProblemInjector() {
191      return new EmptyOperator();
192    }
193
194    protected internal abstract IOperator CreateSelector();
195
196    protected internal abstract IOperator CreateCrossover();
197
198    protected internal abstract IOperator CreateTreeCreator();
199
200    protected internal abstract IOperator CreateFunctionLibraryInjector();
201
202    protected internal virtual IOperator CreateGlobalInjector() {
203      VariableInjector injector = new VariableInjector();
204      injector.Name = "Global Injector";
205      injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
206      injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", new DoubleData()));
207      injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", new IntData()));
208      injector.AddVariable(new HeuristicLab.Core.Variable("Elites", new IntData()));
209      injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
210      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData()));
211      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData()));
212      injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
213      injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
214      injector.AddVariable(new HeuristicLab.Core.Variable("Parents", new IntData()));
215      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
216      injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData()));
217      return injector;
218    }
219
220    protected internal abstract IOperator CreateManipulator();
221
222    protected internal virtual IOperator CreateInitialization() {
223      CombinedOperator init = new CombinedOperator();
224      init.Name = "Initialization";
225      SequentialProcessor seq = new SequentialProcessor();
226      SubScopesCreater subScopesCreater = new SubScopesCreater();
227      subScopesCreater.GetVariableInfo("SubScopes").ActualName = "PopulationSize";
228      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
229      SequentialProcessor individualSeq = new SequentialProcessor();
230      OperatorExtractor treeCreater = new OperatorExtractor();
231      treeCreater.Name = "Tree generator (extr.)";
232      treeCreater.GetVariableInfo("Operator").ActualName = "Tree generator";
233      OperatorExtractor evaluator = new OperatorExtractor();
234      evaluator.Name = "Evaluator (extr.)";
235      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
236      MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
237      validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
238      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
239      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
240      Counter evalCounter = new Counter();
241      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
242      Sorter sorter = new Sorter();
243      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
244      sorter.GetVariableInfo("Value").ActualName = "Quality";
245
246      seq.AddSubOperator(subScopesCreater);
247      seq.AddSubOperator(subScopesProc);
248      seq.AddSubOperator(sorter);
249
250      subScopesProc.AddSubOperator(individualSeq);
251      individualSeq.AddSubOperator(treeCreater);
252      individualSeq.AddSubOperator(evaluator);
253      individualSeq.AddSubOperator(validationEvaluator);
254      individualSeq.AddSubOperator(evalCounter);
255
256      init.OperatorGraph.AddOperator(seq);
257      init.OperatorGraph.InitialOperator = seq;
258      return init;
259    }
260
261    protected internal virtual IOperator CreateMainLoop() {
262      CombinedOperator main = new CombinedOperator();
263      SequentialProcessor seq = new SequentialProcessor();
264      IOperator childCreater = CreateChildCreater();
265      IOperator replacement = CreateReplacement();
266
267      BestSolutionStorer solutionStorer = new BestSolutionStorer();
268      solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
269      solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
270      solutionStorer.AddSubOperator(CreateBestSolutionProcessor());
271
272      BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator();
273      BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator();
274      validationQualityCalculator.Name = "BestAverageWorstValidationQualityCalculator";
275      validationQualityCalculator.GetVariableInfo("Quality").ActualName = "ValidationQuality";
276      validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";
277      validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";
278      validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";
279      IOperator loggingOperator = CreateLoggingOperator();
280      Counter counter = new Counter();
281      counter.GetVariableInfo("Value").ActualName = "Generations";
282      IOperator loopCondition = CreateLoopCondition(seq);
283
284      seq.AddSubOperator(childCreater);
285      seq.AddSubOperator(replacement);
286      seq.AddSubOperator(solutionStorer);
287      seq.AddSubOperator(qualityCalculator);
288      seq.AddSubOperator(validationQualityCalculator);
289      seq.AddSubOperator(loggingOperator);
290      seq.AddSubOperator(counter);
291      seq.AddSubOperator(loopCondition);
292
293      main.OperatorGraph.AddOperator(seq);
294      main.OperatorGraph.InitialOperator = seq;
295      return main;
296    }
297
298    protected internal virtual IOperator CreateLoggingOperator() {
299      return new EmptyOperator();
300    }
301
302    protected internal virtual IOperator CreateLoopCondition(IOperator loop) {
303      SequentialProcessor seq = new SequentialProcessor();
304      seq.Name = "Loop Condition";
305      LessThanComparator comparator = new LessThanComparator();
306      comparator.GetVariableInfo("LeftSide").ActualName = "Generations";
307      comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations";
308      comparator.GetVariableInfo("Result").ActualName = "GenerationsCondition";
309      ConditionalBranch cond = new ConditionalBranch();
310      cond.GetVariableInfo("Condition").ActualName = "GenerationsCondition";
311
312      seq.AddSubOperator(comparator);
313      seq.AddSubOperator(cond);
314
315      cond.AddSubOperator(loop);
316      return seq;
317    }
318
319    protected internal virtual IOperator CreateBestSolutionProcessor() {
320      return new EmptyOperator();
321    }
322
323    protected internal virtual IOperator CreateReplacement() {
324      CombinedOperator replacement = new CombinedOperator();
325      replacement.Name = "Replacement";
326      SequentialProcessor seq = new SequentialProcessor();
327      SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor();
328      SequentialProcessor selectedProc = new SequentialProcessor();
329      LeftSelector leftSelector = new LeftSelector();
330      leftSelector.GetVariableInfo("Selected").ActualName = "Elites";
331      RightReducer rightReducer = new RightReducer();
332
333      SequentialProcessor remainingProc = new SequentialProcessor();
334      RightSelector rightSelector = new RightSelector();
335      rightSelector.GetVariableInfo("Selected").ActualName = "Elites";
336      LeftReducer leftReducer = new LeftReducer();
337      MergingReducer mergingReducer = new MergingReducer();
338      Sorter sorter = new Sorter();
339      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
340      sorter.GetVariableInfo("Value").ActualName = "Quality";
341
342      seq.AddSubOperator(seqScopeProc);
343      seqScopeProc.AddSubOperator(selectedProc);
344      selectedProc.AddSubOperator(leftSelector);
345      selectedProc.AddSubOperator(rightReducer);
346
347      seqScopeProc.AddSubOperator(remainingProc);
348      remainingProc.AddSubOperator(rightSelector);
349      remainingProc.AddSubOperator(leftReducer);
350      seq.AddSubOperator(mergingReducer);
351      seq.AddSubOperator(sorter);
352      replacement.OperatorGraph.AddOperator(seq);
353      replacement.OperatorGraph.InitialOperator = seq;
354      return replacement;
355    }
356
357    protected internal virtual IOperator CreateChildCreater() {
358      CombinedOperator childCreater = new CombinedOperator();
359      childCreater.Name = "Create children";
360      SequentialProcessor seq = new SequentialProcessor();
361      OperatorExtractor selector = new OperatorExtractor();
362      selector.Name = "Selector (extr.)";
363      selector.GetVariableInfo("Operator").ActualName = "Selector";
364
365      SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor();
366      EmptyOperator emptyOpt = new EmptyOperator();
367      SequentialProcessor selectedProc = new SequentialProcessor();
368      ChildrenInitializer childInitializer = new ChildrenInitializer();
369      ((IntData)childInitializer.GetVariable("ParentsPerChild").Value).Data = 2;
370
371      OperatorExtractor crossover = new OperatorExtractor();
372      crossover.Name = "Crossover (extr.)";
373      crossover.GetVariableInfo("Operator").ActualName = "Crossover";
374      UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
375      SequentialProcessor individualSeqProc = new SequentialProcessor();
376      StochasticBranch cond = new StochasticBranch();
377      cond.GetVariableInfo("Probability").ActualName = "MutationRate";
378      OperatorExtractor manipulator = new OperatorExtractor();
379      manipulator.Name = "Manipulator (extr.)";
380      manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
381      OperatorExtractor evaluator = new OperatorExtractor();
382      evaluator.Name = "Evaluator (extr.)";
383      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
384      MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
385      validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
386      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
387      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
388      Counter evalCounter = new Counter();
389      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
390      SubScopesRemover parentRefRemover = new SubScopesRemover();
391
392      Sorter sorter = new Sorter();
393      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
394      sorter.GetVariableInfo("Value").ActualName = "Quality";
395
396
397      seq.AddSubOperator(selector);
398      seq.AddSubOperator(seqScopesProc);
399      seqScopesProc.AddSubOperator(emptyOpt);
400      seqScopesProc.AddSubOperator(selectedProc);
401      selectedProc.AddSubOperator(childInitializer);
402      selectedProc.AddSubOperator(individualProc);
403      individualProc.AddSubOperator(individualSeqProc);
404      individualSeqProc.AddSubOperator(crossover);
405      individualSeqProc.AddSubOperator(cond);
406      cond.AddSubOperator(manipulator);
407      individualSeqProc.AddSubOperator(evaluator);
408      individualSeqProc.AddSubOperator(validationEvaluator);
409      individualSeqProc.AddSubOperator(evalCounter);
410      individualSeqProc.AddSubOperator(parentRefRemover);
411      selectedProc.AddSubOperator(sorter);
412
413      childCreater.OperatorGraph.AddOperator(seq);
414      childCreater.OperatorGraph.InitialOperator = seq;
415      return childCreater;
416    }
417
418    protected internal virtual Model CreateGPModel(IScope bestModelScope) {
419      Model model = new Model();
420      Dataset ds = bestModelScope.GetVariableValue<Dataset>("Dataset", true);
421      model.Data = bestModelScope.GetVariableValue<IFunctionTree>("FunctionTree", false);
422      model.Dataset = ds;
423      model.TargetVariable = ds.GetVariableName(bestModelScope.GetVariableValue<IntData>("TargetVariable", true).Data);
424      model.TrainingMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("Quality", false).Data;
425      model.ValidationMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("ValidationQuality", false).Data;
426      return model;
427    }
428
429    public override object Clone(IDictionary<Guid, object> clonedObjects) {
430      AlgorithmBase clone = (AlgorithmBase)base.Clone(clonedObjects);
431      clonedObjects.Add(Guid, clone);
432      clone.engine = (SequentialEngine.SequentialEngine)Auxiliary.Clone(Engine, clonedObjects);
433      return clone;
434    }
435
436    protected VariableInjector GetVariableInjector() {
437      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
438      // SequentialProcessor in GP
439      algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
440      return (VariableInjector)algorithm.SubOperators[2];
441    }
442
443    protected RandomInjector GetRandomInjector() {
444      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
445      // SequentialProcessor in GP
446      algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
447      return (RandomInjector)algorithm.SubOperators[1];
448    }
449
450    #region Persistence Methods
451    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
452      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
453      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
454      return node;
455    }
456    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
457      base.Populate(node, restoredObjects);
458      engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
459    }
460    #endregion
461
462
463  }
464}
Note: See TracBrowser for help on using the repository browser.