45 lines
3.1 KiB
R
45 lines
3.1 KiB
R
###########Series of functions to calculate the gross consumer surplus from a CIFS for each facility, in each year from 1960 to 2082.
|
|
|
|
#Function to find the net present revenue of a facility,given a discount rate, and the current year, and year the facility will close. This is the sum of discounted costs that WOULD have taken place if the facility was not built
|
|
VALUE_ADD <- function(r,CURRENT_YEAR,CLOSE_YEAR){
|
|
Years_Until_Close <- max(CLOSE_YEAR-CURRENT_YEAR+1,0)
|
|
VALUES <- (1+r)^-(1:10^4)
|
|
if(Years_Until_Close==0){return(sum(VALUES))}else if(Years_Until_Close>40){return(0)} else{return(sum(VALUES[-1:-Years_Until_Close]))}
|
|
}
|
|
#A function to extend the revenues calculations to the closure date of all of the facilities.
|
|
VALUE_ADD_SINGLE_YEAR <- function(r,CURRENT_YEAR,CLOSE_YEARS){return(sapply(CLOSE_YEARS,function(x){VALUE_ADD(r,CURRENT_YEAR,x)}))}
|
|
#A function to extend the calculation of the net present revenues of each facility to all years of interest. That is what is the NPV of building the facility in each year, for each facility.
|
|
NPV_CALC <- function(r,DATA,YEARS=1960:2083){
|
|
Facility <- pull(DATA,Facility)
|
|
RES <- cbind(Facility,do.call(cbind,lapply(YEARS,function(x){VALUE_ADD_SINGLE_YEAR(r,x,DATA$CLOSE_YEAR)})))
|
|
colnames(RES) <- c("Facility",YEARS)
|
|
RES <- as_tibble(RES) %>% pivot_longer(cols=-Facility,names_to="Year",values_to="Revenue") %>% arrange(Year) %>% mutate(Year=parse_number(Year),Revenue=parse_number(Revenue),Discount=r)
|
|
return(RES)
|
|
}
|
|
#A function which returns a list, of net present revenue calculation tables (facility by year) for a range of possible discount rates. This allows for the results to be quickly looked up, when we want to adjust the time value of money. These results combine costs savings to calculate NPV
|
|
MULTI_DISCOUNT_RATE_NPV <- function(DISCOUNT_INCREMENT,DATA,YEARS=1960:2083,DOLLARS_SAVED_PER_YEAR){
|
|
NCORES <- detectCores()-1
|
|
RES <- mclapply(DISCOUNT_INCREMENT,function(r,TABLE=DATA){NPV_CALC(r,TABLE)},mc.cores = NCORES)
|
|
RES <- do.call(rbind,RES) %>% mutate(Revenue=Revenue*DOLLARS_SAVED_PER_YEAR)
|
|
return(RES)
|
|
}
|
|
|
|
|
|
#Function to find the maximum Revenue BUT it does not take dollars just relative savings
|
|
MAX_REV <- function(YEAR,TBL,DISCOUNT,CIFS_SIZE,SHIPPING_COST=0){
|
|
TBL <- TBL %>% filter(Year==YEAR,Discount==DISCOUNT,Revenue/Total_Tons>SHIPPING_COST)
|
|
REV <- TBL %>% pull(Revenue)
|
|
VOL <- TBL %>% pull(Total_Tons)
|
|
RES <- lp(direction = "max", objective.in = REV, const.mat = matrix(VOL, nrow = 1),const.dir = "<=", const.rhs = CIFS_SIZE, all.bin = TRUE)
|
|
return(RES[[11]])
|
|
}
|
|
|
|
#Calculate all results for the start year to the end year. Discount rate, and capacities are fixed.
|
|
#DATA <- TOTAL_VALUE_METRICS;DISCOUNT<-0.05;SHIPPING_COSTS <-0
|
|
YEARLY_RESULTS <- function(DATA,DISCOUNT,CAPACITY,SHIPPING_COSTS=0){
|
|
RES <- cbind(1960:2083,sapply(1960:2083, function(x){MAX_REV(YEAR=x,TBL=DATA,DISCOUNT,CAPACITY,SHIPPING_COSTS)})) %>% as_tibble
|
|
colnames(RES) <- c("Year","Revenue")
|
|
RES <- RES %>% mutate(Discount=DISCOUNT,Capacity=CAPACITY) %>% select(Year,Discount,Capacity,Revenue) %>% mutate(Shipping_Cost_Cuttoff=SHIPPING_COSTS)
|
|
return(RES)
|
|
}
|