Changeset 14259
- Timestamp:
- 08/17/16 16:20:08 (8 years ago)
- Location:
- branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r14185 r14259 28 28 using HeuristicLab.Core; 29 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HeuristicLab.Optimization.Operators; 30 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 32 … … 71 72 } 72 73 74 73 75 public string Format(ISymbolicExpressionTree symbolicExpressionTree, IDataset dataset) { 74 76 var stringBuilder = new StringBuilder(); … … 88 90 int columnIndex = 0; 89 91 int inputIndex = 0; 90 var usedVariables = tree.IterateNodesPrefix().OfType< VariableTreeNode>().Select(v => v.VariableName).Distinct();92 var usedVariables = tree.IterateNodesPrefix().OfType<IVariableTreeNode>().Select(v => v.VariableName).Distinct(); 91 93 foreach (var variable in dataset.VariableNames) { 92 94 columnIndex++; … … 190 192 stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture)); 191 193 stringBuilder.Append("*"); 192 stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName));// + LagToString(currentLag)); 194 stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName)); // + LagToString(currentLag)); 195 } else if (symbol is BinaryFactorVariable) { 196 var binFactorNode = node as BinaryFactorVariableTreeNode; 197 stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, 0)", 198 GetColumnToVariableName(binFactorNode.VariableName), 199 binFactorNode.VariableValue, 200 binFactorNode.Weight.ToString(CultureInfo.InvariantCulture) 201 ); 202 } else if (symbol is FactorVariable) { 203 var factorNode = node as FactorVariableTreeNode; 204 var values = factorNode.Symbol.GetVariableValues(factorNode.VariableName).ToArray(); 205 var w = factorNode.Weights; 206 // create nested if 207 for (int i = 0; i < values.Length; i++) { 208 stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, ", 209 GetColumnToVariableName(factorNode.VariableName), 210 values[i], 211 w[i].ToString(CultureInfo.InvariantCulture)); 212 } 213 stringBuilder.Append("\"\""); // return empty string on unknown value 214 stringBuilder.Append(')', values.Length); // add closing parenthesis 193 215 } else if (symbol is Power) { 194 216 stringBuilder.Append("POWER("); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r14185 r14259 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; … … 41 42 private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner) 42 43 : base(original, cloner) { 43 constants = new List<double>(original.constants); 44 constants = new List<KeyValuePair<string, double>>(original.constants); 45 constIndex = original.constIndex; 46 currentLag = original.currentLag; 47 targetCount = original.targetCount; 44 48 } 45 49 public SymbolicDataAnalysisExpressionLatexFormatter() … … 47 51 Name = ItemName; 48 52 Description = ItemDescription; 49 constants = new List< double>();53 constants = new List<KeyValuePair<string, double>>(); 50 54 } 51 55 … … 58 62 StringBuilder strBuilder = new StringBuilder(); 59 63 constants.Clear(); 64 constIndex = 0; 60 65 strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root)); 61 66 return strBuilder.ToString(); 62 } 63 catch (NotImplementedException ex) { 67 } catch (NotImplementedException ex) { 64 68 return ex.Message + Environment.NewLine + ex.StackTrace; 65 69 } … … 75 79 } 76 80 int i = 1; 77 foreach ( SymbolicExpressionTreeNodesubTree in node.Subtrees.Skip(1)) {81 foreach (var subTree in node.Subtrees.Skip(1)) { 78 82 FormatSep(node, strBuilder, i); 79 83 // format the whole subtree … … 166 170 strBuilder.Append(@" \operatorname{if} \left( "); 167 171 } else if (node.Symbol is Constant) { 168 strBuilder.Append("c_{" + constants.Count + "} "); 172 var constName = "c_{" + constIndex + "}"; 173 constIndex++; 174 strBuilder.Append(constName + " "); 169 175 var constNode = node as ConstantTreeNode; 170 constants.Add(constNode.Value); 176 constants.Add(new KeyValuePair<string, double>(constName, constNode.Value)); 177 } else if (node.Symbol is FactorVariable) { 178 var factorNode = node as FactorVariableTreeNode; 179 var constName = "c_{" + constIndex + "}"; 180 strBuilder.Append(constName + " "); 181 foreach (var e in factorNode.Symbol.GetVariableValues(factorNode.VariableName) 182 .Zip(factorNode.Weights, Tuple.Create)) { 183 constants.Add(new KeyValuePair<string, double>("c_{" + constIndex + ", " + EscapeLatexString(factorNode.VariableName) + "=" + EscapeLatexString(e.Item1) + "}", e.Item2)); 184 } 185 constIndex++; 186 } else if (node.Symbol is BinaryFactorVariable) { 187 var binFactorNode = node as BinaryFactorVariableTreeNode; 188 if (!binFactorNode.Weight.IsAlmost((1.0))) { 189 var constName = "c_{" + constIndex + "}"; 190 strBuilder.Append(constName + " \\cdot"); 191 constants.Add(new KeyValuePair<string, double>(constName, binFactorNode.Weight)); 192 constIndex++; 193 } 194 strBuilder.Append(EscapeLatexString(binFactorNode.VariableName)); 195 strBuilder.Append(LagToString(currentLag)); 196 strBuilder.Append(" = " + EscapeLatexString(binFactorNode.VariableValue)); 171 197 } else if (node.Symbol is LaggedVariable) { 172 198 var laggedVarNode = node as LaggedVariableTreeNode; 173 199 if (!laggedVarNode.Weight.IsAlmost(1.0)) { 174 strBuilder.Append("c_{" + constants.Count + "} \\cdot "); 175 constants.Add(laggedVarNode.Weight); 200 var constName = "c_{" + constIndex + "}"; 201 strBuilder.Append(constName + " \\cdot"); 202 constants.Add(new KeyValuePair<string, double>(constName, laggedVarNode.Weight)); 203 constIndex++; 176 204 } 177 205 strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName)); … … 181 209 var varNode = node as VariableTreeNode; 182 210 if (!varNode.Weight.IsAlmost((1.0))) { 183 strBuilder.Append("c_{" + constants.Count + "} \\cdot "); 184 constants.Add(varNode.Weight); 211 var constName = "c_{" + constIndex + "}"; 212 strBuilder.Append(constName + " \\cdot"); 213 constants.Add(new KeyValuePair<string, double>(constName, varNode.Weight)); 214 constIndex++; 185 215 } 186 216 strBuilder.Append(EscapeLatexString(varNode.VariableName)); … … 216 246 } else if (node.Symbol is VariableCondition) { 217 247 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); 248 var constName = "c_{" + constants.Count + "}"; 249 string p = @"1 / 1 + \exp - " + constName + " "; 250 constants.Add(new KeyValuePair<string, double>(constName, conditionTreeNode.Slope)); 251 constIndex++; 252 var const2Name = "c_{" + constants.Count + @"}"; 253 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + " "; 254 constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold)); 255 constIndex++; 222 256 strBuilder.Append(@" \left( " + p + @"\cdot "); 223 257 } else { … … 310 344 } else if (node.Symbol is VariableCondition) { 311 345 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); 346 var const1Name = "c_{" + constants.Count + "}"; 347 string p = @"1 / \left( 1 + \exp \left( - " + const1Name + " "; 348 constants.Add(new KeyValuePair<string, double>(const1Name, conditionTreeNode.Slope)); 349 constIndex++; 350 var const2Name = "c_{" + constants.Count + "}"; 351 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + " \right) \right) \right) "; 352 constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold)); 353 constIndex++; 316 354 strBuilder.Append(@" + \left( 1 - " + p + @" \right) \cdot "); 317 355 } else { … … 391 429 } else if (node.Symbol is LaggedVariable) { 392 430 } else if (node.Symbol is Variable) { 431 } else if (node.Symbol is FactorVariable) { 432 } else if (node.Symbol is BinaryFactorVariable) { 393 433 } else if (node.Symbol is ProgramRootSymbol) { 394 434 strBuilder … … 398 438 // output all constant values 399 439 if (constants.Count > 0) { 400 int i = 0;401 440 foreach (var constant in constants) { 402 441 // replace "." with ".&" to align decimal points 403 var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant );442 var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value); 404 443 if (!constStr.Contains(".")) constStr = constStr + ".0"; 405 444 constStr = constStr.Replace(".", "&."); // fix problem in rendering of aligned expressions 406 strBuilder.Append( "c_{" + i + "}& = & " + constStr);445 strBuilder.Append(constant.Key + "& = & " + constStr); 407 446 strBuilder.Append(@"\\"); 408 i++;409 447 } 410 448 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
r14251 r14259 35 35 public class SymbolicDataAnalysisExpressionTreeSimplifier { 36 36 private Addition addSymbol = new Addition(); 37 private Subtraction subSymbol = new Subtraction();38 37 private Multiplication mulSymbol = new Multiplication(); 39 38 private Division divSymbol = new Division(); … … 1034 1033 return a; 1035 1034 } else if (IsConstant(b) && IsAddition(a) || 1036 IsFactor(a) && IsAddition(b) || 1037 IsAddition(b) && IsFactor(a)) { 1035 IsFactor(b) && IsAddition(a)) { 1038 1036 // multiply constants into additions 1039 1037 return a.Subtrees.Select(x => MakeProduct(x, b)).Aggregate((c, d) => MakeSum(c, d)); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariableTreeNode.cs
r14251 r14259 114 114 115 115 public override string ToString() { 116 return VariableName + " (factor)"; 116 var weightStr = string.Join("; ", 117 Symbol.GetVariableValues(VariableName).Select(value => value + ": " + GetValue(value).ToString("E4"))); 118 return VariableName + " (factor) " 119 + "[" + weightStr + "]"; 117 120 } 118 121 }
Note: See TracChangeset
for help on using the changeset viewer.