-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
165 lines (139 loc) · 4.97 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Edit and knit README.Rmd to update README.md. -->
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
fig.path = "man/figures/README-",
warning = FALSE,
message = FALSE
)
```
# Presumed Open Data Energy Challenge
This is the code used by the winning team "Cameron" in the Presumed Open Data Data Science Challenge. The challenge focused on scheduling battery charging to ensure most charging used energy from PV generation and discharging was scheduled so as to minimise peak demand in the evening. Team Cameron finished in first place at the conclusion of the competition.
## Installation
To install the `podEnergyComp` library in this repository, run the following code.
```{r installation, eval=FALSE, include=TRUE}
install.packages("devtools")
devtools::install_github("camroach87/pod-energy-comp")
```
## Tuning demand and PV models
**TODO:** The `cv_ts_folds` function was incredibly useful when tuning the `num_leaves` and `learning_rate` hyperparameters. I'll add a short demonstration soon.
## Producing a battery schedule
Here is an example of how I produced the forecasts for the final round of the competition.
```{r usage}
library(podEnergyComp)
library(tidyverse)
library(lubridate)
fcst_start_date <- ymd("2020-07-03")
# predict PV and demand
demand.data <- load_demand_data()
demand.cv <- cv_ts_folds(demand.data$datetime,
start_date = fcst_start_date,
horizon = 7,
iterations = 1)
demand.forecast <- pred_demand(
select(demand.data, -datetime),
demand.cv[[1]]$train,
demand.cv[[1]]$test,
nrounds = 500L,
num_leaves = 10L,
learning_rate = 0.1,
obj = "regression",
metric = "regression"
)
pv.data <- load_pv_data()
pv.cv <- cv_ts_folds(pv.data$datetime,
start_date = fcst_start_date,
horizon = 7,
iterations = 1)
pv.forecast <- pred_pv_quantile(
pv.data,
pv.cv[[1]]$train,
pv.cv[[1]]$test,
alpha = 0.5, # High PV generation so don't need to fit all quantiles
num_iterations = 250L,
num_leaves = 31L,
learning_rate = 0.03
)
# Tidy predictions
demand.pred_df <- tibble(
datetime = getElement(demand.data[demand.cv[[1]]$test,], "datetime"),
demand_mw = demand.forecast
)
pv.pred_df <- tibble(
datetime = getElement(pv.data[pv.cv[[1]]$test,], "datetime"),
pv_power_mw = pv.forecast
)
fcst_df <- full_join(pv.pred_df, demand.pred_df, by = "datetime") %>%
mutate(period = 2*hour(datetime) + minute(datetime)/30 + 1) %>%
arrange(datetime)
# schedule battery
b_sched <- schedule_battery(fcst_df)
bat_df <- format_charge_data(b_sched$B)
```
## Plots
The demand and PV forecasts are shown below.
```{r plot-forecasts}
fcst_df %>%
select(datetime, Demand = demand_mw, PV = pv_power_mw) %>%
pivot_longer(-datetime) %>%
ggplot(aes(x = datetime, y = value, colour = name)) +
geom_line() +
labs(title = "Demand and PV generation forecasts",
y = "Power (MW)",
x = "Date",
colour = "Forecast")
```
The following plot shows the charging schedule and the total energy stored in the battery for each half hour of the day.
```{r plot-schedule}
b_sched$B %>%
as_tibble(rownames = "date") %>%
pivot_longer(cols = -date, names_to = "period", values_to = "charge_mw") %>%
mutate(date = factor(ymd(date)),
period = as.numeric(period)) %>%
ggplot(aes(x = period, y = charge_mw, colour = date)) +
geom_line() +
labs(title = "Battery charging schedule",
colour = "Date",
x = "Period",
y = "Charge (MW)")
```
```{r plot-battery-energy}
b_sched$C %>%
as_tibble(rownames = "date") %>%
pivot_longer(cols = -date, names_to = "period", values_to = "charge_mw") %>%
mutate(date = factor(ymd(date)),
period = as.numeric(period)) %>%
ggplot(aes(x = period, y = charge_mw, colour = date)) +
geom_line() +
labs(title = "Battery energy stored",
colour = "Date",
x = "Period",
y = "Energy (MWh)")
```
Since the completion of the competition, the actual demand and PV data has now been released. We can compare how well our battery schedule reduces the peak reduction.
```{r plot-outcome}
bat_df %>%
inner_join(fcst_df %>%
rename(`Demand (forecast)` = demand_mw),
by = "datetime") %>%
inner_join(demand.data %>%
select(datetime, `Demand (actual)` = demand_mw),
by = "datetime") %>%
mutate(`Demand reduction (actual)` = `Demand (actual)` + charge_MW,
`Demand reduction (forecast)` = `Demand (forecast)` + charge_MW,
idx = row_number()) %>%
mutate(date = date(datetime)) %>%
select(date, period, starts_with("Demand")) %>%
pivot_longer(cols = -c(date, period)) %>%
ggplot(aes(x = period, y = value, colour = name)) +
geom_line() +
ylim(0, NA) +
facet_wrap(~date) +
labs(title = "Forecast and actual demand reduction",
colour = "",
x = "Period",
y = "Power (MW)")
```