Changeset 3223 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbol.cs
- Timestamp:
- 03/26/10 19:34:29 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbol.cs
r3219 r3223 25 25 using HeuristicLab.Core; 26 26 using System.Linq; 27 28 namespace HeuristicLab.Encodings.SymbolicExpressionTree { 29 public abstract class Symbol { 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 30 [StorableClass] 31 [Item("Symbol", "Represents a symbol in a symbolic function tree.")] 32 public abstract class Symbol : Item { 30 33 private List<List<Symbol>> allowedSubFunctions = new List<List<Symbol>>(); 31 34 private int minArity = -1; … … 44 47 if (value != name) { 45 48 name = value; 46 FireChanged(); 47 } 48 } 49 } 50 51 protected Function() { 49 } 50 } 51 } 52 53 protected Symbol() { 52 54 name = this.GetType().Name; 53 }54 55 public virtual string Description {56 get { return "Description for this function is missing (TODO)"; }57 55 } 58 56 … … 65 63 if (minArity != value) { 66 64 minArity = value; 67 while (minArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List< IFunction>());65 while (minArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<Symbol>()); 68 66 ResetCachedValues(); 69 FireChanged();70 67 } 71 68 } … … 92 89 } 93 90 ResetCachedValues(); 94 FireChanged();95 91 } 96 92 } … … 102 98 if (minTreeSize <= 0) { 103 99 RecalculateMinimalTreeSize(); 104 FireChanged();105 100 } 106 101 // Debug.Assert(minTreeSize > 0); … … 113 108 if (minTreeHeight <= 0) { 114 109 RecalculateMinimalTreeHeight(); 115 FireChanged();116 110 } 117 111 // Debug.Assert(minTreeHeight > 0); … … 126 120 if (value != tickets) { 127 121 tickets = value; 128 FireChanged();129 122 } 130 123 } … … 136 129 if (initializer != value) { 137 130 initializer = value; 138 FireChanged();139 131 } 140 132 } … … 146 138 if (manipulator != value) { 147 139 manipulator = value; 148 FireChanged(); 149 } 150 } 151 } 152 153 public virtual IFunctionTree GetTreeNode() { 154 return new FunctionTree(this); 155 } 156 157 public ICollection<IFunction> GetAllowedSubFunctions(int index) { 140 } 141 } 142 } 143 144 public virtual SymbolicExpressionTreeNode CreateTreeNode() { 145 return new SymbolicExpressionTreeNode(this); 146 } 147 148 public IEnumerable<Symbol> GetAllowedSubFunctions(int index) { 158 149 if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index); 159 150 return allowedSubFunctions[index]; 160 151 } 161 152 162 public void AddAllowedSubFunction( IFunction function, int index) {153 public void AddAllowedSubFunction(Symbol symbol, int index) { 163 154 if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index); 164 155 if (allowedSubFunctions[index] == null) { 165 allowedSubFunctions[index] = new List< IFunction>();166 } 167 if (!allowedSubFunctions[index].Contains( function)) {168 allowedSubFunctions[index].Add( function);156 allowedSubFunctions[index] = new List<Symbol>(); 157 } 158 if (!allowedSubFunctions[index].Contains(symbol)) { 159 allowedSubFunctions[index].Add(symbol); 169 160 } 170 161 ResetCachedValues(); 171 FireChanged(); 172 } 173 public void RemoveAllowedSubFunction(IFunction function, int index) { 162 } 163 public void RemoveAllowedSubFunction(Symbol symbol, int index) { 174 164 if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index); 175 if (allowedSubFunctions[index].Contains( function)) {176 allowedSubFunctions[index].Remove( function);165 if (allowedSubFunctions[index].Contains(symbol)) { 166 allowedSubFunctions[index].Remove(symbol); 177 167 ResetCachedValues(); 178 FireChanged();179 168 } 180 169 } … … 185 174 } 186 175 187 public bool IsAllowedSubFunction( IFunction function, int index) {188 return GetAllowedSubFunctions(index).Contains( function);176 public bool IsAllowedSubFunction(Symbol symbol, int index) { 177 return GetAllowedSubFunctions(index).Contains(symbol); 189 178 } 190 179 … … 213 202 } 214 203 215 public override IView CreateView() {216 return new FunctionView(this);217 }218 219 204 public override string ToString() { 220 205 return name; 221 206 } 222 223 #region persistence224 public override object Clone(IDictionary<Guid, object> clonedObjects) {225 Function clone = (Function)base.Clone(clonedObjects);226 if (initializer != null) clone.initializer = (IOperator)Auxiliary.Clone(initializer, clonedObjects);227 else clone.initializer = null;228 if (manipulator != null) clone.manipulator = (IOperator)Auxiliary.Clone(manipulator, clonedObjects);229 else clone.manipulator = null;230 clone.MaxSubTrees = maxArity;231 clone.MinSubTrees = minArity;232 clone.Tickets = tickets;233 clone.allowedSubFunctions.Clear();234 for (int i = 0; i < MaxSubTrees; i++) {235 var allowedSubFunctionsForSlot = new List<IFunction>();236 foreach (IFunction f in GetAllowedSubFunctions(i)) {237 allowedSubFunctionsForSlot.Add((IFunction)Auxiliary.Clone(f, clonedObjects));238 }239 clone.allowedSubFunctions.Add(allowedSubFunctionsForSlot);240 }241 return clone;242 }243 244 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {245 XmlNode node = base.GetXmlNode(name, document, persistedObjects);246 XmlAttribute minSubTreesAttr = document.CreateAttribute("MinSubTrees");247 minSubTreesAttr.Value = XmlConvert.ToString(MinSubTrees);248 XmlAttribute maxSubTreesAttr = document.CreateAttribute("MaxSubTrees");249 maxSubTreesAttr.Value = XmlConvert.ToString(MaxSubTrees);250 node.Attributes.Append(minSubTreesAttr);251 node.Attributes.Append(maxSubTreesAttr);252 if (initializer != null)253 node.AppendChild(PersistenceManager.Persist("Initializer", initializer, document, persistedObjects));254 if (manipulator != null)255 node.AppendChild(PersistenceManager.Persist("Manipulator", manipulator, document, persistedObjects));256 for (int i = 0; i < MaxSubTrees; i++) {257 XmlNode slotNode = document.CreateElement("AllowedSubFunctions");258 XmlAttribute slotAttr = document.CreateAttribute("Slot");259 slotAttr.Value = XmlConvert.ToString(i);260 slotNode.Attributes.Append(slotAttr);261 node.AppendChild(slotNode);262 foreach (IFunction f in GetAllowedSubFunctions(i)) {263 slotNode.AppendChild(PersistenceManager.Persist(f, document, persistedObjects));264 }265 }266 return node;267 }268 269 public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {270 base.Populate(node, restoredObjects);271 MinSubTrees = XmlConvert.ToInt32(node.Attributes["MinSubTrees"].Value);272 MaxSubTrees = XmlConvert.ToInt32(node.Attributes["MaxSubTrees"].Value);273 if (node.SelectSingleNode("Initializer") != null) {274 initializer = (IOperator)PersistenceManager.Restore(node.SelectSingleNode("Initializer"), restoredObjects);275 }276 if (node.SelectSingleNode("Manipulator") != null) {277 manipulator = (IOperator)PersistenceManager.Restore(node.SelectSingleNode("Manipulator"), restoredObjects);278 }279 foreach (var subFunctionsList in allowedSubFunctions) subFunctionsList.Clear();280 foreach (XmlNode allowedSubFunctionsNode in node.SelectNodes("AllowedSubFunctions")) {281 int slot = XmlConvert.ToInt32(allowedSubFunctionsNode.Attributes["Slot"].Value);282 foreach (XmlNode fNode in allowedSubFunctionsNode.ChildNodes) {283 AddAllowedSubFunction((IFunction)PersistenceManager.Restore(fNode, restoredObjects), slot);284 }285 }286 }287 #endregion288 207 } 289 208 }
Note: See TracChangeset
for help on using the changeset viewer.