Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.4/AlgorithmBase.cs @ 2212

Last change on this file since 2212 was 1914, checked in by epitzer, 15 years ago

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

File size: 20.5 KB
RevLine 
[1230]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;
[1335]36using HeuristicLab.Evolutionary;
[1857]37using HeuristicLab.Modeling;
[1914]38using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1230]39
40namespace HeuristicLab.GP.StructureIdentification {
[1857]41  public abstract class AlgorithmBase : ItemBase, IAlgorithm, IStochasticAlgorithm {
[1914]42
[1857]43    public virtual string Name { get { return "GP"; } }
[1914]44
[1857]45    public virtual string Description { get { return "TODO"; } }
46
[1231]47    public virtual double MutationRate {
[1287]48      get { return GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data; }
49      set { GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data = value; }
[1230]50    }
[1231]51    public virtual int PopulationSize {
[1287]52      get { return GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data; }
53      set { GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data = value; }
[1230]54    }
55
[1231]56    public virtual bool SetSeedRandomly {
[1287]57      get { return GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data; }
58      set { GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data = value; }
[1230]59    }
60
[1857]61    public virtual int RandomSeed {
[1287]62      get { return GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data; }
63      set { GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data = value; }
[1230]64    }
65
[1231]66    public virtual IOperator ProblemInjector {
[1230]67      get { return algorithm.SubOperators[0]; }
68      set {
69        value.Name = "ProblemInjector";
70        algorithm.RemoveSubOperator(0);
71        algorithm.AddSubOperator(value, 0);
72      }
73    }
74
[1906]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
[1231]83    public virtual int Elites {
[1287]84      get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; }
85      set { GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data = value; }
[1230]86    }
87
[1231]88    public virtual int MaxTreeSize {
[1287]89      get { return GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data; }
90      set { GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data = value; }
[1230]91    }
92
[1231]93    public virtual int MaxTreeHeight {
[1287]94      get { return GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data; }
95      set { GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data = value; }
[1230]96    }
97
[1231]98    public virtual int Parents {
[1287]99      get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; }
100      set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; }
[1231]101    }
102
[1287]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
[1230]113    private IOperator algorithm;
114
[1914]115    [Storable]
[1230]116    private SequentialEngine.SequentialEngine engine;
[1914]117
[1230]118    public IEngine Engine {
119      get { return engine; }
[1231]120      protected set { engine = (SequentialEngine.SequentialEngine)value; }
[1230]121    }
122
123    public AlgorithmBase() {
124      engine = new SequentialEngine.SequentialEngine();
125      CombinedOperator algo = CreateAlgorithm();
126      engine.OperatorGraph.AddOperator(algo);
127      engine.OperatorGraph.InitialOperator = algo;
[1287]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;
[1230]137    }
138
[1287]139    protected internal virtual CombinedOperator CreateAlgorithm() {
[1230]140      CombinedOperator algo = new CombinedOperator();
141      algo.Name = "GP";
142      SequentialProcessor seq = new SequentialProcessor();
[1287]143      IOperator problemInjector = CreateProblemInjector();
[1230]144
145      RandomInjector randomInjector = new RandomInjector();
146      randomInjector.Name = "Random Injector";
147
148      IOperator globalInjector = CreateGlobalInjector();
149      IOperator initialization = CreateInitialization();
[1287]150      IOperator funLibInjector = CreateFunctionLibraryInjector();
[1873]151      IOperator treeEvaluatorInjector = new HL2TreeEvaluatorInjector();
152
[1230]153      IOperator mainLoop = CreateMainLoop();
154      mainLoop.Name = "Main loop";
155
156      IOperator treeCreator = CreateTreeCreator();
[1287]157
[1230]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";
[1287]163
[1230]164      IOperator crossover = CreateCrossover();
165      IOperator manipulator = CreateManipulator();
166
167      IOperator selector = CreateSelector();
168      LeftReducer cleanUp = new LeftReducer();
169
[1287]170      seq.AddSubOperator(problemInjector);
[1230]171      seq.AddSubOperator(randomInjector);
172      seq.AddSubOperator(globalInjector);
173      seq.AddSubOperator(funLibInjector);
[1873]174      seq.AddSubOperator(treeEvaluatorInjector);
[1230]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
[1287]192    protected internal virtual IOperator CreateProblemInjector() {
193      return new EmptyOperator();
194    }
[1230]195
[1287]196    protected internal abstract IOperator CreateSelector();
[1230]197
[1287]198    protected internal abstract IOperator CreateCrossover();
[1230]199
[1287]200    protected internal abstract IOperator CreateTreeCreator();
[1230]201
[1287]202    protected internal abstract IOperator CreateFunctionLibraryInjector();
203
204    protected internal virtual IOperator CreateGlobalInjector() {
[1230]205      VariableInjector injector = new VariableInjector();
206      injector.Name = "Global Injector";
207      injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
[1287]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()));
[1230]211      injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
[1287]212      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData()));
213      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData()));
[1230]214      injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
215      injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
[1287]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()));
[1230]219      return injector;
220    }
221
[1287]222    protected internal abstract IOperator CreateManipulator();
[1230]223
[1287]224    protected internal virtual IOperator CreateInitialization() {
[1230]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
[1287]263    protected internal virtual IOperator CreateMainLoop() {
[1230]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();
[1287]276      validationQualityCalculator.Name = "BestAverageWorstValidationQualityCalculator";
277      validationQualityCalculator.GetVariableInfo("Quality").ActualName = "ValidationQuality";
[1230]278      validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";
279      validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";
280      validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";
[1287]281      IOperator loggingOperator = CreateLoggingOperator();
[1230]282      Counter counter = new Counter();
283      counter.GetVariableInfo("Value").ActualName = "Generations";
[1287]284      IOperator loopCondition = CreateLoopCondition(seq);
[1230]285
286      seq.AddSubOperator(childCreater);
287      seq.AddSubOperator(replacement);
288      seq.AddSubOperator(solutionStorer);
289      seq.AddSubOperator(qualityCalculator);
290      seq.AddSubOperator(validationQualityCalculator);
[1287]291      seq.AddSubOperator(loggingOperator);
[1230]292      seq.AddSubOperator(counter);
[1287]293      seq.AddSubOperator(loopCondition);
[1230]294
295      main.OperatorGraph.AddOperator(seq);
296      main.OperatorGraph.InitialOperator = seq;
297      return main;
298    }
299
[1287]300    protected internal virtual IOperator CreateLoggingOperator() {
[1230]301      return new EmptyOperator();
302    }
303
[1287]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() {
[1230]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
[1287]359    protected internal virtual IOperator CreateChildCreater() {
[1230]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";
[1335]366
[1230]367      SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor();
368      EmptyOperator emptyOpt = new EmptyOperator();
369      SequentialProcessor selectedProc = new SequentialProcessor();
[1335]370      ChildrenInitializer childInitializer = new ChildrenInitializer();
371      ((IntData)childInitializer.GetVariable("ParentsPerChild").Value).Data = 2;
372
[1230]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";
[1335]392      SubScopesRemover parentRefRemover = new SubScopesRemover();
[1230]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);
[1335]403      selectedProc.AddSubOperator(childInitializer);
[1230]404      selectedProc.AddSubOperator(individualProc);
405      individualProc.AddSubOperator(individualSeqProc);
[1335]406      individualSeqProc.AddSubOperator(crossover);
[1230]407      individualSeqProc.AddSubOperator(cond);
408      cond.AddSubOperator(manipulator);
409      individualSeqProc.AddSubOperator(evaluator);
410      individualSeqProc.AddSubOperator(validationEvaluator);
411      individualSeqProc.AddSubOperator(evalCounter);
[1335]412      individualSeqProc.AddSubOperator(parentRefRemover);
[1230]413      selectedProc.AddSubOperator(sorter);
414
415      childCreater.OperatorGraph.AddOperator(seq);
416      childCreater.OperatorGraph.InitialOperator = seq;
417      return childCreater;
418    }
419
[1906]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
[1230]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
[1287]438    protected VariableInjector GetVariableInjector() {
[1230]439      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
[1287]440      // SequentialProcessor in GP
[1230]441      algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
[1287]442      return (VariableInjector)algorithm.SubOperators[2];
[1230]443    }
444
[1287]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    }
[1230]451  }
452}
Note: See TracBrowser for help on using the repository browser.