Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1332 was 1287, checked in by gkronber, 16 years ago

Merged change sets from CEDMA branch to trunk:

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