Skip to content

Commit 5f9c955

Browse files
New function getBellmanValuesWithPlaia (#20)
* New function getBellmanValuesWithPlaia * Avoid errors * Cvar in getBellmanValuesWithPlaia * Update function get bellman plaia * Fix errors * Refactor plaia (#23) * validate_and_normalize_areas * validate_api_study_for_plaia * prepare_areas_for_simulation * run_plaia_simulation and download_output_zip * write_api_file * extract_from_zip * Update dowload_output_zip * Fix check * fix * prepare_and_launch_plaia * Fux error * Fix erros zipfile * other_options in prepare_and_launch_plaia * Fix bellman values * Improve bellman overflow (#24) * Simplify bellman values interpolation * Simplify next states values * Reorganize build_all_possible_decisions * Better overflow * Improve documentation (#25) * Rewrite README * New table for methods overview * Remove section multistock * Overflow comment * Increment version number to 2.2.0 * Argument final level for all functions * New function to wait for task end * Reduce time * Update NEWS.md
1 parent 5299f77 commit 5f9c955

31 files changed

+767
-449
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: antaresWaterValues
33
Title: Generating water values for Antares
4-
Version: 2.1.1
4+
Version: 2.2.0
55
Authors@R: c(
66
person("Dhia", "Gharsallaoui", role = "aut"),
77
person("Juliette", "Gerbaux", email = "juliette.gerbaux@rte-france.com",role = c("aut","cre")),
@@ -42,6 +42,7 @@ Suggests:
4242
spsComps (>= 0.3.0),
4343
spsUtil,
4444
testthat (>= 3.0.0),
45+
yaml,
4546
zoo
4647
VignetteBuilder:
4748
knitr

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export(changeHydroManagement)
77
export(constraint_generator)
88
export(getBellmanValuesFromOneSimulationMultistock)
99
export(getBellmanValuesSequentialMultiStockWithPlaia)
10+
export(getBellmanValuesWithPlaia)
1011
export(getPumpEfficiency)
1112
export(get_Reward)
1213
export(get_inflow)

NEWS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# antaresWaterValues 2.2.0
2+
3+
## New features
4+
5+
* New function to compute Bellman values directly with plaia.
6+
* Verification of task ending in API mode.
7+
* Argument final_level on almost all Bellman values functions.
8+
9+
## Breaking change
10+
11+
* New way to handle overflow in dynamic programming. It can be done anytime but is limited by weekly inflows and is penalized by spillage cost.
12+
113
# antaresWaterValues 2.1.0
214

315
## New features

R/Bellman.R

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@
4747
# Get interpolation function of rewards for each possible transition for each MC year
4848
f_reward_year <- get_reward_interpolation(Data_week)
4949

50-
#Get interpolation function of next Bellman values
51-
f_next_value <- get_bellman_values_interpolation(next_state,next_week_values_l,mcyears)
52-
5350
# Build a data.table from Data_week that list for each state and each MC year, the possible transitions
54-
df_SDP <- build_all_possible_decisions(Data_week,decision_space,f_next_value,
55-
mcyears,lvl_high,lvl_low,E_max,P_max,
51+
df_SDP <- build_all_possible_decisions(Data_week,decision_space,
52+
lvl_high,lvl_low,E_max,P_max,
5653
next_week_values_l,niveau_max,overflow_cost,
5754
next_states = next_state)
5855

R/Bellman_subfunctions.R

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,6 @@ get_reward_interpolation <- function(Data_week){
1515
return(f_reward_year)
1616
}
1717

18-
#' Create approximation of Bellman function for next week for each scenario, used in \code{Bellman} and
19-
#' in \code{getOptimalTrend}
20-
#'
21-
#' @param next_state possible states for next week
22-
#' @param next_week_values Vector of Bellman values to interpolate
23-
#' @param mcyears Vector of monte carlo years to take into account
24-
#'
25-
#' @return List of \code{stats::approxfun} for each scenario
26-
#' @keywords internal
27-
get_bellman_values_interpolation <- function(next_state,next_week_values,mcyears){
28-
29-
df_next_week <- data.frame(years = mcyears,
30-
next_state = next_state,
31-
next_value = next_week_values)
32-
33-
f_next_value <- c()
34-
for (year in mcyears){
35-
df <- dplyr::filter(df_next_week, .data$years==year)
36-
f <- stats::approxfun(df$next_state, df$next_value)#,yleft = 0, yright=0)
37-
f_next_value <- c(f_next_value,f)
38-
}
39-
40-
return(f_next_value)
41-
}
4218

4319
#' Create a data frame with all possible transition and associated next state for
4420
#' each scenario and each state for one particular week, used in \code{Bellman}.
@@ -49,9 +25,6 @@ get_bellman_values_interpolation <- function(next_state,next_week_values,mcyears
4925
#' as weel as inflows
5026
#' @param decision_space Simulation constraints values for the week
5127
#' generated by \code{get_Reward} or \code{runWaterValuesSimulation}
52-
#' @param f_next_value List of approximated Bellman functions for the next week,
53-
#' generated by \code{get_bellman_values_interpolation}
54-
#' @param mcyears Vector of monte carlo years to take into account
5528
#' @param lvl_high Double, top rule curve for the end of the week in MWh
5629
#' @param lvl_low Double, bottom rule curve for the end of the week in MWh
5730
#' @param E_max Double, weekly maximum turbining power for the week
@@ -65,42 +38,47 @@ get_bellman_values_interpolation <- function(next_state,next_week_values,mcyears
6538
#' next_state, control (transition to next state) and
6639
#' next value (Bellman value of next state)
6740
#' @keywords internal
68-
build_all_possible_decisions <- function(Data_week,decision_space,f_next_value,
69-
mcyears,lvl_high,lvl_low,E_max,P_max,
41+
build_all_possible_decisions <- function(Data_week,decision_space,
42+
lvl_high,lvl_low,E_max,P_max,
7043
next_week_values,niveau_max,overflow_cost,
7144
next_states){
7245

73-
df_next_week <- data.frame(years = Data_week$years,
74-
next_state = next_states,
75-
next_value = next_week_values)
46+
df_next_week <- dplyr::distinct(data.frame(
47+
next_state = next_states,
48+
next_value = next_week_values
49+
))
7650

77-
future_states <- Data_week %>%
78-
dplyr::inner_join(df_next_week,by="years", relationship="many-to-many") %>%
79-
dplyr::mutate(control = -.data$next_state+.data$states+.data$hydroStorage)
51+
#Get interpolation function of next Bellman values
52+
f_next_value <- stats::approxfun(df_next_week$next_state, df_next_week$next_value)
8053

81-
control_possible <- Data_week %>%
82-
dplyr::right_join(decision_space,by=c("years"="mcYear"), relationship="many-to-many") %>%
83-
dplyr::rename("control"="u") %>%
84-
dplyr::mutate(next_state=dplyr::if_else(.data$states+.data$hydroStorage-.data$control>niveau_max,niveau_max,
85-
.data$states+.data$hydroStorage-.data$control)) %>%
86-
dplyr::mutate(overflow=dplyr::if_else(.data$states+.data$hydroStorage-.data$control>niveau_max,
87-
.data$states+.data$hydroStorage-.data$control-niveau_max,0)) %>%
88-
dplyr::mutate(next_value=mapply(function(y,x)f_next_value[[which(y==mcyears)]](x), .data$years, .data$next_state)+
89-
.data$overflow*overflow_cost) %>%
90-
dplyr::select(-c("overflow"))
91-
92-
control_min <- Data_week %>%
93-
dplyr::mutate(next_state=lvl_high) %>%
94-
dplyr::mutate(control = -.data$next_state+.data$states+.data$hydroStorage) %>%
95-
dplyr::mutate(next_value=mapply(function(y,x)f_next_value[[which(y==mcyears)]](x), .data$years, .data$next_state))
54+
possible_states = data.frame(next_state = c(df_next_week$next_state,
55+
lvl_low,lvl_high))
9656

97-
control_max <- Data_week %>%
98-
dplyr::mutate(next_state=lvl_low) %>%
57+
possible_control <- Data_week %>%
58+
dplyr::cross_join(possible_states) %>%
9959
dplyr::mutate(control = -.data$next_state+.data$states+.data$hydroStorage) %>%
100-
dplyr::mutate(next_value=mapply(function(y,x)f_next_value[[which(y==mcyears)]](x), .data$years, .data$next_state))
60+
dplyr::filter((-P_max<=.data$control)&(.data$control<=E_max))
10161

102-
df_SDP <- dplyr::bind_rows(future_states, control_possible, control_min, control_max) %>%
103-
dplyr::filter((-P_max<=.data$control)&(.data$control<=E_max)&(.data$next_state>=0))
62+
possible_control <- Data_week %>%
63+
dplyr::right_join(decision_space,by=c("years"="mcYear"), relationship="many-to-many") %>%
64+
dplyr::rename("control"="u") %>%
65+
dplyr::mutate(next_state = -.data$control+.data$states+.data$hydroStorage) %>%
66+
rbind(possible_control)
67+
68+
df_SDP = possible_control %>%
69+
dplyr::select(-c("next_state")) %>%
70+
dplyr::cross_join(possible_states) %>%
71+
dplyr::mutate(overflow = -.data$next_state+.data$states+.data$hydroStorage - .data$control) %>%
72+
dplyr::filter(0<=.data$overflow,.data$overflow<=.data$hydroStorage) %>%
73+
rbind(dplyr::mutate(possible_control, overflow = 0)) %>%
74+
rbind(dplyr::mutate(possible_control,
75+
overflow = .data$hydroStorage,
76+
next_state = .data$next_state-.data$overflow))
77+
78+
df_SDP <- df_SDP %>%
79+
dplyr::filter(.data$next_state>=0, .data$next_state<=niveau_max)%>%
80+
dplyr::mutate(next_value = f_next_value(.data$next_state) + .data$overflow*overflow_cost) %>%
81+
dplyr::select(-c("overflow"))
10482

10583
return(df_SDP)
10684
}

R/Grid_Matrix.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#' Compute Bellman values
22
#'
33
#' Compute Bellman values and water values with dynamic programming based on reward functions computed with \code{get_Reward()}.
4-
#' Mode information about this function in \code{vignette("grid_Matrix-parameters")}.
4+
#' Mode information about this function in \code{vignette("grid_Matrix-parameters")}. In the backward recursion, overflow is accepted but it
5+
#' can't exceed weekly inflows and is penalized with the spillage cost.
56
#'
67
#' @inheritParams runWaterValuesSimulation
78
#' @param reward_db Reward functions generated by the function \code{get_Reward()}.
@@ -15,7 +16,7 @@
1516
#' @param max_hydro_weekly Maximum weekly pumping and generating power generated by the function \code{get_max_hydro()} with \code{timeStep="weekly"}.
1617
#' @param force_final_level Binary. Whether final level should be constrained.
1718
#' @param final_level Double. Final level (in percent between 0 and 100) if final level is constrained. If you want initial level, use \code{get_initial_level()}.
18-
#' @param penalty_final_level_low Double. Penalties for both bottom rule curve to constrain final level.
19+
#' @param penalty_final_level_low Double. Penalties for bottom rule curve to constrain final level.
1920
#' @param penalty_final_level_high Double. Penalties for top rule curve to constrain final level.
2021
#' @param until_convergence Binary. \code{TRUE} to repeat cycles until convergence of water values or
2122
#' attending the limit.

R/iterations_simulation_DP.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#' between the reservoir capacity and zero
1919
#' @param cvar_value from 0 to 1. the probability used in cvar method
2020
#' @param penalty_final_level Penalties (for both bottom and top rule curves) to constrain final level
21+
#' @param final_level Double. Final level (in percent between 0 and 100). Initial level computed by \code{get_initial_level()} by default.
2122
#' @param df_previous_cut Data frame containing previous estimations of cuts
2223
#'
2324
#' @export
@@ -28,6 +29,7 @@ calculateBellmanWithIterativeSimulations <- function(area,pumping, pump_eff=1,op
2829
path_solver,
2930
states_step_ratio=1/50,
3031
cvar_value = 1,
32+
final_level = NULL,
3133
penalty_final_level = NULL,
3234
df_previous_cut = NULL){
3335

@@ -76,7 +78,9 @@ calculateBellmanWithIterativeSimulations <- function(area,pumping, pump_eff=1,op
7678
controls <- tidyr::drop_na(controls) %>%
7779
dplyr::cross_join(data.frame(mcYear=mcyears))
7880

79-
final_level = get_initial_level(area,opts)
81+
if (is.null(final_level)){
82+
final_level = get_initial_level(area,opts)
83+
}
8084

8185
if (!is.null(df_previous_cut)){
8286
controls = df_previous_cut %>%
@@ -512,6 +516,7 @@ getNewConstraint <- function(level_init,watervalues,mcyears,
512516
#' less to it. (quantile(cvar_value))
513517
#' @param penalty_final_level Penalties (for both bottom and top rule curves) to constrain final level
514518
#' @param initial_traj Initial trajectory (used for other storages)
519+
#' @param list_final_level List of double. For each storage, final level (in percent between 0 and 100) if final level is constrained. Initial level computed by \code{get_initial_level()} by default.
515520
#' @param list_areas_to_compute Vector of character. Areas for which to compute Bellman values. If \code{NULL}, all areas in \code{list_areas} are used.
516521
#' @inheritParams runWaterValuesSimulationMultiStock
517522
#' @inheritParams calculateBellmanWithIterativeSimulations
@@ -524,6 +529,7 @@ calculateBellmanWithIterativeSimulationsMultiStock <- function(list_areas,list_p
524529
path_solver,
525530
states_step_ratio=1/50,
526531
cvar_value = 1,
532+
list_final_level = NULL,
527533
penalty_final_level = NULL,
528534
initial_traj = NULL,
529535
df_previous_cut = NULL,
@@ -549,14 +555,13 @@ calculateBellmanWithIterativeSimulationsMultiStock <- function(list_areas,list_p
549555
assertthat::assert_that(opts$antaresVersion>=870,
550556
msg = "Scenarization of rhs of binding constraints not available with the version of Antares. Update the study to 8.7.0 or don't scenarize control values.")
551557

552-
list_areas = tolower(list_areas)
558+
validate_and_normalize_areas(list_areas,opts)
553559

554560
list_backup = list()
555561
list_inflow = list()
556562
list_capacity = list()
557563
for (j in seq_along(list_areas)){
558564
area = list_areas[[j]]
559-
check_area_name(area = area, opts = opts)
560565

561566
list_backup[[j]] = getBackupData(area,mcyears,opts)
562567

@@ -620,7 +625,12 @@ calculateBellmanWithIterativeSimulationsMultiStock <- function(list_areas,list_p
620625
a = area
621626
pumping <- list_pumping[area]
622627
pump_eff <- list_efficiency[area]
623-
final_level <- get_initial_level(area,opts)
628+
if (is.null(list_final_level)){
629+
final_level <- get_initial_level(area,opts)
630+
} else {
631+
final_level <- list_final_level[[area]]
632+
}
633+
624634

625635
max_hydro <- get_max_hydro(area, opts)
626636
max_hydro_weekly <- max_hydro %>%

R/multistock_1simulation.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#'
88
#' @inheritParams runWaterValuesSimulationMultiStock
99
#' @inheritParams Grid_Matrix
10+
#' @param list_final_level List of double. For each storage, final level (in percent between 0 and 100) if final level is constrained. Initial level computed by \code{get_initial_level()} by default.
1011
#' @param write_vu Binary. True to write water values in the Antares study.
1112
#' @param simu List of simulation parameters returned by the function \code{antaresRead::setSimulationPath()} with the simulation selected from which to use the storage trajectory to run the simulation.
1213
#'
@@ -19,6 +20,7 @@ getBellmanValuesFromOneSimulationMultistock <- function(opts,
1920
list_areas,
2021
list_pumping,
2122
list_efficiency,
23+
list_final_level = NULL,
2224
force_final_level,
2325
penalty_final_level_low,
2426
penalty_final_level_high,
@@ -92,7 +94,11 @@ getBellmanValuesFromOneSimulationMultistock <- function(opts,
9294
for (a in list_areas) {
9395
pumping <- list_pumping[a]
9496
pump_eff <- list_efficiency[a]
95-
final_level <- get_initial_level(a, opts)
97+
if (!is.null(list_final_level)){
98+
final_level <- list_final_level[[a]]
99+
} else {
100+
final_level <- get_initial_level(a, opts)
101+
}
96102

97103
print(a)
98104
local_sim_values <- simulation_res$simulation_values %>%

0 commit comments

Comments
 (0)