Free cookie consent management tool by TermsFeed Policy Generator

Changeset 317 for trunk/sources


Ignore:
Timestamp:
06/17/08 18:17:11 (16 years ago)
Author:
gkronber
Message:

fixed the defect (ticket #168)

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

Legend:

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

    r260 r317  
    3131
    3232namespace HeuristicLab.Functions {
     33
     34  class LightWeightFunction {
     35    public int arity = 0;
     36    public IFunction functionType;
     37    public List<double> data = new List<double>();
     38
     39    public LightWeightFunction Clone() {
     40      LightWeightFunction clone = new LightWeightFunction();
     41      clone.arity = arity;
     42      clone.functionType = functionType;
     43      clone.data.AddRange(data);
     44      return clone;
     45    }
     46  }
     47
    3348  class BakedFunctionTree : ItemBase, IFunctionTree {
    34     private List<int> code;
    35     private List<double> data;
    36     private static EvaluatorSymbolTable symbolTable = EvaluatorSymbolTable.SymbolTable;
     49    private List<LightWeightFunction> linearRepresentation;
     50    private bool treesExpanded = false;
     51    private List<IFunctionTree> subTrees;
     52    private bool variablesExpanded = false;
     53    private List<IVariable> variables;
     54    private BakedTreeEvaluator evaluator = null;
     55
    3756    public BakedFunctionTree() {
    38       code = new List<int>();
    39       data = new List<double>();
     57      linearRepresentation = new List<LightWeightFunction>();
    4058    }
    4159
    4260    internal BakedFunctionTree(IFunction function)
    4361      : this() {
    44       code.Add(0);
    45       code.Add(symbolTable.MapFunction(function));
    46       code.Add(0);
     62      LightWeightFunction fun = new LightWeightFunction();
     63      fun.functionType = function;
     64      linearRepresentation.Add(fun);
    4765      treesExpanded = true;
    4866      subTrees = new List<IFunctionTree>();
     
    5876    internal BakedFunctionTree(IFunctionTree tree)
    5977      : this() {
    60       code.Add(0);
    61       code.Add(symbolTable.MapFunction(tree.Function));
    62       code.Add(tree.LocalVariables.Count);
     78      LightWeightFunction fun = new LightWeightFunction();
     79      fun.functionType = tree.Function;
     80      linearRepresentation.Add(fun);
    6381      foreach(IVariable variable in tree.LocalVariables) {
    6482        IItem value = variable.Value;
    65         data.Add(GetDoubleValue(value));
     83        fun.data.Add(GetDoubleValue(value));
    6684      }
    6785      foreach(IFunctionTree subTree in tree.SubTrees) {
     
    82100    }
    83101
    84     private void BranchLength(int branchRoot, out int codeLength, out int dataLength) {
    85       int arity = code[branchRoot];
    86       int nLocalVariables = code[branchRoot + 2];
    87       codeLength = 3;
    88       dataLength = nLocalVariables;
    89       int subBranchStart = branchRoot + codeLength;
     102    private int BranchLength(int branchRoot) {
     103      int arity = linearRepresentation[branchRoot].arity;
     104      int length = 1;
    90105      for(int i = 0; i < arity; i++) {
    91         int branchCodeLength;
    92         int branchDataLength;
    93         BranchLength(subBranchStart, out branchCodeLength, out branchDataLength);
    94         subBranchStart += branchCodeLength;
    95         codeLength += branchCodeLength;
    96         dataLength += branchDataLength;
    97       }
     106        length += BranchLength(branchRoot + length);
     107      }
     108      return length;
    98109    }
    99110
    100111    private void FlattenTrees() {
    101112      if(treesExpanded) {
    102         code[0] = subTrees.Count;
     113        linearRepresentation[0].arity = subTrees.Count;
    103114        foreach(BakedFunctionTree subTree in subTrees) {
    104115          subTree.FlattenVariables();
    105116          subTree.FlattenTrees();
    106           code.AddRange(subTree.code);
    107           data.AddRange(subTree.data);
     117          linearRepresentation.AddRange(subTree.linearRepresentation);
    108118        }
    109119        treesExpanded = false;
     
    114124    private void FlattenVariables() {
    115125      if(variablesExpanded) {
    116         code[2] = variables.Count;
     126        linearRepresentation[0].data.Clear();
    117127        foreach(IVariable variable in variables) {
    118           data.Add(GetDoubleValue(variable.Value));
     128          linearRepresentation[0].data.Add(GetDoubleValue(variable.Value));
    119129        }
    120130        variablesExpanded = false;
     
    123133    }
    124134
    125     private bool treesExpanded = false;
    126     private List<IFunctionTree> subTrees;
    127135    public IList<IFunctionTree> SubTrees {
    128136      get {
    129137        if(!treesExpanded) {
    130138          subTrees = new List<IFunctionTree>();
    131           int arity = code[0];
    132           int nLocalVariables = code[2];
    133           int branchIndex = 3;
    134           int dataIndex = nLocalVariables; // skip my local variables to reach the local variables of the first branch
     139          int arity = linearRepresentation[0].arity;
     140          int branchIndex = 1;
    135141          for(int i = 0; i < arity; i++) {
    136142            BakedFunctionTree subTree = new BakedFunctionTree();
    137             int codeLength;
    138             int dataLength;
    139             BranchLength(branchIndex, out codeLength, out dataLength);
    140             subTree.code = code.GetRange(branchIndex, codeLength);
    141             subTree.data = data.GetRange(dataIndex, dataLength);
    142             branchIndex += codeLength;
    143             dataIndex += dataLength;
     143            int length = BranchLength(branchIndex);
     144            for(int j = branchIndex; j < branchIndex + length; j++) {
     145              subTree.linearRepresentation.Add(linearRepresentation[j].Clone());
     146            }
     147            branchIndex += length;
    144148            subTrees.Add(subTree);
    145149          }
    146150          treesExpanded = true;
    147           code.RemoveRange(3, code.Count - 3);
    148           code[0] = 0;
    149           data.RemoveRange(nLocalVariables, data.Count - nLocalVariables);
     151          linearRepresentation.RemoveRange(1, linearRepresentation.Count - 1);
     152          linearRepresentation[0].arity = 0;
    150153        }
    151154        return subTrees;
     
    153156    }
    154157
    155     private bool variablesExpanded = false;
    156     private List<IVariable> variables;
    157158    public ICollection<IVariable> LocalVariables {
    158159      get {
    159160        if(!variablesExpanded) {
    160161          variables = new List<IVariable>();
    161           IFunction function = symbolTable.MapSymbol(code[1]);
     162          IFunction function = Function;
    162163          int localVariableIndex = 0;
    163164          foreach(IVariableInfo variableInfo in function.VariableInfos) {
     
    166167              IItem value = clone.Value;
    167168              if(value is ConstrainedDoubleData) {
    168                 ((ConstrainedDoubleData)value).Data = data[localVariableIndex];
     169                ((ConstrainedDoubleData)value).Data = linearRepresentation[0].data[localVariableIndex];
    169170              } else if(value is ConstrainedIntData) {
    170                 ((ConstrainedIntData)value).Data = (int)data[localVariableIndex];
     171                ((ConstrainedIntData)value).Data = (int)linearRepresentation[0].data[localVariableIndex];
    171172              } else if(value is DoubleData) {
    172                 ((DoubleData)value).Data = data[localVariableIndex];
     173                ((DoubleData)value).Data = linearRepresentation[0].data[localVariableIndex];
    173174              } else if(value is IntData) {
    174                 ((IntData)value).Data = (int)data[localVariableIndex];
     175                ((IntData)value).Data = (int)linearRepresentation[0].data[localVariableIndex];
    175176              } else throw new NotSupportedException("Invalid local variable type for GP.");
    176177              variables.Add(clone);
     
    179180          }
    180181          variablesExpanded = true;
    181           code[2] = 0;
    182           data.RemoveRange(0, variables.Count);
     182          linearRepresentation[0].data.Clear();
    183183        }
    184184        return variables;
     
    187187
    188188    public IFunction Function {
    189       get { return symbolTable.MapSymbol(code[1]); }
     189      get { return linearRepresentation[0].functionType; }
    190190    }
    191191
     
    221221    }
    222222
    223     private BakedTreeEvaluator evaluator = null;
    224223    public double Evaluate(Dataset dataset, int sampleIndex) {
    225224      FlattenVariables();
    226225      FlattenTrees();
    227       if(evaluator == null) evaluator = new BakedTreeEvaluator(code, data);
     226      if(evaluator == null) evaluator = new BakedTreeEvaluator(linearRepresentation);
    228227      return evaluator.Evaluate(dataset, sampleIndex);
    229228    }
     
    238237        node.AppendChild(evaluatorNode);
    239238      }
    240       XmlAttribute codeAttribute = document.CreateAttribute("Code");
    241       codeAttribute.Value = GetString<int>(code);
    242       node.Attributes.Append(codeAttribute);
    243       XmlAttribute dataAttribute = document.CreateAttribute("Data");
    244       dataAttribute.Value = GetString<double>(data);
    245       node.Attributes.Append(dataAttribute);
    246       return node;
     239      throw new NotImplementedException();
     240      //XmlAttribute codeAttribute = document.CreateAttribute("LinearRepresentation");
     241      //codeAttribute.Value = GetString<int>(code);
     242      //node.Attributes.Append(codeAttribute);
     243      //return node;
    247244    }
    248245
    249246    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    250       base.Populate(node, restoredObjects);
    251       XmlNode evaluatorNode = node.SelectSingleNode("Evaluator");
    252       if(evaluatorNode != null) {
    253         this.evaluator = (BakedTreeEvaluator)PersistenceManager.Restore(evaluatorNode, restoredObjects);
    254       }
    255       code = GetList<int>(node.Attributes["Code"].Value, s => int.Parse(s, CultureInfo.InvariantCulture));
    256       data = GetList<double>(node.Attributes["Data"].Value, s => double.Parse(s, CultureInfo.InvariantCulture));
    257       treesExpanded = false;
    258       variablesExpanded = false;
    259     }
    260 
    261     private string GetString<T>(IEnumerable<T> xs) where T : IConvertible {
    262       StringBuilder builder = new StringBuilder();
    263       foreach(T x in xs) {
    264         builder.Append(x.ToString(CultureInfo.InvariantCulture) + "; ");
    265       }
    266       if(builder.Length > 0) builder.Remove(builder.Length - 2, 2);
    267       return builder.ToString();
    268     }
    269 
    270     private List<T> GetList<T>(string s, Converter<string, T> converter) {
    271       List<T> result = new List<T>();
    272       string[] tokens = s.Split(new char[] {';',' '}, StringSplitOptions.RemoveEmptyEntries);
    273       foreach(string token in tokens) {
    274         T x = converter(token.Trim());
    275         result.Add(x);
    276       }
    277       return result;
    278     }
     247      throw new NotImplementedException();
     248      //base.Populate(node, restoredObjects);
     249      //XmlNode evaluatorNode = node.SelectSingleNode("Evaluator");
     250      //if(evaluatorNode != null) {
     251      //  this.evaluator = (BakedTreeEvaluator)PersistenceManager.Restore(evaluatorNode, restoredObjects);
     252      //}
     253      //code = GetList<int>(node.Attributes["Code"].Value, s => int.Parse(s, CultureInfo.InvariantCulture));
     254      //data = GetList<double>(node.Attributes["Data"].Value, s => double.Parse(s, CultureInfo.InvariantCulture));
     255      //treesExpanded = false;
     256      //variablesExpanded = false;
     257    }
     258
     259    //private string GetString<T>(IEnumerable<T> xs) where T : IConvertible {
     260    //  StringBuilder builder = new StringBuilder();
     261    //  foreach(T x in xs) {
     262    //    builder.Append(x.ToString(CultureInfo.InvariantCulture) + "; ");
     263    //  }
     264    //  if(builder.Length > 0) builder.Remove(builder.Length - 2, 2);
     265    //  return builder.ToString();
     266    //}
     267
     268    //private List<T> GetList<T>(string s, Converter<string, T> converter) {
     269    //  List<T> result = new List<T>();
     270    //  string[] tokens = s.Split(new char[] {';',' '}, StringSplitOptions.RemoveEmptyEntries);
     271    //  foreach(string token in tokens) {
     272    //    T x = converter(token.Trim());
     273    //    result.Add(x);
     274    //  }
     275    //  return result;
     276    //}
    279277
    280278    public override object Clone(IDictionary<Guid, object> clonedObjects) {
     
    283281      if(treesExpanded) FlattenTrees();
    284282      if(variablesExpanded) FlattenVariables();
    285       clone.code.AddRange(code);
    286       clone.data.AddRange(data);
     283      foreach(LightWeightFunction f in linearRepresentation) {
     284        clone.linearRepresentation.Add(f.Clone());
     285      }
    287286      return clone;
    288287    }
  • trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs

    r308 r317  
    3333    private double[] dataArr;
    3434    private static EvaluatorSymbolTable symbolTable = EvaluatorSymbolTable.SymbolTable;
    35 
    36     // for persistence mechanism only
    37     public BakedTreeEvaluator() {
    38     }
    39 
    40     public BakedTreeEvaluator(List<int> code, List<double> data) {
    41       codeArr = code.ToArray();
    42       dataArr = data.ToArray();
    43     }
    44 
    4535    private int PC;
    4636    private int DP;
     
    4838    private int sampleIndex;
    4939
    50     internal double Evaluate(Dataset _dataset, int _sampleIndex) {
     40    // for persistence mechanism only
     41    public BakedTreeEvaluator() {
     42    }
     43
     44    public BakedTreeEvaluator(List<LightWeightFunction> linearRepresentation) {
     45      List<int> code = new List<int>();
     46      List<double> data = new List<double>();
     47      foreach(LightWeightFunction fun in linearRepresentation) {
     48        code.Add(fun.arity);
     49        code.Add(symbolTable.MapFunction(fun.functionType));
     50        code.Add(fun.data.Count);
     51        data.AddRange(fun.data);
     52      }
     53      codeArr = code.ToArray();
     54      dataArr = data.ToArray();
     55    }
     56
     57    internal double Evaluate(Dataset dataset, int sampleIndex) {
    5158      PC = 0;
    5259      DP = 0;
    53       sampleIndex = _sampleIndex;
    54       dataset = _dataset;
     60      this.sampleIndex = sampleIndex;
     61      this.dataset = dataset;
    5562      return EvaluateBakedCode();
    5663    }
Note: See TracChangeset for help on using the changeset viewer.