Free cookie consent management tool by TermsFeed Policy Generator

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

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

Location:
trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2
Files:
2 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()},
Note: See TracChangeset for help on using the changeset viewer.