- Timestamp:
- 01/22/10 16:06:48 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2/NetworkToFunctionTransformer.cs
r2635 r2674 30 30 using HeuristicLab.DataAnalysis; 31 31 using System.Diagnostics; 32 using HeuristicLab.Common; 32 33 33 34 namespace HeuristicLab.GP.StructureIdentification.Networks { … … 93 94 } else if (tree.Function is Flip) { 94 95 var partiallyAppliedBranch = ApplyFlips(tree.SubTrees[0]); 95 if (partiallyAppliedBranch.Function is OpenParameter) return partiallyAppliedBranch; 96 else return InvertChain(partiallyAppliedBranch); 96 if (partiallyAppliedBranch.Function is OpenParameter) { 97 var openParFunTree = (OpenParameterFunctionTree)partiallyAppliedBranch; 98 openParFunTree.Weight = 1.0 / openParFunTree.Weight; 99 return partiallyAppliedBranch; 100 } else return InvertChain(partiallyAppliedBranch); 97 101 } else { 98 102 List<IFunctionTree> subTrees = new List<IFunctionTree>(tree.SubTrees); … … 116 120 // get a list of function trees from bottom to top 117 121 List<IFunctionTree> reversedChain = new List<IFunctionTree>(currentChain.Reverse<IFunctionTree>().Skip(1)); 118 IFunctionTree openParam =currentChain.Last();122 OpenParameterFunctionTree openParam = (OpenParameterFunctionTree)currentChain.Last(); 119 123 120 124 // build new tree by inverting every function in the reversed chain and keeping f0 branches untouched. … … 136 140 } 137 141 } 142 // invert factor of openParam 143 openParam.Weight = 1.0 / openParam.Weight; 138 144 // append open param at the end 139 145 invParent.InsertSubTree(0, openParam); … … 195 201 196 202 197 private static IFunctionTree AppendLeft(IFunctionTree tree, IFunctionTree node) {198 IFunctionTree originalTree = tree;199 while (!IsBottomLeft(tree)) tree = tree.SubTrees[0];200 tree.InsertSubTree(0, node);201 return originalTree;202 }203 //private static IFunctionTree AppendLeft(IFunctionTree tree, IFunctionTree node) { 204 // IFunctionTree originalTree = tree; 205 // while (!IsBottomLeft(tree)) tree = tree.SubTrees[0]; 206 // tree.InsertSubTree(0, node); 207 // return originalTree; 208 //} 203 209 204 210 private static bool IsBottomLeft(IFunctionTree tree) { … … 265 271 combinator.AddSubTree(subTrees[i]); 266 272 } 267 if (subTrees[targetIndex].Function is Variable) return combinator;273 if (subTrees[targetIndex].Function is Variable) return MakeMultiplication(combinator, 1.0 / GetTargetVariableWeight(subTrees[targetIndex])); 268 274 else { 269 275 IFunctionTree bottomLeft; 270 276 IFunctionTree targetChain = InvertF0Chain(subTrees[targetIndex], out bottomLeft); 271 277 bottomLeft.InsertSubTree(0, combinator); 272 return targetChain; 273 } 274 } 278 return MakeMultiplication(targetChain, 1.0 / GetTargetVariableWeight(subTrees[targetIndex])); 279 } 280 } 281 } 282 283 private static IFunctionTree MakeMultiplication(IFunctionTree tree, double p) { 284 if (p.IsAlmost(1.0)) return tree; 285 var mul = (new Multiplication()).GetTreeNode(); 286 var constP = (ConstantFunctionTree)(new Constant()).GetTreeNode(); 287 constP.Value = p; 288 mul.AddSubTree(tree); 289 mul.AddSubTree(constP); 290 return mul; 291 } 292 293 private static double GetTargetVariableWeight(IFunctionTree tree) { 294 while (tree.SubTrees.Count > 0) { 295 tree = tree.SubTrees[0]; 296 } 297 return ((VariableFunctionTree)tree).Weight; 275 298 } 276 299 … … 377 400 varTreeNode.VariableName = targetVariables.Current; 378 401 varTreeNode.SampleOffset = ((OpenParameterFunctionTree)tree).SampleOffset; 379 varTreeNode.Weight = 1.0;402 varTreeNode.Weight = ((OpenParameterFunctionTree)tree).Weight; 380 403 return varTreeNode; 381 404 //} else if (matchingFunction is Power) { -
trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2/Symbols/OpenParameterFunctionTree.cs
r2616 r2674 30 30 public string VariableName { get; set; } 31 31 public int SampleOffset { get; set; } 32 public double Weight { get; set; } 32 33 33 34 public OpenParameterFunctionTree(OpenParameter openParameter) 34 35 : base(openParameter) { 36 this.Weight = 1.0; 35 37 } 36 38 … … 39 41 VariableName = original.VariableName; 40 42 SampleOffset = original.SampleOffset; 43 Weight = original.Weight; 41 44 } 42 45 … … 51 54 scope.AddSubScope(myVariableScope); 52 55 myVariableScope.AddVariable(CreateSampleOffsetVariable()); 56 myVariableScope.AddVariable(CreateWeightVariable()); 53 57 return new AtomicOperation(Function.Manipulator, myVariableScope); 54 58 } … … 58 62 scope.AddSubScope(myVariableScope); 59 63 myVariableScope.AddVariable(CreateSampleOffsetVariable()); 64 myVariableScope.AddVariable(CreateWeightVariable()); 60 65 return new AtomicOperation(Function.Initializer, myVariableScope); 61 66 } … … 72 77 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) { 73 78 XmlNode node = document.CreateElement(name); 79 XmlAttribute weightAttr = document.CreateAttribute("Weight"); 80 weightAttr.Value = XmlConvert.ToString(Weight); 74 81 XmlAttribute variableAttr = document.CreateAttribute("Variable"); 75 82 variableAttr.Value = VariableName; 76 83 XmlAttribute sampleOffsetAttr = document.CreateAttribute("SampleOffset"); 77 84 sampleOffsetAttr.Value = XmlConvert.ToString(SampleOffset); 85 node.Attributes.Append(weightAttr); 78 86 node.Attributes.Append(sampleOffsetAttr); 79 87 node.Attributes.Append(variableAttr); … … 83 91 public override void Populate(XmlNode node, IDictionary<System.Guid, IStorable> restoredObjects) { 84 92 base.Populate(node, restoredObjects); 93 Weight = XmlConvert.ToDouble(node.Attributes["Weight"].Value); 85 94 SampleOffset = XmlConvert.ToInt32(node.Attributes["SampleOffset"].Value); 86 95 VariableName = node.Attributes["Variable"].Value; … … 103 112 return variable; 104 113 } 114 115 private IVariable CreateWeightVariable() { 116 DoubleData data = new DoubleData(Weight); 117 data.Changed += (sender, args) => Weight = data.Data; 118 var variable = new HeuristicLab.Core.Variable(Variable.WEIGHT, data); 119 variable.ValueChanged += (sender, args) => Weight = ((DoubleData)variable.Value).Data; 120 return variable; 121 } 105 122 } 106 123 }
Note: See TracChangeset
for help on using the changeset viewer.