Free cookie consent management tool by TermsFeed Policy Generator

Changeset 319 for trunk


Ignore:
Timestamp:
06/17/08 19:48:43 (16 years ago)
Author:
gkronber
Message:

simplified symbol-table mechanism (ticket #168)

Location:
trunk/sources/HeuristicLab.Functions
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Functions/BakedFunctionTree.cs

    r317 r319  
    233233      FlattenTrees();
    234234      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    235       if(evaluator != null) {
    236         XmlNode evaluatorNode = PersistenceManager.Persist("Evaluator", evaluator, document, persistedObjects);
    237         node.AppendChild(evaluatorNode);
    238       }
    239235      throw new NotImplementedException();
    240236      //XmlAttribute codeAttribute = document.CreateAttribute("LinearRepresentation");
  • trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs

    r318 r319  
    5858      Instr instr = new Instr();
    5959      instr.arity = f.arity;
    60       instr.symbol = EvaluatorSymbolTable.SymbolTable.MapFunction(f.functionType);
     60      instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
    6161      switch(instr.symbol) {
    6262        case EvaluatorSymbolTable.VARIABLE: {
     
    234234      throw new NotImplementedException();
    235235    }
    236 
    237     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    238       XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    239       node.AppendChild(PersistenceManager.Persist("SymbolTable", EvaluatorSymbolTable.SymbolTable, document, persistedObjects));
    240       return node;
    241     }
    242 
    243     public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    244       base.Populate(node, restoredObjects);
    245       PersistenceManager.Restore(node.SelectSingleNode("SymbolTable"), restoredObjects);
    246     }
    247236  }
    248237}
  • trunk/sources/HeuristicLab.Functions/SymbolTable.cs

    r309 r319  
    2929namespace HeuristicLab.Functions {
    3030  class EvaluatorSymbolTable : StorableBase{
    31     public const int ADDITION = 10010;
    32     public const int AND = 10020;
    33     public const int AVERAGE = 10030;
    34     public const int CONSTANT = 10040;
    35     public const int COSINUS = 10050;
    36     public const int DIVISION = 10060;
    37     public const int EQU = 10070;
    38     public const int EXP = 10080;
    39     public const int GT = 10090;
    40     public const int IFTE = 10100;
    41     public const int LT = 10110;
    42     public const int LOG = 10120;
    43     public const int MULTIPLICATION = 10130;
    44     public const int NOT = 10140;
    45     public const int OR = 10150;
    46     public const int POWER = 10160;
    47     public const int SIGNUM = 10170;
    48     public const int SINUS = 10180;
    49     public const int SQRT = 10190;
    50     public const int SUBTRACTION = 10200;
    51     public const int TANGENS = 10210;
    52     public const int VARIABLE = 10220;
    53     public const int XOR = 10230;
     31    public const int ADDITION = 1;
     32    public const int AND = 2;
     33    public const int AVERAGE = 3;
     34    public const int CONSTANT = 4;
     35    public const int COSINUS = 5;
     36    public const int DIVISION = 6;
     37    public const int EQU = 7;
     38    public const int EXP = 8;
     39    public const int GT = 9;
     40    public const int IFTE = 10;
     41    public const int LT = 11;
     42    public const int LOG = 12;
     43    public const int MULTIPLICATION = 13;
     44    public const int NOT = 14;
     45    public const int OR = 15;
     46    public const int POWER = 16;
     47    public const int SIGNUM = 17;
     48    public const int SINUS = 18;
     49    public const int SQRT = 19;
     50    public const int SUBTRACTION = 20;
     51    public const int TANGENS = 21;
     52    public const int VARIABLE = 22;
     53    public const int XOR = 23;
     54    public const int UNKNOWN = 24;
    5455
    55     private int nextFunctionSymbol = 10240;
    56     private Dictionary<int, IFunction> table;
    57     private Dictionary<IFunction, int> reverseTable;
    58     private Dictionary<Type, int> staticTypes;
    59 
    60     private static EvaluatorSymbolTable symbolTable = new EvaluatorSymbolTable();
    61     public static EvaluatorSymbolTable SymbolTable {
    62       get { return EvaluatorSymbolTable.symbolTable; }
    63     }
     56    private static Dictionary<Type, int> staticTypes = new Dictionary<Type,int>();
    6457
    6558    // needs to be public for persistence mechanism (Activator.CreateInstance needs empty constructor)
    66     public EvaluatorSymbolTable () {
    67       table = new Dictionary<int, IFunction>();
    68       reverseTable = new Dictionary<IFunction, int>();
     59    static EvaluatorSymbolTable () {
    6960      staticTypes = new Dictionary<Type, int>();
    7061      staticTypes[typeof(Addition)] = ADDITION;
     
    9384    }
    9485
    95     internal int MapFunction(IFunction function) {
    96       if(!reverseTable.ContainsKey(function)) {
    97         int curFunctionSymbol;
    98         if(staticTypes.ContainsKey(function.GetType())) curFunctionSymbol = staticTypes[function.GetType()];
    99         else {
    100           curFunctionSymbol = nextFunctionSymbol;
    101           nextFunctionSymbol++;
    102         }
    103         reverseTable[function] = curFunctionSymbol;
    104         table[curFunctionSymbol] = function;
    105       }
    106       return reverseTable[function];
    107     }
    108 
    109     internal IFunction MapSymbol(int symbol) {
    110       return table[symbol];
    111     }
    112 
    113     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    114       XmlNode node =  base.GetXmlNode(name, document, persistedObjects);
    115       XmlAttribute nextFunctionSymbolAttribute = document.CreateAttribute("NextFunctionSymbol");
    116       nextFunctionSymbolAttribute.Value = nextFunctionSymbol.ToString();
    117       node.Attributes.Append(nextFunctionSymbolAttribute);
    118       XmlNode symbolTableNode = document.CreateNode(XmlNodeType.Element, "Table", null);
    119       foreach(KeyValuePair<int, IFunction> entry in table) {
    120         XmlNode entryNode = PersistenceManager.Persist("Entry", entry.Value, document, persistedObjects);
    121         XmlAttribute symbolAttr = document.CreateAttribute("Symbol");
    122         symbolAttr.Value = entry.Key.ToString();
    123         entryNode.Attributes.Append(symbolAttr);
    124         symbolTableNode.AppendChild(entryNode);
    125       }
    126       node.AppendChild(symbolTableNode);
    127       return node;
    128     }
    129 
    130     public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    131       if(this == symbolTable) {
    132         base.Populate(node, restoredObjects);
    133         table.Clear();
    134         reverseTable.Clear();
    135         nextFunctionSymbol = int.Parse(node.Attributes["NextFunctionSymbol"].Value);
    136         XmlNode symbolTableNode = node.SelectSingleNode("Table");
    137         foreach(XmlNode entry in symbolTableNode.ChildNodes) {
    138           IFunction function = (IFunction)PersistenceManager.Restore(entry, restoredObjects);
    139           int symbol = int.Parse(entry.Attributes["Symbol"].Value);
    140           table[symbol] = function;
    141           reverseTable[function] = symbol;
    142         }
    143       } else {
    144         symbolTable.Populate(node, restoredObjects);
    145       }
     86    internal static int MapFunction(IFunction function) {
     87      return staticTypes[function.GetType()];
    14688    }
    14789  }
Note: See TracChangeset for help on using the changeset viewer.