Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.GP.StructureIdentification/3.3/AlgorithmBase.cs @ 1835

Last change on this file since 1835 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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