Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ChangeNodeTypeMoveGenerator.cs @ 7802

Last change on this file since 7802 was 7802, checked in by gkronber, 12 years ago

#1847 added initial implementation of move operators for symbolic expression tree encoding

File size: 4.8 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.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  [Item("ChangeNodeTypeMoveGenerator", "Operator that generates moves for changing the symbol of a single symbolic expression tree node.")]
33  [StorableClass]
34  public class ChangeNodeTypeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IMoveGenerator, IStochasticOperator {
35    public override bool CanChangeName {
36      get { return false; }
37    }
38    public ILookupParameter<IRandom> RandomParameter {
39      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
40    }
41    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
42      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
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 ChangeNodeTypeMoveGenerator(bool deserializing) : base(deserializing) { }
53    protected ChangeNodeTypeMoveGenerator(ChangeNodeTypeMoveGenerator original, Cloner cloner) : base(original, cloner) { }
54    public ChangeNodeTypeMoveGenerator()
55      : base() {
56      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
57      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
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 ChangeNodeTypeMoveGenerator(this, cloner);
64    }
65
66
67    public override IOperation Apply() {
68      SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
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        if (m.NewChild.HasLocalParameters)
75          m.NewChild.ResetLocalParameters(random);
76        var moveScope = new Scope(moveScopes.Count.ToString());
77        moveScope.Variables.Add(new Variable(moveParameterName, m));
78        moveScopes.Add(moveScope);
79      }
80      CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
81      return base.Apply();
82    }
83
84    public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(SymbolicExpressionTree tree) {
85      var g = tree.Root.Grammar;
86      return (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
87              from i in Enumerable.Range(0, parent.SubtreeCount)
88              from s in g.GetAllowedChildSymbols(parent.Symbol, i)
89              where s.Enabled
90              where s.InitialFrequency > 0.0
91              let currentChild = parent.GetSubtree(i)
92              where s.MinimumArity >= currentChild.SubtreeCount
93              where s.MaximumArity <= currentChild.SubtreeCount
94              where (from j in Enumerable.Range(0, currentChild.SubtreeCount)
95                     let childOfChild = currentChild.GetSubtree(j)
96                     select g.IsAllowedChildSymbol(s, childOfChild.Symbol, j)).All(p => p)
97              let newChild = s.CreateTreeNode()
98              select new ChangeNodeTypeMove(tree, parent, i, newChild));
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.