You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we can see, the forecast model overestimates the demand for the first item and underestimates the demand for the second item (both items average demand is 55), following the incentives from the model structure.
Copy file name to clipboardExpand all lines: docs/src/tutorials/custom_forecast.md
+41-20Lines changed: 41 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,53 +4,74 @@ The basic approach to define a forecast model is to use a `Chain` from the `Flux
4
4
5
5
## Input-Output mapping
6
6
7
-
The connection between predictive model outputs and plan model inputs is not always a straightforward one. Because of this, the `set_forecast_model` function, used to define the predictive model in the ApplicationDrivenLearning.jl package, includes the `input_output_map` parameter.
8
-
This parameter allows users to declare an explicit mapping between the outputs produced by Flux models and the forecast variables used in the planning model. This is useful in contexts where the same prediction logic can be applied across several entities (such as production units or geographical locations), promoting model reuse and computational efficiency.
7
+
The connection between predictive model outputs and plan model inputs is not always a straightforward one. Because of this, the definition of a `PredictiveModel` structure, used to define the predictive model in the ApplicationDrivenLearning.jl package, includes the `input_output_map` parameter.
8
+
This parameter allows users to declare an explicit mapping between the outputs produced by Flux models and the forecast variables used in the planning model. This is useful in contexts where the same prediction logic can be applied across several entities (such as production units or geographical locations), promoting model reuse and computational and parameter efficiency.
9
9
10
-
Consider a scenario where the input dataset contains 3 predictive variables (for example expected temperature on location 1, expected temperature on location 2 and weekday), there are 2 forecast variables (energy demand on the two locations of interest) and the forecast model should use only the expected temperature of a location to predict it’s demand. That means we would make two predictions using the same model and concatenate those values. This can be easily achieved with a dictionary mapping the data input and forecast variable indexes.
10
+
Consider a scenario where the input dataset contains 3 variables (for example expected temperature on location 1, expected temperature on location 2 and weekday), there are 2 forecast variables (energy demand on the two locations of interest) and the forecast model should use only the expected temperature of a location to predict it’s demand. That means we would make two predictions using the same model and concatenate those values. This can be easily achieved with a dictionary mapping the data input and forecast variable indexes.
The definition of the predictive model can also be done using multiple Flux models. This supports the modular construction of predictive architectures, where specialized components are trained to forecast different aspects of the problem, without the difficulty of defining custom architectures.
31
39
32
-
This can be achieved providing an array of model objects and an array of dictionaries as input-output mapping to the `set_forecast_model` function. Using the context from previous example, let’s assume there is an additional variable that has to be predicted to each location but not variable on time (that is, on dataset samples). This can be achieved defining an additional model that maps a constant input value to the correct output indexes.
40
+
This can be achieved providing an array of model objects and an array of dictionaries as input-output mapping to the `PredictiveModel` construction. Using the context from previous example, let’s assume we also want to predict price for each location using a single model that receives average lagged price. This can be achieved defining an additional model and mapping.
33
41
34
42
```julia
43
+
model = ApplicationDrivenLearning.Model()
44
+
@variables(model, begin
45
+
demand[1:2], ApplicationDrivenLearning.Forecast
46
+
price[1:2], ApplicationDrivenLearning.Forecast
47
+
end)
48
+
35
49
X = [
36
-
768921;
37
-
728531
38
-
] # input dataset of size 2 by 4
50
+
76892103;
51
+
7285389
52
+
] .|> Float32 # input dataset of size 2 by 4
53
+
Y =Dict(
54
+
demand[1] => [101, 89] .|> Float32,
55
+
demand[2] => [68, 49] .|> Float32,
56
+
price[1] => [101, 89] .|> Float32,
57
+
price[2] => [68, 49] .|> Float32,
58
+
)
39
59
dem_forecast =Dense(
40
60
2=>1
41
61
) # demand forecast model takes 2 inputs and outputs single value
42
-
aux_forecast=Dense(
62
+
prc_forecast=Dense(
43
63
1=>2
44
-
) #auxiliar forecast model takes 1 input and outputs 2 values
45
-
forecast_objs = [dem_forecast, aux_forecast]
64
+
) #price forecast model takes 1 input and outputs 2 values
65
+
forecast_objs = [dem_forecast, prc_forecast]
46
66
input_output_map = [
47
67
Dict(
48
-
[1, 3] => [1],
49
-
[2, 3] => [2]
50
-
), # input indexes 1,2,3 are used to compute forecast vars 1,2 with 1st Flux.Dense object
68
+
[1, 3] => [demand[1]],
69
+
[2, 3] => [demand[2]]
70
+
), # input indexes 1,2,3 are used to compute demand forecast vars separately with 1st Flux.Dense object
51
71
Dict(
52
-
[4] =>[3, 4]
53
-
), # input index 4 is used to compute forecast vars 3,4 with 2nd Flux.Dense object
72
+
[4] =>price
73
+
), # input index 4 is used to compute both price forecast vars with 2nd Flux.Dense object
@@ -89,13 +89,6 @@ We have to include a solver for solving the optimization models. In this case, w
89
89
using HiGHS
90
90
```
91
91
92
-
As explained, the data used to train the model is very limited, composed of only two samples of energy demand. Values of one are used as input data, without adding any real additional information to the model. Both `X` and `Y` values are transformed to `Float32` type to match Flux parameters.
93
-
94
-
```julia
95
-
X =reshape([11], (2, 1)) .|> Float32
96
-
Y =reshape([1020], (2, 1)) .|> Float32
97
-
```
98
-
99
92
Just like regular JuMP, ApplicationDrivenLearning has a `Model` function to initialize an empty model. After initializing, we can declare the policy and forecast variables.
100
93
101
94
- Policy variables represent decision variables that should be maintained from the `Plan` to the `Assess` model.
As explained, the data used to train the model is very limited, composed of only two samples of energy demand. Values of one are used as input data, without adding any real additional information to the model. `X` is a matrix representing input values and the dictionary `Y` maps the forecast variable `θ` to numerical values to be used. Both `X` and `Y` values are transformed to `Float32` type to match Flux parameters.
145
+
146
+
```julia
147
+
X =reshape([11], (2, 1)) .|> Float32
148
+
Y =Dict(θ => [10, 20] .|> Float32)
149
+
```
150
+
151
+
151
152
A simple forecast model with only one parameter can be defined as a `Flux.Dense` layer with just 1 weight and no bias. We can associate the predictive model with our ApplicationDrivenLearning model only if its output size matches the number of declared forecast variables.
0 commit comments