Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/04/17 13:58:05 (8 years ago)
Author:
gkronber
Message:

#2650: extended simplifier to pass all new unit tests for factors and binary factors

Location:
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs

    r14535 r14539  
    228228      // create a set of (virtual) values to match the number of weights
    229229      t.Symbol.VariableNames = new string[] { t.VariableName };
    230       t.Symbol.VariableValues = new KeyValuePair<string, List<string>>[] { new KeyValuePair<string, List<string>>(t.VariableName, weights.Select((_, i) => "x" + i).ToList()) };
     230      t.Symbol.VariableValues = new KeyValuePair<string, List<string>>[] { new KeyValuePair<string, List<string>>(t.VariableName, weights.Select((_, i) => "X" + i).ToList()) };
    231231      return t;
    232232    }
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs

    r14535 r14539  
    659659      } else if(IsBinFactor(node)) {
    660660        var binFactor = node as BinaryFactorVariableTreeNode;
    661         return MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Cos(binFactor.Weight));
     661        // cos(0) = 1 see similar case for Exp(binfactor)
     662        return MakeSum(MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Cos(binFactor.Weight) - 1),
     663          MakeConstant(1.0));
    662664      } else {
    663665        var cosNode = cosineSymbol.CreateTreeNode();
     
    675677        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Exp(w)));
    676678      } else if(IsBinFactor(node)) {
     679        // exp( binfactor w val=a) = if(val=a) exp(w) else exp(0) = binfactor( (exp(w) - 1) val a) + 1
    677680        var binFactor = node as BinaryFactorVariableTreeNode;
    678         return MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Exp(binFactor.Weight));
     681        return
     682          MakeSum(MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Exp(binFactor.Weight) - 1), MakeConstant(1.0));
    679683      } else if(IsLog(node)) {
    680684        return node.GetSubtree(0);
     
    696700        var factNode = node as FactorVariableTreeNode;
    697701        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Log(w)));
    698       } else if(IsBinFactor(node)) {
    699         var binFactor = node as BinaryFactorVariableTreeNode;
    700         return MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Log(binFactor.Weight));
    701702      } else if(IsExp(node)) {
    702703        return node.GetSubtree(0);
     
    881882        var node1 = b as FactorVariableTreeNode;
    882883        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u / v));
     884      } else if(IsFactor(a) && IsBinFactor(b) && ((IVariableTreeNode)a).VariableName == ((IVariableTreeNode)b).VariableName) {
     885        var node0 = a as FactorVariableTreeNode;
     886        var node1 = b as BinaryFactorVariableTreeNode;
     887        var varValues = node0.Symbol.GetVariableValues(node0.VariableName).ToArray();
     888        var wi = Array.IndexOf(varValues, node1.VariableValue);
     889        if(wi < 0) throw new ArgumentException();
     890        var newWeighs = new double[varValues.Length];
     891        node0.Weights.CopyTo(newWeighs, 0);
     892        for(int i = 0; i < newWeighs.Length; i++)
     893          if(wi == i) newWeighs[i] /= node1.Weight;
     894          else newWeighs[i] /= 0.0;
     895        return MakeFactor(node0.Symbol, node0.VariableName, newWeighs);
    883896      } else if(IsFactor(a)) {
    884897        return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a)));
     
    935948        var node1 = b as FactorVariableTreeNode;
    936949        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u + v));
     950      } else if(IsBinFactor(a) && IsFactor(b)) {
     951        return MakeSum(b, a);
     952      } else if(IsFactor(a) && IsBinFactor(b) &&
     953        ((IVariableTreeNode)a).VariableName == ((IVariableTreeNode)b).VariableName) {
     954        var node0 = a as FactorVariableTreeNode;
     955        var node1 = b as BinaryFactorVariableTreeNode;
     956        var varValues = node0.Symbol.GetVariableValues(node0.VariableName).ToArray();
     957        var wi = Array.IndexOf(varValues, node1.VariableValue);
     958        if(wi < 0) throw new ArgumentException();
     959        var newWeighs = new double[varValues.Length];
     960        node0.Weights.CopyTo(newWeighs, 0);
     961        newWeighs[wi] += node1.Weight;
     962        return MakeFactor(node0.Symbol, node0.VariableName, newWeighs);
    937963      } else if(IsAddition(a) && IsAddition(b)) {
    938964        // merge additions
     
    10791105        var node1 = b as ConstantTreeNode;
    10801106        return MakeBinFactor(node0.Symbol, node0.VariableName, node0.VariableValue, node0.Weight * node1.Value);
     1107      } else if(IsBinFactor(a) && IsFactor(b)) {
     1108        return MakeProduct(b, a);
     1109      } else if(IsFactor(a) && IsBinFactor(b) &&
     1110        ((IVariableTreeNode)a).VariableName == ((IVariableTreeNode)b).VariableName) {
     1111        var node0 = a as FactorVariableTreeNode;
     1112        var node1 = b as BinaryFactorVariableTreeNode;
     1113        var varValues = node0.Symbol.GetVariableValues(node0.VariableName).ToArray();
     1114        var wi = Array.IndexOf(varValues, node1.VariableValue);
     1115        if(wi < 0) throw new ArgumentException();
     1116        return MakeBinFactor(node1.Symbol, node1.VariableName, node1.VariableValue, node1.Weight * node0.Weights[wi]);
    10811117      } else if(IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(1.0)) {
    10821118        // $ * 1.0 => $
Note: See TracChangeset for help on using the changeset viewer.