Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Algorithms/3.2/AlgorithmBase.cs @ 3446

Last change on this file since 3446 was 2683, checked in by gkronber, 15 years ago

Implemented operator to calculate variable impacts based on the log of variable frequencies over the whole GP run and integrated operators into default SGP and OSGP algorithms. #853 (Operator to calculate variable impacts as integral over the relative frequencies of variable references over the whole GP run and the whole population)

File size: 21.8 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.Xml;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Evolutionary;
28using HeuristicLab.Logging;
29using HeuristicLab.Operators;
30using HeuristicLab.Random;
31using HeuristicLab.Selection;
32using HeuristicLab.GP.Operators;
33using HeuristicLab.GP.Interfaces;
34
35namespace HeuristicLab.GP.Algorithms {
36  public abstract class AlgorithmBase : ItemBase, IGeneticProgrammingAlgorithm {
37    public virtual string Name { get { return "GP"; } }
38    public virtual string Description { get { return "TODO"; } }
39
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 int MaxGenerations {
50      get { return GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data; }
51      set { GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data = value; }
52    }
53
54    public virtual int Elites {
55      get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; }
56      set { GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data = value; }
57    }
58
59    public virtual int MaxTreeSize {
60      get { return GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data; }
61      set { GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data = value; }
62    }
63
64    public virtual int MinTreeSize {
65      get { return GetVariableInjector().GetVariable("MinTreeSize").GetValue<IntData>().Data; }
66      set { GetVariableInjector().GetVariable("MinTreeSize").GetValue<IntData>().Data = value; }
67    }
68
69    public virtual int MaxTreeHeight {
70      get { return GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data; }
71      set { GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data = value; }
72    }
73
74    public virtual int Parents {
75      get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; }
76      set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; }
77    }
78
79    public virtual bool SetSeedRandomly {
80      get { return GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data; }
81      set { GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data = value; }
82    }
83
84    public virtual int RandomSeed {
85      get { return GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data; }
86      set { GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data = value; }
87    }
88
89    public virtual IOperator ProblemInjector {
90      get { return GetInitializationOperator().SubOperators[0]; }
91      set {
92        value.Name = "ProblemInjector";
93        IOperator init = GetInitializationOperator();
94        init.RemoveSubOperator(0);
95        init.AddSubOperator(value, 0);
96      }
97    }
98
99    public virtual IOperator FunctionLibraryInjector {
100      get { return GetInitializationOperator().SubOperators[1]; }
101      set {
102        value.Name = "FunctionLibraryInjector";
103        IOperator init = GetInitializationOperator();
104        init.RemoveSubOperator(1);
105        init.AddSubOperator(value, 1);
106      }
107    }
108
109    private IEngine engine;
110    public IEngine Engine {
111      get { return engine; }
112      protected set { engine = value; }
113    }
114
115    public AlgorithmBase() {
116      engine = new SequentialEngine.SequentialEngine();
117      CombinedOperator algo = CreateAlgorithm();
118      engine.OperatorGraph.AddOperator(algo);
119      engine.OperatorGraph.InitialOperator = algo;
120      SetSeedRandomly = true;
121      Elites = 1;
122      MutationRate = 0.15;
123      PopulationSize = 1000;
124      MaxGenerations = 100;
125      MaxTreeSize = 100;
126      MinTreeSize = 1;
127      MaxTreeHeight = 10;
128      Parents = 2000;
129    }
130
131    protected internal virtual CombinedOperator CreateAlgorithm() {
132      CombinedOperator algo = new CombinedOperator();
133      algo.Name = Name;
134      SequentialProcessor seq = new SequentialProcessor();
135      seq.Name = Name;
136      IOperator init = CreateInitializationOperator();
137      init.AddSubOperator(CreateProblemInjector());
138      init.AddSubOperator(CreateFunctionLibraryInjector());
139      seq.AddSubOperator(init);
140
141      IOperator initPopulation = CreateInitialPopulationGenerator();
142      initPopulation.AddSubOperator(CreateRandomSolutionGenerator());
143      initPopulation.AddSubOperator(CreateInitialPopulationEvaluator());
144      seq.AddSubOperator(initPopulation);
145
146      IOperator mainLoop = CreateMainLoop();
147      mainLoop.AddSubOperator(CreateSelectionOperator());
148      mainLoop.AddSubOperator(CreateCrossoverOperator());
149      mainLoop.AddSubOperator(CreateManipulationOperator());
150      mainLoop.AddSubOperator(CreateEvaluationOperator());
151      mainLoop.AddSubOperator(CreateTerminationCondition());
152      seq.AddSubOperator(mainLoop);
153
154      IOperator postProcess = CreatePostProcessingOperator();
155      seq.AddSubOperator(postProcess);
156
157      algo.OperatorGraph.AddOperator(seq);
158      algo.OperatorGraph.InitialOperator = seq;
159      return algo;
160    }
161
162    #region global init
163    protected virtual IOperator CreateInitializationOperator() {
164      CombinedOperator init = new CombinedOperator();
165      init.Name = "Initialization";
166      SequentialProcessor seq = new SequentialProcessor();
167      seq.AddSubOperator(CreateRandomInjector());
168      seq.AddSubOperator(CreateGlobalInjector());
169
170      OperatorExtractor probInjectorExtractor = new OperatorExtractor();
171      probInjectorExtractor.Name = "ProblemInjector";
172      probInjectorExtractor.GetVariableInfo("Operator").ActualName = "ProblemInjector";
173      seq.AddSubOperator(probInjectorExtractor);
174
175      OperatorExtractor funLibInjectorExtractor = new OperatorExtractor();
176      funLibInjectorExtractor.Name = "FunctionLibraryInjector";
177      funLibInjectorExtractor.GetVariableInfo("Operator").ActualName = "FunctionLibraryInjector";
178      seq.AddSubOperator(funLibInjectorExtractor);
179
180      init.OperatorGraph.AddOperator(seq);
181      init.OperatorGraph.InitialOperator = seq;
182      return init;
183    }
184
185    protected virtual IOperator CreateRandomInjector() {
186      RandomInjector randomInjector = new RandomInjector();
187      randomInjector.Name = "Random Injector";
188      return randomInjector;
189    }
190
191    protected virtual VariableInjector CreateGlobalInjector() {
192      VariableInjector injector = new VariableInjector();
193      injector.Name = "Global Injector";
194      injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
195      injector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", new IntData()));
196      injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", new DoubleData()));
197      injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", new IntData()));
198      injector.AddVariable(new HeuristicLab.Core.Variable("Elites", new IntData()));
199      injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
200      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData()));
201      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData()));
202      injector.AddVariable(new HeuristicLab.Core.Variable("MinTreeSize", new IntData()));
203      injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
204      injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
205      injector.AddVariable(new HeuristicLab.Core.Variable("Parents", new IntData()));
206      return injector;
207    }
208
209
210    protected virtual IOperator CreateProblemInjector() {
211      return new EmptyOperator();
212    }
213
214    protected virtual IOperator CreateFunctionLibraryInjector() {
215      return new EmptyOperator();
216    }
217    #endregion
218
219    #region population init
220    private IOperator CreateInitialPopulationGenerator() {
221      CombinedOperator initPopulation = new CombinedOperator();
222      initPopulation.Name = "Init population";
223      SequentialProcessor seq = new SequentialProcessor();
224      SubScopesCreater subScopesCreater = new SubScopesCreater();
225      subScopesCreater.GetVariableInfo("SubScopes").ActualName = "PopulationSize";
226      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
227      SequentialProcessor individualSeq = new SequentialProcessor();
228      OperatorExtractor treeCreater = new OperatorExtractor();
229      treeCreater.Name = "Tree generator (extr.)";
230      treeCreater.GetVariableInfo("Operator").ActualName = "Solution generator";
231
232      OperatorExtractor evaluator = new OperatorExtractor();
233      evaluator.Name = "Evaluator (extr.)";
234      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
235      Counter evalCounter = new Counter();
236      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
237      Sorter sorter = new Sorter();
238      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
239      sorter.GetVariableInfo("Value").ActualName = "Quality";
240
241      seq.AddSubOperator(subScopesCreater);
242      seq.AddSubOperator(subScopesProc);
243      seq.AddSubOperator(sorter);
244
245      subScopesProc.AddSubOperator(individualSeq);
246      individualSeq.AddSubOperator(treeCreater);
247      individualSeq.AddSubOperator(evaluator);
248      individualSeq.AddSubOperator(evalCounter);
249
250      initPopulation.OperatorGraph.AddOperator(seq);
251      initPopulation.OperatorGraph.InitialOperator = seq;
252      return initPopulation;
253    }
254
255    protected virtual IOperator CreateRandomSolutionGenerator() {
256      ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator();
257      treeCreator.Name = "Solution generator";
258      return treeCreator;
259    }
260
261    protected virtual IOperator CreateInitialPopulationEvaluator() {
262      return new EmptyOperator();
263    }
264    #endregion
265
266    #region mainloop
267    protected virtual IOperator CreateMainLoop() {
268      CombinedOperator main = new CombinedOperator();
269      main.Name = "Main";
270      SequentialProcessor seq = new SequentialProcessor();
271      IOperator childCreater = CreateChildCreater();
272      IOperator replacement = CreateReplacement();
273
274      BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator();
275      IOperator loggingOperator = CreateLoggingOperator();
276      Counter counter = new Counter();
277      counter.GetVariableInfo("Value").ActualName = "Generations";
278
279      OperatorExtractor terminationCriterionExtractor = new OperatorExtractor();
280      terminationCriterionExtractor.Name = "TerminationCondition (extr.)";
281      terminationCriterionExtractor.GetVariableInfo("Operator").ActualName = "TerminationCondition";
282
283      ConditionalBranch loop = new ConditionalBranch();
284      loop.Name = "Main loop";
285      loop.GetVariableInfo("Condition").ActualName = "TerminationCriterion";
286      loop.AddSubOperator(new EmptyOperator());
287      loop.AddSubOperator(seq);
288
289      seq.AddSubOperator(CreateGenerationStepHook());
290      seq.AddSubOperator(qualityCalculator);
291      seq.AddSubOperator(loggingOperator);
292      seq.AddSubOperator(counter);
293      seq.AddSubOperator(childCreater);
294      seq.AddSubOperator(replacement);
295      seq.AddSubOperator(terminationCriterionExtractor);
296      seq.AddSubOperator(loop);
297
298      main.OperatorGraph.AddOperator(seq);
299      main.OperatorGraph.InitialOperator = seq;
300      return main;
301    }
302
303    protected virtual IOperator CreateChildCreater() {
304      CombinedOperator childCreater = new CombinedOperator();
305      childCreater.Name = "Create children";
306      SequentialProcessor seq = new SequentialProcessor();
307      OperatorExtractor selector = new OperatorExtractor();
308      selector.Name = "Selector (extr.)";
309      selector.GetVariableInfo("Operator").ActualName = "Selector";
310
311      SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor();
312      EmptyOperator emptyOpt = new EmptyOperator();
313      SequentialProcessor selectedProc = new SequentialProcessor();
314      ChildrenInitializer childInitializer = new ChildrenInitializer();
315      ((IntData)childInitializer.GetVariable("ParentsPerChild").Value).Data = 2;
316
317      OperatorExtractor crossover = new OperatorExtractor();
318      crossover.Name = "Crossover (extr.)";
319      crossover.GetVariableInfo("Operator").ActualName = "Crossover";
320      UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
321      SequentialProcessor individualSeqProc = new SequentialProcessor();
322      StochasticBranch cond = new StochasticBranch();
323      cond.GetVariableInfo("Probability").ActualName = "MutationRate";
324      OperatorExtractor manipulator = new OperatorExtractor();
325      manipulator.Name = "Manipulator (extr.)";
326      manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
327      OperatorExtractor evaluator = new OperatorExtractor();
328      evaluator.Name = "Evaluator (extr.)";
329      evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
330      Counter evalCounter = new Counter();
331      evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
332      SubScopesRemover parentRefRemover = new SubScopesRemover();
333
334      Sorter sorter = new Sorter();
335      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
336      sorter.GetVariableInfo("Value").ActualName = "Quality";
337
338
339      seq.AddSubOperator(selector);
340      seq.AddSubOperator(seqScopesProc);
341      seqScopesProc.AddSubOperator(emptyOpt);
342      seqScopesProc.AddSubOperator(selectedProc);
343      selectedProc.AddSubOperator(childInitializer);
344      selectedProc.AddSubOperator(individualProc);
345      individualProc.AddSubOperator(individualSeqProc);
346      individualSeqProc.AddSubOperator(crossover);
347      individualSeqProc.AddSubOperator(cond);
348      cond.AddSubOperator(manipulator);
349      individualSeqProc.AddSubOperator(evaluator);
350      individualSeqProc.AddSubOperator(evalCounter);
351      individualSeqProc.AddSubOperator(parentRefRemover);
352      selectedProc.AddSubOperator(sorter);
353
354      childCreater.OperatorGraph.AddOperator(seq);
355      childCreater.OperatorGraph.InitialOperator = seq;
356      return childCreater;
357    }
358
359    protected virtual IOperator CreateReplacement() {
360      CombinedOperator replacement = new CombinedOperator();
361      replacement.Name = "Replacement";
362      SequentialProcessor seq = new SequentialProcessor();
363      SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor();
364      SequentialProcessor selectedProc = new SequentialProcessor();
365      LeftSelector leftSelector = new LeftSelector();
366      leftSelector.GetVariableInfo("Selected").ActualName = "Elites";
367      RightReducer rightReducer = new RightReducer();
368
369      SequentialProcessor remainingProc = new SequentialProcessor();
370      RightSelector rightSelector = new RightSelector();
371      rightSelector.GetVariableInfo("Selected").ActualName = "Elites";
372      LeftReducer leftReducer = new LeftReducer();
373      MergingReducer mergingReducer = new MergingReducer();
374      Sorter sorter = new Sorter();
375      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
376      sorter.GetVariableInfo("Value").ActualName = "Quality";
377
378      seq.AddSubOperator(seqScopeProc);
379      seqScopeProc.AddSubOperator(selectedProc);
380      selectedProc.AddSubOperator(leftSelector);
381      selectedProc.AddSubOperator(rightReducer);
382
383      seqScopeProc.AddSubOperator(remainingProc);
384      remainingProc.AddSubOperator(rightSelector);
385      remainingProc.AddSubOperator(leftReducer);
386      seq.AddSubOperator(mergingReducer);
387      seq.AddSubOperator(sorter);
388      replacement.OperatorGraph.AddOperator(seq);
389      replacement.OperatorGraph.InitialOperator = seq;
390      return replacement;
391    }
392
393    protected virtual IOperator CreateLoggingOperator() {
394      CombinedOperator loggingOperator = new CombinedOperator();
395      loggingOperator.Name = "Logging";
396      SequentialProcessor seq = new SequentialProcessor();
397
398      DataCollector collector = new DataCollector();
399      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
400      names.Add(new StringData("BestQuality"));
401      names.Add(new StringData("AverageQuality"));
402      names.Add(new StringData("WorstQuality"));
403      LinechartInjector lineChartInjector = new LinechartInjector();
404      lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
405      lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
406      QualityLogger qualityLogger = new QualityLogger();
407
408      seq.AddSubOperator(collector);
409      seq.AddSubOperator(lineChartInjector);
410      seq.AddSubOperator(qualityLogger);
411
412      loggingOperator.OperatorGraph.AddOperator(seq);
413      loggingOperator.OperatorGraph.InitialOperator = seq;
414      return loggingOperator;
415    }
416
417    protected virtual IOperator CreateGenerationStepHook() {
418      return new EmptyOperator();
419    }
420
421    protected virtual IOperator CreateTerminationCondition() {
422      CombinedOperator terminationCriterion = new CombinedOperator();
423      terminationCriterion.Name = "TerminationCondition";
424      SequentialProcessor seq = new SequentialProcessor();
425      GreaterThanComparator comparator = new GreaterThanComparator();
426      comparator.GetVariableInfo("LeftSide").ActualName = "Generations";
427      comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations";
428      comparator.GetVariableInfo("Result").ActualName = "TerminationCriterion";
429
430      seq.AddSubOperator(comparator);
431      terminationCriterion.OperatorGraph.AddOperator(seq);
432      terminationCriterion.OperatorGraph.InitialOperator = seq;
433      return terminationCriterion;
434    }
435
436    protected virtual IOperator CreateEvaluationOperator() {
437      return new EmptyOperator();
438    }
439
440    protected virtual IOperator CreateManipulationOperator() {
441      ChangeNodeTypeManipulation manipulator = new ChangeNodeTypeManipulation();
442      manipulator.Name = "Manipulator";
443      return manipulator;
444    }
445
446    protected virtual IOperator CreateCrossoverOperator() {
447      StandardCrossOver crossover = new StandardCrossOver();
448      crossover.Name = "Crossover";
449      return crossover;
450    }
451
452    protected virtual IOperator CreateSelectionOperator() {
453      TournamentSelector selector = new TournamentSelector();
454      selector.GetVariableInfo("Selected").ActualName = "Parents";
455      selector.GetVariable("GroupSize").Value = new IntData(7);
456      selector.Name = "Selector";
457      return selector;
458    }
459
460    #endregion
461
462    protected virtual IOperator CreatePostProcessingOperator() {
463      return new EmptyOperator();
464    }
465
466    protected virtual IOperator GetVariableInjector() {
467      CombinedOperator init = (CombinedOperator)GetInitializationOperator();
468      return init.OperatorGraph.InitialOperator.SubOperators[1];
469    }
470
471    protected virtual IOperator GetRandomInjector() {
472      CombinedOperator init = (CombinedOperator)GetInitializationOperator();
473      return init.OperatorGraph.InitialOperator.SubOperators[0];
474    }
475
476    protected virtual IOperator GetInitializationOperator() {
477      CombinedOperator algo = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
478      return algo.OperatorGraph.InitialOperator.SubOperators[0];
479    }
480
481    #region Persistence Methods
482    public override object Clone(IDictionary<Guid, object> clonedObjects) {
483      AlgorithmBase clone = (AlgorithmBase)base.Clone(clonedObjects);
484      clone.engine = (IEngine)Auxiliary.Clone(Engine, clonedObjects);
485      return clone;
486    }
487
488    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
489      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
490      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
491      return node;
492    }
493    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
494      base.Populate(node, restoredObjects);
495      engine = (IEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
496    }
497    #endregion
498
499
500    public static IOperator CombineTerminationCriterions(IOperator criterion1, IOperator criterion2) {
501      ConditionalBranch branch = new ConditionalBranch();
502      branch.GetVariableInfo("Condition").ActualName = "TerminationCriterion";
503      branch.AddSubOperator(new EmptyOperator());
504      branch.AddSubOperator(criterion2);
505
506      SequentialProcessor seq = new SequentialProcessor();
507      seq.AddSubOperator(criterion1);
508      seq.AddSubOperator(branch);
509
510      return seq;
511    }
512  }
513}
Note: See TracBrowser for help on using the repository browser.