Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.Tests/NetworkToFunctionTransformerTest.cs @ 9674

Last change on this file since 9674 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
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;
10using HeuristicLab.Common;
11
12namespace HeuristicLab.GP.Test {
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;
90      actual = NetworkToFunctionTransformer_Accessor.InvertChain(tree);
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() {
105      SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
106
107      {
108        IFunctionTree tree = importer.Import("(open-+ (open-param 1.0 - 0) (open-param 2.0 - 0) (open-param 4.0 - 0))");
109
110        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
111
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)");
115
116        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
117        t0, t1, t2
118      });
119      }
120
121      {
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))");
123
124        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
125
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)");
129
130        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
131        t0, t1, t2
132      });
133      }
134
135      {
136        IFunctionTree tree = importer.Import("(cycle (open-* (open-param 1.0 - 0) (open-param 2.0 - 0) (open-param 4.0 - 0)))");
137
138        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
139
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))");
143
144
145        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
146        t0, t1, t2
147      });
148      }
149
150
151      {
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))");
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
163        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
164        t0, t1, t2
165      });
166      }
167      {
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))");
169
170        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
171
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)");
178
179        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
180        t0, t1, t2
181      });
182      }
183      {
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))))");
187
188        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
189
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)");
196
197        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
198        t0, t1, t2
199      });
200      }
201      {
202        IFunctionTree tree = importer.Import(@"(open-+
203                                                 (flip (f1-+
204                                                   (flip (f1--
205                                                     (flip (f1-/
206                                                       (open-param 1.0 - 0) 4.0)) 3.0)) 2.0))
207                                                 (open-param 1.0 - 0)
208                                                 (open-param 1.0 - 0)))");
209        // -3*4-2 x
210        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
211
212        IFunctionTree t0 = importer.Import("(+ (/ (+ (+ (variable 1.0 b 0) (variable 1.0 c 0)) 3.0) 4.0) 2.0)");
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
217        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
218        t0, t1, t2
219      });
220      }
221      {
222        // constant expression
223        IFunctionTree tree = importer.Import(@"(* (variable 1.0 d 0) (variable 1.0 d 0))");
224
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
232        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
233        t0, t1, t2
234        });
235      }
236      {
237        // expression with one parameter
238        IFunctionTree tree = importer.Import(@"(f1-* (open-param 1.0 - 0) (variable 1.0 d 0))");
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
247        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
248        t0, t1, t2
249        });
250      }
251      {
252        // expression with one parameter
253        IFunctionTree tree = importer.Import(@"(open-log (open-param 1.0 - 0))");
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
262        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
263        t0, t1, t2
264        });
265      }
266      {
267        // expression with flip and one parameter
268        IFunctionTree tree = importer.Import(@"(flip (open-log (open-param 1.0 - 0)))");
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
277        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
278        t0, t1, t2
279        });
280      }
281
282      {
283        // expression with flip and one parameter
284        IFunctionTree tree = importer.Import(@"(open-param 1.0 - 0)");
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
293        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
294        t0, t1, t2
295        });
296      }
297    }
298
299    private void CompareTrees(List<IFunctionTree> actual, List<IFunctionTree> expected) {
300      var expectedEnumerator = expected.GetEnumerator();
301      foreach (var actualTree in actual) {
302        if (!expectedEnumerator.MoveNext()) Assert.Fail();
303        IFunctionTree expectedTree = expectedEnumerator.Current;
304        var e = (from x in FunctionTreeIterator.IteratePrefix(expectedTree)
305                 select x).GetEnumerator();
306        var a = (from x in FunctionTreeIterator.IteratePrefix(actualTree)
307                 select x).GetEnumerator();
308        Assert.AreEqual(expectedTree.GetSize(), actualTree.GetSize());
309        while (e.MoveNext() && a.MoveNext()) {
310          Assert.AreEqual(e.Current.Function.GetType(), a.Current.Function.GetType());
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);
315            Assert.IsTrue(expectedVar.Weight.IsAlmost(actualVar.Weight));
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          }
322        }
323      }
324    }
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);
332      IFunctionTree[] randomTrees = Util.CreateRandomTrees(twister, ds, FunctionLibraryInjector.Create(), 1000, 1, 100);
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    }
338  }
339}
Note: See TracBrowser for help on using the repository browser.