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.StructureIdentification {
|
---|
33 | public class FullTreeShaker : OperatorBase {
|
---|
34 | public override string Description {
|
---|
35 | get { return "Manipulates all tree nodes for which a '"+GPOperatorLibrary.MANIPULATION+"' variable is defined."; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public FullTreeShaker()
|
---|
39 | : base() {
|
---|
40 | AddVariableInfo(new VariableInfo("OperatorLibrary", "Operator library that defines operator mutations", typeof(GPOperatorLibrary), VariableKind.In));
|
---|
41 | AddVariableInfo(new VariableInfo("OperatorTree", "The operator tree that should be mutated", typeof(IOperator), VariableKind.In));
|
---|
42 | AddVariableInfo(new VariableInfo("Random", "A random generator (uniform)", typeof(MersenneTwister), VariableKind.In));
|
---|
43 | AddVariableInfo(new VariableInfo("ShakingFactor", "Variable that determines the force of the shakeing operation", typeof(DoubleData), VariableKind.In));
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IOperation Apply(IScope scope) {
|
---|
47 | GPOperatorLibrary library = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
|
---|
48 | IOperator op = GetVariableValue<IOperator>("OperatorTree", scope, false);
|
---|
49 | MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
|
---|
50 |
|
---|
51 | // enqueue all mutation operations as parallel operations
|
---|
52 | CompositeOperation next = new CompositeOperation();
|
---|
53 | next.ExecuteInParallel = true;
|
---|
54 |
|
---|
55 | Scope tempScope = new Scope("Temp. manipulation scope");
|
---|
56 |
|
---|
57 | TreeGardener gardener = new TreeGardener(mt, library);
|
---|
58 | var parametricNodes = gardener.GetAllOperators(op).Where(o => o.GetVariable(GPOperatorLibrary.MANIPULATION) != null);
|
---|
59 | foreach(IOperator subOperator in parametricNodes) {
|
---|
60 | IOperator mutation =(IOperator)subOperator.GetVariable(GPOperatorLibrary.MANIPULATION).Value;
|
---|
61 |
|
---|
62 | // store all local variables into a temporary scope
|
---|
63 | Scope mutationScope = new Scope();
|
---|
64 | foreach(IVariableInfo info in subOperator.VariableInfos) {
|
---|
65 | if(info.Local) {
|
---|
66 | mutationScope.AddVariable(subOperator.GetVariable(info.FormalName));
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | tempScope.AddSubScope(mutationScope);
|
---|
71 | next.AddOperation(new AtomicOperation(mutation, mutationScope));
|
---|
72 | }
|
---|
73 |
|
---|
74 | // save all existing sub-scopes in a backup scope
|
---|
75 | Scope backupScope = new Scope("backup");
|
---|
76 | foreach (Scope subScope in scope.SubScopes) {
|
---|
77 | backupScope.AddSubScope(subScope);
|
---|
78 | }
|
---|
79 |
|
---|
80 | scope.AddSubScope(tempScope); // scope containing a subscope for each manipulation
|
---|
81 | scope.AddSubScope(backupScope); // scope containing the old subscopes
|
---|
82 |
|
---|
83 | // schedule a reducer operation to delete all temporary scopes and restore
|
---|
84 | // the subscopes of the backup scope after all manipulations are finished.
|
---|
85 | next.AddOperation(new AtomicOperation(new RightReducer(), scope));
|
---|
86 | return next;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|