Changeset 16692 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters
- Timestamp:
- 03/18/19 17:24:30 (6 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 8 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r13116 r16692 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. … … 25 25 using System.Linq; 26 26 using System.Text; 27 using System.Text.RegularExpressions; 27 28 using HeuristicLab.Common; 28 29 using HeuristicLab.Core; … … 53 54 GenerateFooter(strBuilder); 54 55 return strBuilder.ToString(); 56 } 57 58 private string VariableName2Identifier(string name) { 59 /* 60 * identifier-start-character: 61 * letter-character 62 * _ (the underscore character U+005F) 63 * identifier-part-characters: 64 * identifier-part-character 65 * identifier-part-characters identifier-part-character 66 * identifier-part-character: 67 * letter-character 68 * decimal-digit-character 69 * connecting-character 70 * combining-character 71 * formatting-character 72 * letter-character: 73 * A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl 74 * A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl 75 * combining-character: 76 * A Unicode character of classes Mn or Mc 77 * A unicode-escape-sequence representing a character of classes Mn or Mc 78 * decimal-digit-character: 79 * A Unicode character of the class Nd 80 * A unicode-escape-sequence representing a character of the class Nd 81 * connecting-character: 82 * A Unicode character of the class Pc 83 * A unicode-escape-sequence representing a character of the class Pc 84 * formatting-character: 85 * A Unicode character of the class Cf 86 * A unicode-escape-sequence representing a character of the class Cf 87 */ 88 89 var invalidIdentifierStarts = new Regex(@"[^_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}]"); 90 var invalidIdentifierParts = new Regex(@"[^\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Cf}]"); 91 return "@" + 92 (invalidIdentifierStarts.IsMatch(name.Substring(0, 1)) ? "_" : "") + // prepend '_' if necessary 93 invalidIdentifierParts.Replace(name, "_"); 55 94 } 56 95 … … 106 145 if (node is VariableTreeNode) { 107 146 var varNode = node as VariableTreeNode; 108 strBuilder.AppendFormat("{0} * {1}", varNode.VariableName, varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));147 strBuilder.AppendFormat("{0} * {1}", VariableName2Identifier(varNode.VariableName), varNode.Weight.ToString("g17", CultureInfo.InvariantCulture)); 109 148 } else if (node is ConstantTreeNode) { 110 149 var constNode = node as ConstantTreeNode; 111 150 strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture)); 151 } else if (node.Symbol is FactorVariable) { 152 var factorNode = node as FactorVariableTreeNode; 153 FormatFactor(factorNode, strBuilder); 154 } else if (node.Symbol is BinaryFactorVariable) { 155 var binFactorNode = node as BinaryFactorVariableTreeNode; 156 FormatBinaryFactor(binFactorNode, strBuilder); 112 157 } else { 113 158 throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter."); 114 159 } 115 160 } 161 } 162 163 private void FormatFactor(FactorVariableTreeNode node, StringBuilder strBuilder) { 164 strBuilder.AppendFormat("EvaluateFactor({0}, new [] {{ {1} }}, new [] {{ {2} }})", VariableName2Identifier(node.VariableName), 165 string.Join(",", node.Symbol.GetVariableValues(node.VariableName).Select(name => "\"" + name + "\"")), string.Join(",", node.Weights.Select(v => v.ToString(CultureInfo.InvariantCulture)))); 166 } 167 168 private void FormatBinaryFactor(BinaryFactorVariableTreeNode node, StringBuilder strBuilder) { 169 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "EvaluateBinaryFactor({0}, \"{1}\", {2})", VariableName2Identifier(node.VariableName), node.VariableValue, node.Weight); 116 170 } 117 171 … … 182 236 GenerateAverageSource(strBuilder); 183 237 GenerateIfThenElseSource(strBuilder); 238 GenerateFactorSource(strBuilder); 239 GenerateBinaryFactorSource(strBuilder); 184 240 strBuilder.Append(Environment.NewLine + "public static double Evaluate ("); 185 241 186 HashSet<string> varNames = new HashSet<string>(); 187 foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode)) { 188 varNames.Add(((VariableTreeNode)node).VariableName); 189 } 190 191 var orderedNames = varNames.OrderBy(n => n, new NaturalStringComparer()).Select(n => "double " + n); 242 // here we don't have access to problemData to determine the type for each variable (double/string) therefore we must distinguish based on the symbol type 243 HashSet<string> doubleVarNames = new HashSet<string>(); 244 foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode || x is VariableConditionTreeNode)) { 245 doubleVarNames.Add(((IVariableTreeNode)node).VariableName); 246 } 247 248 HashSet<string> stringVarNames = new HashSet<string>(); 249 foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is BinaryFactorVariableTreeNode || x is FactorVariableTreeNode)) { 250 stringVarNames.Add(((IVariableTreeNode)node).VariableName); 251 } 252 253 var orderedNames = stringVarNames.OrderBy(n => n, new NaturalStringComparer()).Select(n => "string " + VariableName2Identifier(n) + " /* " + n + " */"); 192 254 strBuilder.Append(string.Join(", ", orderedNames)); 255 256 if (stringVarNames.Any() && doubleVarNames.Any()) 257 strBuilder.AppendLine(","); 258 orderedNames = doubleVarNames.OrderBy(n => n, new NaturalStringComparer()).Select(n => "double " + VariableName2Identifier(n) + " /* " + n + " */"); 259 strBuilder.Append(string.Join(", ", orderedNames)); 260 193 261 194 262 strBuilder.AppendLine(") {"); … … 198 266 private void GenerateFooter(StringBuilder strBuilder) { 199 267 strBuilder.AppendLine(";"); 268 200 269 strBuilder.AppendLine("return result;"); 201 270 strBuilder.AppendLine("}"); … … 215 284 strBuilder.AppendLine("}"); 216 285 } 286 287 private void GenerateFactorSource(StringBuilder strBuilder) { 288 strBuilder.AppendLine("private static double EvaluateFactor(string factorValue, string[] factorValues, double[] constants) {"); 289 strBuilder.AppendLine(" for(int i=0;i<factorValues.Length;i++) " + 290 " if(factorValues[i] == factorValue) return constants[i];" + 291 " throw new ArgumentException();"); 292 strBuilder.AppendLine("}"); 293 } 294 295 private void GenerateBinaryFactorSource(StringBuilder strBuilder) { 296 strBuilder.AppendLine("private static double EvaluateBinaryFactor(string factorValue, string targetValue, double weight) {"); 297 strBuilder.AppendLine(" return factorValue == targetValue ? weight : 0.0;"); 298 strBuilder.AppendLine("}"); 299 } 300 217 301 } 218 302 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r12509 r16692 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. … … 51 51 while (dividend > 0) { 52 52 int modulo = (dividend - 1) % 26; 53 columnName = Convert.ToChar(65 + modulo).ToString() + columnName;53 columnName = System.Convert.ToChar(65 + modulo) + columnName; 54 54 dividend = (int)((dividend - modulo) / 26); 55 55 } … … 60 60 private readonly Dictionary<string, string> variableNameMapping = new Dictionary<string, string>(); 61 61 private int currentVariableIndex = 0; 62 private string GetColumnToVariableName(string var iabelName) {63 if (!variableNameMapping.ContainsKey(var iabelName)) {62 private string GetColumnToVariableName(string varName) { 63 if (!variableNameMapping.ContainsKey(varName)) { 64 64 currentVariableIndex++; 65 variableNameMapping.Add(var iabelName, GetExcelColumnName(currentVariableIndex));66 } 67 return string.Format("${0}1", variableNameMapping[var iabelName]);65 variableNameMapping.Add(varName, GetExcelColumnName(currentVariableIndex)); 66 } 67 return string.Format("${0}1", variableNameMapping[varName]); 68 68 } 69 69 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { … … 71 71 } 72 72 73 73 74 public string Format(ISymbolicExpressionTree symbolicExpressionTree, IDataset dataset) { 75 if (dataset != null) 76 return FormatWithMapping(symbolicExpressionTree, CalculateVariableMapping(symbolicExpressionTree, dataset)); 77 else return FormatWithMapping(symbolicExpressionTree, new Dictionary<string, string>()); 78 } 79 80 public string FormatWithMapping(ISymbolicExpressionTree symbolicExpressionTree, Dictionary<string, string> variableNameMapping) { 81 foreach (var kvp in variableNameMapping) this.variableNameMapping.Add(kvp.Key, kvp.Value); 74 82 var stringBuilder = new StringBuilder(); 75 if (dataset != null) CalculateVariableMapping(symbolicExpressionTree, dataset);76 83 77 84 stringBuilder.Append("="); 78 85 stringBuilder.Append(FormatRecursively(symbolicExpressionTree.Root)); 79 86 80 foreach (var variable in variableNameMapping) {87 foreach (var variable in this.variableNameMapping) { 81 88 stringBuilder.AppendLine(); 82 89 stringBuilder.Append(variable.Key + " = " + variable.Value); … … 85 92 } 86 93 87 private voidCalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {88 int columnIndex = 0;94 private Dictionary<string, string> CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) { 95 var mapping = new Dictionary<string, string>(); 89 96 int inputIndex = 0; 90 var usedVariables = tree.IterateNodesPrefix().OfType< VariableTreeNode>().Select(v => v.VariableName).Distinct();97 var usedVariables = tree.IterateNodesPrefix().OfType<IVariableTreeNode>().Select(v => v.VariableName).Distinct().ToArray(); 91 98 foreach (var variable in dataset.VariableNames) { 92 columnIndex++;93 99 if (!usedVariables.Contains(variable)) continue; 94 100 inputIndex++; 95 variableNameMapping[variable] = GetExcelColumnName(inputIndex); 96 } 101 mapping[variable] = GetExcelColumnName(inputIndex); 102 } 103 return mapping; 97 104 } 98 105 … … 190 197 stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture)); 191 198 stringBuilder.Append("*"); 192 stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName));// + LagToString(currentLag)); 199 stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName)); 200 } else if (symbol is BinaryFactorVariable) { 201 var binFactorNode = node as BinaryFactorVariableTreeNode; 202 stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, 0)", 203 GetColumnToVariableName(binFactorNode.VariableName), 204 binFactorNode.VariableValue, 205 binFactorNode.Weight.ToString(CultureInfo.InvariantCulture) 206 ); 207 } else if (symbol is FactorVariable) { 208 var factorNode = node as FactorVariableTreeNode; 209 var values = factorNode.Symbol.GetVariableValues(factorNode.VariableName).ToArray(); 210 var w = factorNode.Weights; 211 // create nested if 212 for (int i = 0; i < values.Length; i++) { 213 stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, ", 214 GetColumnToVariableName(factorNode.VariableName), 215 values[i], 216 w[i].ToString(CultureInfo.InvariantCulture)); 217 } 218 stringBuilder.Append("\"\""); // return empty string on unknown value 219 stringBuilder.Append(')', values.Length); // add closing parenthesis 193 220 } else if (symbol is Power) { 194 221 stringBuilder.Append("POWER("); … … 213 240 } else if (symbol is VariableCondition) { 214 241 VariableConditionTreeNode variableConditionTreeNode = node as VariableConditionTreeNode; 215 double threshold = variableConditionTreeNode.Threshold; 216 double slope = variableConditionTreeNode.Slope; 217 string p = "(1 / (1 + EXP(-" + slope.ToString(CultureInfo.InvariantCulture) + " * (" + GetColumnToVariableName(variableConditionTreeNode.VariableName) + "-" + threshold.ToString(CultureInfo.InvariantCulture) + "))))"; 218 stringBuilder.Append("(("); 219 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 220 stringBuilder.Append("*"); 221 stringBuilder.Append(p); 222 stringBuilder.Append(") + ("); 223 stringBuilder.Append(FormatRecursively(node.GetSubtree(1))); 224 stringBuilder.Append("*("); 225 stringBuilder.Append("1 - " + p + ")"); 226 stringBuilder.Append("))"); 242 if (!variableConditionTreeNode.Symbol.IgnoreSlope) { 243 double threshold = variableConditionTreeNode.Threshold; 244 double slope = variableConditionTreeNode.Slope; 245 string p = "(1 / (1 + EXP(-" + slope.ToString(CultureInfo.InvariantCulture) + " * (" + 246 GetColumnToVariableName(variableConditionTreeNode.VariableName) + "-" + 247 threshold.ToString(CultureInfo.InvariantCulture) + "))))"; 248 stringBuilder.Append("(("); 249 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 250 stringBuilder.Append("*"); 251 stringBuilder.Append(p); 252 stringBuilder.Append(") + ("); 253 stringBuilder.Append(FormatRecursively(node.GetSubtree(1))); 254 stringBuilder.Append("*("); 255 stringBuilder.Append("1 - " + p + ")"); 256 stringBuilder.Append("))"); 257 } else { 258 stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "(IF({0} <= {1}, {2}, {3}))", 259 GetColumnToVariableName(variableConditionTreeNode.VariableName), 260 variableConditionTreeNode.Threshold, 261 FormatRecursively(node.GetSubtree(0)), 262 FormatRecursively(node.GetSubtree(1)) 263 ); 264 } 227 265 } else if (symbol is Xor) { 228 266 stringBuilder.Append("IF("); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r12012 r16692 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) { -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r12012 r16692 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. … … 21 21 22 22 using System.Globalization; 23 using System.Linq; 23 24 using System.Text; 24 25 using HeuristicLab.Common; … … 77 78 stringBuilder.AppendLine(" y = (f0 + 2*f1 - 2*f3 - f4) / 8;"); 78 79 stringBuilder.AppendLine("end"); 80 81 var factorVariableNames = 82 symbolicExpressionTree.IterateNodesPostfix() 83 .OfType<FactorVariableTreeNode>() 84 .Select(n => n.VariableName) 85 .Distinct(); 86 87 foreach (var factorVarName in factorVariableNames) { 88 var factorSymb = symbolicExpressionTree.IterateNodesPostfix() 89 .OfType<FactorVariableTreeNode>() 90 .First(n => n.VariableName == factorVarName) 91 .Symbol; 92 stringBuilder.AppendFormat("function y = switch_{0}(val, v)", factorVarName).AppendLine(); 93 var values = factorSymb.GetVariableValues(factorVarName).ToArray(); 94 stringBuilder.AppendLine("switch val"); 95 for (int i = 0; i < values.Length; i++) { 96 stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " case \"{0}\" y = v({1})", values[i], i).AppendLine(); 97 } 98 stringBuilder.AppendLine("end"); 99 stringBuilder.AppendLine(); 100 } 101 79 102 return stringBuilder.ToString(); 80 103 } … … 296 319 stringBuilder.Append("*"); 297 320 stringBuilder.Append(variableTreeNode.VariableName + LagToString(currentLag)); 321 } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.FactorVariable) { 322 var factorNode = node as FactorVariableTreeNode; 323 var weights = string.Join(" ", factorNode.Weights.Select(w => w.ToString("G17", CultureInfo.InvariantCulture))); 324 stringBuilder.AppendFormat("switch_{0}(\"{1}\",[{2}])", 325 factorNode.VariableName, factorNode.VariableName, weights) 326 .AppendLine(); 327 } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.BinaryFactorVariable) { 328 var factorNode = node as BinaryFactorVariableTreeNode; 329 stringBuilder.AppendFormat(CultureInfo.InvariantCulture, 330 "((strcmp({0},\"{1}\")==1) * {2:G17})", factorNode.VariableName, factorNode.VariableValue, factorNode.Weight); 298 331 } else if (symbol is Power) { 299 332 stringBuilder.Append("("); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r12982 r16692 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. … … 110 110 } 111 111 } else { 112 if (node is VariableTreeNode) { 112 // terminals 113 if (node.Symbol is Variable) { 113 114 var varNode = node as VariableTreeNode; 114 115 strBuilder.AppendFormat("Times[{0}, {1}]", varNode.VariableName, varNode.Weight.ToString("G17", CultureInfo.InvariantCulture)); 115 } else if (node is ConstantTreeNode) {116 } else if (node.Symbol is Constant) { 116 117 var constNode = node as ConstantTreeNode; 117 118 strBuilder.Append(constNode.Value.ToString("G17", CultureInfo.InvariantCulture)); 119 } else if (node.Symbol is FactorVariable) { 120 var factorNode = node as FactorVariableTreeNode; 121 strBuilder.AppendFormat("Switch[{0},", factorNode.VariableName); 122 var varValues = factorNode.Symbol.GetVariableValues(factorNode.VariableName).ToArray(); 123 var weights = varValues.Select(factorNode.GetValue).ToArray(); 124 125 var weightStr = string.Join(", ", 126 varValues.Zip(weights, (s, d) => string.Format(CultureInfo.InvariantCulture, "\"{0}\", {1:G17}", s, d))); 127 strBuilder.Append(weightStr); 128 strBuilder.Append("]"); 129 } else if (node.Symbol is BinaryFactorVariable) { 130 var factorNode = node as BinaryFactorVariableTreeNode; 131 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "If[{0}==\"{1}\",{2:G17},0.0]", 132 factorNode.VariableName, factorNode.VariableValue, factorNode.Weight); 118 133 } else { 119 134 throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " is not supported."); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r12012 r16692 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. … … 115 115 stringBuilder.Append("]"); 116 116 } else if (symbol is LaggedVariable) { 117 stringBuilder.Append("lagged variable not implemented");117 stringBuilder.Append("lagged variables are not supported"); 118 118 } else if (symbol is LessThan) { 119 119 stringBuilder.Append("("); … … 165 165 stringBuilder.Append("*"); 166 166 stringBuilder.Append(variableTreeNode.VariableName); 167 } else if (symbol is BinaryFactorVariable || symbol is FactorVariable) { 168 stringBuilder.Append("factor variables are not supported"); 167 169 } else { 168 170 stringBuilder.Append("(");
Note: See TracChangeset
for help on using the changeset viewer.