- Timestamp:
- 04/04/17 17:52:44 (8 years ago)
- Location:
- trunk/sources
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r14350 r14826 141 141 strBuilder.Append(")"); 142 142 } 143 } else if (node.Symbol is FactorVariable) { 144 var factorNode = node as FactorVariableTreeNode; 145 if (factorNode.VariableName.Contains("'")) { 146 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName); 147 } else { 148 strBuilder.AppendFormat("'{0}'", factorNode.VariableName); 149 } 150 strBuilder.AppendFormat("[{0}]", 151 string.Join(", ", factorNode.Weights.Select(w => w.ToString(CultureInfo.InvariantCulture)))); 152 } else if (node.Symbol is BinaryFactorVariable) { 153 var factorNode = node as BinaryFactorVariableTreeNode; 154 if (!factorNode.Weight.IsAlmost(1.0)) { 155 strBuilder.Append("("); 156 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", factorNode.Weight); 157 strBuilder.Append("*"); 158 } 159 if (factorNode.VariableName.Contains("'")) { 160 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName); 161 } else { 162 strBuilder.AppendFormat("'{0}'", factorNode.VariableName); 163 } 164 strBuilder.Append(" = "); 165 if (factorNode.VariableValue.Contains("'")) { 166 strBuilder.AppendFormat("\"{0}\"", factorNode.VariableValue); 167 } else { 168 strBuilder.AppendFormat("'{0}'", factorNode.VariableValue); 169 } 170 171 if (!factorNode.Weight.IsAlmost(1.0)) { 172 strBuilder.Append(")"); 173 } 174 143 175 } else if (node.Symbol is Constant) { 144 176 var constNode = node as ConstantTreeNode; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r14185 r14826 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 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r14400 r14826 51 51 while (dividend > 0) { 52 52 int modulo = (dividend - 1) % 26; 53 columnName = Convert.ToChar(65 + modulo) .ToString()+ columnName;53 columnName = 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 { 82 foreach(var kvp in variableNameMapping) this.variableNameMapping.Add(kvp.Key,kvp.Value); 74 83 var stringBuilder = new StringBuilder(); 75 if (dataset != null) CalculateVariableMapping(symbolicExpressionTree, dataset); 76 84 77 85 stringBuilder.Append("="); 78 86 stringBuilder.Append(FormatRecursively(symbolicExpressionTree.Root)); 79 87 80 foreach (var variable in variableNameMapping) {88 foreach (var variable in this.variableNameMapping) { 81 89 stringBuilder.AppendLine(); 82 90 stringBuilder.Append(variable.Key + " = " + variable.Value); … … 85 93 } 86 94 87 private voidCalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {88 int columnIndex = 0;95 private Dictionary<string,string> CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) { 96 var mapping = new Dictionary<string,string>(); 89 97 int inputIndex = 0; 90 var usedVariables = tree.IterateNodesPrefix().OfType< VariableTreeNode>().Select(v => v.VariableName).Distinct();98 var usedVariables = tree.IterateNodesPrefix().OfType<IVariableTreeNode>().Select(v => v.VariableName).Distinct().ToArray(); 91 99 foreach (var variable in dataset.VariableNames) { 92 columnIndex++;93 100 if (!usedVariables.Contains(variable)) continue; 94 101 inputIndex++; 95 variableNameMapping[variable] = GetExcelColumnName(inputIndex); 96 } 102 mapping[variable] = GetExcelColumnName(inputIndex); 103 } 104 return mapping; 97 105 } 98 106 … … 190 198 stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture)); 191 199 stringBuilder.Append("*"); 192 stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName));// + LagToString(currentLag)); 200 stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName)); 201 } else if (symbol is BinaryFactorVariable) { 202 var binFactorNode = node as BinaryFactorVariableTreeNode; 203 stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, 0)", 204 GetColumnToVariableName(binFactorNode.VariableName), 205 binFactorNode.VariableValue, 206 binFactorNode.Weight.ToString(CultureInfo.InvariantCulture) 207 ); 208 } else if (symbol is FactorVariable) { 209 var factorNode = node as FactorVariableTreeNode; 210 var values = factorNode.Symbol.GetVariableValues(factorNode.VariableName).ToArray(); 211 var w = factorNode.Weights; 212 // create nested if 213 for (int i = 0; i < values.Length; i++) { 214 stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, ", 215 GetColumnToVariableName(factorNode.VariableName), 216 values[i], 217 w[i].ToString(CultureInfo.InvariantCulture)); 218 } 219 stringBuilder.Append("\"\""); // return empty string on unknown value 220 stringBuilder.Append(')', values.Length); // add closing parenthesis 193 221 } else if (symbol is Power) { 194 222 stringBuilder.Append("POWER("); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r14367 r14826 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; … … 43 44 private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner) 44 45 : base(original, cloner) { 45 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; 46 50 } 47 51 public SymbolicDataAnalysisExpressionLatexFormatter() … … 49 53 Name = ItemName; 50 54 Description = ItemDescription; 51 constants = new List< double>();55 constants = new List<KeyValuePair<string, double>>(); 52 56 } 53 57 … … 63 67 StringBuilder strBuilder = new StringBuilder(); 64 68 constants.Clear(); 69 constIndex = 0; 65 70 this.targetVariable = targetVariable; 66 71 containsTimeSeriesSymbol = symbolicExpressionTree.IterateNodesBreadth().Any(n => IsTimeSeriesSymbol(n.Symbol)); … … 84 89 } 85 90 int i = 1; 86 foreach ( SymbolicExpressionTreeNodesubTree in node.Subtrees.Skip(1)) {91 foreach (var subTree in node.Subtrees.Skip(1)) { 87 92 FormatSep(node, strBuilder, i); 88 93 // format the whole subtree … … 175 180 strBuilder.Append(@" \operatorname{if} \left( "); 176 181 } else if (node.Symbol is Constant) { 182 var constName = "c_{" + constIndex + "}"; 183 constIndex++; 177 184 var constNode = node as ConstantTreeNode; 178 185 if (constNode.Value.IsAlmost(1.0)) { 179 186 strBuilder.Append("1 "); 180 187 } else { 181 strBuilder.Append("c_{" + constants.Count + "} "); 182 constants.Add(constNode.Value); 183 } 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) + " )"); 184 212 } else if (node.Symbol is LaggedVariable) { 185 213 var laggedVarNode = node as LaggedVariableTreeNode; 186 214 if (!laggedVarNode.Weight.IsAlmost(1.0)) { 187 strBuilder.Append("c_{" + constants.Count + "} \\cdot "); 188 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++; 189 219 } 190 220 strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName)); … … 194 224 var varNode = node as VariableTreeNode; 195 225 if (!varNode.Weight.IsAlmost((1.0))) { 196 strBuilder.Append("c_{" + constants.Count + "} \\cdot "); 197 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++; 198 230 } 199 231 strBuilder.Append(EscapeLatexString(varNode.VariableName)); … … 229 261 } else if (node.Symbol is VariableCondition) { 230 262 var conditionTreeNode = node as VariableConditionTreeNode; 231 string p = @"1 / 1 + \exp - c_{" + constants.Count + "} "; 232 constants.Add(conditionTreeNode.Slope); 233 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} "; 234 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++; 235 271 strBuilder.Append(@" \left( " + p + @"\cdot "); 236 272 } else { … … 323 359 } else if (node.Symbol is VariableCondition) { 324 360 var conditionTreeNode = node as VariableConditionTreeNode; 325 string p = @"1 / \left( 1 + \exp \left( - c_{" + constants.Count + "} "; 326 constants.Add(conditionTreeNode.Slope); 327 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} \right) \right) \right) "; 328 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++; 329 369 strBuilder.Append(@" + \left( 1 - " + p + @" \right) \cdot "); 330 370 } else { … … 404 444 } else if (node.Symbol is LaggedVariable) { 405 445 } else if (node.Symbol is Variable) { 446 } else if (node.Symbol is FactorVariable) { 447 } else if (node.Symbol is BinaryFactorVariable) { 406 448 } else if (node.Symbol is ProgramRootSymbol) { 407 449 strBuilder … … 411 453 // output all constant values 412 454 if (constants.Count > 0) { 413 int i = 0;414 455 foreach (var constant in constants) { 415 456 // replace "." with ".&" to align decimal points 416 var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant );457 var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value); 417 458 if (!constStr.Contains(".")) constStr = constStr + ".0"; 418 459 constStr = constStr.Replace(".", "&."); // fix problem in rendering of aligned expressions 419 strBuilder.Append( "c_{" + i + "}& = & " + constStr);460 strBuilder.Append(constant.Key + "& = & " + constStr); 420 461 strBuilder.Append(@"\\"); 421 i++;422 462 } 423 463 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r14185 r14826 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("("); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r14185 r14826 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."); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r14185 r14826 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.