Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ShakeMoveGenerator.cs @ 11301

Last change on this file since 11301 was 7832, checked in by gkronber, 13 years ago

#1847 added operator for replacing branches with semantically similar branches

File size: 4.2 KB
RevLine 
[7802]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[7832]32  [Item("ShakeExhaustiveMoveGenerator", "Operator that generates moves for shaking a single symbolic expression tree node.")]
[7802]33  [StorableClass]
[7832]34  public class ShakeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IExhaustiveMoveGenerator, IStochasticOperator {
[7802]35    public override bool CanChangeName {
36      get { return false; }
37    }
38    public ILookupParameter<IRandom> RandomParameter {
39      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
40    }
[7832]41    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
42      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
[7802]43    }
44    public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter {
45      get { return (LookupParameter<ChangeNodeTypeMove>)Parameters["ChangeNodeTypeMove"]; }
46    }
47    protected ScopeParameter CurrentScopeParameter {
48      get { return (ScopeParameter)Parameters["CurrentScope"]; }
49    }
50
51    [StorableConstructor]
52    protected ShakeMoveGenerator(bool deserializing) : base(deserializing) { }
53    protected ShakeMoveGenerator(ShakeMoveGenerator original, Cloner cloner) : base(original, cloner) { }
54    public ShakeMoveGenerator()
55      : base() {
56      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
[7832]57      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
[7802]58      Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes."));
59      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new ShakeMoveGenerator(this, cloner);
64    }
65
66
67    public override IOperation Apply() {
[7832]68      var tree = SymbolicExpressionTreeParameter.ActualValue;
[7802]69      var moves = GenerateMoves(tree);
70      string moveParameterName = ChangeNodeTypeMoveParameter.ActualName;
71      var moveScopes = new List<Scope>();
72      var random = RandomParameter.ActualValue;
73      foreach (var m in moves) {
74        m.NewChild.ShakeLocalParameters(random, 1);
75        var moveScope = new Scope(moveScopes.Count.ToString());
76        moveScope.Variables.Add(new Variable(moveParameterName, m));
77        moveScopes.Add(moveScope);
78      }
79      CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
80      return base.Apply();
81    }
82
[7832]83    public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(ISymbolicExpressionTree tree) {
[7802]84      return (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
85              from i in Enumerable.Range(0, parent.SubtreeCount)
86              let currentChild = parent.GetSubtree(i)
87              where currentChild.HasLocalParameters
88              let newChild = (ISymbolicExpressionTreeNode)currentChild.Clone()
89              select new ChangeNodeTypeMove(tree, parent, i, newChild));
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.