Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTree/3.3/Manipulators/FullTreeShaker.cs @ 3219

Last change on this file since 3219 was 3219, checked in by gkronber, 14 years ago

Worked on SymbolicTreeEncoding plugin. #937 (Data types and operators for symbolic expression tree encoding)

File size: 3.4 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.Linq;
23using HeuristicLab.Core;
24using HeuristicLab.Operators;
25using HeuristicLab.Random;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTree {
29  public class FullTreeShaker : DelegatingOperator {
30    public override string Description {
31      get { return "Manipulates all tree nodes for which a manipulator is defined."; }
32    }
33
34    public FullTreeShaker()
35      : base() {
36      AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In));
37      AddVariableInfo(new VariableInfo("FunctionLibrary", "Function library that defines function mutations", typeof(FunctionLibrary), VariableKind.In));
38      AddVariableInfo(new VariableInfo("ShakingFactor", "Variable that determines the force of the shaking operation", typeof(DoubleData), VariableKind.In));
39      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IGeneticProgrammingModel), VariableKind.In | VariableKind.Out));
40    }
41
42    public override IOperation Apply(IScope scope) {
43      FunctionLibrary library = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
44      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, false);
45      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
46
47      // save all existing sub-scopes in a backup scope
48      Scope backupScope = new Scope("backup");
49      foreach (Scope subScope in scope.SubScopes) {
50        backupScope.AddSubScope(subScope);
51      }
52
53      // create a scope for all shaking operations
54      Scope tempScope = new Scope("Temp. manipulation scope");
55      scope.AddSubScope(tempScope); // scope containing a subscope for each manipulation
56      scope.AddSubScope(backupScope); // scope containing the old subscopes
57
58      // create a composite operation for all shaking operations
59      CompositeOperation next = new CompositeOperation();
60      next.ExecuteInParallel = false;
61
62      // enqueue all shaking operations
63      foreach (IFunctionTree subTree in TreeGardener.GetAllSubTrees(gpModel.FunctionTree).Where(x=>x.HasLocalParameters)) {
64        IOperation shakingOperation = subTree.CreateShakingOperation(tempScope);
65        next.AddOperation(shakingOperation);
66      }
67
68      // schedule a reducer operation to delete all temporary scopes and restore
69      // the subscopes of the backup scope after all manipulations are finished.
70      next.AddOperation(new AtomicOperation(new RightReducer(), scope));
71      return next;
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.