Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.GP.Operators/3.3/Manipulation/OnePointShaker.cs @ 3962

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

Merged changes from GP-refactoring branch back into the trunk #713.

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.Linq;
23using HeuristicLab.Core;
24using HeuristicLab.Operators;
25using HeuristicLab.Random;
26using HeuristicLab.Data;
27using HeuristicLab.Selection;
28using HeuristicLab.GP.Interfaces;
29
30namespace HeuristicLab.GP.Operators {
31  public class OnePointShaker : DelegatingOperator {
32    public override string Description {
33      get { return "Selects a random node of all tree-nodes that have a manipulator defined and manipulates the selected node."; }
34    }
35
36    public OnePointShaker()
37      : base() {
38      AddVariableInfo(new VariableInfo("FunctionLibrary", "Function library that defines mutation operations for functions", typeof(FunctionLibrary), VariableKind.In));
39      AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In));
40      AddVariableInfo(new VariableInfo("ShakingFactor", "Factor that determines the force of the shaking operation", typeof(DoubleData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IGeneticProgrammingModel), VariableKind.In | VariableKind.Out));
42    }
43
44    public override IOperation Apply(IScope scope) {
45      FunctionLibrary library = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
46      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, false);
47      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
48
49      // get all nodes with local parameters
50      var parametricBranches = TreeGardener.GetAllSubTrees(gpModel.FunctionTree).Where(branch => branch.HasLocalParameters);
51      if (parametricBranches.Count() == 0) return null; // don't manipulate anything if there are no nodes with a manipulation operator
52      IFunctionTree selectedBranch = parametricBranches.ElementAt(mt.Next(parametricBranches.Count()));
53
54      // save the exising sub-scopes in a backup scope
55      Scope backupScope = new Scope("backup");
56      foreach (Scope subScope in scope.SubScopes) {
57        backupScope.AddSubScope(subScope);
58      }
59
60      // create a scope for all shaking operations
61      Scope tempScope = new Scope("Temp. manipulation scope");
62     
63      // add the new sub-scopes
64      scope.AddSubScope(tempScope);
65      scope.AddSubScope(backupScope);
66
67      CompositeOperation next = new CompositeOperation();
68      next.ExecuteInParallel = false;
69      // the next operation should first manipulate and then restore the sub-scopes from the backup scope
70      next.AddOperation(selectedBranch.CreateShakingOperation(tempScope));
71      next.AddOperation(new AtomicOperation(new RightReducer(), scope));
72
73      return next;
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.