intermediate: R6, too slow
[talweg.git] / pkg / R / Forecaster.R
index d5d5280..4c8437e 100644 (file)
@@ -1,50 +1,71 @@
-#' @title Forecaster (abstract class)
+#' Forecaster
 #'
-#' @description Abstract class to represent a forecaster (they all inherit this)
+#' Forecaster (abstract class, implemented by all forecasters)
 #'
 #' @field params List of computed parameters, for post-run analysis (dev)
 #' @field data Dataset, object of class Data
 #' @field pjump Function: how to predict the jump at day interface ?
-Forecaster = setRefClass(
-       Class = "Forecaster",
-
-       fields = list(
-               params = "list",
-               data = "Data",
-               pjump = "function"
+#'
+#' @docType class
+#' @importFrom R6 R6Class
+Forecaster = R6::R6Class("Forecaster",
+       private = list(
+               .params = "list",
+               .data = "Data",
+               .pjump = "function"
        ),
-
-       methods = list(
-               initialize = function(...)
-               {
-                       "Initialize (generic) Forecaster object"
-
-                       callSuper(...)
-                       if (!hasArg(data))
-                               stop("Forecaster must be initialized with a Data object")
-                       params <<- list()
-               },
-               predict = function(today, memory, horizon, ...)
-               {
-                       "Obtain a new forecasted time-serie"
-
-                       # Parameters (potentially) computed during shape prediction stage
-                       predicted_shape = predictShape(today, memory, horizon, ...)
-                       predicted_delta = pjump(data, today, memory, horizon, params, ...)
-                       # Predicted shape is aligned it on the end of current day + jump
-                       predicted_shape + tail(data$getSerie(today),1) - predicted_shape[1] + predicted_delta
-               },
+       public = list(
+               initialize = function(data, pjump)
+                       initialize(self, private, data, pjump)
+               ,
+               predictSerie = function(today, memory, horizon, ...)
+                       predictSerie(private, today, memory, horizon, ...)
+               ,
                predictShape = function(today, memory, horizon, ...)
-               {
-                       "Shape prediction (centered curve)"
-
-                       #empty default implementation: to implement in inherited classes
-               },
+                       predictShape(private, today, memory, horizon, ...)
+               ,
                getParameters = function()
-               {
-                       "Get parameters list"
-
-                       params
-               }
+                       getParameters(private)
        )
 )
+
+#' Initialize (generic) Forecaster object
+#'
+#' @param o Object of class Forecaster
+#' @param private List of private members in o
+#' @param data Object of class Data
+#' @param pjump Function to predict jump
+initialize = function(o, private, data, pjump)
+{
+       .params <<- list()
+       .data <<- data
+       .pjump <<- pjump
+       invisible(o)
+}
+
+#' Obtain a new forecasted time-serie
+#'
+#' @inheritParams initialize
+#' @param today Index for current prediction
+#' @param memory Depth in data (in days)
+#' @param horizon Number of hours to forecast
+predictSerie = function(private, today, memory, horizon, ...)
+{
+       # Parameters (potentially) computed during shape prediction stage
+       predicted_shape = predictShape(today, memory, horizon, ...)
+       predicted_delta = private$.pjump(private$.data, today, memory, horizon, params, ...)
+       # Predicted shape is aligned it on the end of current day + jump
+       predicted_shape + tail(private$.data$getSerie(today),1) - predicted_shape[1] + predicted_delta
+}
+
+#' Shape prediction (centered curve)
+#'
+#' @inheritParams predictSerie
+predictShape = function(private, today, memory, horizon, ...)
+       #empty default implementation: to implement in inherited classes
+
+#' Get parameters list
+#'
+#' @inheritParams initialize
+getParameters = function(private)
+       private$.params