Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ShakeMultiMoveGenerator.cs @ 8085

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

#1847 added operator for replacing branches with semantically similar branches

File size: 5.1 KB
Line 
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.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
33  [Item("ShakeMultiMoveGenerator", "Randomly samples n from all possible shake moves from a given tree.")]
34  [StorableClass]
35  public class ShakeMultiMoveGenerator : SingleSuccessorOperator, IStochasticOperator, ISymbolicExpressionTreeMoveOperator, IMultiMoveGenerator {
36    public ILookupParameter<IRandom> RandomParameter {
37      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
38    }
39    public IValueLookupParameter<IntValue> SampleSizeParameter {
40      get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
41    }
42
43    public IntValue SampleSize {
44      get { return SampleSizeParameter.Value; }
45      set { SampleSizeParameter.Value = value; }
46    }
47    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
48      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
49    }
50    public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter {
51      get { return (LookupParameter<ChangeNodeTypeMove>)Parameters["ChangeNodeTypeMove"]; }
52    }
53    protected ScopeParameter CurrentScopeParameter {
54      get { return (ScopeParameter)Parameters["CurrentScope"]; }
55    }
56
57    [StorableConstructor]
58    protected ShakeMultiMoveGenerator(bool deserializing) : base(deserializing) { }
59    protected ShakeMultiMoveGenerator(ShakeMultiMoveGenerator original, Cloner cloner) : base(original, cloner) { }
60    public ShakeMultiMoveGenerator()
61      : base() {
62      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
63      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
64
65      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
66      Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes."));
67      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
68
69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new ShakeMultiMoveGenerator(this, cloner);
73    }
74
75    public override IOperation Apply() {
76      var tree = SymbolicExpressionTreeParameter.ActualValue;
77      var random = RandomParameter.ActualValue;
78      int n = SampleSizeParameter.ActualValue.Value;
79      var moves = ChangeNodeTypeMultiMoveGenerator.GenerateMoves(tree, random, n);
80      string moveParameterName = ChangeNodeTypeMoveParameter.ActualName;
81      var moveScopes = new List<Scope>();
82
83      foreach (var m in moves) {
84        m.NewChild.ShakeLocalParameters(random, 1);
85        var moveScope = new Scope(moveScopes.Count.ToString());
86        moveScope.Variables.Add(new Variable(moveParameterName, m));
87        moveScopes.Add(moveScope);
88      }
89      CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
90      return base.Apply();
91    }
92
93    public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(ISymbolicExpressionTree tree, IRandom random, int n) {
94      int count = 0;
95      var g = tree.Root.Grammar;
96      var possibleChildren = (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
97                              from i in Enumerable.Range(0, parent.SubtreeCount)
98                              let currentChild = parent.GetSubtree(i)
99                              where currentChild.HasLocalParameters
100                              select new { parent, i, currentChild }).ToArray();
101      if (possibleChildren.Length < n) n = possibleChildren.Length;
102      while (count < n) {
103        var selected = possibleChildren[random.Next(possibleChildren.Length)];
104        yield return new ChangeNodeTypeMove(tree, selected.parent, selected.i, (ISymbolicExpressionTreeNode)selected.currentChild.Clone());
105        count++;
106      }
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.