Changeset 2622 for trunk/sources
- Timestamp:
- 01/11/10 20:00:15 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 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 } -
trunk/sources/HeuristicLab.GP.Tests/NetworkToFunctionTransformerTest.cs
r2616 r2622 7 7 using System.Collections.Generic; 8 8 9 namespace HeuristicLab.GP.Test s{9 namespace HeuristicLab.GP.Test { 10 10 11 11 … … 65 65 66 66 /// <summary> 67 ///A test for TransformExpression68 ///</summary>69 [TestMethod()]70 [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")]71 public void TransformExpressionTest() {72 IFunctionTree tree = null; // TODO: Initialize to an appropriate value73 string targetVariable = string.Empty; // TODO: Initialize to an appropriate value74 IFunctionTree expected = null; // TODO: Initialize to an appropriate value75 IFunctionTree actual;76 actual = NetworkToFunctionTransformer_Accessor.TransformExpression(tree, targetVariable);77 Assert.AreEqual(expected, actual);78 Assert.Inconclusive("Verify the correctness of this test method.");79 }80 81 /// <summary>82 67 ///A test for InvertFunction 83 68 ///</summary> … … 115 100 [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")] 116 101 public void TransformTest() { 117 var log = new OpenLog(); 118 var exp = new OpenExp(); 119 var openAdd = new AdditionF1(); 120 var openSub = new SubtractionF1(); 121 var openMul = new MultiplicationF1(); 122 var openDiv = new DivisionF1(); 123 var param = new OpenParameter(); 124 var rootAdd = new OpenAddition(); 125 var rootSub = new OpenSubtraction(); 126 var rootMul = new OpenMultiplication(); 127 var rootDiv = new OpenDivision(); 128 var closedAdd = new Addition(); 129 var closedSub = new Subtraction(); 130 var closedMul = new Multiplication(); 131 var closedDiv = new Division(); 132 var closedVar = new HeuristicLab.GP.StructureIdentification.Variable(); 102 SymbolicExpressionImporter importer = new SymbolicExpressionImporter(); 133 103 134 IFunctionTree tree = rootAdd.GetTreeNode(); 135 tree.AddSubTree(param.GetTreeNode()); 136 tree.AddSubTree(param.GetTreeNode()); 137 tree.AddSubTree(param.GetTreeNode()); 104 { 105 IFunctionTree tree = importer.Import("(open-+ (open-param - 0) (open-param - 0) (open-param - 0))"); 138 106 139 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "1", "2", "3" }); 140 IFunctionTree t0 = closedAdd.GetTreeNode(); t0.AddSubTree(param.GetTreeNode()); t0.AddSubTree(param.GetTreeNode()); 141 IFunctionTree t1 = closedSub.GetTreeNode(); t1.AddSubTree(param.GetTreeNode()); t1.AddSubTree(param.GetTreeNode()); 142 IFunctionTree t2 = closedSub.GetTreeNode(); t2.AddSubTree(param.GetTreeNode()); t2.AddSubTree(param.GetTreeNode()); 107 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 143 108 144 var expectedTrees = (new List<IFunctionTree>() { 109 IFunctionTree t0 = importer.Import("(+ (variable 1.0 b 0) (variable 1.0 c 0))"); 110 IFunctionTree t1 = importer.Import("(- (variable 1.0 a 0) (variable 1.0 c 0))"); 111 IFunctionTree t2 = importer.Import("(- (variable 1.0 a 0) (variable 1.0 b 0))"); 112 113 CompareTrees(actualTrees, new List<IFunctionTree>() { 145 114 t0, t1, t2 146 }).GetEnumerator(); 147 foreach (var actualTree in actualTrees) { 148 if (!expectedTrees.MoveNext()) Assert.Fail(); 149 IFunctionTree expectedTree = expectedTrees.Current; 115 }); 116 } 117 118 { 119 IFunctionTree tree = importer.Import("(open-- (open-param - 0) (f1-+ (open-param - 0) 1.0) (f1-* (open-param - 0) 1.0))"); 120 121 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 122 123 IFunctionTree t0 = importer.Import("(- (+ 1.0 (variable 1.0 b 0)) (* 1.0 (variable 1.0 c 0)))"); 124 IFunctionTree t1 = importer.Import("(- (+ (variable 1.0 a 0) (* 1.0 (variable 1.0 c 0) 1.0)))"); 125 IFunctionTree t2 = importer.Import("(/ (+ (variable 1.0 a 0) (+ 1.0 (variable 1.0 b 0) 1.0)))"); 126 127 CompareTrees(actualTrees, new List<IFunctionTree>() { 128 t0, t1, t2 129 }); 130 } 131 132 { 133 IFunctionTree tree = importer.Import("(cycle (open-* (open-param - 0) (open-param - 0) (open-param - 0)))"); 134 135 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 136 137 IFunctionTree t0 = importer.Import("(* (variable 1.0 b 0) (variable 1.0 c 0))"); 138 IFunctionTree t1 = importer.Import("(/ (variable 1.0 a 0) (variable 1.0 c 0))"); 139 IFunctionTree t2 = importer.Import("(/ (variable 1.0 a 0) (variable 1.0 b 0))"); 140 141 142 CompareTrees(actualTrees, new List<IFunctionTree>() { 143 t0, t1, t2 144 }); 145 } 146 147 148 { 149 IFunctionTree tree = importer.Import("(open-- (open-log (open-param - 0)) (open-exp (open-param - 0)) (open-param - 0))"); 150 151 IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" }); 152 153 IFunctionTree t0 = importer.Import(@"(exp (- (exp (variable 1.0 b 0)) 154 (variable 1.0 c 0))))"); 155 IFunctionTree t1 = importer.Import(@"(log (+ (log (variable 1.0 a 0)) 156 (variable 1.0 c 0))))"); 157 IFunctionTree t2 = importer.Import(@"(+ (log (variable 1.0 a 0)) 158 (exp (variable 1.0 b 0)))"); 159 160 CompareTrees(actualTrees, new List<IFunctionTree>() { 161 t0, t1, t2 162 }); 163 } 164 165 166 167 } 168 169 private void CompareTrees(IEnumerable<IFunctionTree> actual, IEnumerable<IFunctionTree> expected) { 170 var expectedEnumerator = expected.GetEnumerator(); 171 foreach (var actualTree in actual) { 172 if (!expectedEnumerator.MoveNext()) Assert.Fail(); 173 IFunctionTree expectedTree = expectedEnumerator.Current; 150 174 var e = (from x in FunctionTreeIterator.IteratePostfix(expectedTree) 151 select x .Function.GetType()).GetEnumerator();175 select x).GetEnumerator(); 152 176 var a = (from x in FunctionTreeIterator.IteratePostfix(actualTree) 153 select x .Function.GetType()).GetEnumerator();177 select x).GetEnumerator(); 154 178 Assert.AreEqual(expectedTree.GetSize(), actualTree.GetSize()); 155 179 while (e.MoveNext() && a.MoveNext()) { 156 Assert.AreEqual(e.Current, a.Current); 180 Assert.AreEqual(e.Current.GetType(), a.Current.GetType()); 181 if (e.Current.Function is HeuristicLab.GP.StructureIdentification.Variable) { 182 var expectedVar = (VariableFunctionTree)e.Current; 183 var actualVar = (VariableFunctionTree)a.Current; 184 Assert.AreEqual(expectedVar.VariableName, actualVar.VariableName); 185 Assert.AreEqual(expectedVar.Weight, actualVar.Weight); 186 Assert.AreEqual(expectedVar.SampleOffset, actualVar.SampleOffset); 187 } else if (e.Current.Function is Constant) { 188 var expectedConst = (ConstantFunctionTree)e.Current; 189 var actualConst = (ConstantFunctionTree)a.Current; 190 Assert.AreEqual(expectedConst.Value, actualConst.Value); 191 } 157 192 } 158 193 } 159 194 } 160 161 195 } 162 196 } -
trunk/sources/HeuristicLab.GP.Tests/SymbolicExpressionImporter.cs
r2447 r2622 33 33 private const string DIFFSTART = "dif"; 34 34 private const string VARSTART = "var"; 35 private const string OPENPARAMSTART = "open-param"; 35 36 private Dictionary<string, IFunction> knownFunctions = new Dictionary<string, IFunction>() 36 37 { … … 55 56 {"-", new Subtraction()}, 56 57 {"tan", new Tangens()}, 57 {"xor", new Xor()} 58 {"xor", new Xor()}, 59 {"open-param", new HeuristicLab.GP.StructureIdentification.Networks.OpenParameter()}, 60 {"open-+", new HeuristicLab.GP.StructureIdentification.Networks.OpenAddition()}, 61 {"open--", new HeuristicLab.GP.StructureIdentification.Networks.OpenSubtraction()}, 62 {"open-*", new HeuristicLab.GP.StructureIdentification.Networks.OpenMultiplication()}, 63 {"open-/", new HeuristicLab.GP.StructureIdentification.Networks.OpenDivision()}, 64 {"open-log", new HeuristicLab.GP.StructureIdentification.Networks.OpenExp()}, 65 {"open-exp", new HeuristicLab.GP.StructureIdentification.Networks.OpenLog()}, 66 {"open-sqr", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqr()}, 67 {"open-sqrt", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqrt()}, 68 {"f1-+", new HeuristicLab.GP.StructureIdentification.Networks.AdditionF1()}, 69 {"f1--", new HeuristicLab.GP.StructureIdentification.Networks.SubtractionF1()}, 70 {"f1-/", new HeuristicLab.GP.StructureIdentification.Networks.DivisionF1()}, 71 {"f1-*", new HeuristicLab.GP.StructureIdentification.Networks.MultiplicationF1()}, 72 {"cycle", new HeuristicLab.GP.StructureIdentification.Networks.Cycle()}, 73 {"flip", new HeuristicLab.GP.StructureIdentification.Networks.Flip()}, 74 58 75 }; 59 76 Constant constant = new Constant(); 60 77 HeuristicLab.GP.StructureIdentification.Variable variable = new HeuristicLab.GP.StructureIdentification.Variable(); 61 78 Differential differential = new Differential(); 62 79 HeuristicLab.GP.StructureIdentification.Networks.OpenParameter openParam = new HeuristicLab.GP.StructureIdentification.Networks.OpenParameter(); 63 80 public SymbolicExpressionImporter() { 64 81 } … … 85 102 } else if (tokens.Peek().StringValue.StartsWith(DIFFSTART)) { 86 103 tree = ParseDifferential(tokens); 104 } else if(tokens.Peek().StringValue.StartsWith(OPENPARAMSTART)) { 105 tree = ParseOpenParameter(tokens); 87 106 } else { 88 107 Token curToken = tokens.Dequeue(); … … 99 118 return t; 100 119 } else throw new FormatException("Expected function or constant symbol"); 120 } 121 122 private IFunctionTree ParseOpenParameter(Queue<Token> tokens) { 123 Token tok = tokens.Dequeue(); 124 Debug.Assert(tok.StringValue == "open-param"); 125 HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree t = (HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree)openParam.GetTreeNode(); 126 t.VariableName = tokens.Dequeue().StringValue; 127 t.SampleOffset = (int)tokens.Dequeue().DoubleValue; 128 return t; 101 129 } 102 130
Note: See TracChangeset
for help on using the changeset viewer.