Changeset 14259


Ignore:
Timestamp:
08/17/16 16:20:08 (5 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.

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  
    2828using HeuristicLab.Core;
    2929using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     30using HeuristicLab.Optimization.Operators;
    3031using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3132
     
    7172    }
    7273
     74
    7375    public string Format(ISymbolicExpressionTree symbolicExpressionTree, IDataset dataset) {
    7476      var stringBuilder = new StringBuilder();
     
    8890      int columnIndex = 0;
    8991      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();
    9193      foreach (var variable in dataset.VariableNames) {
    9294        columnIndex++;
     
    190192        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
    191193        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
    193215      } else if (symbol is Power) {
    194216        stringBuilder.Append("POWER(");
  • 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        }
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs

    r14251 r14259  
    3535  public class SymbolicDataAnalysisExpressionTreeSimplifier {
    3636    private Addition addSymbol = new Addition();
    37     private Subtraction subSymbol = new Subtraction();
    3837    private Multiplication mulSymbol = new Multiplication();
    3938    private Division divSymbol = new Division();
     
    10341033        return a;
    10351034      } else if (IsConstant(b) && IsAddition(a) ||
    1036           IsFactor(a) && IsAddition(b) ||
    1037           IsAddition(b) && IsFactor(a)) {
     1035          IsFactor(b) && IsAddition(a)) {
    10381036        // multiply constants into additions
    10391037        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  
    114114
    115115    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 + "]";
    117120    }
    118121  }
Note: See TracChangeset for help on using the changeset viewer.