Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Manipulators/ReplaceBranchManipulation.cs @ 5166

Last change on this file since 5166 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
33  [StorableClass]
34  [Item("ReplaceBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")]
35  public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator {
36    [StorableConstructor]
37    private ReplaceBranchManipulation(bool deserializing) : base(deserializing) { }
38    private ReplaceBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { }
39    public ReplaceBranchManipulation() : base() { }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new ReplaceBranchManipulation(this, cloner);
43    }
44
45    protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
46      ReplaceRandomBranch(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, out success);
47    }
48
49    public static void ReplaceRandomBranch(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight, out bool success) {
50      success = false;
51      // select any node as parent (except the root node)
52      var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
53                               from subtree in parent.SubTrees
54                               select new { Parent = parent, Node = subtree, Index = parent.SubTrees.IndexOf(subtree) }).SelectRandom(random);
55      // find possible symbols for the node (also considering the existing branches below it)
56      var allowedSymbols = from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index)
57                           select symbol;
58
59      if (allowedSymbols.Count() <= 1) {
60        return;
61      }
62      var oldBranch = manipulationPoint.Node;
63      int oldBranchSize = manipulationPoint.Node.GetSize();
64
65      var seedSymbol = SelectRandomSymbol(random, allowedSymbols);
66
67      // replace the old node with the new node
68      var seedNode = seedSymbol.CreateTreeNode();
69      if (seedNode.HasLocalParameters)
70        seedNode.ResetLocalParameters(random);
71
72      manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
73      manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, seedNode);
74      int maxSize = Math.Max(oldBranchSize, seedNode.Grammar.GetMinExpressionLength(seedNode.Symbol)) * 2;
75      var bla = ProbabilisticTreeCreator.PTC2(random, seedNode, maxSize, maxSize, 0, 0);
76      success = true;
77    }
78
79    private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {
80      var symbolList = symbols.ToList();
81      var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum();
82      if (ticketsSum == 0.0) throw new ArgumentException("The initial frequency of all allowed symbols is zero.");
83      var r = random.NextDouble() * ticketsSum;
84      double aggregatedTickets = 0;
85      for (int i = 0; i < symbolList.Count; i++) {
86        aggregatedTickets += symbolList[i].InitialFrequency;
87        if (aggregatedTickets > r) {
88          return symbolList[i];
89        }
90      }
91      // this should never happen
92      throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols.");
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.