[3223] | 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;
|
---|
[3294] | 23 | using System.Linq;
|
---|
[3223] | 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using System.Xml;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3244] | 29 | using HeuristicLab.Data;
|
---|
[3294] | 30 | using System.Diagnostics;
|
---|
[3223] | 31 |
|
---|
| 32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
| 33 | [StorableClass]
|
---|
[3252] | 34 | public class SymbolicExpressionTreeNode : ICloneable {
|
---|
| 35 | [Storable]
|
---|
[3223] | 36 | private List<SymbolicExpressionTreeNode> subTrees;
|
---|
[3252] | 37 | [Storable]
|
---|
[3223] | 38 | private Symbol symbol;
|
---|
| 39 |
|
---|
[3338] | 40 | public SymbolicExpressionTreeNode() {
|
---|
| 41 | subTrees = new List<SymbolicExpressionTreeNode>();
|
---|
| 42 | }
|
---|
[3223] | 43 |
|
---|
[3338] | 44 | public SymbolicExpressionTreeNode(Symbol symbol)
|
---|
| 45 | : this() {
|
---|
[3223] | 46 | this.symbol = symbol;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[3252] | 49 | // copy constructor
|
---|
| 50 | protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original) {
|
---|
| 51 | symbol = original.symbol;
|
---|
[3294] | 52 | subTrees = new List<SymbolicExpressionTreeNode>();
|
---|
[3338] | 53 | grammar = original.grammar;
|
---|
[3252] | 54 | foreach (var subtree in original.SubTrees) {
|
---|
[3338] | 55 | SubTrees.Add((SymbolicExpressionTreeNode)subtree.Clone());
|
---|
[3252] | 56 | }
|
---|
| 57 | }
|
---|
[3223] | 58 |
|
---|
[3244] | 59 | public virtual bool HasLocalParameters {
|
---|
[3223] | 60 | get { return false; }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public virtual IList<SymbolicExpressionTreeNode> SubTrees {
|
---|
| 64 | get { return subTrees; }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public Symbol Symbol {
|
---|
| 68 | get { return symbol; }
|
---|
| 69 | protected set { symbol = value; }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[3338] | 72 | private ISymbolicExpressionGrammar grammar;
|
---|
| 73 | public virtual ISymbolicExpressionGrammar Grammar {
|
---|
| 74 | get { return grammar; }
|
---|
| 75 | set {
|
---|
| 76 | grammar = value;
|
---|
| 77 | foreach (var subtree in subTrees)
|
---|
| 78 | subtree.Grammar = value;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[3244] | 82 | public int GetSize() {
|
---|
[3223] | 83 | int size = 1;
|
---|
| 84 | foreach (SymbolicExpressionTreeNode tree in SubTrees) size += tree.GetSize();
|
---|
| 85 | return size;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[3244] | 88 | public int GetHeight() {
|
---|
[3223] | 89 | int maxHeight = 0;
|
---|
| 90 | foreach (SymbolicExpressionTreeNode tree in SubTrees) maxHeight = Math.Max(maxHeight, tree.GetHeight());
|
---|
| 91 | return maxHeight + 1;
|
---|
| 92 | }
|
---|
[3294] | 93 |
|
---|
[3338] | 94 | public virtual void ResetLocalParameters(IRandom random) { }
|
---|
| 95 | public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
|
---|
| 96 |
|
---|
| 97 | public virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
|
---|
| 98 | SubTrees.Add(tree);
|
---|
| 99 | //if (tree != null)
|
---|
| 100 | tree.Grammar = Grammar;
|
---|
[3294] | 101 | }
|
---|
| 102 |
|
---|
[3338] | 103 | public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
|
---|
| 104 | SubTrees.Insert(index, tree);
|
---|
| 105 | //if (tree != null)
|
---|
| 106 | tree.Grammar = Grammar;
|
---|
[3294] | 107 | }
|
---|
| 108 |
|
---|
[3338] | 109 | public virtual void RemoveSubTree(int index) {
|
---|
| 110 | //if (SubTrees[index] != null)
|
---|
| 111 | SubTrees[index].Grammar = null;
|
---|
| 112 | SubTrees.RemoveAt(index);
|
---|
[3294] | 113 | }
|
---|
| 114 |
|
---|
[3338] | 115 | public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
|
---|
| 116 | yield return this;
|
---|
| 117 | foreach (var subtree in subTrees) {
|
---|
| 118 | foreach (var n in subtree.IterateNodesPrefix())
|
---|
| 119 | yield return n;
|
---|
| 120 | }
|
---|
[3294] | 121 | }
|
---|
| 122 |
|
---|
[3338] | 123 | public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
|
---|
| 124 | foreach (var subtree in subTrees) {
|
---|
| 125 | foreach (var n in subtree.IterateNodesPrefix())
|
---|
| 126 | yield return n;
|
---|
| 127 | }
|
---|
| 128 | yield return this;
|
---|
[3294] | 129 | }
|
---|
[3338] | 130 | //public int GetMinExpressionLength() {
|
---|
| 131 | // return Grammar.GetMinExpressionLength(Symbol);
|
---|
| 132 | //}
|
---|
| 133 | //public int GetMaxExpressionLength() {
|
---|
| 134 | // return Grammar.GetMaxExpressionLength(Symbol);
|
---|
| 135 | //}
|
---|
| 136 | //public int GetMinExpressionDepth() {
|
---|
| 137 | // return Grammar.GetMinExpressionDepth(Symbol);
|
---|
| 138 | //}
|
---|
| 139 | public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
|
---|
| 140 | return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
|
---|
[3294] | 141 | }
|
---|
[3338] | 142 | public int GetMinSubtreeCount() {
|
---|
| 143 | return Grammar.GetMinSubtreeCount(Symbol);
|
---|
[3223] | 144 | }
|
---|
[3338] | 145 | public int GetMaxSubtreeCount() {
|
---|
| 146 | return Grammar.GetMaxSubtreeCount(Symbol);
|
---|
[3223] | 147 | }
|
---|
[3338] | 148 | //public int GetMaxExpressionLength(Symbol s) {
|
---|
| 149 | // return Grammar.GetMaxExpressionLength(s);
|
---|
| 150 | //}
|
---|
| 151 | //public int GetMinExpressionLength(Symbol s) {
|
---|
| 152 | // return Grammar.GetMinExpressionLength(s);
|
---|
| 153 | //}
|
---|
| 154 | //public int GetMinExpressionDepth(Symbol s) {
|
---|
| 155 | // return Grammar.GetMinExpressionDepth(s);
|
---|
| 156 | //}
|
---|
[3223] | 157 |
|
---|
[3252] | 158 | #region ICloneable Members
|
---|
| 159 |
|
---|
| 160 | public virtual object Clone() {
|
---|
| 161 | return new SymbolicExpressionTreeNode(this);
|
---|
[3237] | 162 | }
|
---|
[3252] | 163 |
|
---|
| 164 | #endregion
|
---|
[3338] | 165 |
|
---|
| 166 |
|
---|
| 167 | public bool IsValidTree() {
|
---|
| 168 | var matchingSymbol = (from symb in Grammar.Symbols
|
---|
| 169 | where symb.Name == Symbol.Name
|
---|
| 170 | select symb).SingleOrDefault();
|
---|
| 171 |
|
---|
| 172 | if (SubTrees.Count < Grammar.GetMinSubtreeCount(matchingSymbol)) return false;
|
---|
| 173 | else if (SubTrees.Count > Grammar.GetMaxSubtreeCount(matchingSymbol)) return false;
|
---|
| 174 | else for (int i = 0; i < SubTrees.Count; i++) {
|
---|
| 175 | if (!GetAllowedSymbols(i).Select(x => x.Name).Contains(SubTrees[i].Symbol.Name)) return false;
|
---|
| 176 | if (!SubTrees[i].IsValidTree()) return false;
|
---|
| 177 | }
|
---|
| 178 | return true;
|
---|
| 179 | }
|
---|
[3223] | 180 | }
|
---|
| 181 | }
|
---|