Changeset 2627
- Timestamp:
- 01/14/10 19:42:10 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 5 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()}, -
trunk/sources/HeuristicLab.GP.Tests/NetworkToFunctionTransformerTest.cs
r2625 r2627 6 6 using System.Linq; 7 7 using System.Collections.Generic; 8 using HeuristicLab.Random; 9 using HeuristicLab.DataAnalysis; 8 10 9 11 namespace HeuristicLab.GP.Test { … … 197 199 } 198 200 { 199 IFunctionTree tree = importer.Import(@"( cycle (open-+200 201 202 203 204 205 201 IFunctionTree tree = importer.Import(@"(open-+ 202 (flip (f1-+ 203 (flip (f1-- 204 (flip (f1-/ 205 (open-param - 0) 4.0)) 3.0)) 2.0)) 206 (open-param - 0) 207 (open-param - 0)))"); 206 208 // -3*4-2 x 207 209 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 208 210 209 IFunctionTree t0 = importer.Import("(+ (/ ( -(+ (variable 1.0 b 0) (variable 1.0 c 0)) 3.0) 4.0) 2.0)");211 IFunctionTree t0 = importer.Import("(+ (/ (+ (+ (variable 1.0 b 0) (variable 1.0 c 0)) 3.0) 4.0) 2.0)"); 210 212 IFunctionTree t1 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 c 0))"); 211 213 IFunctionTree t2 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 b 0))"); … … 216 218 }); 217 219 } 218 220 { 221 // constant expression 222 IFunctionTree tree = importer.Import(@"(* (variable 1.0 d 0) (variable 1.0 d 0))"); 223 224 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 225 226 IFunctionTree t0 = importer.Import("(* (variable 1.0 d 0) (variable 1.0 d 0))"); 227 IFunctionTree t1 = importer.Import("(* (variable 1.0 d 0) (variable 1.0 d 0))"); 228 IFunctionTree t2 = importer.Import("(* (variable 1.0 d 0) (variable 1.0 d 0))"); 229 230 231 CompareTrees(actualTrees, new List<IFunctionTree>() { 232 t0, t1, t2 233 }); 234 } 235 { 236 // expression with one parameter 237 IFunctionTree tree = importer.Import(@"(f1-* (variable 1.0 a 0) (variable 1.0 d 0))"); 238 239 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 240 241 IFunctionTree t0 = importer.Import("(/ (variable 1.0 b 0) (variable 1.0 d 0))"); 242 IFunctionTree t1 = importer.Import("(* (variable 1.0 a 0) (variable 1.0 d 0))"); 243 IFunctionTree t2 = importer.Import("(* (variable 1.0 a 0) (variable 1.0 d 0))"); 244 245 246 CompareTrees(actualTrees, new List<IFunctionTree>() { 247 t0, t1, t2 248 }); 249 } 250 { 251 // expression with one parameter 252 IFunctionTree tree = importer.Import(@"(open-log (variable 1.0 a 0))"); 253 254 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 255 256 IFunctionTree t0 = importer.Import("(exp (variable 1.0 b 0))"); 257 IFunctionTree t1 = importer.Import("(log (variable 1.0 a 0))"); 258 IFunctionTree t2 = importer.Import("(log (variable 1.0 a 0))"); 259 260 261 CompareTrees(actualTrees, new List<IFunctionTree>() { 262 t0, t1, t2 263 }); 264 } 265 { 266 // expression with flip and one parameter 267 IFunctionTree tree = importer.Import(@"(flip (open-log (variable 1.0 a 0)))"); 268 269 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 270 271 IFunctionTree t0 = importer.Import("(log (variable 1.0 b 0))"); 272 IFunctionTree t1 = importer.Import("(exp (variable 1.0 a 0))"); 273 IFunctionTree t2 = importer.Import("(exp (variable 1.0 a 0))"); 274 275 276 CompareTrees(actualTrees, new List<IFunctionTree>() { 277 t0, t1, t2 278 }); 279 } 219 280 } 220 281 … … 245 306 } 246 307 } 308 309 [TestMethod()] 310 [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")] 311 public void TransformRandomTreesTest() { 312 313 MersenneTwister twister = new MersenneTwister(); 314 Dataset ds = Util.CreateRandomDataset(twister, 1, 20); 315 IFunctionTree[] randomTrees = Util.CreateRandomTrees(twister, ds, FunctionLibraryInjector.Create(false, 0, 0), 1000, 1, 100); 316 foreach (var tree in randomTrees) { 317 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 318 actualTrees.ToList(); 319 } 320 } 247 321 } 248 322 } -
trunk/sources/HeuristicLab.GP.Tests/SymbolicExpressionImporter.cs
r2624 r2627 62 62 {"open-*", new HeuristicLab.GP.StructureIdentification.Networks.OpenMultiplication()}, 63 63 {"open-/", new HeuristicLab.GP.StructureIdentification.Networks.OpenDivision()}, 64 {"open-log", new HeuristicLab.GP.StructureIdentification.Networks.Open Exp()},65 {"open-exp", new HeuristicLab.GP.StructureIdentification.Networks.Open Log()},64 {"open-log", new HeuristicLab.GP.StructureIdentification.Networks.OpenLog()}, 65 {"open-exp", new HeuristicLab.GP.StructureIdentification.Networks.OpenExp()}, 66 66 //{"open-sqr", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqr()}, 67 67 //{"open-sqrt", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqrt()}, -
trunk/sources/HeuristicLab/Files.txt
r2600 r2627 46 46 HeuristicLab.GP.StructureIdentification.ConditionalEvaluation\3.3:HeuristicLab.GP.StructureIdentification.ConditionalEvaluation-3.3.dll 47 47 HeuristicLab.GP.StructureIdentification.TimeSeries\3.3:HeuristicLab.GP.StructureIdentification.TimeSeries-3.3.dll 48 HeuristicLab.GP.StructureIdentification.Networks\3.2:HeuristicLab.GP.StructureIdentification.Networks-3.2.dll 48 49 49 50 HeuristicLab.Grid\3.2:HeuristicLab.Grid-3.2.dll
Note: See TracChangeset
for help on using the changeset viewer.