@@ -237,14 +237,18 @@ get_weekly_cost <- function(district, opts=antaresRead::simOptions(),mcyears,exp
237
237
ov_cost = sum(.data $ `OV. COST` )) %> %
238
238
dplyr :: rename(" timeId" = " week" )
239
239
} else {
240
- assertthat :: assert_that(! is.null(fictive_areas ))
241
- cost <- antaresRead :: readAntares(areas = fictive_areas , mcYears = mcyears ,
242
- timeStep = " hourly" , opts = opts , select = c(" OV. COST" ))
243
- cost $ week <- (cost $ timeId - 1 )%/% 168 + 1
244
- cost <- dplyr :: summarise(dplyr :: group_by(cost ,.data $ week ,.data $ mcYear ),
245
- ov_cost = sum(.data $ `OV. COST` )) %> %
246
- dplyr :: rename(" timeId" = " week" ) %> %
247
- dplyr :: mutate(cost_xpansion = 0 )
240
+ if (is.null(fictive_areas )){
241
+ cost <- data.frame (tidyr :: expand_grid(mcYear = mcyears ,timeId = 1 : 52 ))%> %
242
+ dplyr :: mutate(ov_cost = 0 ,cost_xpansion = 0 )
243
+ } else {
244
+ cost <- antaresRead :: readAntares(areas = fictive_areas , mcYears = mcyears ,
245
+ timeStep = " hourly" , opts = opts , select = c(" OV. COST" ))
246
+ cost $ week <- (cost $ timeId - 1 )%/% 168 + 1
247
+ cost <- dplyr :: summarise(dplyr :: group_by(cost ,.data $ week ,.data $ mcYear ),
248
+ ov_cost = sum(.data $ `OV. COST` )) %> %
249
+ dplyr :: rename(" timeId" = " week" ) %> %
250
+ dplyr :: mutate(cost_xpansion = 0 )
251
+ }
248
252
249
253
for (week in 1 : 52 ){
250
254
for (scenario in mcyears ){
@@ -389,6 +393,101 @@ to_Antares_Format <- function(data,constant=T){
389
393
return (reshaped_matrix )
390
394
}
391
395
396
+ # ' Convert water values to Antares format
397
+ # '
398
+ # ' This function converts water values generated by \code{Grid_Matrix}
399
+ # ' to the format expected by Antares: a 365*101 matrix, where
400
+ # ' the rows are the 365 days of the year and the columns are round percentage values
401
+ # ' ranging from 0 to 100 assessing the reservoir level.
402
+ # ' Since \code{Grid_Matrix} output weekly values for an
403
+ # ' arbitrary number of reservoir levels, interpolation is performed on both scales
404
+ # ' in order to fit the desired format.
405
+ # '
406
+ # ' @param data A data.table generated by \code{Grid_Matrix}
407
+ # '
408
+ # ' @return A 365*101 numeric matrix
409
+ # ' @export
410
+ to_Antares_Format_bis <- function (data ){
411
+
412
+ vb <- convert_to_percent(data )
413
+
414
+ for (i in 1 : nrow(vb )){
415
+ vb [i ,4 ] <- ifelse(vb [i ,2 ]!= 0 ,2 * vb [i ,3 ]- vb [i - 1 ,4 ],0 )
416
+ }
417
+
418
+ res <- vb %> %
419
+ dplyr :: ungroup() %> %
420
+ as.data.table()
421
+
422
+
423
+ # reshape
424
+ value_nodes_matrix <- dcast(
425
+ data = res ,
426
+ formula = weeks ~ states_round_percent ,
427
+ value.var = " vu"
428
+ )
429
+
430
+ value_nodes_matrix $ weeks <- NULL
431
+
432
+ value_nodes_matrix_0 <- value_nodes_matrix [52 ,]
433
+
434
+ reshaped_matrix <- value_nodes_matrix [rep(seq_len(nrow(value_nodes_matrix )), each = 7 ), ]
435
+
436
+ reshaped_matrix <- rbind(value_nodes_matrix_0 ,reshaped_matrix )
437
+
438
+ return (reshaped_matrix )
439
+ }
440
+
441
+ convert_to_percent <- function (data ){
442
+ capa <- max(data $ states )
443
+ res <- tidyr :: expand_grid(
444
+ states_round_percent = 0 : 100 , weeks = unique(data $ weeks )
445
+ )
446
+
447
+ res <- res %> %
448
+ dplyr :: mutate(state_ref = .data $ states_round_percent * capa / 100 ) %> %
449
+ dplyr :: left_join(data ,by = c(" weeks" )) %> %
450
+ dplyr :: select(c(" weeks" ," states" ," value_node" ," state_ref" ,
451
+ " states_round_percent" ," level_low" ," level_high" ,
452
+ " penalty_low" ," penalty_high" ))
453
+
454
+ top <- res %> %
455
+ dplyr :: filter(.data $ states > = .data $ state_ref ) %> %
456
+ dplyr :: group_by(.data $ weeks ,.data $ state_ref ) %> %
457
+ dplyr :: filter(.data $ states == min(.data $ states )) %> %
458
+ dplyr :: select(c(" weeks" ," state_ref" ," states" ," value_node" ,
459
+ " states_round_percent" ," level_low" ," level_high" ,
460
+ " penalty_low" ," penalty_high" ))
461
+
462
+ bottom <- res %> %
463
+ dplyr :: filter(.data $ states < = .data $ state_ref ) %> %
464
+ dplyr :: group_by(.data $ weeks ,.data $ state_ref ) %> %
465
+ dplyr :: filter(.data $ states == max(.data $ states )) %> %
466
+ dplyr :: select(c(" weeks" ," state_ref" ," states" ," value_node" ," states_round_percent" ))
467
+
468
+ vb <- dplyr :: left_join(bottom ,top ,by = c(" weeks" ," state_ref" ," states_round_percent" ),
469
+ suffix = c(" _min" ," _max" )) %> %
470
+ dplyr :: mutate(vb = (.data $ value_node_max - .data $ value_node_min )/ (
471
+ .data $ states_max - .data $ states_min )* (.data $ state_ref - .data $ states_min )+ .data $ value_node_min ,
472
+ vb = dplyr :: if_else(.data $ states_min == .data $ states_max ,.data $ value_node_min ,.data $ vb )) %> %
473
+ dplyr :: arrange(.data $ weeks , .data $ state_ref ) %> %
474
+ dplyr :: group_by(.data $ weeks ) %> %
475
+ dplyr :: mutate(value_node_dif = .data $ vb - dplyr :: lag(.data $ vb ),# Delta of Bellman values
476
+ states_dif = .data $ state_ref - dplyr :: lag(.data $ state_ref ), # Delta of states
477
+ mu = .data $ value_node_dif / .data $ states_dif , # Ratio
478
+ mu = round(.data $ mu ,2 ),
479
+ vu = 0 ) %> %
480
+ dplyr :: mutate(level_low = .data $ level_low / capa * 100 ,
481
+ level_high = .data $ level_high / capa * 100 ) %> %
482
+ dplyr :: mutate(mu = dplyr :: case_when(.data $ states_round_percent > .data $ level_high ~ .data $ mu - .data $ penalty_high ,
483
+ .data $ states_round_percent < = .data $ level_low ~ .data $ mu + .data $ penalty_low ,
484
+ TRUE ~ .data $ mu )) %> %
485
+ dplyr :: select(c(" weeks" ," state_ref" ," mu" ," vu" ," states_round_percent" ))
486
+
487
+ return (vb )
488
+ }
489
+
490
+
392
491
# ' Add fictive production and fictive load to avoid infeasabilities with binding constraints
393
492
# '
394
493
# ' @param area A valid Antares area.
@@ -579,10 +678,11 @@ restoreScenarioBuilder <- function(opts, fictive_area){
579
678
580
679
sb_file <- antaresRead :: readIniFile(file.path(opts $ studyPath , " settings" , " scenariobuilder.dat" ))
581
680
582
- assertthat :: assert_that(names(sb_file )== c( " Default Ruleset " ) ,
583
- msg = " There should be only Default Ruleset in scenario builder." )
681
+ assertthat :: assert_that(length( names(sb_file )) == 1 ,
682
+ msg = " There should be only one ruleset in scenario builder." )
584
683
585
- sb_file $ `Default Ruleset` <- sb_file $ `Default Ruleset` [! grepl(fictive_area ,names(sb_file $ `Default Ruleset` ))]
684
+ name_ruleset <- names(sb_file )[[1 ]]
685
+ sb_file [[name_ruleset ]] <- sb_file [[name_ruleset ]][! grepl(fictive_area ,names(sb_file $ `Default Ruleset` ))]
586
686
587
687
antaresEditObject :: writeIni(listData = sb_file ,
588
688
pathIni = file.path(opts $ studyPath , " settings" , " scenariobuilder.dat" ),
0 commit comments