Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/26/10 19:34:29 (14 years ago)
Author:
gkronber
Message:

Added work in progress for the artificial ant problem #952 (Artificial Ant Problem for 3.3) and for the symbolic expression tree encoding #937 (Data types and operators for symbolic expression tree encoding).

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  
    2525using HeuristicLab.Core;
    2626using System.Linq;
    27 
    28 namespace HeuristicLab.Encodings.SymbolicExpressionTree {
    29   public abstract class Symbol {
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28
     29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     30  [StorableClass]
     31  [Item("Symbol", "Represents a symbol in a symbolic function tree.")]
     32  public abstract class Symbol : Item {
    3033    private List<List<Symbol>> allowedSubFunctions = new List<List<Symbol>>();
    3134    private int minArity = -1;
     
    4447        if (value != name) {
    4548          name = value;
    46           FireChanged();
    47         }
    48       }
    49     }
    50 
    51     protected Function() {
     49        }
     50      }
     51    }
     52
     53    protected Symbol() {
    5254      name = this.GetType().Name;
    53     }
    54 
    55     public virtual string Description {
    56       get { return "Description for this function is missing (TODO)"; }
    5755    }
    5856
     
    6563        if (minArity != value) {
    6664          minArity = value;
    67           while (minArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<IFunction>());
     65          while (minArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<Symbol>());
    6866          ResetCachedValues();
    69           FireChanged();
    7067        }
    7168      }
     
    9289          }
    9390          ResetCachedValues();
    94           FireChanged();
    9591        }
    9692      }
     
    10298        if (minTreeSize <= 0) {
    10399          RecalculateMinimalTreeSize();
    104           FireChanged();
    105100        }
    106101        // Debug.Assert(minTreeSize > 0);
     
    113108        if (minTreeHeight <= 0) {
    114109          RecalculateMinimalTreeHeight();
    115           FireChanged();
    116110        }
    117111        // Debug.Assert(minTreeHeight > 0);
     
    126120        if (value != tickets) {
    127121          tickets = value;
    128           FireChanged();
    129122        }
    130123      }
     
    136129        if (initializer != value) {
    137130          initializer = value;
    138           FireChanged();
    139131        }
    140132      }
     
    146138        if (manipulator != value) {
    147139          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) {
    158149      if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
    159150      return allowedSubFunctions[index];
    160151    }
    161152
    162     public void AddAllowedSubFunction(IFunction function, int index) {
     153    public void AddAllowedSubFunction(Symbol symbol, int index) {
    163154      if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
    164155      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);
    169160      }
    170161      ResetCachedValues();
    171       FireChanged();
    172     }
    173     public void RemoveAllowedSubFunction(IFunction function, int index) {
     162    }
     163    public void RemoveAllowedSubFunction(Symbol symbol, int index) {
    174164      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);
    177167        ResetCachedValues();
    178         FireChanged();
    179168      }
    180169    }
     
    185174    }
    186175
    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);
    189178    }
    190179
     
    213202    }
    214203
    215     public override IView CreateView() {
    216       return new FunctionView(this);
    217     }
    218 
    219204    public override string ToString() {
    220205      return name;
    221206    }
    222 
    223     #region persistence
    224     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     #endregion
    288207  }
    289208}
Note: See TracChangeset for help on using the changeset viewer.