Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev dynamic model #26

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
29535bf
first commit of the investment path functions
Jul 10, 2017
1e56d5c
back_up 10/07/2017, changes in costs (numeric to dataframes
Jul 10, 2017
10062a1
changed costs as data frame, and many corrections
Jul 11, 2017
2a7b05f
back_up
Jul 11, 2017
b5517fb
does run with and display invested capacities (still to be changed)
Jul 11, 2017
4d89b89
x$rentability x$digest and results correctly printed
Jul 12, 2017
ac46383
back_up!
Jul 13, 2017
21b303f
back_up, the simulation runs but still a problem with benders_sol, ou…
Jul 17, 2017
5ddddf8
It seems to be finally working :)!
Jul 18, 2017
b0decc9
new back_up, working on the html (rmd file) right now to display the …
Jul 18, 2017
13b4e57
back_up, working on html output
Jul 19, 2017
2c42953
back_up of the rmd file
Jul 19, 2017
0893a2a
display the invested capacities of the best solution
Jul 19, 2017
95448f9
added some sapply structure, working on plotting the convergence of t…
Jul 19, 2017
3125dd8
benders_tools_path is the new benders_tools taking 2 more arguments( …
Jul 20, 2017
f35c60b
changed update weekly cuts, added update_weekly_cuts_path in the ben…
Jul 21, 2017
7b5a249
back_up
Jul 21, 2017
9bf03bb
Finally the static model works with the new version. Now working on t…
Jul 21, 2017
57ce055
corrected mistake and back_up
Jul 21, 2017
bfa9043
chngaes in RMD and investment_path
Aug 1, 2017
41b8d69
This is the version of the investment_path AMPL running with the simp…
Aug 2, 2017
37584d9
commit fin de stage
Aug 25, 2017
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ Imports:
data.table,
magrittr,
plotly
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
Suggests: knitr
VignetteBuilder: knitr
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ export(filter_output_areas)
export(filter_output_links)
export(get_playlist)
export(get_whole_simulation_name)
export(investment_path)
export(read_candidates)
export(read_options)
export(read_studies)
export(read_studies_OLD)
export(report)
export(run_simulation)
export(set_first_day)
Expand Down
56 changes: 56 additions & 0 deletions R/TEST_read_studies.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
read_studies <- function(studies_file_name)
{
# read file
param_data <- scan(studies_file_name, what=character(), sep="", quiet = TRUE)

#initialisation du data frame
#search of the first line of the studies.ini file

empty_line=TRUE
first_line=1

while(empty_line&&first_line<=length(param_data)){
option_name <- strsplit(param_data[first_line], "=")[[1]][1]
if(option_name == ""){
first_line<-first_line+1
} # empty line
else{
option_value <- strsplit(param_data[first_line], "=")[[1]][2]
empty_line=FALSE
}
}
# remove white spaces in the beginning and the end
option_name <- sub("^\\s+", "", option_name)
option_name <- sub("\\s+$", "", option_name)
option_value <- sub("^\\s+", "", option_value)
option_value <- sub("\\s+$", "", option_value)

#initialisation of the data frame
studies<-data.frame(option_name,option_value)
names(studies)<-c("simulated_years","paths")


# go through every line of the file from the first non-empty line to the end
for(line in (first_line+1):length(param_data))
{
# read option and value
option_name <- strsplit(param_data[line], "=")[[1]][1]
if(option_name == ""){next} # empty line

option_value <- strsplit(param_data[line], "=")[[1]][2]

# remove white spaces in the beginning and the end
option_name <- sub("^\\s+", "", option_name)
option_name <- sub("\\s+$", "", option_name)
option_value <- sub("^\\s+", "", option_value)
option_value <- sub("\\s+$", "", option_value)

df<-data.frame(option_name,option_value)
names(df)<-c("simulated_years","paths")

studies<-rbind(df,studies)
}
return(studies)
}

TEST<-read_studies("D:/testdavid.ini")
17 changes: 10 additions & 7 deletions R/benders.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ benders <- function(path_solver, display = TRUE, report = TRUE, clean = TRUE, pa
assertthat::assert_that(file.copy(from = paste0(opts$studyPath, "/settings/generaldata.ini"),
to = paste0(opts$studyPath, "/settings/generaldata_tmpsvg.ini"),
overwrite = TRUE))

# read expansion planning options
exp_options <- read_options(opts)
option_file_name <- paste0(opts$studyPath,"/user/expansion/settings.ini")
exp_options <- read_options(option_file_name)

# read investment candidates file
candidates <- read_candidates(opts)
candidates_file_name <- paste0(opts$studyPath,"/user/expansion/candidates.ini")
candidates <- read_candidates(candidates_file_name,opts)
n_candidates <- length(candidates)
assertthat::assert_that(n_candidates > 0)

Expand Down Expand Up @@ -118,8 +120,8 @@ benders <- function(path_solver, display = TRUE, report = TRUE, clean = TRUE, pa
}
else
{ out <- c$max_invest/2}
return(out)}))

#return(out)}))
return(0)}))
row.names(x$invested_capacities) <- sapply(candidates, FUN = function(c){c$name})


Expand Down Expand Up @@ -477,9 +479,10 @@ benders <- function(path_solver, display = TRUE, report = TRUE, clean = TRUE, pa


# add information in the output file
x$expansion_options <- read_options(opts)
option_file_name_2 <- paste0(opts$studyPath,"/user/expansion/settings.ini")
x$expansion_options <- read_options(option_file_name_2)
x$study_options <- opts
x$candidates <- read_candidates(opts)
x$candidates <- read_candidates(candidates_file_name,opts)

# reset options of the ANTARES study to their initial values
assertthat::assert_that(file.remove(paste0(opts$studyPath, "/settings/generaldata.ini")))
Expand Down
245 changes: 240 additions & 5 deletions R/benders_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,26 @@ update_weekly_cuts <- function(current_it, candidates, output_area_w, output_lin

if (y != last_y || w != last_w) {script_cost <- paste0(script_cost, "\n")}


for(c in 1:n_candidates)
{
if(candidates[[c]]$has_link_profile)
{
first_h <- 7*24*(w-1)+1
last_h <- 7*24*w
first_h <- 7*24*(w-1)+1
last_h <- 7*24*w
tmp_rentability <- sum(as.numeric(subset(output_link_h, link == candidates[[c]]$link & mcYear == y & timeId >= first_h & timeId <= last_h)$"MARG. COST")* candidates[[c]]$link_profile[first_h:last_h,1]) - candidates[[c]]$cost /52
#tmp_rentability <- sum(as.numeric(subset(output_link_h, link == candidates[[c]]$link & mcYear == y & timeId >= first_h & timeId <= last_h)$"MARG. COST")* candidates[[c]]$link_profile[first_h:last_h,1])
}
else
{
tmp_rentability <- sum(as.numeric(subset(output_link_w, link == candidates[[c]]$link & mcYear == y & timeId == w)$"MARG. COST")) - candidates[[c]]$cost /52
#tmp_rentability <- sum(as.numeric(subset(output_link_w, link == candidates[[c]]$link & mcYear == y & timeId == w)$"MARG. COST"))
}

script_rentability <- paste0(script_rentability, current_it$id, " ", y , " ", w , " ", candidates[[c]]$name, " ", tmp_rentability)

if (c != n_candidates || y != last_y || w != last_w)
{
script_rentability <- paste0(script_rentability, "\n")
script_rentability <- paste0(script_rentability, "\n")
}
}
}
Expand Down Expand Up @@ -392,3 +393,237 @@ convergence <- function(best_sol, best_under, benders_options)
# is convergence of the relaxed problem reached ?
return((best_sol - best_under) <= opt_gap)
}

#' Update costs and rentability files of the weekly cuts
#' Create files in_weeklyrentability.txt and in_weeklycuts.txt which
#' will be later read by AMPL as input of the master problem
#'
#'
#' @param current_it
#' list of current iteration characteristics
#' @param candidates
#' list of investment candidates, as returned by
#' \code{\link{read_candidates}}
#' @param output_area_w
#' antaresDataList of all the areas of the study with a weekly time step
#' @param output_link_w
#' antaresDataList of all the links of the study with a weekly time step
#' @param output_link_h
#' antaresDataList of all the links of the study with a weekly time step
#' @param inv_cost
#' investments costs of this iteration
#' @param tmp_folder
#' temporary folder in which to write the files
#' @param benders_options
#' list of benders decomposition options, as returned by
#' \code{\link{read_options}}.
#'
#' @return nothing
#'
#'
update_weekly_cuts_path <- function(current_it, simulated_year, candidates, output_area_w, output_link_w, output_link_h, inv_cost, tmp_folder, benders_options)
{
#INV COST TO BE REMOVER FROM THE FUNCTION
# compute a few intermediate variables
n_candidates <- length(candidates)
last_w <- current_it$weeks[length(current_it$weeks)]
last_y <- current_it$mc_years[length(current_it$mc_years)]

# initiate scripts
script_rentability <- ""
script_cost <- ""

# for every mc years and every week
for(y in current_it$mc_years)
{
for(w in current_it$weeks)

{

if(benders_options$uc_type == "relaxed_fast")
{
# in that case, non-linear cost has to be removed because they are computed in a post-processing and are not
# part of the ANTARES optimization
w_cost <- sum(as.numeric(subset(output_area_w, mcYear == y & timeId == w)$"OV. COST")) +
sum(as.numeric(subset(output_link_w, mcYear == y & timeId == w)$"HURDLE COST")) -
sum(as.numeric(subset(output_area_w, mcYear == y & timeId == w)$"NP COST"))
}
else
{
w_cost <- sum(as.numeric(subset(output_area_w, mcYear == y & timeId == w)$"OV. COST")) +
sum(as.numeric(subset(output_link_w, mcYear == y & timeId == w)$"HURDLE COST"))
}

script_cost <- paste0(script_cost, simulated_year, " ", current_it$id, " ", y , " ", w, " ", w_cost)


if (y != last_y || w != last_w) {script_cost <- paste0(script_cost, "\n")}


for(c in 1:n_candidates)
{
if(candidates[[c]]$has_link_profile)
{
first_h <- 7*24*(w-1)+1
last_h <- 7*24*w
#tmp_rentability <- sum(as.numeric(subset(output_link_h, link == candidates[[c]]$link & mcYear == y & timeId >= first_h & timeId <= last_h)$"MARG. COST")* candidates[[c]]$link_profile[first_h:last_h,1]) - candidates[[c]]$cost /52
tmp_rentability <- sum(as.numeric(subset(output_link_h, link == candidates[[c]]$link & mcYear == y & timeId >= first_h & timeId <= last_h)$"MARG. COST")* candidates[[c]]$link_profile[first_h:last_h,1])
}
else
{
#tmp_rentability <- sum(as.numeric(subset(output_link_w, link == candidates[[c]]$link & mcYear == y & timeId == w)$"MARG. COST")) - candidates[[c]]$cost /52
tmp_rentability <- sum(as.numeric(subset(output_link_w, link == candidates[[c]]$link & mcYear == y & timeId == w)$"MARG. COST"))
}
script_rentability <- paste0(script_rentability, simulated_year, " ", current_it$id, " ", y , " ", w , " ", candidates[[c]]$name, " ", tmp_rentability)

if (c != n_candidates || y != last_y || w != last_w)
{
script_rentability <- paste0(script_rentability, "\n")
}
}
}
}
write(script_rentability, file = paste0(tmp_folder, "/in_weeklyrentability.txt"), append = TRUE )
write(script_cost, file = paste0(tmp_folder, "/in_weeklycuts.txt"), append = TRUE )
}
#' Update costs and rentability files of the average cuts
#' Create files in_avgrentability.txt and in_avgcuts.txt which
#' will be later read by AMPL as input of the master problem
#'
#'
#' @param current_it
#' list of current iteration characteristics
#' @param candidates
#' list of investment candidates, as returned by
#' \code{\link{read_candidates}}
#' @param output_link_s
#' antaresDataList of all the links of the study with a yearly time step
#' and averaged on all MC years
#' @param output_link_h_s
#' antaresDataList of all the links of the study with an hourly time step
#' and averaged on all MC years
#' @param op_cost
#' operation costs of this iteration
#' @param n_w
#' number of weeks per year
#' @param tmp_folder
#' temporary folder in which to write the files
#' @param benders_options
#' list of benders decomposition options, as returned by
#' \code{\link{read_options}}.
#'
#' @return nothing
#'

update_average_cuts_path <- function(current_it, simulated_year, candidates, output_link_s, output_link_h_s, op_cost, n_w, tmp_folder, benders_options)
{
n_candidates <- length(candidates)

#write in_avgcuts.txt file
script <- paste0(simulated_year," ", current_it$id, " ", op_cost)
write(script, file = paste0(tmp_folder, "/in_avgcuts.txt"), append = TRUE )

# write in_avgrentability.txt file
script <- ""
for (c in 1:n_candidates)
{
if(candidates[[c]]$has_link_profile)
{
len = length(subset(output_link_h_s, link == candidates[[c]]$link)$"MARG. COST")
#tmp_rentability <- sum(as.numeric(subset(output_link_h_s, link == candidates[[c]]$link)$"MARG. COST")*candidates[[c]]$link_profile[1:len, 1]) - candidates[[c]]$cost * n_w / 52
tmp_rentability <- sum(as.numeric(subset(output_link_h_s, link == candidates[[c]]$link)$"MARG. COST")*candidates[[c]]$link_profile[1:len, 1])
}
else
{
#tmp_rentability <- sum(as.numeric(subset(output_link_s, link == candidates[[c]]$link)$"MARG. COST")) - candidates[[c]]$cost * n_w / 52
tmp_rentability <- sum(as.numeric(subset(output_link_s, link == candidates[[c]]$link)$"MARG. COST"))
}
script <- paste0(script, simulated_year," ", current_it$id, " ", candidates[[c]]$name, " ", tmp_rentability)
if (c != n_candidates) { script <- paste0(script, "\n")}
}
write(script, file = paste0(tmp_folder, "/in_avgrentability.txt"), append = TRUE )
}
#' Update costs and rentability files of the yearly cuts
#' Create files in_yearlyrentability.txt and in_yearlycuts.txt which
#' will be later read by AMPL as input of the master problem
#'
#'
#' @param current_it
#' list of current iteration characteristics
#' @param candidates
#' list of investment candidates, as returned by
#' \code{\link{read_candidates}}
#' @param output_area_y
#' antaresDataList of all the areas of the study with a yearly time step
#' @param output_link_y
#' antaresDataList of all the links of the study with a yearly time step
#' @param output_link_h
#' antaresDataList of all the links of the study with an hourly time step
#' @param inv_cost
#' investments costs of this iteration
#' @param n_w
#' number of weeks per year
#' @param tmp_folder
#' temporary folder in which to write the files
#' @param benders_options
#' list of benders decomposition options, as returned by
#' \code{\link{read_options}}.
#'
#' @return nothing
#'
#'
update_yearly_cuts_path <- function(current_it, simulated_year, candidates, output_area_y,output_link_y, output_link_h, inv_cost, n_w, tmp_folder, benders_options)
{
# compute a few intermediate variables
n_candidates <- length(candidates)
last_y <- current_it$mc_years[length(current_it$mc_years)]

# initiate scripts
script_rentability <- ""
script_cost <- ""

# for every mc years and every week
for(y in current_it$mc_years)
{
if(benders_options$uc_type == "relaxed_fast")
{
# in that case, non-linear cost has to be removed because they are computed in a post-processing and are not
# part of the ANTARES optimization
y_cost <- sum(as.numeric(subset(output_area_y, mcYear == y)$"OV. COST")) +
sum(as.numeric(subset(output_link_y, mcYear == y)$"HURDLE COST")) -
sum(as.numeric(subset(output_area_y, mcYear == y)$"NP COST"))
}
else
{
y_cost <- sum(as.numeric(subset(output_area_y, mcYear == y)$"OV. COST")) +
sum(as.numeric(subset(output_link_y, mcYear == y)$"HURDLE COST"))
}


script_cost <- paste0(script_cost," ", simulated_year, " ", current_it$id, " ", y , " ",y_cost)

if (y != last_y) {script_cost <- paste0(script_cost, "\n")}

for(c in 1:n_candidates)
{
if(candidates[[c]]$has_link_profile)
{
len = length(subset(output_link_h, link == candidates[[c]]$link & mcYear == y)$"MARG. COST")
#tmp_rentability <- sum(as.numeric(subset(output_link_h, link == candidates[[c]]$link & mcYear == y)$"MARG. COST")*candidates[[c]]$link_profile[1:len, 1]) - candidates[[c]]$cost * n_w / 52
tmp_rentability <- sum(as.numeric(subset(output_link_h, link == candidates[[c]]$link & mcYear == y)$"MARG. COST")*candidates[[c]]$link_profile[1:len, 1])
}
else
{
#tmp_rentability <- sum(as.numeric(subset(output_link_y, link == candidates[[c]]$link & mcYear == y)$"MARG. COST")) - candidates[[c]]$cost * n_w / 52
tmp_rentability <- sum(as.numeric(subset(output_link_y, link == candidates[[c]]$link & mcYear == y)$"MARG. COST"))
}
script_rentability <- paste0(script_rentability, simulated_year, " ", current_it$id, " ", y, " ", candidates[[c]]$name, " ", tmp_rentability)
if (c != n_candidates || y != last_y)
{
script_rentability <- paste0(script_rentability, "\n")
}
}
}
write(script_rentability, file = paste0(tmp_folder, "/in_yearlyrentability.txt"), append = TRUE )
write(script_cost, file = paste0(tmp_folder, "/in_yearlycuts.txt"), append = TRUE )
}
Loading