Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2627


Ignore:
Timestamp:
01/14/10 19:42:10 (14 years ago)
Author:
gkronber
Message:

Added test cases for network transformation and fixed bugs in network transformation operator. #833

Location:
trunk/sources
Files:
5 edited

Legend:

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

    r2624 r2627  
    142142      SetAllowedSubOperators(openDivision, f1Functions);
    143143      SetAllowedSubOperators(openMul, f1Functions);
     144      SetAllowedSubOperators(openSub, f1Functions);
    144145
    145146      if (includeDifferential)
     
    153154      openPar.SetConstraints(minTimeOffset, maxTimeOffset);
    154155
     156
    155157      return functionLibrary;
    156158    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2/NetworkToFunctionTransformer.cs

    r2625 r2627  
    6868      // create a new sub-scope for each target variable with the transformed expression
    6969      foreach (var targetVariable in targetVariables) {
    70         yield return TransformExpression(boundExpression, targetVariable);
     70        yield return TransformExpression(boundExpression, targetVariable, targetVariables.Except(new string[] { targetVariable }));
    7171      }
    7272    }
     
    8080    /// <returns></returns>
    8181    private static IFunctionTree ApplyMetaFunctions(IFunctionTree tree) {
    82       IFunctionTree root = ApplyCycles(tree);
    83       List<IFunctionTree> subTrees = new List<IFunctionTree>(root.SubTrees);
    84       while (root.SubTrees.Count > 0) root.RemoveSubTree(0);
    85 
    86       foreach (IFunctionTree subTree in subTrees) {
    87         root.AddSubTree(ApplyFlips(subTree));
    88       }
    89       return root;
     82      return ApplyFlips(ApplyCycles(tree));
    9083    }
    9184
     
    9588      } else if (tree.Function is Flip) {
    9689        if (tree.SubTrees[0].Function is OpenParameter) return tree.SubTrees[0];
    97         else return ApplyFlips(InvertChain(tree.SubTrees[0]));
     90        else return InvertChain(ApplyFlips(tree.SubTrees[0]));
    9891      } else {
    99         IFunctionTree tmp = ApplyFlips(tree.SubTrees[0]);
    100         tree.RemoveSubTree(0); tree.InsertSubTree(0, tmp);
     92        List<IFunctionTree> subTrees = new List<IFunctionTree>(tree.SubTrees);
     93        while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0);
     94        foreach (var subTree in subTrees) {
     95          tree.AddSubTree(ApplyFlips(subTree));
     96        }
    10197        return tree;
    10298      }
     
    191187    }
    192188
    193  
     189
    194190
    195191    private static IFunctionTree AppendLeft(IFunctionTree tree, IFunctionTree node) {
     
    201197
    202198    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;
     199      if (tree.SubTrees.Count == 0) return true;
     200      else if (tree.SubTrees[0].Function is Variable) return true;
     201      else if (tree.SubTrees[0].Function is Constant) return true;
    206202      else return false;
    207203    }
    208204
    209205    /// <summary>
    210     /// recieves a function tree with an F2 root and branches containing only F0 functions and transforms it into a function-tree for the given target variable
     206    /// recieves a function tree transforms it into a function-tree for the given target variable
    211207    /// </summary>
    212208    /// <param name="tree"></param>
    213209    /// <param name="targetVariable"></param>
    214210    /// <returns></returns>
    215     private static IFunctionTree TransformExpression(IFunctionTree tree, string targetVariable) {
     211    private static IFunctionTree TransformExpression(IFunctionTree tree, string targetVariable, IEnumerable<string> parameters) {
     212      if (tree.Function is Addition || tree.Function is Subtraction ||
     213          tree.Function is Multiplication || tree.Function is Division ||
     214          tree.Function is Exponential || tree.Function is Logarithm) {
     215        var occuringVariables = from x in FunctionTreeIterator.IteratePrefix(tree)
     216                                where x is VariableFunctionTree
     217                                let name = ((VariableFunctionTree)x).VariableName
     218                                select name;
     219        var openParameters = (new string[] { targetVariable }).Concat(parameters);
     220        var missingVariables = openParameters.Except(occuringVariables);
     221        if (missingVariables.Count() > 0) {
     222          VariableFunctionTree varTree = (VariableFunctionTree)(new Variable()).GetTreeNode();
     223          varTree.VariableName = missingVariables.First();
     224          varTree.SampleOffset = 0;
     225          varTree.Weight = 1.0;
     226          tree = (IFunctionTree)tree.Clone();
     227          tree.InsertSubTree(0, varTree);
     228        }
     229      }
    216230      int targetIndex = -1;
    217       IFunctionTree combinator;
     231      IFunctionTree combinator = null;
    218232      List<IFunctionTree> subTrees = new List<IFunctionTree>(tree.SubTrees);
    219       //while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0);
    220233      if (HasTargetVariable(subTrees[0], targetVariable)) {
    221234        targetIndex = 0;
    222         combinator = FunctionFromCombinator(tree);
     235        combinator = FunctionFromCombinator(tree).GetTreeNode();
    223236      } else {
    224237        for (int i = 1; i < subTrees.Count; i++) {
    225238          if (HasTargetVariable(subTrees[i], targetVariable)) {
    226239            targetIndex = i;
     240            combinator = GetInvertedFunction(FunctionFromCombinator(tree)).GetTreeNode();
    227241            break;
    228242          }
    229243        }
    230         combinator = FunctionFromCombinator(InvertCombinator(tree));
    231       }
    232       // not found
    233       if (targetIndex == -1) throw new InvalidOperationException();
    234 
    235       for (int i = 0; i < subTrees.Count; i++) {
    236         if (i != targetIndex)
    237           combinator.AddSubTree(subTrees[i]);
    238       }
    239       if (subTrees[targetIndex].Function is Variable) return combinator;
    240       else {
    241         IFunctionTree targetChain = InvertF0Chain(subTrees[targetIndex]);
    242         AppendLeft(targetChain, combinator);
    243         return targetChain;
     244      }
     245      if (targetIndex == -1) {
     246        // target variable was not found
     247        return tree;
     248      } else {
     249        // target variable was found
     250        for (int i = 0; i < subTrees.Count; i++) {
     251          if (i != targetIndex)
     252            combinator.AddSubTree(subTrees[i]);
     253        }
     254        if (subTrees[targetIndex].Function is Variable || subTrees[targetIndex].Function is Constant) return combinator;
     255        else {
     256          IFunctionTree targetChain = InvertF0Chain(subTrees[targetIndex]);
     257          AppendLeft(targetChain, combinator);
     258          return targetChain;
     259        }
    244260      }
    245261    }
     
    273289    }
    274290
    275  
    276     private static IFunctionTree InvertCombinator(IFunctionTree tree) {
    277       if (tree.Function is OpenAddition) {
    278         return (new OpenSubtraction()).GetTreeNode();
    279       } else if (tree.Function is OpenSubtraction) {
    280         return (new OpenAddition()).GetTreeNode();
    281       } else if (tree.Function is OpenMultiplication) {
    282         return (new OpenDivision()).GetTreeNode();
    283       } else if (tree.Function is OpenDivision) {
    284         return (new OpenMultiplication()).GetTreeNode();
    285       } else throw new InvalidOperationException();
    286     }
    287 
    288     private static IFunctionTree FunctionFromCombinator(IFunctionTree tree) {
    289       if (tree.Function is OpenAddition) {
    290         return (new Addition()).GetTreeNode();
    291       } else if (tree.Function is OpenSubtraction) {
    292         return (new Subtraction()).GetTreeNode();
    293       } else if (tree.Function is OpenMultiplication) {
    294         return (new Multiplication()).GetTreeNode();
    295       } else if (tree.Function is OpenDivision) {
    296         return (new Division()).GetTreeNode();
    297       } else throw new InvalidOperationException();
     291
     292
     293    //private static IFunctionTree InvertCombinator(IFunctionTree tree) {
     294    //  if (tree.Function is OpenAddition) {
     295    //    return (new OpenSubtraction()).GetTreeNode();
     296    //  } else if (tree.Function is OpenSubtraction) {
     297    //    return (new OpenAddition()).GetTreeNode();
     298    //  } else if (tree.Function is OpenMultiplication) {
     299    //    return (new OpenDivision()).GetTreeNode();
     300    //  } else if (tree.Function is OpenDivision) {
     301    //    return (new OpenMultiplication()).GetTreeNode();
     302    //  } else throw new InvalidOperationException();
     303    //}
     304
     305    private static Dictionary<Type, IFunction> combinatorFunction = new Dictionary<Type, IFunction>() {
     306      { typeof(OpenAddition), new Addition()},
     307      { typeof(OpenSubtraction), new Subtraction()},
     308      { typeof(OpenDivision), new Division()},
     309      { typeof(OpenMultiplication), new Multiplication()},
     310      { typeof(Addition), new Addition()},
     311      { typeof(Subtraction), new Subtraction()},
     312      { typeof(Division), new Division()},
     313      { typeof(Multiplication), new Multiplication()},
     314      { typeof(Logarithm), new Logarithm()},
     315      { typeof(Exponential), new Exponential()},
     316    };
     317    private static IFunction FunctionFromCombinator(IFunctionTree tree) {
     318      return combinatorFunction[tree.Function.GetType()];
    298319    }
    299320
     
    318339      {typeof(DivisionF1), new Division()},
    319340      {typeof(OpenExp), new Exponential()},
    320       {typeof(OpenLog), new OpenLog()},
     341      {typeof(OpenLog), new Logarithm()},
    321342      //{typeof(OpenSqr), new Power()},
    322343      //{typeof(OpenSqrt), new Sqrt()},
  • trunk/sources/HeuristicLab.GP.Tests/NetworkToFunctionTransformerTest.cs

    r2625 r2627  
    66using System.Linq;
    77using System.Collections.Generic;
     8using HeuristicLab.Random;
     9using HeuristicLab.DataAnalysis;
    810
    911namespace HeuristicLab.GP.Test {
     
    197199      }
    198200      {
    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)))");
     201        IFunctionTree tree = importer.Import(@"(open-+
     202                                                 (flip (f1-+
     203                                                   (flip (f1--
     204                                                     (flip (f1-/
     205                                                       (open-param - 0) 4.0)) 3.0)) 2.0))
     206                                                 (open-param - 0)
     207                                                 (open-param - 0)))");
    206208        // -3*4-2 x
    207209        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
    208210
    209         IFunctionTree t0 = importer.Import("(+ (/ (- (+ (variable 1.0 b 0) (variable 1.0 c 0)) 3.0) 4.0) 2.0)");
     211        IFunctionTree t0 = importer.Import("(+ (/ (+ (+ (variable 1.0 b 0) (variable 1.0 c 0)) 3.0) 4.0) 2.0)");
    210212        IFunctionTree t1 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 c 0))");
    211213        IFunctionTree t2 = importer.Import("(- (- (* (- (variable 1.0 a 0) 2.0) 4.0) 3.0) (variable 1.0 b 0))");
     
    216218      });
    217219      }
    218 
     220      {
     221        // constant expression
     222        IFunctionTree tree = importer.Import(@"(* (variable 1.0 d 0) (variable 1.0 d 0))");
     223
     224        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     225
     226        IFunctionTree t0 = importer.Import("(* (variable 1.0 d 0) (variable 1.0 d 0))");
     227        IFunctionTree t1 = importer.Import("(* (variable 1.0 d 0) (variable 1.0 d 0))");
     228        IFunctionTree t2 = importer.Import("(* (variable 1.0 d 0) (variable 1.0 d 0))");
     229
     230
     231        CompareTrees(actualTrees, new List<IFunctionTree>() {
     232        t0, t1, t2
     233        });
     234      }
     235      {
     236        // expression with one parameter
     237        IFunctionTree tree = importer.Import(@"(f1-* (variable 1.0 a 0) (variable 1.0 d 0))");
     238
     239        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     240
     241        IFunctionTree t0 = importer.Import("(/ (variable 1.0 b 0) (variable 1.0 d 0))");
     242        IFunctionTree t1 = importer.Import("(* (variable 1.0 a 0) (variable 1.0 d 0))");
     243        IFunctionTree t2 = importer.Import("(* (variable 1.0 a 0) (variable 1.0 d 0))");
     244
     245
     246        CompareTrees(actualTrees, new List<IFunctionTree>() {
     247        t0, t1, t2
     248        });
     249      }
     250      {
     251        // expression with one parameter
     252        IFunctionTree tree = importer.Import(@"(open-log (variable 1.0 a 0))");
     253
     254        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     255
     256        IFunctionTree t0 = importer.Import("(exp (variable 1.0 b 0))");
     257        IFunctionTree t1 = importer.Import("(log (variable 1.0 a 0))");
     258        IFunctionTree t2 = importer.Import("(log (variable 1.0 a 0))");
     259
     260
     261        CompareTrees(actualTrees, new List<IFunctionTree>() {
     262        t0, t1, t2
     263        });
     264      }
     265      {
     266        // expression with flip and one parameter
     267        IFunctionTree tree = importer.Import(@"(flip (open-log (variable 1.0 a 0)))");
     268
     269        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     270
     271        IFunctionTree t0 = importer.Import("(log (variable 1.0 b 0))");
     272        IFunctionTree t1 = importer.Import("(exp (variable 1.0 a 0))");
     273        IFunctionTree t2 = importer.Import("(exp (variable 1.0 a 0))");
     274
     275
     276        CompareTrees(actualTrees, new List<IFunctionTree>() {
     277        t0, t1, t2
     278        });
     279      }
    219280    }
    220281
     
    245306      }
    246307    }
     308
     309    [TestMethod()]
     310    [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")]
     311    public void TransformRandomTreesTest() {
     312
     313      MersenneTwister twister = new MersenneTwister();
     314      Dataset ds = Util.CreateRandomDataset(twister, 1, 20);
     315      IFunctionTree[] randomTrees = Util.CreateRandomTrees(twister, ds, FunctionLibraryInjector.Create(false, 0, 0), 1000, 1, 100);
     316      foreach (var tree in randomTrees) {
     317        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     318        actualTrees.ToList();
     319      }
     320    }
    247321  }
    248322}
  • trunk/sources/HeuristicLab.GP.Tests/SymbolicExpressionImporter.cs

    r2624 r2627  
    6262        {"open-*", new HeuristicLab.GP.StructureIdentification.Networks.OpenMultiplication()},
    6363        {"open-/", new HeuristicLab.GP.StructureIdentification.Networks.OpenDivision()},
    64         {"open-log", new HeuristicLab.GP.StructureIdentification.Networks.OpenExp()},
    65         {"open-exp", new HeuristicLab.GP.StructureIdentification.Networks.OpenLog()},
     64        {"open-log", new HeuristicLab.GP.StructureIdentification.Networks.OpenLog()},
     65        {"open-exp", new HeuristicLab.GP.StructureIdentification.Networks.OpenExp()},
    6666        //{"open-sqr", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqr()},
    6767        //{"open-sqrt", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqrt()},
  • trunk/sources/HeuristicLab/Files.txt

    r2600 r2627  
    4646HeuristicLab.GP.StructureIdentification.ConditionalEvaluation\3.3:HeuristicLab.GP.StructureIdentification.ConditionalEvaluation-3.3.dll
    4747HeuristicLab.GP.StructureIdentification.TimeSeries\3.3:HeuristicLab.GP.StructureIdentification.TimeSeries-3.3.dll
     48HeuristicLab.GP.StructureIdentification.Networks\3.2:HeuristicLab.GP.StructureIdentification.Networks-3.2.dll
    4849
    4950HeuristicLab.Grid\3.2:HeuristicLab.Grid-3.2.dll
Note: See TracChangeset for help on using the changeset viewer.