- Timestamp:
- 07/28/09 19:24:23 (15 years ago)
- Location:
- branches/GP-Refactoring-713
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/BakedFunctionTree.cs
r2174 r2202 34 34 public byte arity = 0; 35 35 public IFunction functionType; 36 public List< object> localData = new List<object>();36 public List<IVariable> localData = new List<IVariable>(); 37 37 38 38 public LightWeightFunction Clone() { … … 40 40 clone.arity = arity; 41 41 clone.functionType = functionType; 42 clone.localData .AddRange(localData);42 clone.localData = localData.Select(v=>(IVariable)v.Clone()).ToList(); 43 43 return clone; 44 44 } … … 57 57 private List<IFunctionTree> subTrees; 58 58 private bool variablesExpanded = false; 59 private List<IVariable> variables; 59 private List<IVariable> variables = new List<IVariable>(); 60 61 public IOperator Initializer { 62 get { return Function.Initializer; } 63 } 64 65 public IOperator Manipulator { 66 get { return Function.Manipulator; } 67 } 60 68 61 69 public BakedFunctionTree() { … … 63 71 } 64 72 65 internalBakedFunctionTree(IFunction function)73 public BakedFunctionTree(IFunction function) 66 74 : this() { 67 75 LightWeightFunction fun = new LightWeightFunction(); … … 72 80 variables = new List<IVariable>(); 73 81 variablesExpanded = true; 74 foreach (IVariableInfo variableInfo in function.VariableInfos) { 75 if (variableInfo.Local) { 76 variables.Add((IVariable)function.GetVariable(variableInfo.FormalName).Clone()); 77 } 78 } 79 } 80 81 internal BakedFunctionTree(IFunctionTree tree) 82 : this() { 83 LightWeightFunction fun = new LightWeightFunction(); 84 fun.functionType = tree.Function; 85 linearRepresentation.Add(fun); 86 foreach (IVariable variable in tree.LocalVariables) { 87 IObjectData value = (IObjectData)variable.Value; 88 fun.localData.Add(value.Data); 89 } 90 foreach (IFunctionTree subTree in tree.SubTrees) { 91 AddSubTree(new BakedFunctionTree(subTree)); 92 } 93 } 82 } 83 84 //internal BakedFunctionTree(IFunctionTree tree) 85 // : this() { 86 // LightWeightFunction fun = new LightWeightFunction(); 87 // fun.functionType = tree.Function; 88 // linearRepresentation.Add(fun); 89 // foreach (IVariable variable in tree.LocalVariables) { 90 // IObjectData value = (IObjectData)variable.Value; 91 // fun.localData.Add(value.Data); 92 // } 93 // foreach (IFunctionTree subTree in tree.SubTrees) { 94 // AddSubTree(new BakedFunctionTree(subTree)); 95 // } 96 //} 94 97 95 98 private int BranchLength(int branchRoot) { … … 119 122 linearRepresentation[0].localData.Clear(); 120 123 foreach (IVariable variable in variables) { 121 object objData = variable.Value; 122 while (objData is IObjectData) objData = ((IObjectData)objData).Data; 123 linearRepresentation[0].localData.Add(objData); 124 //object objData = variable.Value; 125 //while (objData is IObjectData) objData = ((IObjectData)objData).Data; 126 //linearRepresentation[0].localData[variable.Name] = objData; 127 linearRepresentation[0].localData.Add(variable); 124 128 } 125 129 variablesExpanded = false; … … 197 201 if (!variablesExpanded) { 198 202 variables = new List<IVariable>(); 199 IFunction function = Function; 200 int localVariableIndex = 0; 201 foreach (IVariableInfo variableInfo in function.VariableInfos) { 202 if (variableInfo.Local) { 203 IVariable clone = (IVariable)function.GetVariable(variableInfo.FormalName).Clone(); 204 IObjectData objData = (IObjectData)clone.Value; 205 if (objData is ConstrainedDoubleData) { 206 ((ConstrainedDoubleData)objData).Data = (double)linearRepresentation[0].localData[localVariableIndex]; 207 } else if (objData is ConstrainedIntData) { 208 ((ConstrainedIntData)objData).Data = (int)linearRepresentation[0].localData[localVariableIndex]; 209 } else { 210 objData.Data = linearRepresentation[0].localData[localVariableIndex]; 211 } 212 variables.Add(clone); 213 localVariableIndex++; 214 } 203 IFunction function = Function; 204 //int localVariableIndex = 0; 205 foreach (IVariable v in linearRepresentation[0].localData) { 206 //IObjectData objData = (IObjectData)clone.Value; 207 //if (objData is ConstrainedDoubleData) { 208 // ((ConstrainedDoubleData)objData).Data = (double)linearRepresentation[0].localData[localVariableIndex]; 209 //} else if (objData is ConstrainedIntData) { 210 // ((ConstrainedIntData)objData).Data = (int)linearRepresentation[0].localData[localVariableIndex]; 211 //} else { 212 // objData.Data = linearRepresentation[0].localData[localVariableIndex]; 213 //} 214 variables.Add(v); 215 //localVariableIndex++; 215 216 } 216 217 variablesExpanded = true; … … 233 234 234 235 public void AddVariable(IVariable variable) { 235 throw new NotSupportedException(); 236 if (!variablesExpanded) throw new InvalidOperationException(); 237 if (!variables.Exists(v => v.Name == variable.Name)) 238 variables.Add(variable); 236 239 } 237 240 238 241 public void RemoveVariable(string name) { 239 throw new NotSupportedException(); 242 if (!variablesExpanded) throw new InvalidOperationException(); 243 variables.RemoveAll(v => v.Name == name); 240 244 } 241 245 … … 256 260 } 257 261 258 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {259 FlattenVariables();260 FlattenTrees();261 XmlNode node = base.GetXmlNode(name, document, persistedObjects);262 XmlNode linearRepresentationNode = document.CreateElement("LinearRepresentation");263 foreach (LightWeightFunction f in linearRepresentation) {264 XmlNode entryNode = PersistenceManager.Persist("Function", f.functionType, document, persistedObjects);265 XmlAttribute arityAttribute = document.CreateAttribute("Arity");266 arityAttribute.Value = XmlConvert.ToString(f.arity);267 entryNode.Attributes.Append(arityAttribute);268 foreach (object o in f.localData) {269 if (f.localData.Count > 0) {270 entryNode.AppendChild(CreateDataNode(document, o));271 }272 }273 linearRepresentationNode.AppendChild(entryNode);274 }275 276 node.AppendChild(linearRepresentationNode);277 return node;278 }262 //public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { 263 // FlattenVariables(); 264 // FlattenTrees(); 265 // XmlNode node = base.GetXmlNode(name, document, persistedObjects); 266 // XmlNode linearRepresentationNode = document.CreateElement("LinearRepresentation"); 267 // foreach (LightWeightFunction f in linearRepresentation) { 268 // XmlNode entryNode = PersistenceManager.Persist("Function", f.functionType, document, persistedObjects); 269 // XmlAttribute arityAttribute = document.CreateAttribute("Arity"); 270 // arityAttribute.Value = XmlConvert.ToString(f.arity); 271 // entryNode.Attributes.Append(arityAttribute); 272 // foreach (object o in f.localData) { 273 // if (f.localData.Count > 0) { 274 // entryNode.AppendChild(CreateDataNode(document, o)); 275 // } 276 // } 277 // linearRepresentationNode.AppendChild(entryNode); 278 // } 279 280 // node.AppendChild(linearRepresentationNode); 281 // return node; 282 //} 279 283 280 284 private XmlNode CreateDataNode(XmlDocument doc, object o) { … … 295 299 } 296 300 297 public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {298 base.Populate(node, restoredObjects);299 XmlNode linearRepresentationNode = node.SelectSingleNode("LinearRepresentation");300 foreach (XmlNode entryNode in linearRepresentationNode.ChildNodes) {301 LightWeightFunction f = new LightWeightFunction();302 f.arity = XmlConvert.ToByte(entryNode.Attributes["Arity"].Value);303 foreach (XmlNode dataNode in entryNode.ChildNodes) {304 f.localData.Add(ParseDataNode(dataNode));305 }306 f.functionType = (IFunction)PersistenceManager.Restore(entryNode, restoredObjects);307 linearRepresentation.Add(f);308 }309 treesExpanded = false;310 variablesExpanded = false;311 }301 //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 302 // base.Populate(node, restoredObjects); 303 // XmlNode linearRepresentationNode = node.SelectSingleNode("LinearRepresentation"); 304 // foreach (XmlNode entryNode in linearRepresentationNode.ChildNodes) { 305 // LightWeightFunction f = new LightWeightFunction(); 306 // f.arity = XmlConvert.ToByte(entryNode.Attributes["Arity"].Value); 307 // foreach (XmlNode dataNode in entryNode.ChildNodes) { 308 // f.localData.Add(ParseDataNode(dataNode)); 309 // } 310 // f.functionType = (IFunction)PersistenceManager.Restore(entryNode, restoredObjects); 311 // linearRepresentation.Add(f); 312 // } 313 // treesExpanded = false; 314 // variablesExpanded = false; 315 //} 312 316 313 317 private object ParseDataNode(XmlNode dataNode) {
Note: See TracChangeset
for help on using the changeset viewer.