using HeuristicLab.GP.StructureIdentification.Networks; using Microsoft.VisualStudio.TestTools.UnitTesting; using HeuristicLab.GP.Interfaces; using HeuristicLab.Core; using HeuristicLab.GP.StructureIdentification; using System.Linq; using System.Collections.Generic; using HeuristicLab.Random; using HeuristicLab.DataAnalysis; using HeuristicLab.Common; namespace HeuristicLab.GP.Test { /// ///This is a test class for NetworkToFunctionTransformerTest and is intended ///to contain all NetworkToFunctionTransformerTest Unit Tests /// [TestClass()] public class NetworkToFunctionTransformerTest { private TestContext testContextInstance; /// ///Gets or sets the test context which provides ///information about and functionality for the current test run. /// public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } #region Additional test attributes // //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //Use ClassCleanup to run code after all tests in a class have run //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // //Use TestInitialize to run code before running each test //[TestInitialize()] //public void MyTestInitialize() //{ //} // //Use TestCleanup to run code after each test has run //[TestCleanup()] //public void MyTestCleanup() //{ //} // #endregion /// ///A test for InvertFunction /// [TestMethod()] [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")] public void InvertFunctionTest() { var log = new OpenLog(); var exp = new OpenExp(); var openAdd = new AdditionF1(); var openSub = new SubtractionF1(); var openMul = new MultiplicationF1(); var openDiv = new DivisionF1(); var param = new OpenParameter(); var rootAdd = new OpenAddition(); var rootSub = new OpenSubtraction(); var rootMul = new OpenMultiplication(); var rootDiv = new OpenDivision(); IFunctionTree tree = exp.GetTreeNode(); tree.AddSubTree(param.GetTreeNode()); IFunctionTree expected = log.GetTreeNode(); expected.AddSubTree(param.GetTreeNode()); IFunctionTree actual; actual = NetworkToFunctionTransformer_Accessor.InvertChain(tree); var e = (from x in FunctionTreeIterator.IteratePostfix(expected) select x.Function.GetType()).GetEnumerator(); var a = (from x in FunctionTreeIterator.IteratePostfix(actual) select x.Function.GetType()).GetEnumerator(); Assert.AreEqual(expected.GetSize(), actual.GetSize()); while (e.MoveNext() && a.MoveNext()) { Assert.AreEqual(e.Current, a.Current); } } [TestMethod()] [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")] public void TransformTest() { SymbolicExpressionImporter importer = new SymbolicExpressionImporter(); { IFunctionTree tree = importer.Import("(open-+ (open-param 1.0 - 0) (open-param 2.0 - 0) (open-param 4.0 - 0))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import("(+ (variable 2.0 b 0) (variable 4.0 c 0))"); IFunctionTree t1 = importer.Import("(* (- (variable 1.0 a 0) (variable 4.0 c 0)) 0.5)"); IFunctionTree t2 = importer.Import("(* (- (variable 1.0 a 0) (variable 2.0 b 0)) 0.25)"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { 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))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import("(- (+ (variable 2.0 b 0) 1.0) (* (variable 4.0 c 0) 1.0 ))"); IFunctionTree t1 = importer.Import("(* (- (+ (variable 1.0 a 0) (* (variable 4.0 c 0) 1.0)) 1.0 ) 0.5)"); IFunctionTree t2 = importer.Import("(* (/ (+ (variable 1.0 a 0) (+ (variable 2.0 b 0) 1.0)) 1.0 ) 0.25)"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { IFunctionTree tree = importer.Import("(cycle (open-* (open-param 1.0 - 0) (open-param 2.0 - 0) (open-param 4.0 - 0)))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import("(* (* (variable 4.0 b 0) (variable 1.0 c 0)) 0.5)"); IFunctionTree t1 = importer.Import("(* (/ (variable 2.0 a 0) (variable 1.0 c 0)) 0.25)"); IFunctionTree t2 = importer.Import("(/ (variable 2.0 a 0) (variable 4.0 b 0))"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { IFunctionTree tree = importer.Import("(open-- (open-log (open-param 1.0 - 0)) (open-exp (open-param 1.0 - 0)) (open-param 1.0 - 0))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import(@"(exp (- (exp (variable 1.0 b 0)) (variable 1.0 c 0))))"); IFunctionTree t1 = importer.Import(@"(log (+ (log (variable 1.0 a 0)) (variable 1.0 c 0))))"); IFunctionTree t2 = importer.Import(@"(+ (log (variable 1.0 a 0)) (exp (variable 1.0 b 0)))"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { 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))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import(@"(exp (exp (- (exp (exp (variable 1.0 b 0))) (* (variable 1.0 c 0) 2.0)))))"); IFunctionTree t1 = importer.Import(@"(log (log (+ (log (log (variable 1.0 a 0))) (* (variable 1.0 c 0) 2.0))))"); IFunctionTree t2 = importer.Import(@"(/ (+ (log (log (variable 1.0 a 0))) (exp (exp (variable 1.0 b 0)))) 2.0)"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { IFunctionTree tree = importer.Import(@"(open-- (flip (open-log (open-param 1.0 - 0))) (flip (f1-* (open-param 2.0 - 0) 2.0)) (flip (flip (f1-* (open-param 4.0 - 0) 2.0))))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import(@"(log (- (/ (variable 0.5 b 0) 2.0) (* (variable 4.0 c 0) 2.0)))"); IFunctionTree t1 = importer.Import(@"(* (* (+ (exp (variable 1.0 a 0)) (* (variable 4.0 c 0) 2.0)) 2.0) 2)"); IFunctionTree t2 = importer.Import(@"(* (/ (+ (exp (variable 1.0 a 0)) (/ (variable 0.5 b 0) 2.0)) 2.0) 0.25)"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { IFunctionTree tree = importer.Import(@"(open-+ (flip (f1-+ (flip (f1-- (flip (f1-/ (open-param 1.0 - 0) 4.0)) 3.0)) 2.0)) (open-param 1.0 - 0) (open-param 1.0 - 0)))"); // -3*4-2 x IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import("(+ (/ (+ (+ (variable 1.0 b 0) (variable 1.0 c 0)) 3.0) 4.0) 2.0)"); IFunctionTree t1 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 c 0))"); IFunctionTree t2 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 b 0))"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { // constant expression IFunctionTree tree = importer.Import(@"(* (variable 1.0 d 0) (variable 1.0 d 0))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import("(* (variable 1.0 d 0) (variable 1.0 d 0))"); IFunctionTree t1 = importer.Import("(* (variable 1.0 d 0) (variable 1.0 d 0))"); IFunctionTree t2 = importer.Import("(* (variable 1.0 d 0) (variable 1.0 d 0))"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { // expression with one parameter IFunctionTree tree = importer.Import(@"(f1-* (open-param 1.0 - 0) (variable 1.0 d 0))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import("(/ (variable 1.0 b 0) (variable 1.0 d 0))"); IFunctionTree t1 = importer.Import("(* (variable 1.0 a 0) (variable 1.0 d 0))"); IFunctionTree t2 = importer.Import("(* (variable 1.0 a 0) (variable 1.0 d 0))"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { // expression with one parameter IFunctionTree tree = importer.Import(@"(open-log (open-param 1.0 - 0))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import("(exp (variable 1.0 b 0))"); IFunctionTree t1 = importer.Import("(log (variable 1.0 a 0))"); IFunctionTree t2 = importer.Import("(log (variable 1.0 a 0))"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { // expression with flip and one parameter IFunctionTree tree = importer.Import(@"(flip (open-log (open-param 1.0 - 0)))"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import("(log (variable 1.0 b 0))"); IFunctionTree t1 = importer.Import("(exp (variable 1.0 a 0))"); IFunctionTree t2 = importer.Import("(exp (variable 1.0 a 0))"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } { // expression with flip and one parameter IFunctionTree tree = importer.Import(@"(open-param 1.0 - 0)"); IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); IFunctionTree t0 = importer.Import("(variable 1.0 b 0)"); IFunctionTree t1 = importer.Import("(variable 1.0 a 0)"); IFunctionTree t2 = importer.Import("(variable 1.0 a 0)"); CompareTrees(actualTrees.ToList(), new List() { t0, t1, t2 }); } } private void CompareTrees(List actual, List expected) { var expectedEnumerator = expected.GetEnumerator(); foreach (var actualTree in actual) { if (!expectedEnumerator.MoveNext()) Assert.Fail(); IFunctionTree expectedTree = expectedEnumerator.Current; var e = (from x in FunctionTreeIterator.IteratePrefix(expectedTree) select x).GetEnumerator(); var a = (from x in FunctionTreeIterator.IteratePrefix(actualTree) select x).GetEnumerator(); Assert.AreEqual(expectedTree.GetSize(), actualTree.GetSize()); while (e.MoveNext() && a.MoveNext()) { Assert.AreEqual(e.Current.Function.GetType(), a.Current.Function.GetType()); if (e.Current.Function is HeuristicLab.GP.StructureIdentification.Variable) { var expectedVar = (VariableFunctionTree)e.Current; var actualVar = (VariableFunctionTree)a.Current; Assert.AreEqual(expectedVar.VariableName, actualVar.VariableName); Assert.IsTrue(expectedVar.Weight.IsAlmost(actualVar.Weight)); Assert.AreEqual(expectedVar.SampleOffset, actualVar.SampleOffset); } else if (e.Current.Function is Constant) { var expectedConst = (ConstantFunctionTree)e.Current; var actualConst = (ConstantFunctionTree)a.Current; Assert.AreEqual(expectedConst.Value, actualConst.Value); } } } } [TestMethod()] [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")] public void TransformRandomTreesTest() { MersenneTwister twister = new MersenneTwister(); Dataset ds = Util.CreateRandomDataset(twister, 1, 20); IFunctionTree[] randomTrees = Util.CreateRandomTrees(twister, ds, FunctionLibraryInjector.Create(), 1000, 1, 100); foreach (var tree in randomTrees) { IEnumerable actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List() { "a", "b", "c" }); actualTrees.ToList(); } } } }