Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/17/16 16:20:08 (8 years ago)
Author:
gkronber
Message:

#2650: added support for factor variables to Excel formatter and Excel exporter as well as to the Latex formatter and consequently the mathematical representation view.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

    r14185 r14259  
    3333  [StorableClass]
    3434  public sealed class SymbolicDataAnalysisExpressionLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
    35     private readonly List<double> constants;
     35    private readonly List<KeyValuePair<string, double>> constants;
     36    private int constIndex;
    3637    private int targetCount;
    3738    private int currentLag;
     
    4142    private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner)
    4243      : 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;
    4448    }
    4549    public SymbolicDataAnalysisExpressionLatexFormatter()
     
    4751      Name = ItemName;
    4852      Description = ItemDescription;
    49       constants = new List<double>();
     53      constants = new List<KeyValuePair<string, double>>();
    5054    }
    5155
     
    5862        StringBuilder strBuilder = new StringBuilder();
    5963        constants.Clear();
     64        constIndex = 0;
    6065        strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
    6166        return strBuilder.ToString();
    62       }
    63       catch (NotImplementedException ex) {
     67      } catch (NotImplementedException ex) {
    6468        return ex.Message + Environment.NewLine + ex.StackTrace;
    6569      }
     
    7579      }
    7680      int i = 1;
    77       foreach (SymbolicExpressionTreeNode subTree in node.Subtrees.Skip(1)) {
     81      foreach (var subTree in node.Subtrees.Skip(1)) {
    7882        FormatSep(node, strBuilder, i);
    7983        // format the whole subtree
     
    166170        strBuilder.Append(@" \operatorname{if}  \left( ");
    167171      } else if (node.Symbol is Constant) {
    168         strBuilder.Append("c_{" + constants.Count + "} ");
     172        var constName = "c_{" + constIndex + "}";
     173        constIndex++;
     174        strBuilder.Append(constName + " ");
    169175        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));
    171197      } else if (node.Symbol is LaggedVariable) {
    172198        var laggedVarNode = node as LaggedVariableTreeNode;
    173199        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++;
    176204        }
    177205        strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName));
     
    181209        var varNode = node as VariableTreeNode;
    182210        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++;
    185215        }
    186216        strBuilder.Append(EscapeLatexString(varNode.VariableName));
     
    216246      } else if (node.Symbol is VariableCondition) {
    217247        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++;
    222256        strBuilder.Append(@" \left( " + p + @"\cdot ");
    223257      } else {
     
    310344      } else if (node.Symbol is VariableCondition) {
    311345        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++;
    316354        strBuilder.Append(@" +  \left( 1 - " + p + @" \right) \cdot ");
    317355      } else {
     
    391429      } else if (node.Symbol is LaggedVariable) {
    392430      } else if (node.Symbol is Variable) {
     431      } else if (node.Symbol is FactorVariable) {
     432      } else if (node.Symbol is BinaryFactorVariable) {
    393433      } else if (node.Symbol is ProgramRootSymbol) {
    394434        strBuilder
     
    398438        // output all constant values
    399439        if (constants.Count > 0) {
    400           int i = 0;
    401440          foreach (var constant in constants) {
    402441            // 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);
    404443            if (!constStr.Contains(".")) constStr = constStr + ".0";
    405444            constStr = constStr.Replace(".", "&.");  // fix problem in rendering of aligned expressions
    406             strBuilder.Append("c_{" + i + "}& = & " + constStr);
     445            strBuilder.Append(constant.Key + "& = & " + constStr);
    407446            strBuilder.Append(@"\\");
    408             i++;
    409447          }
    410448        }
Note: See TracChangeset for help on using the changeset viewer.