1 | using HeuristicLab.GP.StructureIdentification.Networks;
|
---|
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
3 | using HeuristicLab.GP.Interfaces;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.GP.StructureIdentification;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Collections.Generic;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.GP.Test {
|
---|
10 |
|
---|
11 |
|
---|
12 | /// <summary>
|
---|
13 | ///This is a test class for NetworkToFunctionTransformerTest and is intended
|
---|
14 | ///to contain all NetworkToFunctionTransformerTest Unit Tests
|
---|
15 | ///</summary>
|
---|
16 | [TestClass()]
|
---|
17 | public class NetworkToFunctionTransformerTest {
|
---|
18 |
|
---|
19 |
|
---|
20 | private TestContext testContextInstance;
|
---|
21 |
|
---|
22 | /// <summary>
|
---|
23 | ///Gets or sets the test context which provides
|
---|
24 | ///information about and functionality for the current test run.
|
---|
25 | ///</summary>
|
---|
26 | public TestContext TestContext {
|
---|
27 | get {
|
---|
28 | return testContextInstance;
|
---|
29 | }
|
---|
30 | set {
|
---|
31 | testContextInstance = value;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | #region Additional test attributes
|
---|
36 | //
|
---|
37 | //You can use the following additional attributes as you write your tests:
|
---|
38 | //
|
---|
39 | //Use ClassInitialize to run code before running the first test in the class
|
---|
40 | //[ClassInitialize()]
|
---|
41 | //public static void MyClassInitialize(TestContext testContext)
|
---|
42 | //{
|
---|
43 | //}
|
---|
44 | //
|
---|
45 | //Use ClassCleanup to run code after all tests in a class have run
|
---|
46 | //[ClassCleanup()]
|
---|
47 | //public static void MyClassCleanup()
|
---|
48 | //{
|
---|
49 | //}
|
---|
50 | //
|
---|
51 | //Use TestInitialize to run code before running each test
|
---|
52 | //[TestInitialize()]
|
---|
53 | //public void MyTestInitialize()
|
---|
54 | //{
|
---|
55 | //}
|
---|
56 | //
|
---|
57 | //Use TestCleanup to run code after each test has run
|
---|
58 | //[TestCleanup()]
|
---|
59 | //public void MyTestCleanup()
|
---|
60 | //{
|
---|
61 | //}
|
---|
62 | //
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | ///A test for InvertFunction
|
---|
68 | ///</summary>
|
---|
69 | [TestMethod()]
|
---|
70 | [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")]
|
---|
71 | public void InvertFunctionTest() {
|
---|
72 | var log = new OpenLog();
|
---|
73 | var exp = new OpenExp();
|
---|
74 | var openAdd = new AdditionF1();
|
---|
75 | var openSub = new SubtractionF1();
|
---|
76 | var openMul = new MultiplicationF1();
|
---|
77 | var openDiv = new DivisionF1();
|
---|
78 | var param = new OpenParameter();
|
---|
79 | var rootAdd = new OpenAddition();
|
---|
80 | var rootSub = new OpenSubtraction();
|
---|
81 | var rootMul = new OpenMultiplication();
|
---|
82 | var rootDiv = new OpenDivision();
|
---|
83 |
|
---|
84 | IFunctionTree tree = exp.GetTreeNode(); tree.AddSubTree(param.GetTreeNode());
|
---|
85 | IFunctionTree expected = log.GetTreeNode(); expected.AddSubTree(param.GetTreeNode());
|
---|
86 | IFunctionTree actual;
|
---|
87 | actual = NetworkToFunctionTransformer_Accessor.InvertFunction(tree);
|
---|
88 | var e = (from x in FunctionTreeIterator.IteratePostfix(expected)
|
---|
89 | select x.Function.GetType()).GetEnumerator();
|
---|
90 | var a = (from x in FunctionTreeIterator.IteratePostfix(actual)
|
---|
91 | select x.Function.GetType()).GetEnumerator();
|
---|
92 |
|
---|
93 | Assert.AreEqual(expected.GetSize(), actual.GetSize());
|
---|
94 | while (e.MoveNext() && a.MoveNext()) {
|
---|
95 | Assert.AreEqual(e.Current, a.Current);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | [TestMethod()]
|
---|
100 | [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")]
|
---|
101 | public void TransformTest() {
|
---|
102 | SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
|
---|
103 |
|
---|
104 | {
|
---|
105 | IFunctionTree tree = importer.Import("(open-+ (open-param - 0) (open-param - 0) (open-param - 0))");
|
---|
106 |
|
---|
107 | IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
|
---|
108 |
|
---|
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>() {
|
---|
114 | t0, t1, t2
|
---|
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;
|
---|
174 | var e = (from x in FunctionTreeIterator.IteratePostfix(expectedTree)
|
---|
175 | select x).GetEnumerator();
|
---|
176 | var a = (from x in FunctionTreeIterator.IteratePostfix(actualTree)
|
---|
177 | select x).GetEnumerator();
|
---|
178 | Assert.AreEqual(expectedTree.GetSize(), actualTree.GetSize());
|
---|
179 | while (e.MoveNext() && a.MoveNext()) {
|
---|
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 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|