Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2622


Ignore:
Timestamp:
01/11/10 20:00:15 (14 years ago)
Author:
gkronber
Message:

Worked on evaluation operators for multiple-target modeling. #833

Location:
trunk/sources
Files:
3 edited

Legend:

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

    r2616 r2622  
    6464      //IFunctionTree openExpression = RemoveOpenParameters(networkDescription);
    6565      IFunctionTree paritallyEvaluatedOpenExpression = ApplyMetaFunctions((IFunctionTree)networkDescription.Clone());
    66       IFunctionTree boundExpression = BindVariables(paritallyEvaluatedOpenExpression, targetVariables);
     66      IFunctionTree boundExpression = BindVariables(paritallyEvaluatedOpenExpression, targetVariables.GetEnumerator());
    6767
    6868      // create a new sub-scope for each target variable with the transformed expression
     
    7575      IFunctionTree root = ApplyCycles(tree);
    7676      List<IFunctionTree> subTrees = new List<IFunctionTree>(root.SubTrees);
    77       while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0);
     77      while (root.SubTrees.Count > 0) root.RemoveSubTree(0);
    7878
    7979      foreach (IFunctionTree subTree in subTrees) {
     
    119119    private static IFunctionTree InvertFunction(IFunctionTree tree) {
    120120      IFunctionTree invertedNode = null;
    121       if (tree.Function is OpenParameter) {
     121      if (tree.Function is OpenParameter || tree.Function is Variable) {
    122122        return tree;
    123123      } else if (tree.Function is AdditionF1) {
     
    143143      }
    144144      IFunctionTree invertedTail = ApplyFlips(tree.SubTrees[0]);
    145       if (invertedTail.Function is OpenParameter) {
     145      if (invertedTail.Function is OpenParameter || invertedTail.Function is Variable) {
    146146        invertedNode.InsertSubTree(0, invertedTail);
    147147        return invertedNode;
     
    178178        // not found
    179179        if (targetIndex == -1) throw new InvalidOperationException();
    180         IFunctionTree targetChain = InvertFunction(subTrees[targetIndex]);
     180        IFunctionTree targetChain = TransformToFunction(InvertFunction(subTrees[targetIndex]));
    181181        for (int i = 0; i < subTrees.Count; i++) {
    182182          if (i != targetIndex)
    183             combinator.AddSubTree(subTrees[i]);
    184         }
    185         if (targetChain.Function is OpenParameter) return combinator;
     183            combinator.AddSubTree(TransformToFunction(subTrees[i]));
     184        }
     185        if (targetChain.Function is Variable) return combinator;
    186186        else {
    187187          AppendLeft(targetChain, combinator);
     
    190190      }
    191191      throw new NotImplementedException();
     192    }
     193
     194    private static IFunctionTree TransformToFunction(IFunctionTree tree) {
     195      if (tree.SubTrees.Count == 0) return tree;
     196      else if (tree.Function is AdditionF1) {
     197        var addTree = (new Addition()).GetTreeNode();
     198        foreach (var subTree in tree.SubTrees) {
     199          addTree.AddSubTree(TransformToFunction(subTree));
     200        }
     201        return addTree;
     202      } else if (tree.Function is SubtractionF1) {
     203        var sTree = (new Subtraction()).GetTreeNode();
     204        foreach (var subTree in tree.SubTrees) {
     205          sTree.AddSubTree(TransformToFunction(subTree));
     206        }
     207        return sTree;
     208      } else if (tree.Function is MultiplicationF1) {
     209        var mulTree = (new Multiplication()).GetTreeNode();
     210        foreach (var subTree in tree.SubTrees) {
     211          mulTree.AddSubTree(TransformToFunction(subTree));
     212        }
     213        return mulTree;
     214      } else if (tree.Function is DivisionF1) {
     215        var divTree = (new Division()).GetTreeNode();
     216        foreach (var subTree in tree.SubTrees) {
     217          divTree.AddSubTree(TransformToFunction(subTree));
     218        }
     219        return divTree;
     220      } else if (tree.Function is OpenExp) {
     221        var expTree = (new Exponential()).GetTreeNode();
     222        expTree.AddSubTree(TransformToFunction(tree.SubTrees[0]));
     223        return expTree;
     224      } else if (tree.Function is OpenLog) {
     225        var logTree = (new Logarithm()).GetTreeNode();
     226        logTree.AddSubTree(TransformToFunction(tree.SubTrees[0]));
     227        return logTree;
     228      } else if (tree.Function is OpenSqr) {
     229        var powTree = (new Power()).GetTreeNode();
     230        powTree.AddSubTree(TransformToFunction(tree.SubTrees[0]));
     231        var const2 = (ConstantFunctionTree)(new Constant()).GetTreeNode();
     232        const2.Value = 2.0;
     233        powTree.AddSubTree(const2);
     234        return powTree;
     235      } else if (tree.Function is OpenSqrt) {
     236        var sqrtTree = (new Sqrt()).GetTreeNode();
     237        sqrtTree.AddSubTree(TransformToFunction(tree.SubTrees[0]));
     238        return sqrtTree;
     239      }
     240      throw new ArgumentException();
    192241    }
    193242
     
    218267    private static bool HasTargetVariable(IFunctionTree tree, string targetVariable) {
    219268      if (tree.SubTrees.Count == 0) {
    220         return ((OpenParameterFunctionTree)tree).VariableName == targetVariable;
    221       } else return HasTargetVariable(tree.SubTrees[0], targetVariable);
    222     }
    223 
    224     private static IFunctionTree BindVariables(IFunctionTree tree, IEnumerable<string> targetVariables) {
    225       IEnumerator<string> targetVariablesEnumerator = targetVariables.GetEnumerator();
    226       foreach (IFunctionTree node in FunctionTreeIterator.IteratePrefix(tree)) {
    227         if (node.Function is OpenParameter && targetVariablesEnumerator.MoveNext()) {
    228           var varTreeNode = node as OpenParameterFunctionTree;
    229           varTreeNode.VariableName = targetVariablesEnumerator.Current;
    230         }
    231       }
    232       return tree;
     269        var varTree = tree as VariableFunctionTree;
     270        if (varTree != null) return varTree.VariableName == targetVariable;
     271        else return false;
     272      } else return (from x in tree.SubTrees
     273                     where HasTargetVariable(x, targetVariable)
     274                     select true).Any();
     275    }
     276
     277    private static IFunctionTree BindVariables(IFunctionTree tree, IEnumerator<string> targetVariables) {
     278      if (tree.Function is OpenParameter && targetVariables.MoveNext()) {
     279        var varTreeNode = (VariableFunctionTree)(new Variable()).GetTreeNode();
     280        varTreeNode.VariableName = targetVariables.Current;
     281        varTreeNode.SampleOffset = ((OpenParameterFunctionTree)tree).SampleOffset;
     282        varTreeNode.Weight = 1.0;
     283        return varTreeNode;
     284      } else {
     285        IList<IFunctionTree> subTrees = new List<IFunctionTree>(tree.SubTrees);
     286        while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0);
     287        foreach (IFunctionTree subTree in subTrees) {
     288          tree.AddSubTree(BindVariables(subTree, targetVariables));
     289        }
     290        return tree;
     291      }
    233292    }
    234293  }
  • trunk/sources/HeuristicLab.GP.Tests/NetworkToFunctionTransformerTest.cs

    r2616 r2622  
    77using System.Collections.Generic;
    88
    9 namespace HeuristicLab.GP.Tests {
     9namespace HeuristicLab.GP.Test {
    1010
    1111
     
    6565
    6666    /// <summary>
    67     ///A test for TransformExpression
    68     ///</summary>
    69     [TestMethod()]
    70     [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")]
    71     public void TransformExpressionTest() {
    72       IFunctionTree tree = null; // TODO: Initialize to an appropriate value
    73       string targetVariable = string.Empty; // TODO: Initialize to an appropriate value
    74       IFunctionTree expected = null; // TODO: Initialize to an appropriate value
    75       IFunctionTree actual;
    76       actual = NetworkToFunctionTransformer_Accessor.TransformExpression(tree, targetVariable);
    77       Assert.AreEqual(expected, actual);
    78       Assert.Inconclusive("Verify the correctness of this test method.");
    79     }
    80 
    81     /// <summary>
    8267    ///A test for InvertFunction
    8368    ///</summary>
     
    115100    [DeploymentItem("HeuristicLab.GP.StructureIdentification.Networks-3.2.dll")]
    116101    public void TransformTest() {
    117       var log = new OpenLog();
    118       var exp = new OpenExp();
    119       var openAdd = new AdditionF1();
    120       var openSub = new SubtractionF1();
    121       var openMul = new MultiplicationF1();
    122       var openDiv = new DivisionF1();
    123       var param = new OpenParameter();
    124       var rootAdd = new OpenAddition();
    125       var rootSub = new OpenSubtraction();
    126       var rootMul = new OpenMultiplication();
    127       var rootDiv = new OpenDivision();
    128       var closedAdd = new Addition();
    129       var closedSub = new Subtraction();
    130       var closedMul = new Multiplication();
    131       var closedDiv = new Division();
    132       var closedVar = new HeuristicLab.GP.StructureIdentification.Variable();
     102      SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
    133103
    134       IFunctionTree tree = rootAdd.GetTreeNode();
    135       tree.AddSubTree(param.GetTreeNode());
    136       tree.AddSubTree(param.GetTreeNode());
    137       tree.AddSubTree(param.GetTreeNode());
     104      {
     105        IFunctionTree tree = importer.Import("(open-+ (open-param - 0) (open-param - 0) (open-param - 0))");
    138106
    139       IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "1", "2", "3" });
    140       IFunctionTree t0 = closedAdd.GetTreeNode(); t0.AddSubTree(param.GetTreeNode()); t0.AddSubTree(param.GetTreeNode());
    141       IFunctionTree t1 = closedSub.GetTreeNode(); t1.AddSubTree(param.GetTreeNode()); t1.AddSubTree(param.GetTreeNode());
    142       IFunctionTree t2 = closedSub.GetTreeNode(); t2.AddSubTree(param.GetTreeNode()); t2.AddSubTree(param.GetTreeNode());
     107        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
    143108
    144       var expectedTrees = (new List<IFunctionTree>() {
     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>() {
    145114        t0, t1, t2
    146       }).GetEnumerator();
    147       foreach (var actualTree in actualTrees) {
    148         if (!expectedTrees.MoveNext()) Assert.Fail();
    149         IFunctionTree expectedTree = expectedTrees.Current;
     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;
    150174        var e = (from x in FunctionTreeIterator.IteratePostfix(expectedTree)
    151                  select x.Function.GetType()).GetEnumerator();
     175                 select x).GetEnumerator();
    152176        var a = (from x in FunctionTreeIterator.IteratePostfix(actualTree)
    153                  select x.Function.GetType()).GetEnumerator();
     177                 select x).GetEnumerator();
    154178        Assert.AreEqual(expectedTree.GetSize(), actualTree.GetSize());
    155179        while (e.MoveNext() && a.MoveNext()) {
    156           Assert.AreEqual(e.Current, a.Current);
     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          }
    157192        }
    158193      }
    159194    }
    160 
    161195  }
    162196}
  • trunk/sources/HeuristicLab.GP.Tests/SymbolicExpressionImporter.cs

    r2447 r2622  
    3333    private const string DIFFSTART = "dif";
    3434    private const string VARSTART = "var";
     35    private const string OPENPARAMSTART = "open-param";
    3536    private Dictionary<string, IFunction> knownFunctions = new Dictionary<string, IFunction>()
    3637      {
     
    5556        {"-", new Subtraction()},
    5657        {"tan", new Tangens()},
    57         {"xor", new Xor()}
     58        {"xor", new Xor()},
     59        {"open-param", new HeuristicLab.GP.StructureIdentification.Networks.OpenParameter()},
     60        {"open-+", new HeuristicLab.GP.StructureIdentification.Networks.OpenAddition()},
     61        {"open--", new HeuristicLab.GP.StructureIdentification.Networks.OpenSubtraction()},
     62        {"open-*", new HeuristicLab.GP.StructureIdentification.Networks.OpenMultiplication()},
     63        {"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()},
     66        {"open-sqr", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqr()},
     67        {"open-sqrt", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqrt()},
     68        {"f1-+", new HeuristicLab.GP.StructureIdentification.Networks.AdditionF1()},
     69        {"f1--", new HeuristicLab.GP.StructureIdentification.Networks.SubtractionF1()},
     70        {"f1-/", new HeuristicLab.GP.StructureIdentification.Networks.DivisionF1()},
     71        {"f1-*", new HeuristicLab.GP.StructureIdentification.Networks.MultiplicationF1()},
     72        {"cycle", new HeuristicLab.GP.StructureIdentification.Networks.Cycle()},
     73        {"flip", new HeuristicLab.GP.StructureIdentification.Networks.Flip()},
     74
    5875      };
    5976    Constant constant = new Constant();
    6077    HeuristicLab.GP.StructureIdentification.Variable variable = new HeuristicLab.GP.StructureIdentification.Variable();
    6178    Differential differential = new Differential();
    62 
     79    HeuristicLab.GP.StructureIdentification.Networks.OpenParameter openParam = new HeuristicLab.GP.StructureIdentification.Networks.OpenParameter();
    6380    public SymbolicExpressionImporter() {
    6481    }
     
    85102        } else if (tokens.Peek().StringValue.StartsWith(DIFFSTART)) {
    86103          tree = ParseDifferential(tokens);
     104        } else if(tokens.Peek().StringValue.StartsWith(OPENPARAMSTART)) {
     105          tree = ParseOpenParameter(tokens);
    87106        } else {
    88107          Token curToken = tokens.Dequeue();
     
    99118        return t;
    100119      } else throw new FormatException("Expected function or constant symbol");
     120    }
     121
     122    private IFunctionTree ParseOpenParameter(Queue<Token> tokens) {
     123      Token tok = tokens.Dequeue();
     124      Debug.Assert(tok.StringValue == "open-param");
     125      HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree t = (HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree)openParam.GetTreeNode();     
     126      t.VariableName = tokens.Dequeue().StringValue;
     127      t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
     128      return t;
    101129    }
    102130
Note: See TracChangeset for help on using the changeset viewer.