Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.GP.StructureIdentification/3.4/AlgorithmBase.cs @ 2173

Last change on this file since 2173 was 1914, checked in by epitzer, 16 years ago

Migration of DataAnalysis, GP, GP.StructureIdentification and Modeling to new Persistence-3.3 (#603)

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