Changeset 15973 for branches/2522_RefactorPluginInfrastructure/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
- Timestamp:
- 06/28/18 11:13:37 (7 years ago)
- Location:
- branches/2522_RefactorPluginInfrastructure
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2522_RefactorPluginInfrastructure
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/2522_RefactorPluginInfrastructure/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r12012 r15973 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 33 33 [StorableClass] 34 34 public sealed class SymbolicDataAnalysisExpressionLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 35 private readonly List<double> constants; 35 private readonly List<KeyValuePair<string, double>> constants; 36 private int constIndex; 36 37 private int targetCount; 37 38 private int currentLag; 39 private string targetVariable; 40 private bool containsTimeSeriesSymbol; 38 41 39 42 [StorableConstructor] … … 41 44 private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner) 42 45 : base(original, cloner) { 43 constants = new List<double>(original.constants); 46 constants = new List<KeyValuePair<string, double>>(original.constants); 47 constIndex = original.constIndex; 48 currentLag = original.currentLag; 49 targetCount = original.targetCount; 44 50 } 45 51 public SymbolicDataAnalysisExpressionLatexFormatter() … … 47 53 Name = ItemName; 48 54 Description = ItemDescription; 49 constants = new List< double>();55 constants = new List<KeyValuePair<string, double>>(); 50 56 } 51 57 … … 55 61 56 62 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 63 return Format(symbolicExpressionTree, null); 64 } 65 public string Format(ISymbolicExpressionTree symbolicExpressionTree, string targetVariable) { 57 66 try { 58 67 StringBuilder strBuilder = new StringBuilder(); 59 68 constants.Clear(); 69 constIndex = 0; 70 this.targetVariable = targetVariable; 71 containsTimeSeriesSymbol = symbolicExpressionTree.IterateNodesBreadth().Any(n => IsTimeSeriesSymbol(n.Symbol)); 60 72 strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root)); 61 73 return strBuilder.ToString(); 62 } 63 catch (NotImplementedException ex) { 74 } catch (NotImplementedException ex) { 64 75 return ex.Message + Environment.NewLine + ex.StackTrace; 65 76 } 77 } 78 static bool IsTimeSeriesSymbol(ISymbol s) { 79 return s is TimeLag || s is Integral || s is Derivative || s is LaggedVariable; 66 80 } 67 81 … … 75 89 } 76 90 int i = 1; 77 foreach ( SymbolicExpressionTreeNodesubTree in node.Subtrees.Skip(1)) {91 foreach (var subTree in node.Subtrees.Skip(1)) { 78 92 FormatSep(node, strBuilder, i); 79 93 // format the whole subtree … … 99 113 } else if (node.Symbol is Division) { 100 114 if (node.SubtreeCount == 1) { 101 strBuilder.Append(@" \cfrac{1 ");115 strBuilder.Append(@" \cfrac{1}{"); 102 116 } else { 103 117 strBuilder.Append(@" \cfrac{ "); … … 166 180 strBuilder.Append(@" \operatorname{if} \left( "); 167 181 } else if (node.Symbol is Constant) { 168 strBuilder.Append("c_{" + constants.Count + "} "); 182 var constName = "c_{" + constIndex + "}"; 183 constIndex++; 169 184 var constNode = node as ConstantTreeNode; 170 constants.Add(constNode.Value); 185 if (constNode.Value.IsAlmost(1.0)) { 186 strBuilder.Append("1 "); 187 } else { 188 strBuilder.Append(constName); 189 constants.Add(new KeyValuePair<string, double>(constName, constNode.Value)); 190 } 191 192 } else if (node.Symbol is FactorVariable) { 193 var factorNode = node as FactorVariableTreeNode; 194 var constName = "c_{" + constIndex + "}"; 195 strBuilder.Append(constName + " "); 196 foreach (var e in factorNode.Symbol.GetVariableValues(factorNode.VariableName) 197 .Zip(factorNode.Weights, Tuple.Create)) { 198 constants.Add(new KeyValuePair<string, double>("c_{" + constIndex + ", " + EscapeLatexString(factorNode.VariableName) + "=" + EscapeLatexString(e.Item1) + "}", e.Item2)); 199 } 200 constIndex++; 201 } else if (node.Symbol is BinaryFactorVariable) { 202 var binFactorNode = node as BinaryFactorVariableTreeNode; 203 if (!binFactorNode.Weight.IsAlmost((1.0))) { 204 var constName = "c_{" + constIndex + "}"; 205 strBuilder.Append(constName + " \\cdot"); 206 constants.Add(new KeyValuePair<string, double>(constName, binFactorNode.Weight)); 207 constIndex++; 208 } 209 strBuilder.Append("(" + EscapeLatexString(binFactorNode.VariableName)); 210 strBuilder.Append(LagToString(currentLag)); 211 strBuilder.Append(" = " + EscapeLatexString(binFactorNode.VariableValue) + " )"); 171 212 } else if (node.Symbol is LaggedVariable) { 172 213 var laggedVarNode = node as LaggedVariableTreeNode; 173 214 if (!laggedVarNode.Weight.IsAlmost(1.0)) { 174 strBuilder.Append("c_{" + constants.Count + "} \\cdot "); 175 constants.Add(laggedVarNode.Weight); 215 var constName = "c_{" + constIndex + "}"; 216 strBuilder.Append(constName + " \\cdot"); 217 constants.Add(new KeyValuePair<string, double>(constName, laggedVarNode.Weight)); 218 constIndex++; 176 219 } 177 220 strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName)); … … 181 224 var varNode = node as VariableTreeNode; 182 225 if (!varNode.Weight.IsAlmost((1.0))) { 183 strBuilder.Append("c_{" + constants.Count + "} \\cdot "); 184 constants.Add(varNode.Weight); 226 var constName = "c_{" + constIndex + "}"; 227 strBuilder.Append(constName + " \\cdot"); 228 constants.Add(new KeyValuePair<string, double>(constName, varNode.Weight)); 229 constIndex++; 185 230 } 186 231 strBuilder.Append(EscapeLatexString(varNode.VariableName)); … … 197 242 strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( "); 198 243 } else if (node.Symbol is StartSymbol) { 199 strBuilder.Append("target_" + (targetCount++) + "(t) & = ");244 FormatStartSymbol(strBuilder); 200 245 } else if (node.Symbol is Argument) { 201 246 var argSym = node.Symbol as Argument; … … 216 261 } else if (node.Symbol is VariableCondition) { 217 262 var conditionTreeNode = node as VariableConditionTreeNode; 218 string p = @"1 / 1 + \exp - c_{" + constants.Count + "} "; 219 constants.Add(conditionTreeNode.Slope); 220 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} "; 221 constants.Add(conditionTreeNode.Threshold); 263 var constName = "c_{" + constants.Count + "}"; 264 string p = @"1 / 1 + \exp - " + constName + " "; 265 constants.Add(new KeyValuePair<string, double>(constName, conditionTreeNode.Slope)); 266 constIndex++; 267 var const2Name = "c_{" + constants.Count + @"}"; 268 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + " "; 269 constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold)); 270 constIndex++; 222 271 strBuilder.Append(@" \left( " + p + @"\cdot "); 223 272 } else { … … 303 352 } else if (node.Symbol is StartSymbol) { 304 353 strBuilder.Append(@"\\" + Environment.NewLine); 305 strBuilder.Append("target_" + (targetCount++) + "(t) & = ");354 FormatStartSymbol(strBuilder); 306 355 } else if (node.Symbol is Power) { 307 356 strBuilder.Append(@"\right) ^ { \operatorname{round} \left("); … … 310 359 } else if (node.Symbol is VariableCondition) { 311 360 var conditionTreeNode = node as VariableConditionTreeNode; 312 string p = @"1 / \left( 1 + \exp \left( - c_{" + constants.Count + "} "; 313 constants.Add(conditionTreeNode.Slope); 314 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} \right) \right) \right) "; 315 constants.Add(conditionTreeNode.Threshold); 361 var const1Name = "c_{" + constants.Count + "}"; 362 string p = @"1 / \left( 1 + \exp \left( - " + const1Name + " "; 363 constants.Add(new KeyValuePair<string, double>(const1Name, conditionTreeNode.Slope)); 364 constIndex++; 365 var const2Name = "c_{" + constants.Count + "}"; 366 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + " \right) \right) \right) "; 367 constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold)); 368 constIndex++; 316 369 strBuilder.Append(@" + \left( 1 - " + p + @" \right) \cdot "); 317 370 } else { … … 391 444 } else if (node.Symbol is LaggedVariable) { 392 445 } else if (node.Symbol is Variable) { 446 } else if (node.Symbol is FactorVariable) { 447 } else if (node.Symbol is BinaryFactorVariable) { 393 448 } else if (node.Symbol is ProgramRootSymbol) { 394 449 strBuilder … … 398 453 // output all constant values 399 454 if (constants.Count > 0) { 400 int i = 0;401 455 foreach (var constant in constants) { 402 456 // replace "." with ".&" to align decimal points 403 var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant );457 var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value); 404 458 if (!constStr.Contains(".")) constStr = constStr + ".0"; 405 constStr = constStr.Replace(".", " \\negthickspace&."); // fix problem in rendering of aligned expressions406 strBuilder.Append( "c_{" + i + "}& = & " + constStr);459 constStr = constStr.Replace(".", "&."); // fix problem in rendering of aligned expressions 460 strBuilder.Append(constant.Key + "& = & " + constStr); 407 461 strBuilder.Append(@"\\"); 408 i++;409 462 } 410 463 } … … 433 486 } 434 487 488 private void FormatStartSymbol(StringBuilder strBuilder) { 489 strBuilder.Append(targetVariable ?? "target_" + (targetCount++)); 490 if (containsTimeSeriesSymbol) 491 strBuilder.Append("(t)"); 492 strBuilder.Append(" & = "); 493 } 494 435 495 private string LagToString(int lag) { 436 496 if (lag < 0) {
Note: See TracChangeset
for help on using the changeset viewer.