Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Tests/NetworkToFunctionTransformerTest.cs @ 2649

Last change on this file since 2649 was 2635, checked in by gkronber, 15 years ago

Fixed a bug in the network transformation operator and changed test-cases to detect the problem. #833

File size: 14.5 KB
Line 
1using HeuristicLab.GP.StructureIdentification.Networks;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using HeuristicLab.GP.Interfaces;
4using HeuristicLab.Core;
5using HeuristicLab.GP.StructureIdentification;
6using System.Linq;
7using System.Collections.Generic;
8using HeuristicLab.Random;
9using HeuristicLab.DataAnalysis;
10
11namespace HeuristicLab.GP.Test {
12
13
14  /// <summary>
15  ///This is a test class for NetworkToFunctionTransformerTest and is intended
16  ///to contain all NetworkToFunctionTransformerTest Unit Tests
17  ///</summary>
18  [TestClass()]
19  public class NetworkToFunctionTransformerTest {
20
21
22    private TestContext testContextInstance;
23
24    /// <summary>
25    ///Gets or sets the test context which provides
26    ///information about and functionality for the current test run.
27    ///</summary>
28    public TestContext TestContext {
29      get {
30        return testContextInstance;
31      }
32      set {
33        testContextInstance = value;
34      }
35    }
36
37    #region Additional test attributes
38    //
39    //You can use the following additional attributes as you write your tests:
40    //
41    //Use ClassInitialize to run code before running the first test in the class
42    //[ClassInitialize()]
43    //public static void MyClassInitialize(TestContext testContext)
44    //{
45    //}
46    //
47    //Use ClassCleanup to run code after all tests in a class have run
48    //[ClassCleanup()]
49    //public static void MyClassCleanup()
50    //{
51    //}
52    //
53    //Use TestInitialize to run code before running each test
54    //[TestInitialize()]
55    //public void MyTestInitialize()
56    //{
57    //}
58    //
59    //Use TestCleanup to run code after each test has run
60    //[TestCleanup()]
61    //public void MyTestCleanup()
62    //{
63    //}
64    //
65    #endregion
66
67
68    /// <summary>
69    ///A test for InvertFunction
70    ///</summary>
71    [TestMethod()]
72    [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")]
73    public void InvertFunctionTest() {
74      var log = new OpenLog();
75      var exp = new OpenExp();
76      var openAdd = new AdditionF1();
77      var openSub = new SubtractionF1();
78      var openMul = new MultiplicationF1();
79      var openDiv = new DivisionF1();
80      var param = new OpenParameter();
81      var rootAdd = new OpenAddition();
82      var rootSub = new OpenSubtraction();
83      var rootMul = new OpenMultiplication();
84      var rootDiv = new OpenDivision();
85
86      IFunctionTree tree = exp.GetTreeNode(); tree.AddSubTree(param.GetTreeNode());
87      IFunctionTree expected = log.GetTreeNode(); expected.AddSubTree(param.GetTreeNode());
88      IFunctionTree actual;
89      actual = NetworkToFunctionTransformer_Accessor.InvertChain(tree);
90      var e = (from x in FunctionTreeIterator.IteratePostfix(expected)
91               select x.Function.GetType()).GetEnumerator();
92      var a = (from x in FunctionTreeIterator.IteratePostfix(actual)
93               select x.Function.GetType()).GetEnumerator();
94
95      Assert.AreEqual(expected.GetSize(), actual.GetSize());
96      while (e.MoveNext() && a.MoveNext()) {
97        Assert.AreEqual(e.Current, a.Current);
98      }
99    }
100
101    [TestMethod()]
102    [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")]
103    public void TransformTest() {
104      SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
105
106      {
107        IFunctionTree tree = importer.Import("(open-+ (open-param - 0) (open-param - 0) (open-param - 0))");
108
109        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
110
111        IFunctionTree t0 = importer.Import("(+ (variable 1.0 b 0) (variable 1.0 c 0))");
112        IFunctionTree t1 = importer.Import("(- (variable 1.0 a 0) (variable 1.0 c 0))");
113        IFunctionTree t2 = importer.Import("(- (variable 1.0 a 0) (variable 1.0 b 0))");
114
115        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
116        t0, t1, t2
117      });
118      }
119
120      {
121        IFunctionTree tree = importer.Import("(open-- (open-param - 0) (f1-+ (open-param - 0) 1.0) (f1-* (open-param - 0) 1.0))");
122
123        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
124
125        IFunctionTree t0 = importer.Import("(- (+ (variable 1.0 b 0) 1.0) (* (variable 1.0 c 0) 1.0 ))");
126        IFunctionTree t1 = importer.Import("(- (+ (variable 1.0 a 0) (* (variable 1.0 c 0) 1.0)) 1.0 )");
127        IFunctionTree t2 = importer.Import("(/ (+ (variable 1.0 a 0) (+ (variable 1.0 b 0) 1.0)) 1.0 )");
128
129        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
130        t0, t1, t2
131      });
132      }
133
134      {
135        IFunctionTree tree = importer.Import("(cycle (open-* (open-param - 0) (open-param - 0) (open-param - 0)))");
136
137        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
138
139        IFunctionTree t0 = importer.Import("(* (variable 1.0 b 0) (variable 1.0 c 0))");
140        IFunctionTree t1 = importer.Import("(/ (variable 1.0 a 0) (variable 1.0 c 0))");
141        IFunctionTree t2 = importer.Import("(/ (variable 1.0 a 0) (variable 1.0 b 0))");
142
143
144        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
145        t0, t1, t2
146      });
147      }
148
149
150      {
151        IFunctionTree tree = importer.Import("(open-- (open-log (open-param - 0)) (open-exp (open-param - 0)) (open-param - 0))");
152
153        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
154
155        IFunctionTree t0 = importer.Import(@"(exp (- (exp (variable 1.0 b 0))
156                                                     (variable 1.0 c 0))))");
157        IFunctionTree t1 = importer.Import(@"(log (+ (log (variable 1.0 a 0))
158                                                     (variable 1.0 c 0))))");
159        IFunctionTree t2 = importer.Import(@"(+ (log (variable 1.0 a 0))
160                                                (exp (variable 1.0 b 0)))");
161
162        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
163        t0, t1, t2
164      });
165      }
166      {
167        IFunctionTree tree = importer.Import("(open-- (open-log (open-log (open-param - 0))) (open-exp (open-exp (open-param - 0))) (f1-* (open-param - 0) 2.0))");
168
169        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
170
171        IFunctionTree t0 = importer.Import(@"(exp (exp (- (exp (exp (variable 1.0 b 0)))
172                                                          (* (variable 1.0 c 0) 2.0)))))");
173        IFunctionTree t1 = importer.Import(@"(log (log (+ (log (log (variable 1.0 a 0)))
174                                                          (* (variable 1.0 c 0) 2.0))))");
175        IFunctionTree t2 = importer.Import(@"(/ (+ (log (log (variable 1.0 a 0)))
176                                                   (exp (exp (variable 1.0 b 0)))) 2.0)");
177
178        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
179        t0, t1, t2
180      });
181      }
182      {
183        IFunctionTree tree = importer.Import(@"(open-- (flip (open-log (open-param - 0)))
184                                                       (flip (f1-* (open-param - 0) 2.0))
185                                                       (flip (flip (f1-* (open-param - 0) 2.0))))");
186
187        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
188
189        IFunctionTree t0 = importer.Import(@"(log (- (/ (variable 1.0 b 0) 2.0)
190                                                     (* (variable 1.0 c 0) 2.0)))");
191        IFunctionTree t1 = importer.Import(@"(* (+ (exp (variable 1.0 a 0))
192                                                   (* (variable 1.0 c 0) 2.0)) 2.0)");
193        IFunctionTree t2 = importer.Import(@"(/ (+ (exp (variable 1.0 a 0))
194                                                   (/ (variable 1.0 b 0) 2.0)) 2.0)");
195
196        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
197        t0, t1, t2
198      });
199      }
200      {
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)))");
208        // -3*4-2 x
209        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
210
211        IFunctionTree t0 = importer.Import("(+ (/ (+ (+ (variable 1.0 b 0) (variable 1.0 c 0)) 3.0) 4.0) 2.0)");
212        IFunctionTree t1 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 c 0))");
213        IFunctionTree t2 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 b 0))");
214
215
216        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
217        t0, t1, t2
218      });
219      }
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.ToList(), new List<IFunctionTree>() {
232        t0, t1, t2
233        });
234      }
235      {
236        // expression with one parameter
237        IFunctionTree tree = importer.Import(@"(f1-* (open-param - 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.ToList(), new List<IFunctionTree>() {
247        t0, t1, t2
248        });
249      }
250      {
251        // expression with one parameter
252        IFunctionTree tree = importer.Import(@"(open-log (open-param - 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.ToList(), 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 (open-param - 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.ToList(), new List<IFunctionTree>() {
277        t0, t1, t2
278        });
279      }
280
281      {
282        // expression with flip and one parameter
283        IFunctionTree tree = importer.Import(@"(open-param - 0)");
284
285        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
286
287        IFunctionTree t0 = importer.Import("(variable 1.0 b 0)");
288        IFunctionTree t1 = importer.Import("(variable 1.0 a 0)");
289        IFunctionTree t2 = importer.Import("(variable 1.0 a 0)");
290
291
292        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
293        t0, t1, t2
294        });
295      }
296    }
297
298    private void CompareTrees(List<IFunctionTree> actual, List<IFunctionTree> expected) {
299      var expectedEnumerator = expected.GetEnumerator();     
300      foreach (var actualTree in actual) {
301        if (!expectedEnumerator.MoveNext()) Assert.Fail();
302        IFunctionTree expectedTree = expectedEnumerator.Current;
303        var e = (from x in FunctionTreeIterator.IteratePrefix(expectedTree)
304                 select x).GetEnumerator();
305        var a = (from x in FunctionTreeIterator.IteratePrefix(actualTree)
306                 select x).GetEnumerator();
307        Assert.AreEqual(expectedTree.GetSize(), actualTree.GetSize());
308        while (e.MoveNext() && a.MoveNext()) {
309          Assert.AreEqual(e.Current.Function.GetType(), a.Current.Function.GetType());
310          if (e.Current.Function is HeuristicLab.GP.StructureIdentification.Variable) {
311            var expectedVar = (VariableFunctionTree)e.Current;
312            var actualVar = (VariableFunctionTree)a.Current;
313            Assert.AreEqual(expectedVar.VariableName, actualVar.VariableName);
314            Assert.AreEqual(expectedVar.Weight, actualVar.Weight);
315            Assert.AreEqual(expectedVar.SampleOffset, actualVar.SampleOffset);
316          } else if (e.Current.Function is Constant) {
317            var expectedConst = (ConstantFunctionTree)e.Current;
318            var actualConst = (ConstantFunctionTree)a.Current;
319            Assert.AreEqual(expectedConst.Value, actualConst.Value);
320          }
321        }
322      }
323    }
324
325    [TestMethod()]
326    [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")]
327    public void TransformRandomTreesTest() {
328
329      MersenneTwister twister = new MersenneTwister();
330      Dataset ds = Util.CreateRandomDataset(twister, 1, 20);
331      IFunctionTree[] randomTrees = Util.CreateRandomTrees(twister, ds, FunctionLibraryInjector.Create(false, 0, 0), 1000, 1, 100);
332      foreach (var tree in randomTrees) {
333        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
334        actualTrees.ToList();
335      }
336    }
337  }
338}
Note: See TracBrowser for help on using the repository browser.