Changeset 317 for trunk/sources
- Timestamp:
- 06/17/08 18:17:11 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Functions
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Functions/BakedFunctionTree.cs
r260 r317 31 31 32 32 namespace 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 33 48 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 37 56 public BakedFunctionTree() { 38 code = new List<int>(); 39 data = new List<double>(); 57 linearRepresentation = new List<LightWeightFunction>(); 40 58 } 41 59 42 60 internal BakedFunctionTree(IFunction function) 43 61 : 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); 47 65 treesExpanded = true; 48 66 subTrees = new List<IFunctionTree>(); … … 58 76 internal BakedFunctionTree(IFunctionTree tree) 59 77 : 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); 63 81 foreach(IVariable variable in tree.LocalVariables) { 64 82 IItem value = variable.Value; 65 data.Add(GetDoubleValue(value));83 fun.data.Add(GetDoubleValue(value)); 66 84 } 67 85 foreach(IFunctionTree subTree in tree.SubTrees) { … … 82 100 } 83 101 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; 90 105 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; 98 109 } 99 110 100 111 private void FlattenTrees() { 101 112 if(treesExpanded) { 102 code[0]= subTrees.Count;113 linearRepresentation[0].arity = subTrees.Count; 103 114 foreach(BakedFunctionTree subTree in subTrees) { 104 115 subTree.FlattenVariables(); 105 116 subTree.FlattenTrees(); 106 code.AddRange(subTree.code); 107 data.AddRange(subTree.data); 117 linearRepresentation.AddRange(subTree.linearRepresentation); 108 118 } 109 119 treesExpanded = false; … … 114 124 private void FlattenVariables() { 115 125 if(variablesExpanded) { 116 code[2] = variables.Count;126 linearRepresentation[0].data.Clear(); 117 127 foreach(IVariable variable in variables) { 118 data.Add(GetDoubleValue(variable.Value));128 linearRepresentation[0].data.Add(GetDoubleValue(variable.Value)); 119 129 } 120 130 variablesExpanded = false; … … 123 133 } 124 134 125 private bool treesExpanded = false;126 private List<IFunctionTree> subTrees;127 135 public IList<IFunctionTree> SubTrees { 128 136 get { 129 137 if(!treesExpanded) { 130 138 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; 135 141 for(int i = 0; i < arity; i++) { 136 142 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; 144 148 subTrees.Add(subTree); 145 149 } 146 150 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; 150 153 } 151 154 return subTrees; … … 153 156 } 154 157 155 private bool variablesExpanded = false;156 private List<IVariable> variables;157 158 public ICollection<IVariable> LocalVariables { 158 159 get { 159 160 if(!variablesExpanded) { 160 161 variables = new List<IVariable>(); 161 IFunction function = symbolTable.MapSymbol(code[1]);162 IFunction function = Function; 162 163 int localVariableIndex = 0; 163 164 foreach(IVariableInfo variableInfo in function.VariableInfos) { … … 166 167 IItem value = clone.Value; 167 168 if(value is ConstrainedDoubleData) { 168 ((ConstrainedDoubleData)value).Data = data[localVariableIndex];169 ((ConstrainedDoubleData)value).Data = linearRepresentation[0].data[localVariableIndex]; 169 170 } else if(value is ConstrainedIntData) { 170 ((ConstrainedIntData)value).Data = (int) data[localVariableIndex];171 ((ConstrainedIntData)value).Data = (int)linearRepresentation[0].data[localVariableIndex]; 171 172 } else if(value is DoubleData) { 172 ((DoubleData)value).Data = data[localVariableIndex];173 ((DoubleData)value).Data = linearRepresentation[0].data[localVariableIndex]; 173 174 } else if(value is IntData) { 174 ((IntData)value).Data = (int) data[localVariableIndex];175 ((IntData)value).Data = (int)linearRepresentation[0].data[localVariableIndex]; 175 176 } else throw new NotSupportedException("Invalid local variable type for GP."); 176 177 variables.Add(clone); … … 179 180 } 180 181 variablesExpanded = true; 181 code[2] = 0; 182 data.RemoveRange(0, variables.Count); 182 linearRepresentation[0].data.Clear(); 183 183 } 184 184 return variables; … … 187 187 188 188 public IFunction Function { 189 get { return symbolTable.MapSymbol(code[1]); }189 get { return linearRepresentation[0].functionType; } 190 190 } 191 191 … … 221 221 } 222 222 223 private BakedTreeEvaluator evaluator = null;224 223 public double Evaluate(Dataset dataset, int sampleIndex) { 225 224 FlattenVariables(); 226 225 FlattenTrees(); 227 if(evaluator == null) evaluator = new BakedTreeEvaluator( code, data);226 if(evaluator == null) evaluator = new BakedTreeEvaluator(linearRepresentation); 228 227 return evaluator.Evaluate(dataset, sampleIndex); 229 228 } … … 238 237 node.AppendChild(evaluatorNode); 239 238 } 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; 247 244 } 248 245 249 246 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 //} 279 277 280 278 public override object Clone(IDictionary<Guid, object> clonedObjects) { … … 283 281 if(treesExpanded) FlattenTrees(); 284 282 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 } 287 286 return clone; 288 287 } -
trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs
r308 r317 33 33 private double[] dataArr; 34 34 private static EvaluatorSymbolTable symbolTable = EvaluatorSymbolTable.SymbolTable; 35 36 // for persistence mechanism only37 public BakedTreeEvaluator() {38 }39 40 public BakedTreeEvaluator(List<int> code, List<double> data) {41 codeArr = code.ToArray();42 dataArr = data.ToArray();43 }44 45 35 private int PC; 46 36 private int DP; … … 48 38 private int sampleIndex; 49 39 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) { 51 58 PC = 0; 52 59 DP = 0; 53 sampleIndex = _sampleIndex;54 dataset = _dataset;60 this.sampleIndex = sampleIndex; 61 this.dataset = dataset; 55 62 return EvaluateBakedCode(); 56 63 }
Note: See TracChangeset
for help on using the changeset viewer.