Changeset 14349


Ignore:
Timestamp:
10/23/16 08:39:17 (3 years ago)
Author:
gkronber
Message:

#2686: added support for lagged variables to constants optimizer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs

    r14185 r14349  
    181181      List<AutoDiff.Variable> parameters = new List<AutoDiff.Variable>();
    182182      List<string> variableNames = new List<string>();
     183      List<int> lags = new List<int>();
    183184
    184185      AutoDiff.Term func;
    185       if (!TryTransformToAutoDiff(tree.Root.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out func))
     186      if (!TryTransformToAutoDiff(tree.Root.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out func))
    186187        throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree.");
    187188      if (variableNames.Count == 0) return 0.0;
     
    222223      foreach (var r in rows) {
    223224        for (int col = 0; col < variableNames.Count; col++) {
    224           x[row, col] = ds.GetDoubleValue(variableNames[col], r);
     225          int lag = lags[col];
     226          x[row, col] = ds.GetDoubleValue(variableNames[col], r + lag);
    225227        }
    226228        row++;
     
    240242        alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, null, null);
    241243        alglib.lsfitresults(state, out info, out c, out rep);
    242       }
    243       catch (ArithmeticException) {
     244      } catch (ArithmeticException) {
    244245        return originalQuality;
    245       }
    246       catch (alglib.alglibexception) {
     246      } catch (alglib.alglibexception) {
    247247        return originalQuality;
    248248      }
     
    286286    }
    287287
    288     private static bool TryTransformToAutoDiff(ISymbolicExpressionTreeNode node, List<AutoDiff.Variable> variables, List<AutoDiff.Variable> parameters, List<string> variableNames, bool updateVariableWeights, out AutoDiff.Term term) {
     288    private static bool TryTransformToAutoDiff(ISymbolicExpressionTreeNode node, List<AutoDiff.Variable> variables, List<AutoDiff.Variable> parameters, List<string> variableNames, List<int> lags, bool updateVariableWeights, out AutoDiff.Term term) {
    289289      if (node.Symbol is Constant) {
    290290        var var = new AutoDiff.Variable();
     
    298298        parameters.Add(par);
    299299        variableNames.Add(varNode.VariableName);
     300        lags.Add(0);
    300301
    301302        if (updateVariableWeights) {
     
    308309        return true;
    309310      }
     311      if (node.Symbol is LaggedVariable) {
     312        var varNode = node as LaggedVariableTreeNode;
     313        var par = new AutoDiff.Variable();
     314        parameters.Add(par);
     315        variableNames.Add(varNode.VariableName);
     316        lags.Add(varNode.Lag);
     317
     318        if (updateVariableWeights) {
     319          var w = new AutoDiff.Variable();
     320          variables.Add(w);
     321          term = AutoDiff.TermBuilder.Product(w, par);
     322        } else {
     323          term = par;
     324        }
     325        return true;
     326      }
    310327      if (node.Symbol is Addition) {
    311328        List<AutoDiff.Term> terms = new List<Term>();
    312329        foreach (var subTree in node.Subtrees) {
    313330          AutoDiff.Term t;
    314           if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, updateVariableWeights, out t)) {
     331          if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    315332            term = null;
    316333            return false;
     
    325342        for (int i = 0; i < node.SubtreeCount; i++) {
    326343          AutoDiff.Term t;
    327           if (!TryTransformToAutoDiff(node.GetSubtree(i), variables, parameters, variableNames, updateVariableWeights, out t)) {
     344          if (!TryTransformToAutoDiff(node.GetSubtree(i), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    328345            term = null;
    329346            return false;
     
    340357        foreach (var subTree in node.Subtrees) {
    341358          AutoDiff.Term t;
    342           if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, updateVariableWeights, out t)) {
     359          if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    343360            term = null;
    344361            return false;
     
    355372        foreach (var subTree in node.Subtrees) {
    356373          AutoDiff.Term t;
    357           if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, updateVariableWeights, out t)) {
     374          if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    358375            term = null;
    359376            return false;
     
    367384      if (node.Symbol is Logarithm) {
    368385        AutoDiff.Term t;
    369         if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out t)) {
     386        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    370387          term = null;
    371388          return false;
     
    377394      if (node.Symbol is Exponential) {
    378395        AutoDiff.Term t;
    379         if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out t)) {
     396        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    380397          term = null;
    381398          return false;
     
    387404      if (node.Symbol is Square) {
    388405        AutoDiff.Term t;
    389         if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out t)) {
     406        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    390407          term = null;
    391408          return false;
     
    397414      if (node.Symbol is SquareRoot) {
    398415        AutoDiff.Term t;
    399         if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out t)) {
     416        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    400417          term = null;
    401418          return false;
     
    407424      if (node.Symbol is Sine) {
    408425        AutoDiff.Term t;
    409         if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out t)) {
     426        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    410427          term = null;
    411428          return false;
     
    417434      if (node.Symbol is Cosine) {
    418435        AutoDiff.Term t;
    419         if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out t)) {
     436        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    420437          term = null;
    421438          return false;
     
    427444      if (node.Symbol is Tangent) {
    428445        AutoDiff.Term t;
    429         if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out t)) {
     446        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    430447          term = null;
    431448          return false;
     
    437454      if (node.Symbol is Erf) {
    438455        AutoDiff.Term t;
    439         if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out t)) {
     456        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    440457          term = null;
    441458          return false;
     
    447464      if (node.Symbol is Norm) {
    448465        AutoDiff.Term t;
    449         if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out t)) {
     466        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out t)) {
    450467          term = null;
    451468          return false;
     
    461478        variables.Add(alpha);
    462479        AutoDiff.Term branchTerm;
    463         if (TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out branchTerm)) {
     480        if (TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, updateVariableWeights, out branchTerm)) {
    464481          term = branchTerm * alpha + beta;
    465482          return true;
     
    478495        where
    479496         !(n.Symbol is Variable) &&
     497         !(n.Symbol is LaggedVariable) &&
    480498         !(n.Symbol is Constant) &&
    481499         !(n.Symbol is Addition) &&
Note: See TracChangeset for help on using the changeset viewer.