- Timestamp:
- 01/11/10 20:00:15 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2/NetworkToFunctionTransformer.cs
r2616 r2622 64 64 //IFunctionTree openExpression = RemoveOpenParameters(networkDescription); 65 65 IFunctionTree paritallyEvaluatedOpenExpression = ApplyMetaFunctions((IFunctionTree)networkDescription.Clone()); 66 IFunctionTree boundExpression = BindVariables(paritallyEvaluatedOpenExpression, targetVariables );66 IFunctionTree boundExpression = BindVariables(paritallyEvaluatedOpenExpression, targetVariables.GetEnumerator()); 67 67 68 68 // create a new sub-scope for each target variable with the transformed expression … … 75 75 IFunctionTree root = ApplyCycles(tree); 76 76 List<IFunctionTree> subTrees = new List<IFunctionTree>(root.SubTrees); 77 while ( tree.SubTrees.Count > 0) tree.RemoveSubTree(0);77 while (root.SubTrees.Count > 0) root.RemoveSubTree(0); 78 78 79 79 foreach (IFunctionTree subTree in subTrees) { … … 119 119 private static IFunctionTree InvertFunction(IFunctionTree tree) { 120 120 IFunctionTree invertedNode = null; 121 if (tree.Function is OpenParameter ) {121 if (tree.Function is OpenParameter || tree.Function is Variable) { 122 122 return tree; 123 123 } else if (tree.Function is AdditionF1) { … … 143 143 } 144 144 IFunctionTree invertedTail = ApplyFlips(tree.SubTrees[0]); 145 if (invertedTail.Function is OpenParameter ) {145 if (invertedTail.Function is OpenParameter || invertedTail.Function is Variable) { 146 146 invertedNode.InsertSubTree(0, invertedTail); 147 147 return invertedNode; … … 178 178 // not found 179 179 if (targetIndex == -1) throw new InvalidOperationException(); 180 IFunctionTree targetChain = InvertFunction(subTrees[targetIndex]);180 IFunctionTree targetChain = TransformToFunction(InvertFunction(subTrees[targetIndex])); 181 181 for (int i = 0; i < subTrees.Count; i++) { 182 182 if (i != targetIndex) 183 combinator.AddSubTree( subTrees[i]);184 } 185 if (targetChain.Function is OpenParameter) return combinator;183 combinator.AddSubTree(TransformToFunction(subTrees[i])); 184 } 185 if (targetChain.Function is Variable) return combinator; 186 186 else { 187 187 AppendLeft(targetChain, combinator); … … 190 190 } 191 191 throw new NotImplementedException(); 192 } 193 194 private static IFunctionTree TransformToFunction(IFunctionTree tree) { 195 if (tree.SubTrees.Count == 0) return tree; 196 else if (tree.Function is AdditionF1) { 197 var addTree = (new Addition()).GetTreeNode(); 198 foreach (var subTree in tree.SubTrees) { 199 addTree.AddSubTree(TransformToFunction(subTree)); 200 } 201 return addTree; 202 } else if (tree.Function is SubtractionF1) { 203 var sTree = (new Subtraction()).GetTreeNode(); 204 foreach (var subTree in tree.SubTrees) { 205 sTree.AddSubTree(TransformToFunction(subTree)); 206 } 207 return sTree; 208 } else if (tree.Function is MultiplicationF1) { 209 var mulTree = (new Multiplication()).GetTreeNode(); 210 foreach (var subTree in tree.SubTrees) { 211 mulTree.AddSubTree(TransformToFunction(subTree)); 212 } 213 return mulTree; 214 } else if (tree.Function is DivisionF1) { 215 var divTree = (new Division()).GetTreeNode(); 216 foreach (var subTree in tree.SubTrees) { 217 divTree.AddSubTree(TransformToFunction(subTree)); 218 } 219 return divTree; 220 } else if (tree.Function is OpenExp) { 221 var expTree = (new Exponential()).GetTreeNode(); 222 expTree.AddSubTree(TransformToFunction(tree.SubTrees[0])); 223 return expTree; 224 } else if (tree.Function is OpenLog) { 225 var logTree = (new Logarithm()).GetTreeNode(); 226 logTree.AddSubTree(TransformToFunction(tree.SubTrees[0])); 227 return logTree; 228 } else if (tree.Function is OpenSqr) { 229 var powTree = (new Power()).GetTreeNode(); 230 powTree.AddSubTree(TransformToFunction(tree.SubTrees[0])); 231 var const2 = (ConstantFunctionTree)(new Constant()).GetTreeNode(); 232 const2.Value = 2.0; 233 powTree.AddSubTree(const2); 234 return powTree; 235 } else if (tree.Function is OpenSqrt) { 236 var sqrtTree = (new Sqrt()).GetTreeNode(); 237 sqrtTree.AddSubTree(TransformToFunction(tree.SubTrees[0])); 238 return sqrtTree; 239 } 240 throw new ArgumentException(); 192 241 } 193 242 … … 218 267 private static bool HasTargetVariable(IFunctionTree tree, string targetVariable) { 219 268 if (tree.SubTrees.Count == 0) { 220 return ((OpenParameterFunctionTree)tree).VariableName == targetVariable; 221 } else return HasTargetVariable(tree.SubTrees[0], targetVariable); 222 } 223 224 private static IFunctionTree BindVariables(IFunctionTree tree, IEnumerable<string> targetVariables) { 225 IEnumerator<string> targetVariablesEnumerator = targetVariables.GetEnumerator(); 226 foreach (IFunctionTree node in FunctionTreeIterator.IteratePrefix(tree)) { 227 if (node.Function is OpenParameter && targetVariablesEnumerator.MoveNext()) { 228 var varTreeNode = node as OpenParameterFunctionTree; 229 varTreeNode.VariableName = targetVariablesEnumerator.Current; 230 } 231 } 232 return tree; 269 var varTree = tree as VariableFunctionTree; 270 if (varTree != null) return varTree.VariableName == targetVariable; 271 else return false; 272 } else return (from x in tree.SubTrees 273 where HasTargetVariable(x, targetVariable) 274 select true).Any(); 275 } 276 277 private static IFunctionTree BindVariables(IFunctionTree tree, IEnumerator<string> targetVariables) { 278 if (tree.Function is OpenParameter && targetVariables.MoveNext()) { 279 var varTreeNode = (VariableFunctionTree)(new Variable()).GetTreeNode(); 280 varTreeNode.VariableName = targetVariables.Current; 281 varTreeNode.SampleOffset = ((OpenParameterFunctionTree)tree).SampleOffset; 282 varTreeNode.Weight = 1.0; 283 return varTreeNode; 284 } else { 285 IList<IFunctionTree> subTrees = new List<IFunctionTree>(tree.SubTrees); 286 while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0); 287 foreach (IFunctionTree subTree in subTrees) { 288 tree.AddSubTree(BindVariables(subTree, targetVariables)); 289 } 290 return tree; 291 } 233 292 } 234 293 }
Note: See TracChangeset
for help on using the changeset viewer.