Changeset 16653


Ignore:
Timestamp:
03/06/19 16:20:36 (7 weeks ago)
Author:
gkronber
Message:

#2925: added support for fixed numeric parameters as "constant variables"

Location:
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs

    r16652 r16653  
    2323using System.Collections.Generic;
    2424using System.Diagnostics;
     25using System.Globalization;
    2526using System.Linq;
    2627using HeuristicLab.Analysis;
     
    286287        }
    287288        var adjustedEpisodes = episodes.Select(ep => new IntRange(ep.Start, ep.End - 1)); // because we lose the last row in the differencing step
     289
     290        // data for input variables is assumed to be known
     291        // input variables in pretuning are all target variables and all variable names that occur in the tree
     292        var inputVariables = targetVars.Concat(t.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName));
     293
    288294        var myState = new OptimizationData(new[] { t },
    289295          targetVars,
    290           problemData.AllowedInputVariables.Concat(targetVars).ToArray(),
     296          inputVariables.ToArray(),
    291297          problemData, new[] { targetValuesDiff.ToArray() }, adjustedEpisodes.ToArray(), -99, latentVariables, string.Empty); // TODO
    292298        var paramCount = myState.nodeValueLookup.ParameterCount;
     
    346352      }
    347353
    348       var myState = new OptimizationData(trees, targetVars, problemData.AllowedInputVariables.ToArray(), problemData, targetValues, episodes.ToArray(), numericIntegrationSteps, latentVariables, odeSolver);
     354      // data for input variables is assumed to be known
     355      // input variables are all variable names that occur in the trees except for target variables (we assume that trees have been generated correctly)
     356      var inputVariables = trees.SelectMany(t => t.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName)).Except(targetVars);
     357
     358      var myState = new OptimizationData(trees, targetVars, inputVariables.ToArray(), problemData, targetValues, episodes.ToArray(), numericIntegrationSteps, latentVariables, odeSolver);
    349359      optTheta = initialTheta;
    350360
     
    402412        // update variable values
    403413        foreach (var variable in variables) {
    404           nodeValueLookup.SetVariableValue(variable, ds.GetDoubleValue(variable, rows[trainIdx])); // TODO: perf
     414          // in this problem we also allow fixed numeric parameters (represented as variables with the value as name)
     415          if (double.TryParse(variable, NumberStyles.Float, CultureInfo.InvariantCulture, out double value)) {
     416            nodeValueLookup.SetVariableValue(variable, value); // TODO: Perf we don't need to set this for each index
     417          } else {
     418            nodeValueLookup.SetVariableValue(variable, ds.GetDoubleValue(variable, rows[trainIdx])); // TODO: perf
     419          }
    405420        }
    406421        // interpret all trees
     
    431446        // update variable values
    432447        foreach (var variable in variables) {
    433           nodeValueLookup.SetVariableValue(variable, ds.GetDoubleValue(variable, rows[trainIdx])); // TODO: perf
     448          // in this problem we also allow fixed numeric parameters (represented as variables with the value as name)
     449          if (double.TryParse(variable, NumberStyles.Float, CultureInfo.InvariantCulture, out double value)) {
     450            nodeValueLookup.SetVariableValue(variable, value); // TODO: Perf we don't need to set this for each index
     451          } else {
     452            nodeValueLookup.SetVariableValue(variable, ds.GetDoubleValue(variable, rows[trainIdx])); // TODO: perf
     453          }
    434454        }
    435455
     
    561581        results["Models"].Value = models;
    562582      } else {
    563         var optimizationData = new OptimizationData(trees, targetVars, problemData.AllowedInputVariables.ToArray(), problemData, null, TrainingEpisodes.ToArray(), NumericIntegrationSteps, latentVariables, OdeSolver);
     583        // data for input variables is assumed to be known
     584        // input variables are all variable names that occur in the trees except for target variables (we assume that trees have been generated correctly)
     585        var inputVariables = trees.SelectMany(t => t.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName)).Except(targetVars);
     586
     587        var optimizationData = new OptimizationData(trees, targetVars, inputVariables.ToArray(), problemData, null, TrainingEpisodes.ToArray(), NumericIntegrationSteps, latentVariables, OdeSolver);
    564588        var trainingPrediction = Integrate(optimizationData).ToArray();
    565589
     
    731755      var trees = optimizationData.trees;
    732756      var dataset = optimizationData.problemData.Dataset;
    733       var inputVariables = optimizationData.problemData.AllowedInputVariables.ToArray();
     757      var inputVariables = optimizationData.variables;
    734758      var targetVariables = optimizationData.targetVariables;
    735759      var latentVariables = optimizationData.latentVariables;
     
    753777        // initialize values for inputs and targets from dataset
    754778        foreach (var varName in inputVariables) {
    755           var y0 = dataset.GetDoubleValue(varName, t0);
    756           nodeValues.SetVariableValue(varName, y0, Vector.Zero);
     779          // in this problem we also allow fixed numeric parameters (represented as variables with the value as name)
     780          if (double.TryParse(varName, NumberStyles.Float, CultureInfo.InvariantCulture, out double value)) {
     781            nodeValues.SetVariableValue(varName, value, Vector.Zero);
     782          } else {
     783            var y0 = dataset.GetDoubleValue(varName, t0);
     784            nodeValues.SetVariableValue(varName, y0, Vector.Zero);
     785          }
    757786        }
    758787        foreach (var varName in targetVariables) {
     
    815844          // update for next time step (only the inputs)
    816845          foreach (var varName in inputVariables) {
    817             nodeValues.SetVariableValue(varName, dataset.GetDoubleValue(varName, t), Vector.Zero);
     846            // in this problem we also allow fixed numeric parameters (represented as variables with the value as name)
     847            if (double.TryParse(varName, NumberStyles.Float, CultureInfo.InvariantCulture, out double value)) {
     848              // value is unchanged
     849            } else {
     850              nodeValues.SetVariableValue(varName, dataset.GetDoubleValue(varName, t), Vector.Zero);
     851            }
    818852          }
    819853        }
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Solution.cs

    r16610 r16653  
    9191      // var random = new FastRandom(12345);
    9292      // snmse = Problem.OptimizeForEpisodes(trees, problemData, targetVars, latentVariables, random, new[] { forecastEpisode }, 100, numericIntegrationSteps, odeSolver);
    93       var optimizationData = new Problem.OptimizationData(trees, targetVars, problemData.AllowedInputVariables.ToArray(), problemData, null, new[] { forecastEpisode }, numericIntegrationSteps, latentVariables, odeSolver);
     93
     94      var inputVariables = trees.SelectMany(t => t.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName)).Except(targetVars);
     95
     96      var optimizationData = new Problem.OptimizationData(trees, targetVars, inputVariables.ToArray(), problemData, null, new[] { forecastEpisode }, numericIntegrationSteps, latentVariables, odeSolver);
    9497      //
    9598      //
Note: See TracChangeset for help on using the changeset viewer.