Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2625


Ignore:
Timestamp:
01/13/10 19:31:02 (14 years ago)
Author:
gkronber
Message:

Fixed bugs in network transformation operator. Added test cases. #833.

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2/NetworkToFunctionTransformer.cs

    r2624 r2625  
    9494        return tree;
    9595      } else if (tree.Function is Flip) {
    96         return InvertChain(tree.SubTrees[0]);
     96        if (tree.SubTrees[0].Function is OpenParameter) return tree.SubTrees[0];
     97        else return ApplyFlips(InvertChain(tree.SubTrees[0]));
    9798      } else {
    9899        IFunctionTree tmp = ApplyFlips(tree.SubTrees[0]);
     
    134135      }
    135136      // append open param at the end
    136       invParent.AddSubTree(openParam);
     137      invParent.InsertSubTree(0, openParam);
    137138      return root;
    138139    }
     
    194195    private static IFunctionTree AppendLeft(IFunctionTree tree, IFunctionTree node) {
    195196      IFunctionTree originalTree = tree;
    196       while (tree.SubTrees[0].SubTrees.Count > 0) tree = tree.SubTrees[0];
     197      while (!IsBottomLeft(tree)) tree = tree.SubTrees[0];
    197198      tree.InsertSubTree(0, node);
    198199      return originalTree;
     200    }
     201
     202    private static bool IsBottomLeft(IFunctionTree tree) {
     203      if(tree.SubTrees.Count==0) return true;
     204      else if(tree.SubTrees[0].Function is Variable) return true;
     205      else if(tree.SubTrees[0].Function is Constant) return true;
     206      else return false;
    199207    }
    200208
  • trunk/sources/HeuristicLab.GP.Tests/NetworkToFunctionTransformerTest.cs

    r2624 r2625  
    162162      });
    163163      }
    164 
    165 
     164      {
     165        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))");
     166
     167        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     168
     169        IFunctionTree t0 = importer.Import(@"(exp (exp (- (exp (exp (variable 1.0 b 0)))
     170                                                          (* (variable 1.0 c 0) 2.0)))))");
     171        IFunctionTree t1 = importer.Import(@"(log (log (+ (log (log (variable 1.0 a 0)))
     172                                                          (* (variable 1.0 c 0) 2.0))))");
     173        IFunctionTree t2 = importer.Import(@"(/ (+ (log (log (variable 1.0 a 0)))
     174                                                   (exp (exp (variable 1.0 b 0)))) 2.0)");
     175
     176        CompareTrees(actualTrees, new List<IFunctionTree>() {
     177        t0, t1, t2
     178      });
     179      }
     180      {
     181        IFunctionTree tree = importer.Import(@"(open-- (flip (open-log (open-param - 0)))
     182                                                       (flip (f1-* (open-param - 0) 2.0))
     183                                                       (flip (flip (f1-* (open-param - 0) 2.0))))");
     184
     185        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     186
     187        IFunctionTree t0 = importer.Import(@"(log (- (/ (variable 1.0 b 0) 2.0)
     188                                                     (* (variable 1.0 c 0) 2.0)))");
     189        IFunctionTree t1 = importer.Import(@"(* (+ (exp (variable 1.0 a 0))
     190                                                   (* (variable 1.0 c 0) 2.0)) 2.0)");
     191        IFunctionTree t2 = importer.Import(@"(/ (+ (exp (variable 1.0 a 0))
     192                                                   (/ (variable 1.0 b 0) 2.0)) 2.0)");
     193
     194        CompareTrees(actualTrees, new List<IFunctionTree>() {
     195        t0, t1, t2
     196      });
     197      }
     198      {
     199        IFunctionTree tree = importer.Import(@"(cycle (open-+
     200                                                         (flip (f1-+
     201                                                           (flip (f1--
     202                                                             (flip (f1-/
     203                                                               (open-param - 0) 4.0)) 3.0)) 2.0))
     204                                                         (open-param - 0)
     205                                                         (open-param - 0)))");
     206        // -3*4-2 x
     207        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     208
     209        IFunctionTree t0 = importer.Import("(+ (/ (- (+ (variable 1.0 b 0) (variable 1.0 c 0)) 3.0) 4.0) 2.0)");
     210        IFunctionTree t1 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 c 0))");
     211        IFunctionTree t2 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 b 0))");
     212
     213
     214        CompareTrees(actualTrees, new List<IFunctionTree>() {
     215        t0, t1, t2
     216      });
     217      }
    166218
    167219    }
     
    172224        if (!expectedEnumerator.MoveNext()) Assert.Fail();
    173225        IFunctionTree expectedTree = expectedEnumerator.Current;
    174         var e = (from x in FunctionTreeIterator.IteratePostfix(expectedTree)
     226        var e = (from x in FunctionTreeIterator.IteratePrefix(expectedTree)
    175227                 select x).GetEnumerator();
    176         var a = (from x in FunctionTreeIterator.IteratePostfix(actualTree)
     228        var a = (from x in FunctionTreeIterator.IteratePrefix(actualTree)
    177229                 select x).GetEnumerator();
    178230        Assert.AreEqual(expectedTree.GetSize(), actualTree.GetSize());
    179231        while (e.MoveNext() && a.MoveNext()) {
    180           Assert.AreEqual(e.Current.GetType(), a.Current.GetType());
     232          Assert.AreEqual(e.Current.Function.GetType(), a.Current.Function.GetType());
    181233          if (e.Current.Function is HeuristicLab.GP.StructureIdentification.Variable) {
    182234            var expectedVar = (VariableFunctionTree)e.Current;
  • trunk/sources/HeuristicLab.GP.Tests/Token.cs

    r2447 r2625  
    2929using HeuristicLab.GP.StructureIdentification;
    3030using System.Diagnostics;
     31using System.Globalization;
    3132
    3233namespace HeuristicLab.GP.Test {
     
    5960      } else if (strToken == ")") {
    6061        t.Symbol = TokenSymbol.RPAR;
    61       } else if (double.TryParse(strToken, out temp)) {
     62      } else if (double.TryParse(strToken, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out temp)) {
    6263        t.Symbol = TokenSymbol.NUMBER;
    63         t.DoubleValue = double.Parse(strToken);
     64        t.DoubleValue = double.Parse(strToken, CultureInfo.InvariantCulture.NumberFormat);
    6465      } else {
    6566        t.Symbol = TokenSymbol.SYMB;
Note: See TracChangeset for help on using the changeset viewer.