- Timestamp:
- 01/14/10 19:42:10 (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/FunctionLibraryInjector.cs
r2624 r2627 142 142 SetAllowedSubOperators(openDivision, f1Functions); 143 143 SetAllowedSubOperators(openMul, f1Functions); 144 SetAllowedSubOperators(openSub, f1Functions); 144 145 145 146 if (includeDifferential) … … 153 154 openPar.SetConstraints(minTimeOffset, maxTimeOffset); 154 155 156 155 157 return functionLibrary; 156 158 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2/NetworkToFunctionTransformer.cs
r2625 r2627 68 68 // create a new sub-scope for each target variable with the transformed expression 69 69 foreach (var targetVariable in targetVariables) { 70 yield return TransformExpression(boundExpression, targetVariable );70 yield return TransformExpression(boundExpression, targetVariable, targetVariables.Except(new string[] { targetVariable })); 71 71 } 72 72 } … … 80 80 /// <returns></returns> 81 81 private static IFunctionTree ApplyMetaFunctions(IFunctionTree tree) { 82 IFunctionTree root = ApplyCycles(tree); 83 List<IFunctionTree> subTrees = new List<IFunctionTree>(root.SubTrees); 84 while (root.SubTrees.Count > 0) root.RemoveSubTree(0); 85 86 foreach (IFunctionTree subTree in subTrees) { 87 root.AddSubTree(ApplyFlips(subTree)); 88 } 89 return root; 82 return ApplyFlips(ApplyCycles(tree)); 90 83 } 91 84 … … 95 88 } else if (tree.Function is Flip) { 96 89 if (tree.SubTrees[0].Function is OpenParameter) return tree.SubTrees[0]; 97 else return ApplyFlips(InvertChain(tree.SubTrees[0]));90 else return InvertChain(ApplyFlips(tree.SubTrees[0])); 98 91 } else { 99 IFunctionTree tmp = ApplyFlips(tree.SubTrees[0]); 100 tree.RemoveSubTree(0); tree.InsertSubTree(0, tmp); 92 List<IFunctionTree> subTrees = new List<IFunctionTree>(tree.SubTrees); 93 while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0); 94 foreach (var subTree in subTrees) { 95 tree.AddSubTree(ApplyFlips(subTree)); 96 } 101 97 return tree; 102 98 } … … 191 187 } 192 188 193 189 194 190 195 191 private static IFunctionTree AppendLeft(IFunctionTree tree, IFunctionTree node) { … … 201 197 202 198 private static bool IsBottomLeft(IFunctionTree tree) { 203 if (tree.SubTrees.Count==0) return true;204 else if (tree.SubTrees[0].Function is Variable) return true;205 else if (tree.SubTrees[0].Function is Constant) return true;199 if (tree.SubTrees.Count == 0) return true; 200 else if (tree.SubTrees[0].Function is Variable) return true; 201 else if (tree.SubTrees[0].Function is Constant) return true; 206 202 else return false; 207 203 } 208 204 209 205 /// <summary> 210 /// recieves a function tree with an F2 root and branches containing only F0 functions andtransforms it into a function-tree for the given target variable206 /// recieves a function tree transforms it into a function-tree for the given target variable 211 207 /// </summary> 212 208 /// <param name="tree"></param> 213 209 /// <param name="targetVariable"></param> 214 210 /// <returns></returns> 215 private static IFunctionTree TransformExpression(IFunctionTree tree, string targetVariable) { 211 private static IFunctionTree TransformExpression(IFunctionTree tree, string targetVariable, IEnumerable<string> parameters) { 212 if (tree.Function is Addition || tree.Function is Subtraction || 213 tree.Function is Multiplication || tree.Function is Division || 214 tree.Function is Exponential || tree.Function is Logarithm) { 215 var occuringVariables = from x in FunctionTreeIterator.IteratePrefix(tree) 216 where x is VariableFunctionTree 217 let name = ((VariableFunctionTree)x).VariableName 218 select name; 219 var openParameters = (new string[] { targetVariable }).Concat(parameters); 220 var missingVariables = openParameters.Except(occuringVariables); 221 if (missingVariables.Count() > 0) { 222 VariableFunctionTree varTree = (VariableFunctionTree)(new Variable()).GetTreeNode(); 223 varTree.VariableName = missingVariables.First(); 224 varTree.SampleOffset = 0; 225 varTree.Weight = 1.0; 226 tree = (IFunctionTree)tree.Clone(); 227 tree.InsertSubTree(0, varTree); 228 } 229 } 216 230 int targetIndex = -1; 217 IFunctionTree combinator ;231 IFunctionTree combinator = null; 218 232 List<IFunctionTree> subTrees = new List<IFunctionTree>(tree.SubTrees); 219 //while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0);220 233 if (HasTargetVariable(subTrees[0], targetVariable)) { 221 234 targetIndex = 0; 222 combinator = FunctionFromCombinator(tree) ;235 combinator = FunctionFromCombinator(tree).GetTreeNode(); 223 236 } else { 224 237 for (int i = 1; i < subTrees.Count; i++) { 225 238 if (HasTargetVariable(subTrees[i], targetVariable)) { 226 239 targetIndex = i; 240 combinator = GetInvertedFunction(FunctionFromCombinator(tree)).GetTreeNode(); 227 241 break; 228 242 } 229 243 } 230 combinator = FunctionFromCombinator(InvertCombinator(tree)); 231 } 232 // not found 233 if (targetIndex == -1) throw new InvalidOperationException(); 234 235 for (int i = 0; i < subTrees.Count; i++) { 236 if (i != targetIndex) 237 combinator.AddSubTree(subTrees[i]); 238 } 239 if (subTrees[targetIndex].Function is Variable) return combinator; 240 else { 241 IFunctionTree targetChain = InvertF0Chain(subTrees[targetIndex]); 242 AppendLeft(targetChain, combinator); 243 return targetChain; 244 } 245 if (targetIndex == -1) { 246 // target variable was not found 247 return tree; 248 } else { 249 // target variable was found 250 for (int i = 0; i < subTrees.Count; i++) { 251 if (i != targetIndex) 252 combinator.AddSubTree(subTrees[i]); 253 } 254 if (subTrees[targetIndex].Function is Variable || subTrees[targetIndex].Function is Constant) return combinator; 255 else { 256 IFunctionTree targetChain = InvertF0Chain(subTrees[targetIndex]); 257 AppendLeft(targetChain, combinator); 258 return targetChain; 259 } 244 260 } 245 261 } … … 273 289 } 274 290 275 276 private static IFunctionTree InvertCombinator(IFunctionTree tree) { 277 if (tree.Function is OpenAddition) { 278 return (new OpenSubtraction()).GetTreeNode(); 279 } else if (tree.Function is OpenSubtraction) { 280 return (new OpenAddition()).GetTreeNode(); 281 } else if (tree.Function is OpenMultiplication) { 282 return (new OpenDivision()).GetTreeNode(); 283 } else if (tree.Function is OpenDivision) { 284 return (new OpenMultiplication()).GetTreeNode(); 285 } else throw new InvalidOperationException(); 286 } 287 288 private static IFunctionTree FunctionFromCombinator(IFunctionTree tree) { 289 if (tree.Function is OpenAddition) { 290 return (new Addition()).GetTreeNode(); 291 } else if (tree.Function is OpenSubtraction) { 292 return (new Subtraction()).GetTreeNode(); 293 } else if (tree.Function is OpenMultiplication) { 294 return (new Multiplication()).GetTreeNode(); 295 } else if (tree.Function is OpenDivision) { 296 return (new Division()).GetTreeNode(); 297 } else throw new InvalidOperationException(); 291 292 293 //private static IFunctionTree InvertCombinator(IFunctionTree tree) { 294 // if (tree.Function is OpenAddition) { 295 // return (new OpenSubtraction()).GetTreeNode(); 296 // } else if (tree.Function is OpenSubtraction) { 297 // return (new OpenAddition()).GetTreeNode(); 298 // } else if (tree.Function is OpenMultiplication) { 299 // return (new OpenDivision()).GetTreeNode(); 300 // } else if (tree.Function is OpenDivision) { 301 // return (new OpenMultiplication()).GetTreeNode(); 302 // } else throw new InvalidOperationException(); 303 //} 304 305 private static Dictionary<Type, IFunction> combinatorFunction = new Dictionary<Type, IFunction>() { 306 { typeof(OpenAddition), new Addition()}, 307 { typeof(OpenSubtraction), new Subtraction()}, 308 { typeof(OpenDivision), new Division()}, 309 { typeof(OpenMultiplication), new Multiplication()}, 310 { typeof(Addition), new Addition()}, 311 { typeof(Subtraction), new Subtraction()}, 312 { typeof(Division), new Division()}, 313 { typeof(Multiplication), new Multiplication()}, 314 { typeof(Logarithm), new Logarithm()}, 315 { typeof(Exponential), new Exponential()}, 316 }; 317 private static IFunction FunctionFromCombinator(IFunctionTree tree) { 318 return combinatorFunction[tree.Function.GetType()]; 298 319 } 299 320 … … 318 339 {typeof(DivisionF1), new Division()}, 319 340 {typeof(OpenExp), new Exponential()}, 320 {typeof(OpenLog), new OpenLog()},341 {typeof(OpenLog), new Logarithm()}, 321 342 //{typeof(OpenSqr), new Power()}, 322 343 //{typeof(OpenSqrt), new Sqrt()},
Note: See TracChangeset
for help on using the changeset viewer.