Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.GP.StructureIdentification/3.3/AlgorithmBase.cs @ 2115

Last change on this file since 2115 was 2057, checked in by gkronber, 16 years ago

Improved stability of hive backend for CEDMA. #642 (Hive backend for CEDMA)

File size: 22.3 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
150      IOperator mainLoop = CreateMainLoop();
151      mainLoop.Name = "Main loop";
152
153      IOperator treeCreator = CreateTreeCreator();
154
155      MeanSquaredErrorEvaluator evaluator = new MeanSquaredErrorEvaluator();
156      evaluator.GetVariableInfo("MSE").ActualName = "Quality";
157      evaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
158      evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
159      evaluator.Name = "Evaluator";
160
161      IOperator crossover = CreateCrossover();
162      IOperator manipulator = CreateManipulator();
163
164      IOperator selector = CreateSelector();
165      LeftReducer cleanUp = new LeftReducer();
166
167      seq.AddSubOperator(problemInjector);
168      seq.AddSubOperator(randomInjector);
169      seq.AddSubOperator(globalInjector);
170      seq.AddSubOperator(funLibInjector);
171      seq.AddSubOperator(initialization);
172      seq.AddSubOperator(mainLoop);
173      seq.AddSubOperator(cleanUp);
174
175      initialization.AddSubOperator(treeCreator);
176      initialization.AddSubOperator(evaluator);
177
178      mainLoop.AddSubOperator(selector);
179      mainLoop.AddSubOperator(crossover);
180      mainLoop.AddSubOperator(manipulator);
181      mainLoop.AddSubOperator(evaluator);
182      algo.OperatorGraph.AddOperator(seq);
183      algo.OperatorGraph.InitialOperator = seq;
184      this.algorithm = seq;
185      return algo;
186    }
187
188    protected internal virtual IOperator CreateProblemInjector() {
189      return new EmptyOperator();
190    }
191
192    protected internal abstract IOperator CreateSelector();
193
194    protected internal abstract IOperator CreateCrossover();
195
196    protected internal abstract IOperator CreateTreeCreator();
197
198    protected internal abstract IOperator CreateFunctionLibraryInjector();
199
200    protected internal virtual IOperator CreateGlobalInjector() {
201      VariableInjector injector = new VariableInjector();
202      injector.Name = "Global Injector";
203      injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
204      injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", new DoubleData()));
205      injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", new IntData()));
206      injector.AddVariable(new HeuristicLab.Core.Variable("Elites", new IntData()));
207      injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
208      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData()));
209      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData()));
210      injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
211      injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
212      injector.AddVariable(new HeuristicLab.Core.Variable("Parents", new IntData()));
213      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
214      injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData()));
215      injector.AddVariable(new HeuristicLab.Core.Variable("TreeEvaluator", new HL2TreeEvaluator()));
216      return injector;
217    }
218
219    protected internal abstract IOperator CreateManipulator();
220
221    protected internal virtual IOperator CreateInitialization() {
222      CombinedOperator init = new CombinedOperator();
223      init.Name = "Initialization";
224      SequentialProcessor seq = new SequentialProcessor();
225      SubScopesCreater subScopesCreater = new SubScopesCreater();
226      subScopesCreater.GetVariableInfo("SubScopes").ActualName = "PopulationSize";
227      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
228      SequentialProcessor individualSeq = new SequentialProcessor();
229      OperatorExtractor treeCreater = new OperatorExtractor();
230      treeCreater.Name = "Tree generator (extr.)";
231      treeCreater.GetVariableInfo("Operator").ActualName = "Tree generator";
232      OperatorExtractor evaluator = new OperatorExtractor();
233      evaluator.Name = "Evaluator (extr.)";
234      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
235      MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
236      validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
237      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
238      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
239      Counter evalCounter = new Counter();
240      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
241      Sorter sorter = new Sorter();
242      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
243      sorter.GetVariableInfo("Value").ActualName = "Quality";
244
245      seq.AddSubOperator(subScopesCreater);
246      seq.AddSubOperator(subScopesProc);
247      seq.AddSubOperator(sorter);
248
249      subScopesProc.AddSubOperator(individualSeq);
250      individualSeq.AddSubOperator(treeCreater);
251      individualSeq.AddSubOperator(evaluator);
252      individualSeq.AddSubOperator(validationEvaluator);
253      individualSeq.AddSubOperator(evalCounter);
254
255      init.OperatorGraph.AddOperator(seq);
256      init.OperatorGraph.InitialOperator = seq;
257      return init;
258    }
259
260    protected internal virtual IOperator CreateMainLoop() {
261      CombinedOperator main = new CombinedOperator();
262      SequentialProcessor seq = new SequentialProcessor();
263      IOperator childCreater = CreateChildCreater();
264      IOperator replacement = CreateReplacement();
265
266      BestSolutionStorer solutionStorer = new BestSolutionStorer();
267      solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
268      solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
269      solutionStorer.AddSubOperator(CreateBestSolutionProcessor());
270
271      BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator();
272      BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator();
273      validationQualityCalculator.Name = "BestAverageWorstValidationQualityCalculator";
274      validationQualityCalculator.GetVariableInfo("Quality").ActualName = "ValidationQuality";
275      validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";
276      validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";
277      validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";
278      IOperator loggingOperator = CreateLoggingOperator();
279      Counter counter = new Counter();
280      counter.GetVariableInfo("Value").ActualName = "Generations";
281      IOperator loopCondition = CreateLoopCondition(seq);
282
283      seq.AddSubOperator(childCreater);
284      seq.AddSubOperator(replacement);
285      seq.AddSubOperator(solutionStorer);
286      seq.AddSubOperator(qualityCalculator);
287      seq.AddSubOperator(validationQualityCalculator);
288      seq.AddSubOperator(loggingOperator);
289      seq.AddSubOperator(counter);
290      seq.AddSubOperator(loopCondition);
291
292      main.OperatorGraph.AddOperator(seq);
293      main.OperatorGraph.InitialOperator = seq;
294      return main;
295    }
296
297    protected internal virtual IOperator CreateLoggingOperator() {
298      return new EmptyOperator();
299    }
300
301    protected internal virtual IOperator CreateLoopCondition(IOperator loop) {
302      SequentialProcessor seq = new SequentialProcessor();
303      seq.Name = "Loop Condition";
304      LessThanComparator comparator = new LessThanComparator();
305      comparator.GetVariableInfo("LeftSide").ActualName = "Generations";
306      comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations";
307      comparator.GetVariableInfo("Result").ActualName = "GenerationsCondition";
308      ConditionalBranch cond = new ConditionalBranch();
309      cond.GetVariableInfo("Condition").ActualName = "GenerationsCondition";
310
311      seq.AddSubOperator(comparator);
312      seq.AddSubOperator(cond);
313
314      cond.AddSubOperator(loop);
315      return seq;
316    }
317
318    protected internal virtual IOperator CreateBestSolutionProcessor() {
319      return new EmptyOperator();
320    }
321
322    protected internal virtual IOperator CreateReplacement() {
323      CombinedOperator replacement = new CombinedOperator();
324      replacement.Name = "Replacement";
325      SequentialProcessor seq = new SequentialProcessor();
326      SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor();
327      SequentialProcessor selectedProc = new SequentialProcessor();
328      LeftSelector leftSelector = new LeftSelector();
329      leftSelector.GetVariableInfo("Selected").ActualName = "Elites";
330      RightReducer rightReducer = new RightReducer();
331
332      SequentialProcessor remainingProc = new SequentialProcessor();
333      RightSelector rightSelector = new RightSelector();
334      rightSelector.GetVariableInfo("Selected").ActualName = "Elites";
335      LeftReducer leftReducer = new LeftReducer();
336      MergingReducer mergingReducer = new MergingReducer();
337      Sorter sorter = new Sorter();
338      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
339      sorter.GetVariableInfo("Value").ActualName = "Quality";
340
341      seq.AddSubOperator(seqScopeProc);
342      seqScopeProc.AddSubOperator(selectedProc);
343      selectedProc.AddSubOperator(leftSelector);
344      selectedProc.AddSubOperator(rightReducer);
345
346      seqScopeProc.AddSubOperator(remainingProc);
347      remainingProc.AddSubOperator(rightSelector);
348      remainingProc.AddSubOperator(leftReducer);
349      seq.AddSubOperator(mergingReducer);
350      seq.AddSubOperator(sorter);
351      replacement.OperatorGraph.AddOperator(seq);
352      replacement.OperatorGraph.InitialOperator = seq;
353      return replacement;
354    }
355
356    protected internal virtual IOperator CreateChildCreater() {
357      CombinedOperator childCreater = new CombinedOperator();
358      childCreater.Name = "Create children";
359      SequentialProcessor seq = new SequentialProcessor();
360      OperatorExtractor selector = new OperatorExtractor();
361      selector.Name = "Selector (extr.)";
362      selector.GetVariableInfo("Operator").ActualName = "Selector";
363
364      SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor();
365      EmptyOperator emptyOpt = new EmptyOperator();
366      SequentialProcessor selectedProc = new SequentialProcessor();
367      ChildrenInitializer childInitializer = new ChildrenInitializer();
368      ((IntData)childInitializer.GetVariable("ParentsPerChild").Value).Data = 2;
369
370      OperatorExtractor crossover = new OperatorExtractor();
371      crossover.Name = "Crossover (extr.)";
372      crossover.GetVariableInfo("Operator").ActualName = "Crossover";
373      UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
374      SequentialProcessor individualSeqProc = new SequentialProcessor();
375      StochasticBranch cond = new StochasticBranch();
376      cond.GetVariableInfo("Probability").ActualName = "MutationRate";
377      OperatorExtractor manipulator = new OperatorExtractor();
378      manipulator.Name = "Manipulator (extr.)";
379      manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
380      OperatorExtractor evaluator = new OperatorExtractor();
381      evaluator.Name = "Evaluator (extr.)";
382      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
383      MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
384      validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
385      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
386      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
387      Counter evalCounter = new Counter();
388      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
389      SubScopesRemover parentRefRemover = new SubScopesRemover();
390
391      Sorter sorter = new Sorter();
392      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
393      sorter.GetVariableInfo("Value").ActualName = "Quality";
394
395
396      seq.AddSubOperator(selector);
397      seq.AddSubOperator(seqScopesProc);
398      seqScopesProc.AddSubOperator(emptyOpt);
399      seqScopesProc.AddSubOperator(selectedProc);
400      selectedProc.AddSubOperator(childInitializer);
401      selectedProc.AddSubOperator(individualProc);
402      individualProc.AddSubOperator(individualSeqProc);
403      individualSeqProc.AddSubOperator(crossover);
404      individualSeqProc.AddSubOperator(cond);
405      cond.AddSubOperator(manipulator);
406      individualSeqProc.AddSubOperator(evaluator);
407      individualSeqProc.AddSubOperator(validationEvaluator);
408      individualSeqProc.AddSubOperator(evalCounter);
409      individualSeqProc.AddSubOperator(parentRefRemover);
410      selectedProc.AddSubOperator(sorter);
411
412      childCreater.OperatorGraph.AddOperator(seq);
413      childCreater.OperatorGraph.InitialOperator = seq;
414      return childCreater;
415    }
416
417    protected internal virtual Model CreateGPModel(IScope bestModelScope) {
418      Engine.GlobalScope.AddSubScope(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      // calculate and set variable impacts
427      VariableEvaluationImpactCalculator evaluationImpactCalculator = new VariableEvaluationImpactCalculator();
428      VariableQualityImpactCalculator qualityImpactCalculator = new VariableQualityImpactCalculator();
429
430      evaluationImpactCalculator.Apply(bestModelScope);
431      qualityImpactCalculator.Apply(bestModelScope);
432
433      ItemList evaluationImpacts = bestModelScope.GetVariableValue<ItemList>("VariableEvaluationImpacts", false);
434      ItemList qualityImpacts = bestModelScope.GetVariableValue<ItemList>("VariableQualityImpacts", false);
435      foreach (ItemList row in evaluationImpacts) {
436        string variableName = ((StringData)row[0]).Data;
437        double impact = ((DoubleData)row[1]).Data;
438        model.SetVariableEvaluationImpact(variableName, impact);
439      }
440      foreach (ItemList row in qualityImpacts) {
441        string variableName = ((StringData)row[0]).Data;
442        double impact = ((DoubleData)row[1]).Data;
443        model.SetVariableQualityImpact(variableName, impact);
444      }
445      Engine.GlobalScope.RemoveSubScope(bestModelScope);
446      return model;
447    }
448
449    public override object Clone(IDictionary<Guid, object> clonedObjects) {
450      AlgorithmBase clone = (AlgorithmBase)base.Clone(clonedObjects);
451      clonedObjects.Add(Guid, clone);
452      clone.engine = (SequentialEngine.SequentialEngine)Auxiliary.Clone(Engine, clonedObjects);
453      return clone;
454    }
455
456    protected VariableInjector GetVariableInjector() {
457      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
458      // SequentialProcessor in GP
459      algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
460      return (VariableInjector)algorithm.SubOperators[2];
461    }
462
463    protected RandomInjector GetRandomInjector() {
464      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
465      // SequentialProcessor in GP
466      algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
467      return (RandomInjector)algorithm.SubOperators[1];
468    }
469
470    #region Persistence Methods
471    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
472      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
473      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
474      return node;
475    }
476    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
477      base.Populate(node, restoredObjects);
478      engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
479    }
480    #endregion
481
482
483  }
484}
Note: See TracBrowser for help on using the repository browser.