Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/Manipulation/FullTreeShaker.cs @ 2210

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

GP Refactoring #713

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