Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2674


Ignore:
Timestamp:
01/22/10 16:06:48 (15 years ago)
Author:
gkronber
Message:

Added weights for open parameters. #833 (Genetic Programming based search for equations (modeling with multiple target variables))

Location:
trunk/sources
Files:
4 edited

Legend:

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

    r2635 r2674  
    3030using HeuristicLab.DataAnalysis;
    3131using System.Diagnostics;
     32using HeuristicLab.Common;
    3233
    3334namespace HeuristicLab.GP.StructureIdentification.Networks {
     
    9394      } else if (tree.Function is Flip) {
    9495        var partiallyAppliedBranch = ApplyFlips(tree.SubTrees[0]);
    95         if (partiallyAppliedBranch.Function is OpenParameter) return partiallyAppliedBranch;
    96         else return InvertChain(partiallyAppliedBranch);
     96        if (partiallyAppliedBranch.Function is OpenParameter) {
     97          var openParFunTree = (OpenParameterFunctionTree)partiallyAppliedBranch;
     98          openParFunTree.Weight = 1.0 / openParFunTree.Weight;
     99          return partiallyAppliedBranch;
     100        } else return InvertChain(partiallyAppliedBranch);
    97101      } else {
    98102        List<IFunctionTree> subTrees = new List<IFunctionTree>(tree.SubTrees);
     
    116120      // get a list of function trees from bottom to top
    117121      List<IFunctionTree> reversedChain = new List<IFunctionTree>(currentChain.Reverse<IFunctionTree>().Skip(1));
    118       IFunctionTree openParam = currentChain.Last();
     122      OpenParameterFunctionTree openParam = (OpenParameterFunctionTree)currentChain.Last();
    119123
    120124      // build new tree by inverting every function in the reversed chain and keeping f0 branches untouched.
     
    136140        }
    137141      }
     142      // invert factor of openParam
     143      openParam.Weight = 1.0 / openParam.Weight;
    138144      // append open param at the end
    139145      invParent.InsertSubTree(0, openParam);
     
    195201
    196202
    197     private static IFunctionTree AppendLeft(IFunctionTree tree, IFunctionTree node) {
    198       IFunctionTree originalTree = tree;
    199       while (!IsBottomLeft(tree)) tree = tree.SubTrees[0];
    200       tree.InsertSubTree(0, node);
    201       return originalTree;
    202     }
     203    //private static IFunctionTree AppendLeft(IFunctionTree tree, IFunctionTree node) {
     204    //  IFunctionTree originalTree = tree;
     205    //  while (!IsBottomLeft(tree)) tree = tree.SubTrees[0];
     206    //  tree.InsertSubTree(0, node);
     207    //  return originalTree;
     208    //}
    203209
    204210    private static bool IsBottomLeft(IFunctionTree tree) {
     
    265271            combinator.AddSubTree(subTrees[i]);
    266272        }
    267         if (subTrees[targetIndex].Function is Variable) return combinator;
     273        if (subTrees[targetIndex].Function is Variable) return MakeMultiplication(combinator, 1.0 / GetTargetVariableWeight(subTrees[targetIndex]));
    268274        else {
    269275          IFunctionTree bottomLeft;
    270276          IFunctionTree targetChain = InvertF0Chain(subTrees[targetIndex], out bottomLeft);
    271277          bottomLeft.InsertSubTree(0, combinator);
    272           return targetChain;
    273         }
    274       }
     278          return MakeMultiplication(targetChain, 1.0 / GetTargetVariableWeight(subTrees[targetIndex]));
     279        }
     280      }
     281    }
     282
     283    private static IFunctionTree MakeMultiplication(IFunctionTree tree, double p) {
     284      if (p.IsAlmost(1.0)) return tree;
     285      var mul = (new Multiplication()).GetTreeNode();
     286      var constP = (ConstantFunctionTree)(new Constant()).GetTreeNode();
     287      constP.Value = p;
     288      mul.AddSubTree(tree);
     289      mul.AddSubTree(constP);
     290      return mul;
     291    }
     292
     293    private static double GetTargetVariableWeight(IFunctionTree tree) {
     294      while (tree.SubTrees.Count > 0) {
     295        tree = tree.SubTrees[0];
     296      }
     297      return ((VariableFunctionTree)tree).Weight;
    275298    }
    276299
     
    377400        varTreeNode.VariableName = targetVariables.Current;
    378401        varTreeNode.SampleOffset = ((OpenParameterFunctionTree)tree).SampleOffset;
    379         varTreeNode.Weight = 1.0;
     402        varTreeNode.Weight = ((OpenParameterFunctionTree)tree).Weight;
    380403        return varTreeNode;
    381404        //} else if (matchingFunction is Power) {
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2/Symbols/OpenParameterFunctionTree.cs

    r2616 r2674  
    3030    public string VariableName { get; set; }
    3131    public int SampleOffset { get; set; }
     32    public double Weight { get; set; }
    3233
    3334    public OpenParameterFunctionTree(OpenParameter openParameter)
    3435      : base(openParameter) {
     36      this.Weight = 1.0;
    3537    }
    3638
     
    3941      VariableName = original.VariableName;
    4042      SampleOffset = original.SampleOffset;
     43      Weight = original.Weight;
    4144    }
    4245
     
    5154      scope.AddSubScope(myVariableScope);
    5255      myVariableScope.AddVariable(CreateSampleOffsetVariable());
     56      myVariableScope.AddVariable(CreateWeightVariable());
    5357      return new AtomicOperation(Function.Manipulator, myVariableScope);
    5458    }
     
    5862      scope.AddSubScope(myVariableScope);
    5963      myVariableScope.AddVariable(CreateSampleOffsetVariable());
     64      myVariableScope.AddVariable(CreateWeightVariable());
    6065      return new AtomicOperation(Function.Initializer, myVariableScope);
    6166    }
     
    7277    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
    7378      XmlNode node = document.CreateElement(name);
     79      XmlAttribute weightAttr = document.CreateAttribute("Weight");
     80      weightAttr.Value = XmlConvert.ToString(Weight);
    7481      XmlAttribute variableAttr = document.CreateAttribute("Variable");
    7582      variableAttr.Value = VariableName;
    7683      XmlAttribute sampleOffsetAttr = document.CreateAttribute("SampleOffset");
    7784      sampleOffsetAttr.Value = XmlConvert.ToString(SampleOffset);
     85      node.Attributes.Append(weightAttr);
    7886      node.Attributes.Append(sampleOffsetAttr);
    7987      node.Attributes.Append(variableAttr);
     
    8391    public override void Populate(XmlNode node, IDictionary<System.Guid, IStorable> restoredObjects) {
    8492      base.Populate(node, restoredObjects);
     93      Weight = XmlConvert.ToDouble(node.Attributes["Weight"].Value);
    8594      SampleOffset = XmlConvert.ToInt32(node.Attributes["SampleOffset"].Value);
    8695      VariableName = node.Attributes["Variable"].Value;
     
    103112      return variable;
    104113    }
     114
     115    private IVariable CreateWeightVariable() {
     116      DoubleData data = new DoubleData(Weight);
     117      data.Changed += (sender, args) => Weight = data.Data;
     118      var variable = new HeuristicLab.Core.Variable(Variable.WEIGHT, data);
     119      variable.ValueChanged += (sender, args) => Weight = ((DoubleData)variable.Value).Data;
     120      return variable;
     121    }
    105122  }
    106123}
  • trunk/sources/HeuristicLab.GP.Tests/NetworkToFunctionTransformerTest.cs

    r2635 r2674  
    88using HeuristicLab.Random;
    99using HeuristicLab.DataAnalysis;
     10using HeuristicLab.Common;
    1011
    1112namespace HeuristicLab.GP.Test {
     
    105106
    106107      {
    107         IFunctionTree tree = importer.Import("(open-+ (open-param - 0) (open-param - 0) (open-param - 0))");
    108 
    109         IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
    110 
    111         IFunctionTree t0 = importer.Import("(+ (variable 1.0 b 0) (variable 1.0 c 0))");
    112         IFunctionTree t1 = importer.Import("(- (variable 1.0 a 0) (variable 1.0 c 0))");
    113         IFunctionTree t2 = importer.Import("(- (variable 1.0 a 0) (variable 1.0 b 0))");
    114 
    115         CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
    116         t0, t1, t2
    117       });
    118       }
    119 
    120       {
    121         IFunctionTree tree = importer.Import("(open-- (open-param - 0) (f1-+ (open-param - 0) 1.0) (f1-* (open-param - 0) 1.0))");
    122 
    123         IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
    124 
    125         IFunctionTree t0 = importer.Import("(- (+ (variable 1.0 b 0) 1.0) (* (variable 1.0 c 0) 1.0 ))");
    126         IFunctionTree t1 = importer.Import("(- (+ (variable 1.0 a 0) (* (variable 1.0 c 0) 1.0)) 1.0 )");
    127         IFunctionTree t2 = importer.Import("(/ (+ (variable 1.0 a 0) (+ (variable 1.0 b 0) 1.0)) 1.0 )");
    128 
    129         CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
    130         t0, t1, t2
    131       });
    132       }
    133 
    134       {
    135         IFunctionTree tree = importer.Import("(cycle (open-* (open-param - 0) (open-param - 0) (open-param - 0)))");
    136 
    137         IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
    138 
    139         IFunctionTree t0 = importer.Import("(* (variable 1.0 b 0) (variable 1.0 c 0))");
    140         IFunctionTree t1 = importer.Import("(/ (variable 1.0 a 0) (variable 1.0 c 0))");
    141         IFunctionTree t2 = importer.Import("(/ (variable 1.0 a 0) (variable 1.0 b 0))");
    142 
    143 
    144         CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
    145         t0, t1, t2
    146       });
    147       }
    148 
    149 
    150       {
    151         IFunctionTree tree = importer.Import("(open-- (open-log (open-param - 0)) (open-exp (open-param - 0)) (open-param - 0))");
     108        IFunctionTree tree = importer.Import("(open-+ (open-param 1.0 - 0) (open-param 2.0 - 0) (open-param 4.0 - 0))");
     109
     110        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     111
     112        IFunctionTree t0 = importer.Import("(+ (variable 2.0 b 0) (variable 4.0 c 0))");
     113        IFunctionTree t1 = importer.Import("(* (- (variable 1.0 a 0) (variable 4.0 c 0)) 0.5)");
     114        IFunctionTree t2 = importer.Import("(* (- (variable 1.0 a 0) (variable 2.0 b 0)) 0.25)");
     115
     116        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
     117        t0, t1, t2
     118      });
     119      }
     120
     121      {
     122        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))");
     123
     124        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     125
     126        IFunctionTree t0 = importer.Import("(- (+ (variable 2.0 b 0) 1.0) (* (variable 4.0 c 0) 1.0 ))");
     127        IFunctionTree t1 = importer.Import("(* (- (+ (variable 1.0 a 0) (* (variable 4.0 c 0) 1.0)) 1.0 ) 0.5)");
     128        IFunctionTree t2 = importer.Import("(* (/ (+ (variable 1.0 a 0) (+ (variable 2.0 b 0) 1.0)) 1.0 ) 0.25)");
     129
     130        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
     131        t0, t1, t2
     132      });
     133      }
     134
     135      {
     136        IFunctionTree tree = importer.Import("(cycle (open-* (open-param 1.0 - 0) (open-param 2.0 - 0) (open-param 4.0 - 0)))");
     137
     138        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     139
     140        IFunctionTree t0 = importer.Import("(* (* (variable 4.0 b 0) (variable 1.0 c 0)) 0.5)");
     141        IFunctionTree t1 = importer.Import("(* (/ (variable 2.0 a 0) (variable 1.0 c 0)) 0.25)");
     142        IFunctionTree t2 = importer.Import("(/ (variable 2.0 a 0) (variable 4.0 b 0))");
     143
     144
     145        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
     146        t0, t1, t2
     147      });
     148      }
     149
     150
     151      {
     152        IFunctionTree tree = importer.Import("(open-- (open-log (open-param 1.0 - 0)) (open-exp (open-param 1.0 - 0)) (open-param 1.0 - 0))");
    152153
    153154        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     
    165166      }
    166167      {
    167         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))");
     168        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))");
    168169
    169170        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     
    181182      }
    182183      {
    183         IFunctionTree tree = importer.Import(@"(open-- (flip (open-log (open-param - 0)))
    184                                                        (flip (f1-* (open-param - 0) 2.0))
    185                                                        (flip (flip (f1-* (open-param - 0) 2.0))))");
    186 
    187         IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
    188 
    189         IFunctionTree t0 = importer.Import(@"(log (- (/ (variable 1.0 b 0) 2.0)
    190                                                      (* (variable 1.0 c 0) 2.0)))");
    191         IFunctionTree t1 = importer.Import(@"(* (+ (exp (variable 1.0 a 0))
    192                                                    (* (variable 1.0 c 0) 2.0)) 2.0)");
    193         IFunctionTree t2 = importer.Import(@"(/ (+ (exp (variable 1.0 a 0))
    194                                                    (/ (variable 1.0 b 0) 2.0)) 2.0)");
     184        IFunctionTree tree = importer.Import(@"(open-- (flip (open-log (open-param 1.0 - 0)))
     185                                                       (flip (f1-* (open-param 2.0 - 0) 2.0))
     186                                                       (flip (flip (f1-* (open-param 4.0 - 0) 2.0))))");
     187
     188        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     189
     190        IFunctionTree t0 = importer.Import(@"(log (- (/ (variable 0.5 b 0) 2.0)
     191                                                     (* (variable 4.0 c 0) 2.0)))");
     192        IFunctionTree t1 = importer.Import(@"(* (* (+ (exp (variable 1.0 a 0))
     193                                                      (* (variable 4.0 c 0) 2.0)) 2.0) 2)");
     194        IFunctionTree t2 = importer.Import(@"(* (/ (+ (exp (variable 1.0 a 0))
     195                                                      (/ (variable 0.5 b 0) 2.0)) 2.0) 0.25)");
    195196
    196197        CompareTrees(actualTrees.ToList(), new List<IFunctionTree>() {
     
    203204                                                   (flip (f1--
    204205                                                     (flip (f1-/
    205                                                        (open-param - 0) 4.0)) 3.0)) 2.0))
    206                                                  (open-param - 0)
    207                                                  (open-param - 0)))");
     206                                                       (open-param 1.0 - 0) 4.0)) 3.0)) 2.0))
     207                                                 (open-param 1.0 - 0)
     208                                                 (open-param 1.0 - 0)))");
    208209        // -3*4-2 x
    209210        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     
    235236      {
    236237        // expression with one parameter
    237         IFunctionTree tree = importer.Import(@"(f1-* (open-param - 0) (variable 1.0 d 0))");
     238        IFunctionTree tree = importer.Import(@"(f1-* (open-param 1.0 - 0) (variable 1.0 d 0))");
    238239
    239240        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     
    250251      {
    251252        // expression with one parameter
    252         IFunctionTree tree = importer.Import(@"(open-log (open-param - 0))");
     253        IFunctionTree tree = importer.Import(@"(open-log (open-param 1.0 - 0))");
    253254
    254255        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     
    265266      {
    266267        // expression with flip and one parameter
    267         IFunctionTree tree = importer.Import(@"(flip (open-log (open-param - 0)))");
     268        IFunctionTree tree = importer.Import(@"(flip (open-log (open-param 1.0 - 0)))");
    268269
    269270        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     
    281282      {
    282283        // expression with flip and one parameter
    283         IFunctionTree tree = importer.Import(@"(open-param - 0)");
     284        IFunctionTree tree = importer.Import(@"(open-param 1.0 - 0)");
    284285
    285286        IEnumerable<IFunctionTree> actualTrees = NetworkToFunctionTransformer_Accessor.Transform(tree, new List<string>() { "a", "b", "c" });
     
    297298
    298299    private void CompareTrees(List<IFunctionTree> actual, List<IFunctionTree> expected) {
    299       var expectedEnumerator = expected.GetEnumerator();     
     300      var expectedEnumerator = expected.GetEnumerator();
    300301      foreach (var actualTree in actual) {
    301302        if (!expectedEnumerator.MoveNext()) Assert.Fail();
     
    312313            var actualVar = (VariableFunctionTree)a.Current;
    313314            Assert.AreEqual(expectedVar.VariableName, actualVar.VariableName);
    314             Assert.AreEqual(expectedVar.Weight, actualVar.Weight);
     315            Assert.IsTrue(expectedVar.Weight.IsAlmost(actualVar.Weight));
    315316            Assert.AreEqual(expectedVar.SampleOffset, actualVar.SampleOffset);
    316317          } else if (e.Current.Function is Constant) {
  • trunk/sources/HeuristicLab.GP.Tests/SymbolicExpressionImporter.cs

    r2627 r2674  
    102102        } else if (tokens.Peek().StringValue.StartsWith(DIFFSTART)) {
    103103          tree = ParseDifferential(tokens);
    104         } else if(tokens.Peek().StringValue.StartsWith(OPENPARAMSTART)) {
     104        } else if (tokens.Peek().StringValue.StartsWith(OPENPARAMSTART)) {
    105105          tree = ParseOpenParameter(tokens);
    106106        } else {
     
    123123      Token tok = tokens.Dequeue();
    124124      Debug.Assert(tok.StringValue == "open-param");
    125       HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree t = (HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree)openParam.GetTreeNode();     
     125      HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree t = (HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree)openParam.GetTreeNode();
     126      t.Weight = tokens.Dequeue().DoubleValue;
    126127      t.VariableName = tokens.Dequeue().StringValue;
    127128      t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
Note: See TracChangeset for help on using the changeset viewer.