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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Random;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Selection;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.GP {
|
---|
33 | public class OnePointShaker : DelegatingOperator {
|
---|
34 | public override string Description {
|
---|
35 | get { return "Selects a random node of all tree-nodes that have a '"+FunctionBase.MANIPULATION+"' variable defined and manipulates the selected node."; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public OnePointShaker()
|
---|
39 | : base() {
|
---|
40 | AddVariableInfo(new VariableInfo("OperatorLibrary", "Operator library that defines mutation operations for operators", typeof(GPOperatorLibrary), VariableKind.In));
|
---|
41 | AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In));
|
---|
42 | AddVariableInfo(new VariableInfo("ShakingFactor", "Factor that determines the force of the shaking operation", typeof(DoubleData), VariableKind.In));
|
---|
43 | AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be mutated", typeof(IFunctionTree), VariableKind.In | VariableKind.Out));
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IOperation Apply(IScope scope) {
|
---|
47 | GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
|
---|
48 | IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, false);
|
---|
49 | MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
|
---|
50 | TreeGardener gardener = new TreeGardener(mt, library);
|
---|
51 |
|
---|
52 | // get all nodes for which a manipulation is defined
|
---|
53 | var parametricBranches = gardener.GetAllSubTrees(tree).Where(branch => branch.Function.GetVariable(FunctionBase.MANIPULATION) != null);
|
---|
54 | IFunctionTree selectedBranch = parametricBranches.ElementAt(mt.Next(parametricBranches.Count()));
|
---|
55 | IOperator mutation = (IOperator)selectedBranch.Function.GetVariable(FunctionBase.MANIPULATION).Value;
|
---|
56 | CompositeOperation next = new CompositeOperation();
|
---|
57 |
|
---|
58 | // store all local variables into a temporary scope
|
---|
59 | Scope tempScope = new Scope("Temp. manipulation scope");
|
---|
60 | foreach(IVariable variable in selectedBranch.LocalVariables) {
|
---|
61 | tempScope.AddVariable(variable);
|
---|
62 | }
|
---|
63 |
|
---|
64 | // save the exising sub-scops in a backup scope
|
---|
65 | Scope backupScope = new Scope("backup");
|
---|
66 | foreach (Scope subScope in scope.SubScopes) {
|
---|
67 | backupScope.AddSubScope(subScope);
|
---|
68 | }
|
---|
69 |
|
---|
70 | // add the new sub-scopes
|
---|
71 | scope.AddSubScope(tempScope);
|
---|
72 | scope.AddSubScope(backupScope);
|
---|
73 |
|
---|
74 | // the next operation should first manipulate and then restore the sub-scopes from the backup scope
|
---|
75 | next.AddOperation(new AtomicOperation(mutation, tempScope));
|
---|
76 | next.AddOperation(new AtomicOperation(new RightReducer(), scope));
|
---|
77 |
|
---|
78 | return next;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|