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