- Timestamp:
- 04/05/10 18:52:23 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbol.cs
r3223 r3269 26 26 using System.Linq; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Data; 29 using HeuristicLab.Parameters; 28 30 29 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 30 32 [StorableClass] 31 33 [Item("Symbol", "Represents a symbol in a symbolic function tree.")] 32 public abstract class Symbol : Item { 33 private List<List<Symbol>> allowedSubFunctions = new List<List<Symbol>>(); 34 private int minArity = -1; 35 private int maxArity = -1; 36 private double tickets = 1.0; 37 private IOperator initializer; 38 private IOperator manipulator; 39 private int minTreeHeight = -1; 40 private int minTreeSize = -1; 34 public abstract class Symbol : ParameterizedNamedItem { 35 #region Parameter Properties 36 public IValueParameter<DoubleValue> TicketsParameter { 37 get { return (IValueParameter<DoubleValue>)Parameters["Tickets"]; } 38 } 39 #endregion 41 40 42 private string name;43 public virtual string Name{44 get { return name; }41 #region Properties 42 public DoubleValue Tickets { 43 get { return TicketsParameter.Value; } 45 44 set { 46 if (string.IsNullOrEmpty(value)) throw new ArgumentException(); 47 if (value != name) { 48 name = value; 49 } 45 if (value.Value < 0.0) throw new ArgumentException("Number of tickets must be positive"); 46 TicketsParameter.Value = value; 50 47 } 51 48 } 49 #endregion 52 50 53 protected Symbol() { 54 name = this.GetType().Name; 55 } 56 57 public int MinSubTrees { 58 get { 59 return minArity; 60 } 61 protected internal set { 62 if (value < 0) throw new ArgumentException(); 63 if (minArity != value) { 64 minArity = value; 65 while (minArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<Symbol>()); 66 ResetCachedValues(); 67 } 68 } 69 } 70 71 public int MaxSubTrees { 72 get { 73 return maxArity; 74 } 75 protected internal set { 76 if (value < 0) throw new ArgumentException(); 77 if (value < minArity) throw new ArgumentException(); 78 if (value != maxArity) { 79 maxArity = value; 80 while (allowedSubFunctions.Count > maxArity) allowedSubFunctions.RemoveAt(allowedSubFunctions.Count - 1); 81 while (maxArity > allowedSubFunctions.Count) { 82 if (allowedSubFunctions.Count > 0) { 83 // copy the list of allowed sub-functions from the previous slot 84 allowedSubFunctions.Add(new List<Symbol>(allowedSubFunctions[allowedSubFunctions.Count - 1])); 85 } else { 86 // add empty list 87 allowedSubFunctions.Add(new List<Symbol>()); 88 } 89 } 90 ResetCachedValues(); 91 } 92 } 93 } 94 95 96 public int MinTreeSize { 97 get { 98 if (minTreeSize <= 0) { 99 RecalculateMinimalTreeSize(); 100 } 101 // Debug.Assert(minTreeSize > 0); 102 return minTreeSize; 103 } 104 } 105 106 public int MinTreeHeight { 107 get { 108 if (minTreeHeight <= 0) { 109 RecalculateMinimalTreeHeight(); 110 } 111 // Debug.Assert(minTreeHeight > 0); 112 return minTreeHeight; 113 } 114 } 115 116 public double Tickets { 117 get { return tickets; } 118 set { 119 if (value < 0.0) throw new ArgumentException("Number of tickets must be positive"); 120 if (value != tickets) { 121 tickets = value; 122 } 123 } 124 } 125 126 public IOperator Initializer { 127 get { return initializer; } 128 set { 129 if (initializer != value) { 130 initializer = value; 131 } 132 } 133 } 134 135 public IOperator Manipulator { 136 get { return manipulator; } 137 set { 138 if (manipulator != value) { 139 manipulator = value; 140 } 141 } 51 protected Symbol() 52 : base() { 53 Parameters.Add(new ValueParameter<DoubleValue>("Tickets", new DoubleValue(1.0))); 142 54 } 143 55 … … 145 57 return new SymbolicExpressionTreeNode(this); 146 58 } 147 148 public IEnumerable<Symbol> GetAllowedSubFunctions(int index) {149 if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);150 return allowedSubFunctions[index];151 }152 153 public void AddAllowedSubFunction(Symbol symbol, int index) {154 if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);155 if (allowedSubFunctions[index] == null) {156 allowedSubFunctions[index] = new List<Symbol>();157 }158 if (!allowedSubFunctions[index].Contains(symbol)) {159 allowedSubFunctions[index].Add(symbol);160 }161 ResetCachedValues();162 }163 public void RemoveAllowedSubFunction(Symbol symbol, int index) {164 if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);165 if (allowedSubFunctions[index].Contains(symbol)) {166 allowedSubFunctions[index].Remove(symbol);167 ResetCachedValues();168 }169 }170 171 private void ResetCachedValues() {172 minTreeHeight = -1;173 minTreeSize = -1;174 }175 176 public bool IsAllowedSubFunction(Symbol symbol, int index) {177 return GetAllowedSubFunctions(index).Contains(symbol);178 }179 180 private void RecalculateMinimalTreeSize() {181 if (MinSubTrees == 0) minTreeSize = 1;182 else {183 minTreeSize = int.MaxValue; // prevent infinite recursion184 minTreeSize = 1 + (from slot in Enumerable.Range(0, MinSubTrees)185 let minForSlot = (from function in GetAllowedSubFunctions(slot)186 where function != this187 select function.MinTreeSize).DefaultIfEmpty(0).Min()188 select minForSlot).Sum();189 }190 }191 192 private void RecalculateMinimalTreeHeight() {193 if (MinSubTrees == 0) minTreeHeight = 1;194 else {195 minTreeHeight = int.MaxValue;196 minTreeHeight = 1 + (from slot in Enumerable.Range(0, MinSubTrees)197 let minForSlot = (from function in GetAllowedSubFunctions(slot)198 where function != this199 select function.MinTreeHeight).DefaultIfEmpty(0).Min()200 select minForSlot).Max();201 }202 }203 204 public override string ToString() {205 return name;206 }207 59 } 208 60 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCreator.cs
r3239 r3269 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Data; … … 72 73 SymbolicExpressionTreeParameter.ActualValue = Create(RandomParameter.ActualValue, SymbolicExpressionGrammarParameter.ActualValue, 73 74 MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue); 74 return base.Apply(); 75 76 foreach (var node in SymbolicExpressionTreeParameter.ActualValue.IterateNodesPostfix()) { 77 node.ResetLocalParameters(RandomParameter.ActualValue); 78 } 79 return null; 75 80 } 76 81 -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs
r3252 r3269 76 76 return maxHeight + 1; 77 77 } 78 79 //public virtual IOperation CreateShakingOperation(IScope scope) { 80 // return null; 81 //} 82 83 //public virtual IOperation CreateInitOperation(IScope scope) { 84 // return null; 85 //} 78 79 public virtual void ResetLocalParameters(IRandom random) { } 80 public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { } 86 81 87 82 protected internal virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
Note: See TracChangeset
for help on using the changeset viewer.