- Timestamp:
- 01/04/17 13:58:05 (8 years ago)
- 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 228 228 // create a set of (virtual) values to match the number of weights 229 229 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()) }; 231 231 return t; 232 232 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
r14535 r14539 659 659 } else if(IsBinFactor(node)) { 660 660 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)); 662 664 } else { 663 665 var cosNode = cosineSymbol.CreateTreeNode(); … … 675 677 return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Exp(w))); 676 678 } 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 677 680 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)); 679 683 } else if(IsLog(node)) { 680 684 return node.GetSubtree(0); … … 696 700 var factNode = node as FactorVariableTreeNode; 697 701 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));701 702 } else if(IsExp(node)) { 702 703 return node.GetSubtree(0); … … 881 882 var node1 = b as FactorVariableTreeNode; 882 883 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); 883 896 } else if(IsFactor(a)) { 884 897 return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a))); … … 935 948 var node1 = b as FactorVariableTreeNode; 936 949 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); 937 963 } else if(IsAddition(a) && IsAddition(b)) { 938 964 // merge additions … … 1079 1105 var node1 = b as ConstantTreeNode; 1080 1106 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]); 1081 1117 } else if(IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(1.0)) { 1082 1118 // $ * 1.0 => $
Note: See TracChangeset
for help on using the changeset viewer.