Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3337 was 2843, checked in by gkronber, 15 years ago

Removed max. and min. time offset constraints as algorithm parameters and from all engines. The time constraints were added to the relevant terminal symbols (variable & differential) instead. The time offset constraint can be changed by editing the symbols in the function library. #880 (Max and min time offsets for variable symbols are not set correctly by FunctionLibraryInjectors)

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