- Timestamp:
- 01/04/17 14:49:12 (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
r14539 r14540 237 237 var t = (BinaryFactorVariableTreeNode)binFactorVar.CreateTreeNode(); 238 238 var varNameTok = tokens.Dequeue(); 239 Debug.Assert( tok.Symbol == TokenSymbol.SYMB);239 Debug.Assert(varNameTok.Symbol == TokenSymbol.SYMB); 240 240 t.VariableName = varNameTok.StringValue; 241 241 242 242 var varValTok = tokens.Dequeue(); 243 Debug.Assert( tok.Symbol == TokenSymbol.SYMB);243 Debug.Assert(varValTok.Symbol == TokenSymbol.SYMB); 244 244 t.VariableValue = varValTok.StringValue; 245 245 246 246 var weightTok = tokens.Dequeue(); 247 Debug.Assert( tok.Symbol == TokenSymbol.NUMBER);247 Debug.Assert(weightTok.Symbol == TokenSymbol.NUMBER); 248 248 t.Weight = weightTok.DoubleValue; 249 249 -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
r14539 r14540 24 24 using System; 25 25 using System.Collections.Generic; 26 using System.Diagnostics; 26 27 using System.Linq; 27 28 using HeuristicLab.Common; … … 64 65 ISymbolicExpressionTreeNode rootNode = (new ProgramRootSymbol()).CreateTreeNode(); 65 66 rootNode.AddSubtree(GetSimplifiedTree(macroExpandedTree)); 67 68 #if DEBUG 69 // check that each node is only referenced once 70 var nodes = rootNode.IterateNodesPrefix().ToArray(); 71 foreach(var n in nodes) if(nodes.Count(ni => ni == n) > 1) throw new InvalidOperationException(); 72 #endif 66 73 return new SymbolicExpressionTree(rootNode); 67 74 } … … 904 911 return a.Subtrees 905 912 .Select(x => GetSimplifiedTree(x)) 906 .Select(x => MakeFraction(x, b))913 .Select(x => MakeFraction(x, GetSimplifiedTree(b))) 907 914 .Aggregate((c, d) => MakeSum(c, d)); 908 915 } else if(IsMultiplication(a) && IsConstant(b)) { … … 1084 1091 if(IsConstant(a) && IsConstant(b)) { 1085 1092 // fold constants 1086 ((ConstantTreeNode)a).Value *= ((ConstantTreeNode)b).Value; 1087 return a; 1093 return MakeConstant(((ConstantTreeNode)a).Value * ((ConstantTreeNode)b).Value); 1088 1094 } else if(IsConstant(a)) { 1089 1095 // a * $ => $ * a … … 1123 1129 return a; 1124 1130 } else if(IsConstant(b) && IsAddition(a) || 1125 IsFactor(b) && IsAddition(a)) { 1131 IsFactor(b) && IsAddition(a) || 1132 IsBinFactor(b) && IsAddition(a)) { 1126 1133 // multiply constants into additions 1127 return a.Subtrees.Select(x => MakeProduct( x, b)).Aggregate((c, d) => MakeSum(c, d));1134 return a.Subtrees.Select(x => MakeProduct(GetSimplifiedTree(x), GetSimplifiedTree(b))).Aggregate((c, d) => MakeSum(c, d)); 1128 1135 } else if(IsDivision(a) && IsDivision(b)) { 1129 1136 // (a1 / a2) * (b1 / b2) => (a1 * b1) / (a2 * b2) … … 1146 1153 } else if(IsMultiplication(a)) { 1147 1154 // a is already an multiplication => append b 1148 a.AddSubtree( b);1155 a.AddSubtree(GetSimplifiedTree(b)); 1149 1156 MergeVariablesAndConstantsInProduct(a); 1150 1157 return a; … … 1256 1263 /// <summary> 1257 1264 /// x => x * -1 1258 /// Doesn't create new trees and manipulates x1265 /// Is only used in cases where it is not necessary to create new tree nodes. Manipulates x directly. 1259 1266 /// </summary> 1260 1267 /// <param name="x"></param> … … 1293 1300 /// <summary> 1294 1301 /// x => 1/x 1295 /// Doesn't create new trees and manipulates x1302 /// Must create new tree nodes 1296 1303 /// </summary> 1297 1304 /// <param name="x"></param> … … 1302 1309 } else if(IsFactor(x)) { 1303 1310 var factorNode = (FactorVariableTreeNode)x; 1304 for(int i = 0; i < factorNode.Weights.Length; i++) factorNode.Weights[i] = 1.0 / factorNode.Weights[i]; 1305 return factorNode; 1311 return MakeFactor(factorNode.Symbol, factorNode.VariableName, factorNode.Weights.Select(w => 1.0 / w)); 1306 1312 } else if(IsDivision(x)) { 1307 1313 return MakeFraction(x.GetSubtree(1), x.GetSubtree(0));
Note: See TracChangeset
for help on using the changeset viewer.