Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
221 commits
Select commit Hold shift + click to select a range
502e920
More robust simulation names in runWaterValuesSimulation
Juliette-Gerbaux Aug 14, 2023
f3032ba
Error in simulation_names in runWaterValuesSimulation
Juliette-Gerbaux Aug 17, 2023
c075532
New method for optimal_trend and small corrections in calculateBellma…
Juliette-Gerbaux Aug 17, 2023
e594748
Errors in get_Reward
Juliette-Gerbaux Aug 22, 2023
6f7e6ca
Replacing write.table() with antares functions
Sep 6, 2023
4914093
Replacing read.table() with antares functions
Sep 6, 2023
c53f38d
Replacing fread() with antares functions
Sep 6, 2023
1d44e27
Checking area with antares functions
Sep 6, 2023
731f41b
Antares API compatibility : replacing writeIniFile() with writeIni()
Sep 13, 2023
b0cf802
Antares API compatibility : turn time_series parameter in createClust…
Sep 13, 2023
6aba68b
Antares API compatibility : turn values parameter in createBindingCon…
Sep 13, 2023
81019a2
Antares API compatibility : use id in removeBindingConstraint()instea…
Sep 13, 2023
5028b34
Merge branch 'main' of https://github.com/rte-antares-rpackage/antare…
Sep 13, 2023
9ad9920
Merge branch 'main' of https://github.com/rte-antares-rpackage/antare…
Sep 13, 2023
9906ad1
Merge branch 'main' of https://github.com/rte-antares-rpackage/antare…
Sep 13, 2023
d24b0e9
Antares API compatibility : restoration is deactivated if API study
Sep 14, 2023
46d7d47
Antares API compatibility : rewrite reset functions
Sep 14, 2023
229493b
Antares API compatibility : deactivated simulation control and export…
Sep 14, 2023
690f84d
Adding check_area_name() in hydro_storage_functions.R
Sep 18, 2023
f510e91
Specifying clearly opts for antares functions
Sep 18, 2023
ba3e9ec
Update main
Sep 18, 2023
bec3747
Shiny : default saving directory is the current working directory if …
Sep 18, 2023
0724a17
Possibility to choose the fast method in calculateBellmanWithIterativ…
Juliette-Gerbaux Nov 9, 2023
6fdc829
Update main
Juliette-Gerbaux Nov 10, 2023
ca6f670
Update main
Juliette-Gerbaux Nov 10, 2023
249353a
Correcting identation
Juliette-Gerbaux Nov 14, 2023
19ebefa
Correction in get_locel_reward_turb
Juliette-Gerbaux Dec 19, 2023
26786a8
Small ameliorations
Juliette-Gerbaux Jan 3, 2024
e6b5a98
Merge branch 'dev_iterative' of https://github.com/rte-antares-rpacka…
Juliette-Gerbaux Jan 3, 2024
0d4f18b
Simplification of shiny's iterative part
Juliette-Gerbaux Jan 3, 2024
acba8f0
Merge branch 'force_final_level' into dev_iterative
Juliette-Gerbaux Jan 3, 2024
5a3993f
Adapt iterative calculation to final level constraint
Juliette-Gerbaux Jan 3, 2024
e97ad91
Modify the simulation process so that the rhs of the binding constrai…
Juliette-Gerbaux Jan 4, 2024
248eb89
Separate binding constraints creation into left side and right side
Juliette-Gerbaux Jan 5, 2024
7be8bf0
Constraint values could be scenarized by mc year in runWaterSimulation
Juliette-Gerbaux Jan 5, 2024
4da7f24
Adaptation of Bellman values calculation if controls are scenarized b…
Juliette-Gerbaux Jan 5, 2024
39be276
Update scenario builder for fictive thermal cluster
Juliette-Gerbaux Jan 8, 2024
e45a4cc
Merge branch 'scenarization_controls' into dev_iterative
Juliette-Gerbaux Jan 8, 2024
3e32213
Merge branch 'scenarization_controls' into dev_iterative
Juliette-Gerbaux Jan 8, 2024
97ccd3a
Adapt iterative part to scenarized controls. All evaluated controls (…
Juliette-Gerbaux Jan 9, 2024
0158d45
Update documentation
Juliette-Gerbaux Jan 9, 2024
bb1d1bd
Merge branch 'dev_iterative' of https://github.com/rte-antares-rpacka…
Juliette-Gerbaux Jan 9, 2024
367bd06
Update reset_study
Juliette-Gerbaux Jan 9, 2024
cbbd6ef
Check initial level is defined properly by rule curves
Juliette-Gerbaux Jan 9, 2024
9c4c4e4
Correction in optimal trend to not have duplicated controls
Juliette-Gerbaux Jan 9, 2024
7ae4afc
Specify arguments in function to_Antares_Format
Juliette-Gerbaux Jan 9, 2024
8d4bf3d
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Jan 10, 2024
babce74
Take into account pumping efficiency in trends
Feb 23, 2024
c001f12
Merge branch 'scenarization_controls' into dev_iterative
Juliette-Gerbaux Mar 6, 2024
910f2d9
Merge branch 'main' into dev_iterative
Juliette-Gerbaux May 2, 2025
8e4cbb1
Merge branch 'main' into dev_iterative
Juliette-Gerbaux May 5, 2025
95c52d4
Fix check
May 6, 2025
a7acd24
Update gitignore
May 6, 2025
9ea3438
Merge branch 'main' into reecriture_antares_main
Juliette-Gerbaux May 6, 2025
d0d2059
Change to readIni and simplify readReservoirLevels
May 7, 2025
7d058ed
Update documentation
May 12, 2025
2b34bc5
Fix error
May 12, 2025
b1fa779
Merge branch 'main' into reecriture_antares_main
Juliette-Gerbaux May 12, 2025
af369b4
Run simulations with API
May 14, 2025
4b1d250
Refactor runWaterValuesSimulation
May 21, 2025
623b7e8
Save simulation res
May 23, 2025
324c1fd
Update documentation
May 23, 2025
dabc533
Improve method
May 28, 2025
6855819
Check min gen is null
Jun 11, 2025
5fe845b
Fix readReservoirLevels
Jun 11, 2025
e2159e8
Fix error
Jun 11, 2025
6032c24
Bug on district
Jun 11, 2025
668c2b9
Avoid rounding errors
Jun 12, 2025
9cac743
Clear scenario builder
Jun 12, 2025
6e794e7
Update version and documentation
Jun 12, 2025
ae65bf6
Merge branch 'reecriture_antares_main' into dev_iterative
Juliette-Gerbaux Jun 13, 2025
1a2de6a
Update documentation on data
Jun 13, 2025
c39b631
Mode Xpansion with API
Jun 20, 2025
b2e3193
Bug in edit bc with old versions of Antares and API
Jun 20, 2025
b47f60e
Mode expansion
Jun 23, 2025
f8263f9
Error without mingen
Jun 23, 2025
afb90ac
Thematic trimming with API
Jun 23, 2025
18cc236
Non interger pumping power
Jun 24, 2025
87f9d36
Update documentation
Jun 25, 2025
f21dd8a
Fix errors and allow to launch a subset of simulation
Jun 26, 2025
b0e8202
Fix errors
Jun 26, 2025
b9f158a
Fix two first iterations
Jun 26, 2025
87e71b2
Iterative method stock per stock
Jun 26, 2025
253c255
Fix errors and allow to launch a subset of simulation
Jun 26, 2025
206dc08
Fix errors and update documentation
Jul 3, 2025
97bd463
Merge branch 'reecriture_antares_main' into dev_iterative
Juliette-Gerbaux Jul 4, 2025
3efa077
Multi stock iterative : do not mix scenario for last optimal trajectory
Jul 4, 2025
bca0aef
Error on last week when computing optimal trajectory
Jul 4, 2025
6072fbc
Fix check
Jul 4, 2025
26bd59c
Update scenario builder api
Jul 7, 2025
14ba081
Api compatibility with updateReward
Jul 7, 2025
de4cf19
Xpansion mode Api 880
Jul 7, 2025
81db085
Small errors
Jul 9, 2025
5d1e1a7
Replace quantile method by cvar in Grid_Matrix
Jul 9, 2025
6af6ca6
Cvar and xpansion mode
Juliette-Gerbaux Aug 6, 2025
5ae4b46
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Aug 18, 2025
2ece27f
Moving getOptimalTrend and deleting getInitialTrend
Juliette-Gerbaux Aug 18, 2025
3d44b52
Add cuts
Juliette-Gerbaux Aug 18, 2025
66e7854
Remove ::: and method_fast
Juliette-Gerbaux Aug 18, 2025
367787d
Update documentation
Juliette-Gerbaux Aug 18, 2025
4f80f5a
Fix check
Juliette-Gerbaux Aug 18, 2025
a6ec934
Fix check
Juliette-Gerbaux Aug 18, 2025
715e007
Initial lintr and bug fixes
Juliette-Gerbaux Aug 19, 2025
b716a3f
Bug in multi stock iterative
Juliette-Gerbaux Aug 19, 2025
4c247f4
Document
Juliette-Gerbaux Aug 19, 2025
e1d87f9
Fix error
Juliette-Gerbaux Aug 19, 2025
ab1e605
Add abs in gap computation
Juliette-Gerbaux Aug 19, 2025
6d088f3
Fix bug in functions.R
Juliette-Gerbaux Aug 20, 2025
f8e9211
Change the study for water values once in iterative method
Juliette-Gerbaux Aug 25, 2025
2e21512
Update documentation
Juliette-Gerbaux Aug 25, 2025
f2c9e7d
Remove unused changeHydroManagement
Juliette-Gerbaux Aug 26, 2025
37dec5c
Bug in interative multi stock
Juliette-Gerbaux Aug 27, 2025
59fe322
Merge branch 'dev_iterative' of https://github.com/rte-antares-rpacka…
Juliette-Gerbaux Aug 27, 2025
ea787be
Remove method_dp and default cvar_value to 1
Juliette-Gerbaux Aug 27, 2025
e46b3a9
Update shiny
Juliette-Gerbaux Aug 27, 2025
5b9a993
Error with constraint_generator in iterative multi stock
Juliette-Gerbaux Aug 27, 2025
8b9ab81
Update documentation
Juliette-Gerbaux Aug 27, 2025
afa70a0
Bug in multi_stock with last trajectory in iterations_simulation_DP.R
Juliette-Gerbaux Sep 1, 2025
74817a7
Bug fixes
Juliette-Gerbaux Sep 10, 2025
beb6ee4
Bug fix
Juliette-Gerbaux Sep 16, 2025
0564401
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Sep 26, 2025
901ebdc
Merge branch 'dev_iterative' of https://github.com/rte-antares-rpacka…
Juliette-Gerbaux Sep 26, 2025
de8b5aa
Improve initial_traj argument
Juliette-Gerbaux Sep 29, 2025
8c3a56c
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Nov 13, 2025
0b43d1b
Add branch investment module
JulienKng Oct 15, 2025
df7a277
Fix bugs and add new function for initial simulations in multi stock
JulienKng Oct 30, 2025
1f37ddf
Change file name
JulienKng Oct 30, 2025
ae37897
Update Optim_H2_multi_nodes_functions.R to fix interpolation bug
JulienKng Oct 31, 2025
dd04df8
Export main function of Optim_H2_multi_nodes_functions.R
JulienKng Oct 31, 2025
7509c3b
Fix check
Juliette-Gerbaux Nov 13, 2025
8baf2f7
Bug in fread
Juliette-Gerbaux Nov 18, 2025
3348275
Remove test_vu in iterative methods
Juliette-Gerbaux Nov 18, 2025
3e9e28a
Fix bug
Juliette-Gerbaux Nov 18, 2025
3514043
Remove study_path from arguments
Juliette-Gerbaux Nov 19, 2025
fd95cb1
Read initial level from scenario builder
Juliette-Gerbaux Nov 26, 2025
2c82624
merge dev_iterative
Juliette-Gerbaux Nov 27, 2025
3137c40
Fix check
Juliette-Gerbaux Nov 28, 2025
3227c86
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Nov 28, 2025
4cb2735
Clean code (#13)
Juliette-Gerbaux Nov 28, 2025
247783b
Check NA values in Bellman and optimal trend
Juliette-Gerbaux Nov 28, 2025
2edfec7
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Nov 28, 2025
a5edf2b
Fix error
Juliette-Gerbaux Nov 28, 2025
0a47b8c
Simplify update_reward_cluster_bande
Juliette-Gerbaux Dec 1, 2025
a79b156
Faster build_all_possible_decisions
Juliette-Gerbaux Dec 1, 2025
30b1e87
Fix error
Juliette-Gerbaux Dec 1, 2025
f159218
Fix error initial level per year
Juliette-Gerbaux Dec 1, 2025
4b07a37
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Dec 1, 2025
f30251d
Fix error initial level per year
Juliette-Gerbaux Dec 1, 2025
99683d6
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Dec 3, 2025
72194c1
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Dec 3, 2025
c6d51bb
Remove plots in test
Juliette-Gerbaux Dec 3, 2025
160b754
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Dec 3, 2025
92f0ed3
Remove duplicate column
Juliette-Gerbaux Dec 3, 2025
2e812b0
New argument list_areas_to_compute
Juliette-Gerbaux Dec 4, 2025
a871c6d
New function create_disctrict for API
Juliette-Gerbaux Dec 5, 2025
8bf42ec
Merge branch 'create_district' into dev_iterative
Juliette-Gerbaux Dec 5, 2025
25a9566
New file
Juliette-Gerbaux Dec 5, 2025
e3d437d
Remove unuseful line
Juliette-Gerbaux Dec 5, 2025
d2cacd1
Merge branch 'create_district' into dev_iterative
Juliette-Gerbaux Dec 5, 2025
03b294f
Fix merge errors
Juliette-Gerbaux Dec 5, 2025
2029675
Remove force_final_level
Juliette-Gerbaux Dec 5, 2025
8066b5f
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Dec 5, 2025
7811fee
Document
Juliette-Gerbaux Dec 5, 2025
5700a28
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Dec 5, 2025
0be0407
Faster grid matrix (#16)
Juliette-Gerbaux Dec 8, 2025
9fa1904
Update max_hydro_power before simulations
Juliette-Gerbaux Dec 8, 2025
7548bf8
Avoid to evaluate again the same candidate
Juliette-Gerbaux Dec 8, 2025
d94bc88
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Dec 10, 2025
7338056
Fix error
Juliette-Gerbaux Dec 10, 2025
c7f5fd0
Fix export of functions in parallel
Juliette-Gerbaux Dec 10, 2025
5a0e6d4
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Dec 11, 2025
b30178b
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Dec 11, 2025
25c1345
Generate constraint without restriction from inflow and reservoir cap…
Juliette-Gerbaux Dec 12, 2025
da1c3fc
New function getBellmanValuesWithPlaia
Juliette-Gerbaux Dec 12, 2025
e853635
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Dec 15, 2025
3c0d586
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Dec 15, 2025
8104e6a
Fix error on reward columns
Juliette-Gerbaux Dec 15, 2025
0fe35e1
Grid cost function (#19)
Juliette-Gerbaux Dec 15, 2025
e4f7842
Avoid errors
Juliette-Gerbaux Dec 15, 2025
ba90119
New function edit_study_with_results
Juliette-Gerbaux Dec 15, 2025
27fbe1f
Fix error on to_remove
Juliette-Gerbaux Dec 16, 2025
1a9a3c5
Deal with overflow in build_all_possible_decisions
Juliette-Gerbaux Dec 17, 2025
9614275
Max hydro is proportionnal to invested storage capacity
Juliette-Gerbaux Dec 17, 2025
aa343bf
Storage bounds for each area
Juliette-Gerbaux Dec 17, 2025
02442c5
Remove back_to_first_node
Juliette-Gerbaux Dec 18, 2025
dcc86c8
Simplify and add test getOptimalTrend (#21)
Juliette-Gerbaux Dec 18, 2025
664b93b
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Dec 18, 2025
2e36a46
Deal with part_fixe
Juliette-Gerbaux Dec 19, 2025
b7010d5
New function remove_candidate_cluster
Juliette-Gerbaux Dec 19, 2025
7a41158
Remove cluster only if file doesn't exist
Juliette-Gerbaux Dec 19, 2025
ffe1881
Document
Juliette-Gerbaux Dec 19, 2025
b1e7b79
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Dec 19, 2025
2a323e1
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Dec 19, 2025
d223361
Check adeuacy-pactch
Juliette-Gerbaux Dec 19, 2025
94868d2
Merge branch 'main' into plaia
Juliette-Gerbaux Dec 19, 2025
c902e9c
Check on adequacy patch
Juliette-Gerbaux Dec 19, 2025
3a325c2
Revert unused changes
Juliette-Gerbaux Dec 31, 2025
7c08d83
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Jan 6, 2026
078f550
Merge branch 'main' into dev_iterative
Juliette-Gerbaux Jan 6, 2026
8e1a1d2
Revert changes
Juliette-Gerbaux Jan 6, 2026
f8106ad
Fix test
Juliette-Gerbaux Jan 6, 2026
9c26d3e
Increment version number to 2.1.0
Juliette-Gerbaux Jan 7, 2026
7849003
Merge branch 'dev_iterative' into invest_flex
Juliette-Gerbaux Jan 7, 2026
66bc2aa
Merge branch 'main' into invest_flex
Juliette-Gerbaux Jan 7, 2026
88b8383
Fix check
Juliette-Gerbaux Jan 7, 2026
2f9a078
Fix error
Juliette-Gerbaux Jan 7, 2026
058ca85
Fix error
Juliette-Gerbaux Jan 12, 2026
b730a3b
Merge branch 'main' into invest_flex
Juliette-Gerbaux Jan 28, 2026
52e1409
Cvar in getBellmanValuesWithPlaia
Juliette-Gerbaux Feb 5, 2026
93f75dc
Merge branch 'main' into plaia
Juliette-Gerbaux Feb 6, 2026
842c328
Update function get bellman plaia
Juliette-Gerbaux Feb 6, 2026
c25fe77
Merge branch 'plaia' of https://github.com/rte-antares-rpackage/antar…
Juliette-Gerbaux Feb 6, 2026
14363e3
Merge branch 'plaia' into invest_flex
Juliette-Gerbaux Feb 6, 2026
c07b9d2
Fix errors
Juliette-Gerbaux Feb 16, 2026
a5a1bae
Merge branch 'plaia' into invest_flex
Juliette-Gerbaux Feb 16, 2026
7141f58
Improve documentation
Juliette-Gerbaux Feb 25, 2026
9a8c407
Fix check
Juliette-Gerbaux Feb 26, 2026
c40351e
Merge branch 'main' into invest_flex
Juliette-Gerbaux Feb 27, 2026
4a3e84d
Fix check
Juliette-Gerbaux Feb 27, 2026
436bac9
Document
Juliette-Gerbaux Feb 27, 2026
90380d6
Merge branch 'main' into invest_flex
Juliette-Gerbaux Mar 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Generated by roxygen2: do not edit by hand

export(Grid_Matrix)
export(MultiStock_H2_Investment_reward_compute_once)
export(calculateBellmanWithIterativeSimulations)
export(calculateBellmanWithIterativeSimulationsMultiStock)
export(changeHydroManagement)
export(constraint_generator)
export(edit_study_with_results)
export(getBellmanValuesFromOneSimulationMultistock)
export(getBellmanValuesSequentialMultiStockWithPlaia)
export(getBellmanValuesWithPlaia)
Expand All @@ -23,6 +25,7 @@ export(plot_reward_mc)
export(plot_reward_variation)
export(plot_reward_variation_mc)
export(readReservoirLevels)
export(remove_candidate_cluster)
export(remove_out)
export(reward_offset)
export(runWaterValuesSimulation)
Expand Down
40 changes: 21 additions & 19 deletions R/Bellman.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#' * Rewards for each simulation value and each Monte-Carlo year.
#' * level_high Numeric. Highest possible reservoir value.
#' * level_low Numeric. Lowest possible reservoir value.
#' * states_next List of vectors enumerating all reachable states
#' @param decision_space Simulation constraints values
#' @param reward Data.frame reward generated by \code{get_Reward()}
#' @param next_week_values_l Numeric. Bellman values at step i+1.
#' @param E_max Numeric of length 1. Maximum energy that can be generated by
#' hydro storage over one step of time.
Expand All @@ -27,47 +26,50 @@
#'
#' @return a \code{data.table} like Data_week with the Bellman values
#' @keywords internal
Bellman <- function(Data_week,next_week_values_l,decision_space,E_max,P_max=0,
Bellman <- function(Data_week,next_week_values_l,reward,E_max,P_max=0,
mcyears,cvar_value=1,niveau_max,
penalty_level_low,penalty_level_high,
lvl_high,lvl_low,overflow_cost,
next_state=NULL){

# Getting all possible transitions between a state for the current week and a state for the next week
decision_space <- dplyr::select(decision_space,-c("week"))
if (!("mcYear") %in% names(decision_space)){
decision_space <- decision_space %>%
dplyr::cross_join(data.frame(mcYear=mcyears))
}
decision_space <- dplyr::select(reward,-c("timeId","reward"))

if (is.null(next_state)){
next_state = Data_week$states
}

# Get interpolation function of rewards for each possible transition for each MC year
f_reward_year <- get_reward_interpolation(Data_week)

# Build a data.table from Data_week that list for each state and each MC year, the possible transitions
df_SDP <- build_all_possible_decisions(Data_week,decision_space,
lvl_high,lvl_low,E_max,P_max,
next_week_values_l,niveau_max,overflow_cost,
next_states = next_state)

# Reward interpolation
setDT(df_SDP)
setDT(reward)
interp_reward <- reward[
, .(interp_fun = list({
ctrl <- sort(unique(control))
rew <- reward[order(control)][match(ctrl, sort(control))]
approxfun(ctrl, rew)
})),
by = mcYear
]
df_SDP <- interp_reward[df_SDP, on = .(mcYear = years)][, reward := interp_fun[[1]](control), by = mcYear][, interp_fun := NULL]

# For each transition (control), find the associated reward and for each next state,
# calculate penalties for violating rule curves. Then, find for each MC year and each state,
# the maximum sum of reward, next bellman value and penalties
df_SDP <- df_SDP %>%
dplyr::mutate(gain=mapply(function(y,x)f_reward_year[[which(y==mcyears)]](x), df_SDP$years, df_SDP$control),
penalty_low = dplyr::if_else(.data$next_state<=lvl_low,penalty_level_low*(.data$next_state-lvl_low),0),
dplyr::mutate(penalty_low = dplyr::if_else(.data$next_state<=lvl_low,penalty_level_low*(.data$next_state-lvl_low),0),
penalty_high = dplyr::if_else(.data$next_state>=lvl_high,penalty_level_high*(lvl_high-.data$next_state),0),
sum=.data$gain+.data$next_value+.data$penalty_low+.data$penalty_high) %>%
dplyr::group_by(.data$years,.data$states) %>%
sum=.data$reward+.data$next_value+.data$penalty_low+.data$penalty_high) %>%
dplyr::group_by(.data$mcYear,.data$states) %>%
dplyr::filter(.data$sum==max(.data$sum)) %>%
dplyr::slice_max(.data$next_state, with_ties = F) %>%
dplyr::select(-c("value_node","transition","transition_reward",
"next_bellman_value")) %>%
dplyr::rename("value_node"="sum","transition"="control","transition_reward"="gain",
"next_bellman_value"="next_value")
dplyr::rename("value_node"="sum","transition"="control","transition_reward"="reward",
"next_bellman_value"="next_value","years"="mcYear")

assertthat::assert_that(nrow(df_SDP)==nrow(Data_week),msg=paste0("Problem with Bellman"))

Expand Down
22 changes: 2 additions & 20 deletions R/Bellman_subfunctions.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@

#' Create approximation of reward function for each scenario, used in \code{Bellman}
#'
#' @param Data_week Data frame generated in \code{Grid_Matrix} code containing
#' reward database for each scenario (we suppose there is only one week at a time)
#'
#' @return List of \code{stats::approxfun} for each scenario
#' @keywords internal
get_reward_interpolation <- function(Data_week){

reward <- dplyr::distinct(Data_week[,c('years','reward_db')])

f_reward_year <- sapply(reward$reward_db,FUN = function(df) stats::approxfun(df$control,df$reward))

return(f_reward_year)
}


#' Create a data frame with all possible transition and associated next state for
#' each scenario and each state for one particular week, used in \code{Bellman}.
#' For each next state, Bellman value is calculated.
Expand Down Expand Up @@ -61,7 +43,6 @@ build_all_possible_decisions <- function(Data_week,decision_space,

possible_control <- Data_week %>%
dplyr::right_join(decision_space,by=c("years"="mcYear"), relationship="many-to-many") %>%
dplyr::rename("control"="u") %>%
dplyr::mutate(next_state = -.data$control+.data$states+.data$hydroStorage) %>%
rbind(possible_control)

Expand All @@ -78,7 +59,8 @@ build_all_possible_decisions <- function(Data_week,decision_space,
df_SDP <- df_SDP %>%
dplyr::filter(.data$next_state>=0, .data$next_state<=niveau_max)%>%
dplyr::mutate(next_value = f_next_value(.data$next_state) + .data$overflow*overflow_cost) %>%
dplyr::select(-c("overflow"))
dplyr::select(c("years","states","next_state","control","hydroStorage","next_value"))

return(df_SDP)
}

33 changes: 12 additions & 21 deletions R/Grid_Matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#' @param correct_concavity Binary argument (default to \code{FALSE}). \code{TRUE} to correct concavity of Bellman values.
#' @param reservoir_capacity Double. Reservoir capacity for the given area in MWh given by \code{get_reservoir_capacity()}.
#' @param inflow Generated by \code{get_inflow()}
#' @param plot_watervalues Binary.
#' @returns
#' \item{watervalues}{A \code{dplyr::tibble()} with multiple columns and detailed results.}
#' \item{aggregated_results}{A \code{dplyr::tibble()} with multiple columns and summarized results.}
Expand Down Expand Up @@ -57,7 +58,8 @@ Grid_Matrix <- function(area,
force_final_level = F,
final_level = NULL,
penalty_final_level_low = NULL,
penalty_final_level_high = NULL) {
penalty_final_level_high = NULL,
plot_watervalues = TRUE) {


area = tolower(area)
Expand Down Expand Up @@ -104,8 +106,7 @@ Grid_Matrix <- function(area,
inflow <- get_inflow(area=area, opts=opts,mcyears=mcyears)
}

decision_space = reward_db$decision_space
reward_db = reward_db$reward
reward = reward_db$reward

# Reservoir (rule curves)
{
Expand All @@ -130,24 +131,12 @@ Grid_Matrix <- function(area,
statesdt <- melt(data = statesdt, measure.vars = seq_len(ncol(states)), variable.name = "weeks", value.name = "states")
statesdt[, "weeks" := as.numeric(gsub("V", "", statesdt$weeks))] #turn weeks to numbers V1==> 1
statesdt[, "statesid" := seq_along(states), by = c("weeks")] # add id to refer to the state
}

# add states plus 1 (ie states for the following week)
{
statesplus1 <- copy(statesdt)
statesplus1[, "weeks" := statesplus1$weeks - 1]
statesplus1 <- statesplus1[, list(states_next = list(unlist(states))), by = c("weeks")]
statesplus1 <- dplyr::left_join(x = statesdt, y = statesplus1, by = c("weeks"))
watervalues <- dplyr::right_join(x = watervalues, y = statesplus1, by = c("weeks","statesid"))
watervalues <- dplyr::right_join(x = watervalues, y = statesdt, by = c("weeks","statesid"))
}

# add inflow
watervalues <- dplyr::left_join(x = watervalues, y = inflow[, list(weeks = inflow$timeId, years = inflow$tsId, hydroStorage=inflow$hydroStorage)], by = c("weeks", "years"))
#at this point water values is the table containing (weeks,year,states,statesid;states_next,hydroStorage)

#add reward
watervalues <- dplyr::nest_join(x = watervalues, y = reward_db, by = c("weeks"="timeId","years"="mcYear"))

#at this point water values is the table containing (weeks,year,states,statesid,hydroStorage)

#at this point we added the rewards for each weekly_amount

Expand Down Expand Up @@ -211,7 +200,7 @@ Grid_Matrix <- function(area,
# Bellman equation for week i
temp <- Bellman(Data_week=temp,
next_week_values_l = next_week_values,
decision_space=dplyr::filter(decision_space,week==i),
reward=dplyr::filter(reward,timeId==i),
E_max=E_max[i],
P_max=P_max[i],
mcyears = mcyears,
Expand Down Expand Up @@ -261,7 +250,8 @@ Grid_Matrix <- function(area,
}
# Calculate water values by derivating Bellman values and applying penalties on rules curves for the current week
value_nodes_dt <- build_data_watervalues(watervalues,statesdt,reservoir,penalty_high,penalty_low,
force_final_level=if(n_cycl==1){force_final_level}else{F},penalty_final_level_low,penalty_final_level_high,final_level=final_level)
force_final_level=if(n_cycl==1){force_final_level}else{F},penalty_final_level_low,penalty_final_level_high,final_level=final_level,
plot_watervalues = plot_watervalues)

}

Expand All @@ -283,7 +273,7 @@ Grid_Matrix <- function(area,

temp <- Bellman(Data_week=temp,
next_week_values_l = next_week_values,
decision_space=dplyr::filter(decision_space,week==i),
reward=dplyr::filter(reward,timeId==i),
E_max=E_max[i],
P_max=P_max[i],
mcyears = mcyears,
Expand Down Expand Up @@ -331,7 +321,8 @@ Grid_Matrix <- function(area,
}
value_nodes_dt <- build_data_watervalues(watervalues,statesdt,reservoir,penalty_high,penalty_low,
force_final_level=if(n_cycl==1){force_final_level}else{F},
penalty_final_level_low,penalty_final_level_high,final_level=final_level)
penalty_final_level_low,penalty_final_level_high,final_level=final_level,
plot_watervalues = plot_watervalues)


if(n_cycl>1){
Expand Down
Loading