Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/06/17 10:19:37 (7 years ago)
Author:
gkronber
Message:

#2650: merged r14826 from trunk to stable. The only remaining conflict is DataTableControl and ScatterPlotControl which have been renamed within r14982 (-> tree conflict).

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Algorithms.DataAnalysis

  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs

    r15061 r15131  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using System.Threading;
     
    208209      var parser = new InfixExpressionParser();
    209210      var tree = parser.Parse(modelStructure);
     211      // parser handles double and string variables equally by creating a VariableTreeNode
     212      // post-process to replace VariableTreeNodes by FactorVariableTreeNodes for all string variables
     213      var factorSymbol = new FactorVariable();
     214      factorSymbol.VariableNames =
     215        problemData.AllowedInputVariables.Where(name => problemData.Dataset.VariableHasType<string>(name));
     216      factorSymbol.AllVariableNames = factorSymbol.VariableNames;
     217      factorSymbol.VariableValues =
     218        factorSymbol.VariableNames.Select(name =>
     219        new KeyValuePair<string, Dictionary<string, int>>(name,
     220        problemData.Dataset.GetReadOnlyStringValues(name).Distinct()
     221        .Select((n, i) => Tuple.Create(n, i))
     222        .ToDictionary(tup => tup.Item1, tup => tup.Item2)));
     223
     224      foreach (var parent in tree.IterateNodesPrefix().ToArray()) {
     225        for (int i = 0; i < parent.SubtreeCount; i++) {
     226          var varChild = parent.GetSubtree(i) as VariableTreeNode;
     227          var factorVarChild = parent.GetSubtree(i) as FactorVariableTreeNode;
     228          if (varChild != null && factorSymbol.VariableNames.Contains(varChild.VariableName)) {
     229            parent.RemoveSubtree(i);
     230            var factorTreeNode = (FactorVariableTreeNode)factorSymbol.CreateTreeNode();
     231            factorTreeNode.VariableName = varChild.VariableName;
     232            factorTreeNode.Weights =
     233              factorTreeNode.Symbol.GetVariableValues(factorTreeNode.VariableName).Select(_ => 1.0).ToArray();
     234            // weight = 1.0 for each value
     235            parent.InsertSubtree(i, factorTreeNode);
     236          } else if (factorVarChild != null && factorSymbol.VariableNames.Contains(factorVarChild.VariableName)) {
     237            if (factorSymbol.GetVariableValues(factorVarChild.VariableName).Count() != factorVarChild.Weights.Length)
     238              throw new ArgumentException(
     239                string.Format("Factor variable {0} needs exactly {1} weights",
     240                factorVarChild.VariableName,
     241                factorSymbol.GetVariableValues(factorVarChild.VariableName).Count()));
     242            parent.RemoveSubtree(i);
     243            var factorTreeNode = (FactorVariableTreeNode)factorSymbol.CreateTreeNode();
     244            factorTreeNode.VariableName = factorVarChild.VariableName;
     245            factorTreeNode.Weights = factorVarChild.Weights;
     246            parent.InsertSubtree(i, factorTreeNode);
     247          }
     248        }
     249      }
    210250
    211251      if (!SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree)) throw new ArgumentException("The optimizer does not support the specified model structure.");
Note: See TracChangeset for help on using the changeset viewer.