Closed mdakyen closed 3 years ago
Hi @mdakyen . There is little I can do without a minimal reproducible example
Btw, You can put your R code inside a Markdown code fence: ```r
```
@hongyuanjia Ok Sir. Will create time to understand how to put my codes inside a Markdown code fence. However, I am faced with a major challenge at the moment. I am trying to run an optimization that optimizes insulation thickness and fenestration properties simultaneously. The insulation aspect seems ok, but am faced with serious errors in the aspect of the fenestration. Please below is my code, am open to suggestions from you.
#load packages
library(eplusr)
library(tidyverse)
library(epluspar)
library(openxlsx)
library(ggplot2)
library(formattable)
#-------------------------------------------------------------------------------
# read combined measure case file paths
path_idf <- system.file("extdata/1D 2F insulation_window_combine_case1.idf", package = "eplusr")
#Optimization section
# Create a GA optimization Job for floor insulation
ga <- gaoptim_job(path_idf, path_epw)
#define a measure to change the floor insulation thickness
set_insulation_floor <- function (idf, insul_tick1){
# set certain fields in different classes
idf$set(.("polystyrene eps floor") := list(Thickness = formattable(insul_tick1, digits= 3)))
idf}
#define a measure to change the wall insulation thickness
set_insulation_wall <- function (idf, insul_tick2){
# set certain fields in different classes
idf$set(.("polystyrene eps wall") := list(Thickness = formattable(insul_tick2, digits= 3)))
idf}
#define a measure to change the roof insulation thickness
set_insulation_roof <- function (idf, insul_tick3){
# set certain fields in different classes
idf$set(.("polystyrene eps roof") := list(Thickness = formattable(insul_tick3, digits= 3)))
idf}
#nested functions starts
#define a measure to change the operation of window
set_type <- function (idf,win1){
win1 <- as.character(win1)
#define a measure to change the frame of window
set_frame <- function (idf,win2)
win2 <- as.character(win2)
#-------------------------------------------------------------------------------
if(win1 == "fixed window" & win2 == "wood"){
windowdata <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Fixed window Wd",
"U-Factor" = 2.706,
Solar_Heat_Gain_Coefficient =0.612,
Visible_Transmittance =0.643))
idf$add(windowdata)
configuration1 <- list(Construction = list(Name = "Exterior Window Fixed", Outside_Layer= "Fixed window Wd"))
idf$set(configuration1)
configuration2 <- list(Construction = list(Name = "Exterior Window fixed Shade",
Outside_Layer= "Fixed window Wd",
Layer_2= "MEDIUM REFLECT - MEDIUM TRANS SHADE"))
idf$set(configuration2)
windowdata01 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Dual action window Wd restrooms",
"U-Factor" = 2.706,
Solar_Heat_Gain_Coefficient =0.612,
Visible_Transmittance =0.643))
idf$add(windowdata01)
configuration01 <- list(Construction = list(Name = "Exterior Window restroom", Outside_Layer= "Dual action window Wd restrooms"))
idf$set(configuration01)
configuration02 <- list(Construction = list(Name = "Exterior Window restroom Shade",
Outside_Layer= "Dual action window Wd restrooms",
Layer_2= "MEDIUM REFLECT - MEDIUM TRANS SHADE"))
idf$set(configuration2)
}
if(win1 == "fixed window"& win2 == "vinyl"){
windowdata <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Fixed window Vy",
"U-Factor" = 2.592,
Solar_Heat_Gain_Coefficient =0.607,
Visible_Transmittance =0.643))
idf$add(windowdata)
configuration1 <- list(Construction = list(Name = "Exterior Window Fixed", Outside_Layer= "Fixed window Vy"))
idf$set(configuration1)
configuration2 <- list(Construction = list(Name = "Exterior Window fixed Shade", Outside_Layer= "Fixed window Vy",
Layer_2= "MEDIUM REFLECT - MEDIUM TRANS SHADE"))
idf$set(configuration2)
windowdata01 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Dual action window Vy restrooms",
"U-Factor" = 2.592,
Solar_Heat_Gain_Coefficient =0.607,
Visible_Transmittance =0.643))
idf$add(windowdata01)
configuration01 <- list(Construction = list(Name = "Exterior Window restroom", Outside_Layer= "Dual action window Vy restrooms"))
idf$set(configuration01)
configuration02 <- list(Construction = list(Name = "Exterior Window restroom Shade",
Outside_Layer= "Dual action window Vy restrooms",
Layer_2= "MEDIUM REFLECT - MEDIUM TRANS SHADE"))
idf$set(configuration2)
}
#-------------------------------------------------------------------------------
if(win1 == "horizontal slider window" & win2 == "wood"){
windowdata <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Horizontal slider window Wd",
"U-Factor" = 2.741,
Solar_Heat_Gain_Coefficient =0.0586,
Visible_Transmittance =0.610))
idf$add(windowdata)
configuration1 <- list(Construction = list(Name = "Exterior Window horizontal slider", Outside_Layer= "Horizontal slider window Wd"))
idf$set(configuration1)
configuration2 <- list(Construction = list(Name = "Exterior Window horizontal Shade",
Outside_Layer= "Horizontal slider window Wd",
Layer_2= "MEDIUM REFLECT - MEDIUM TRANS SHADE"))
idf$set(configuration2)
}
if(win1 == "horizontal slider window" & win2 == "vinyl"){
windowdata <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Horizontal slider window Vl",
"U-Factor" = 2.604,
Solar_Heat_Gain_Coefficient =0.580,
Visible_Transmittance =0.610))
idf$add(windowdata)
configuration1 <- list(Construction = list(Name = "Exterior Window horizontal slider", Outside_Layer= "Horizontal slider window Vl"))
idf$set(configuration1)
configuration2 <- list(Construction = list(Name = "Exterior Window horizontal Shade", Outside_Layer= "Horizontal slider window Vl",
Layer_2= "MEDIUM REFLECT - MEDIUM TRANS SHADE"))
idf$set(configuration2)
}
}
#-------------------------------------------------------------------------------
# combine all measures into one
design_options <- function (idf,floor_insulation_thickness,
wall_insulation_thickness,
roof_insulation_thickness,
window_type,window_frame){
idf <- set_insulation_floor(idf, floor_insulation_thickness)
idf <- set_insulation_wall(idf, wall_insulation_thickness)
idf <- set_insulation_roof(idf, roof_insulation_thickness)
idf <- set_type(idf, window_type )
idf <- set_frame(idf, window_frame)
idf
}
# specify design space of parameters
ga$apply_measure(design_options,floor_insulation_thickness = float_space(0,0.1),
wall_insulation_thickness = float_space(0,0.1),
roof_insulation_thickness = float_space(0,0.1),
window_type = choice_space(c("fixed window","horizontal slider window")),
window_frame = choice_space(c("wood","vinyl")))
combined_primary_energy<- function (idf) {
primary_energy_per_square_meter <- as.double((idf$last_job()$tabular_data(
report_name = "AnnualBuildingUtilityPerformanceSummary",
table_name = "Site and Source Energy",
row_name = "Total Site Energy",
column_name = "Total Energy")
$value))
#converting annually consumed energy into primary energy then diving it by
# building floor area
primary_energy_per_square_meter <- ((primary_energy_per_square_meter*2.6)/190.55)
primary_energy_per_square_meter
}
combined_global_cost <- function(idf,param){
#Local definition of consumed energy kWh
hold_consumed_energy <- as.double(idf$last_job()$tabular_data(
report_name = "AnnualBuildingUtilityPerformanceSummary",
table_name = "Site and Source Energy",
row_name = "Total Site Energy",
column_name = "Total Energy")
$value)
#Insulation manipulation y = mx+c
#cost per square meter= 145.407061x-0.295028819
#Insulation present parameters for floor
present_thickness_insulation_floor <- param$floor_insulation_thickness
cost_per_square_meter_floor = ((145.407061*present_thickness_insulation_floor)-0.295028819)
#Insulation present parameters for wall
present_thickness_insulation_wall <- param$wall_insulation_thickness
cost_per_square_meter_wall = ((145.407061*present_thickness_insulation_wall)-0.295028819)
#Insulation present parameters for roof
present_thickness_insulation_roof <- param$roof_insulation_thickness
cost_per_square_meter_roof = ((145.407061*present_thickness_insulation_roof)-0.295028819)
# Read the Discount factors excel worksheet
discount_factors <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 1)
# Read the energy price over 30 years excel worksheet
energy_price <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 2)
# Evaluate present values for energy, note inflation is included
energy <- dplyr::mutate(energy_price,
# add new variable for yearly consumed_electricity kWh
consumed_electrcity_kWh = hold_consumed_energy,
# add new variable for yearly cost value of energy (YCE)
YCE_EUR = energy_price*hold_consumed_energy)
# Read the measures from excel worksheet
data_base <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 3)
data_base_new <- dplyr::mutate(data_base,
#Initial investment
initial_investment_floor = 0.00,
initial_investment_wall = 0.00,
initial_investment_roof = 0.00,
initial_investment_window = 0.00,
initial_investment = 0.00,
#Global cost of combined measures
global_cost_floor = 0.00,
global_cost_wall = 0.00,
global_cost_floor = 0.00,
global_cost_window = 0.00,
global_cost = 0.00)
#Initial investment floor
data_base_new$initial_investment_floor = cost_per_square_meter_floor*data_base_new$floor_area
# Includes VAT (10%)
data_base_new$initial_investment_floor = data_base_new$initial_investment_floor+(data_base_new$initial_investment_floor*0.1)
#Initial investment wall
data_base_new$initial_investment_wall = cost_per_square_meter_wall*data_base_new$wall_area
# Includes VAT (10%)
data_base_new$initial_investment_wall = data_base_new$initial_investment_wall+(data_base_new$initial_investment_wall*0.1)
#Initial investment roof
data_base_new$initial_investment_roof = cost_per_square_meter_roof*data_base_new$roof_area
# Includes VAT (10%)
data_base_new$initial_investment_roof = data_base_new$initial_investment_roof+(data_base_new$initial_investment_roof*0.1)
#Initial investment windows
checker1 <- as.character(param$window_type)
if (checker1 == "fixed window"){
#average cost per square meter in Euros
cost_per_square_meter_window = 85.98
window_area = 41.3
#Initial investment
data_base_new$initial_investment_window = cost_per_square_meter_window* window_area
# Includes VAT (10%)
data_base_new$initial_investment_window = data_base_new$initial_investment_window+(data_base_new$initial_investment_window*0.1)}
if (checker1 == "horizontal slider window"){
#average cost per square meter in Euros
cost_per_square_meter_window = 120.96
window_area = 18.58
#Initial investment
data_base_new$initial_investment_window = cost_per_square_meter_window*window_area
# Includes VAT (10%)
data_base_new$initial_investment_window = data_base_new$initial_investment_window+(data_base_new$initial_investment_window*0.1)}
#summation of initial investment
data_base_new$initial_investment = data_base_new$initial_investment_floor+
data_base_new$initial_investment_wall+
data_base_new$initial_investment_roof+
data_base_new$initial_investment_window
if(checker1 == "fixed window"){
# Read the replacement cost m2 for over 30 years excel worksheet
replacement_costs <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 4)
present_values <- dplyr::mutate(energy,
# add new variable for replacement cost
replacement_cost_EUR=(replacement_costs$replacement_cost*41.3),
# add new variable for annual cost
annual_cost_EUR=((energy$YCE_EUR+replacement_cost_EUR)*discount_factors$discount_factor))
#Evaluation of the NPV for energy alone = (sum years 1 to 30)-(day 0)
NPV <- present_values$annual_cost_EUR
hold_sum <- sum(NPV [2:31])
hold_sub <- present_values$annual_cost_EUR[1]
NPV <- (hold_sum-hold_sub)
NPV
net_present_vaule <- NPV
}
if(checker1 == "horizontal slider window"){
# Read the replacement cost for over 30 years excel worksheet
replacement_costs <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 5)
present_values <- dplyr::mutate(energy,
# add new variable for replacement cost
replacement_cost_EUR=(replacement_costs$replacement_cost*18.58),
# add new variable for annual cost
annual_cost_EUR=((energy$YCE_EUR+replacement_cost_EUR)*discount_factors$discount_factor))
#Evaluation of the NPV for energy alone = (sum years 1 to 30)-(day 0)
NPV <- present_values$annual_cost_EUR
hold_sum <- sum(NPV [2:31])
hold_sub <- present_values$annual_cost_EUR[1]
NPV <- (hold_sum-hold_sub)
NPV
net_present_vaule <- NPV
}
data_base$global_cost = data_base$initial_investment+(net_present_vaule - data_base$residual_value)
#global cost aggregate
data_base$global_cost = data_base$global_cost/190.55
#return global cost
data_base$global_cost
}
# set optimization objectives
ga$objective(combined_primary_energy,combined_global_cost, .dir = "min")
# specify how to mix solutions
ga$recombinator()
# specify how to change parts of one solution randomly
ga$mutator()
# specify how to select best solutions
ga$selector()
# specify the conditions when to terminate the computation
ga$terminator(max_gen = 100L)
# run optimization
ga$run(mu = 20, p_recomb = 0.7, p_mut = 0.2)
# get all population
population <- ga$population()
# get Pareto set
pareto <- ga$pareto_set()
# plot Pareto front
p_pareto <- ggplot() +
#geom_point(aes(basecase_primary_energy,basecase_global_cost), data_base_basecase, color = "red", size = 4,alpha = 1) +
geom_point(aes(combined_primary_energy,combined_global_cost), population, color = "darkblue", alpha = 0.5) +
geom_point(aes(combined_primary_energy,combined_global_cost), pareto, position= "jitter",color = "red", size = 2,alpha = 0.5) +
geom_line(aes(combined_primary_energy,combined_global_cost), pareto, position= "jitter",color = "red", linetype = 2) +
scale_x_continuous("Primary energy kWh/yr m² ", labels = scales::number_format(scale = 1)) +
scale_y_continuous("Global cost EUR/ m² ",labels = scales::number_format(big.mark = ","))
p_pareto
Here is the error I got sir:
The error comes from here:
set_type <- function (idf,win1){
win1 <- as.character(win1)
#define a measure to change the frame of window
set_frame <- function (idf,win2)
win2 <- as.character(win2)
......
}
You are defining one function called set_type
which inside defines a function names set_frame
@hongyuanjia thanks for your suggestions. I carefully corrected my mistakes, however am still faced with an error. Below is my code and the error.
#load packages
library(eplusr)
library(tidyverse)
library(epluspar)
library(openxlsx)
library(ggplot2)
library(formattable)
#-------------------------------------------------------------------------------
# read weather file path
path_epw <- file.path(eplus_config(8.9)$dir, "WeatherData/Nicosia_hour.epw")
# read combined measure case file paths
path_idf <- system.file("extdata/1D 2F insulation_window_combine_case1.idf", package = "eplusr")
#Optimization section
# Create a GA optimization Job for floor insulation
ga <- gaoptim_job(path_idf, path_epw)
#define a measure to change the floor insulation thickness
set_insulation_floor <- function (idf, insul_tick1){
# set certain fields in different classes
idf$set(.("polystyrene eps floor") := list(Thickness = formattable(insul_tick1, digits= 3)))
idf}
#define a measure to change the wall insulation thickness
set_insulation_wall <- function (idf, insul_tick2){
# set certain fields in different classes
idf$set(.("polystyrene eps wall") := list(Thickness = formattable(insul_tick2, digits= 3)))
idf}
#define a measure to change the roof insulation thickness
set_insulation_roof <- function (idf, insul_tick3){
# set certain fields in different classes
idf$set(.("polystyrene eps roof") := list(Thickness = formattable(insul_tick3, digits= 3)))
idf}
#define a measure to change the operation of window
set_type <- function (idf,win1){
win1 <- as.character(win1)
#check for fixed window with wood frame
if(win1 == "Fixed window Wd"){
windowdata <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Fixed window Wd",
"U-Factor" = 2.706,
Solar_Heat_Gain_Coefficient =0.612,
Visible_Transmittance =0.643))
idf$add(windowdata)
idf$set(.("Exterior Window Fixed") := list(Outside_Layer= "Fixed window Wd"))
idf$set(.("Exterior Window fixed Shade") := list(Outside_Layer= "Fixed window Wd"))
windowdata1 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Dual action window Wd restrooms",
"U-Factor" = 2.706,
Solar_Heat_Gain_Coefficient =0.612,
Visible_Transmittance =0.643))
idf$add(windowdata1)
idf$set(.("Exterior Window restroom") := list(Outside_Layer= "Dual action window Wd restrooms"))
idf$set(.("Exterior Window restroom Shade") := list(Outside_Layer= "Dual action window Wd restrooms"))
}
#check for fixed window with PVC frame
if(win1 == "Fixed window Vy"){
windowdata2 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Fixed window Vy",
"U-Factor" = 2.592,
Solar_Heat_Gain_Coefficient =0.607,
Visible_Transmittance =0.643))
idf$add(windowdata2)
idf$set(.("Exterior Window Fixed") := list(Outside_Layer= "Fixed window Vy"))
idf$set(.("Exterior Window fixed Shade") := list(Outside_Layer= "Fixed window Vy"))
windowdata3 <- list(WindowMaterial_SimpleGlazingSystem := list(Name = "Dual action window Vy restrooms",
"U-Factor" = 2.592,
Solar_Heat_Gain_Coefficient =0.607,
Visible_Transmittance =0.643))
idf$add(windowdata3)
idf$set(.("Exterior Window restroom") := list(Outside_Layer= "Dual action window Vy restrooms"))
idf$set(.("Exterior Window restroom Shade") := list(Outside_Layer= "Dual action window Vy restrooms")) }
#check for horizontal slider window with wood frame
if(win1 == "Horizontal slider window Wd"){
windowdata4 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Horizontal slider window Wd",
"U-Factor" = 2.741,
Solar_Heat_Gain_Coefficient =0.0586,
Visible_Transmittance =0.610))
idf$add(windowdata4)
idf$set(.("Exterior Window horizontal slider") := list(Outside_Layer= "Horizontal slider window Wd"))
idf$set(.("Exterior Window horizontal Shade") := list(Outside_Layer= "Horizontal slider window Wd"))
windowdata5 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Dual action window Wd restrooms",
"U-Factor" = 2.706,
Solar_Heat_Gain_Coefficient =0.612,
Visible_Transmittance =0.643))
idf$add(windowdata5)
idf$set(.("Exterior Window restroom") := list(Outside_Layer= "Dual action window Wd restrooms"))
idf$set(.("Exterior Window restroom Shade") := list(Outside_Layer= "Dual action window Wd restrooms")) }
#check for horizontal slider window with PVC frame
if(win1 == "Horizontal slider window Vl"){
windowdata6 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Horizontal slider window Vl",
"U-Factor" = 2.604,
Solar_Heat_Gain_Coefficient =0.580,
Visible_Transmittance =0.610))
idf$add(windowdata6)
idf$set(.("Exterior Window horizontal slider") := list(Outside_Layer= "Horizontal slider window Vl"))
idf$set(.("Exterior Window horizontal Shade") := list(Outside_Layer= "Horizontal slider window Vl")) }
}
#-------------------------------------------------------------------------------
# combine all measures into one
design_options <- function (idf,floor_insulation_thickness,
wall_insulation_thickness,
roof_insulation_thickness,
window_type){
idf <- set_insulation_floor(idf, floor_insulation_thickness)
idf <- set_insulation_wall(idf, wall_insulation_thickness)
idf <- set_insulation_roof(idf, roof_insulation_thickness)
idf <- set_type(idf, window_type )
idf
}
# specify design space of parameters
ga$apply_measure(design_options,floor_insulation_thickness = float_space(0,0.1),
wall_insulation_thickness = float_space(0,0.1),
roof_insulation_thickness = float_space(0,0.1),
window_type = choice_space(c("Fixed window Wd",
"Fixed window Vy",
"Horizontal slider window Wd",
"Horizontal slider window Vl")))
combined_primary_energy<- function (idf) {
primary_energy_per_square_meter <- as.double((idf$last_job()$tabular_data(
report_name = "AnnualBuildingUtilityPerformanceSummary",
table_name = "Site and Source Energy",
row_name = "Total Site Energy",
column_name = "Total Energy")
$value))
#converting annually consumed energy into primary energy then diving it by
# building floor area
primary_energy_per_square_meter <- ((primary_energy_per_square_meter*2.6)/190.55)
primary_energy_per_square_meter
}
combined_global_cost <- function(idf,param){
#Local definition of consumed energy kWh
hold_consumed_energy <- as.double(idf$last_job()$tabular_data(
report_name = "AnnualBuildingUtilityPerformanceSummary",
table_name = "Site and Source Energy",
row_name = "Total Site Energy",
column_name = "Total Energy")
$value)
#Insulation manipulation y = mx+c
#cost per square meter= 145.407061x-0.295028819
#Insulation present parameters for floor
present_thickness_insulation_floor <- param$floor_insulation_thickness
cost_per_square_meter_floor = ((145.407061*present_thickness_insulation_floor)-0.295028819)
#Insulation present parameters for wall
present_thickness_insulation_wall <- param$wall_insulation_thickness
cost_per_square_meter_wall = ((145.407061*present_thickness_insulation_wall)-0.295028819)
#Insulation present parameters for roof
present_thickness_insulation_roof <- param$roof_insulation_thickness
cost_per_square_meter_roof = ((145.407061*present_thickness_insulation_roof)-0.295028819)
# Read the Discount factors excel worksheet
discount_factors <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 1)
# Read the energy price over 30 years excel worksheet
energy_price <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 2)
# Evaluate present values for energy, note inflation is included
energy <- dplyr::mutate(energy_price,
# add new variable for yearly consumed_electricity kWh
consumed_electrcity_kWh = hold_consumed_energy,
# add new variable for yearly cost value of energy (YCE)
YCE_EUR = energy_price*hold_consumed_energy)
# Read the measures from excel worksheet
data_base <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 3)
data_base_new <- dplyr::mutate(data_base,
#Initial investment
initial_investment_floor = 0.00,
initial_investment_wall = 0.00,
initial_investment_roof = 0.00,
initial_investment_window = 0.00,
initial_investment = 0.00,
#Global cost of combined measures
global_cost_floor = 0.00,
global_cost_wall = 0.00,
global_cost_floor = 0.00,
global_cost_window = 0.00,
global_cost = 0.00)
#Initial investment floor
data_base_new$initial_investment_floor = cost_per_square_meter_floor*data_base_new$floor_area
# Includes VAT (10%)
data_base_new$initial_investment_floor = data_base_new$initial_investment_floor+(data_base_new$initial_investment_floor*0.1)
#Initial investment wall
data_base_new$initial_investment_wall = cost_per_square_meter_wall*data_base_new$wall_area
# Includes VAT (10%)
data_base_new$initial_investment_wall = data_base_new$initial_investment_wall+(data_base_new$initial_investment_wall*0.1)
#Initial investment roof
data_base_new$initial_investment_roof = cost_per_square_meter_roof*data_base_new$roof_area
# Includes VAT (10%)
data_base_new$initial_investment_roof = data_base_new$initial_investment_roof+(data_base_new$initial_investment_roof*0.1)
#Initial investment windows
checker1 <- as.character(param$window_type)
if (checker1 == "Fixed window Wd"|checker1 == "Fixed window Vy"){
#average cost per square meter in Euros
cost_per_square_meter_window = 85.98
window_area = 41.3
#Initial investment
data_base_new$initial_investment_window = cost_per_square_meter_window* window_area
# Includes VAT (10%)
data_base_new$initial_investment_window = data_base_new$initial_investment_window+(data_base_new$initial_investment_window*0.1)}
if (checker1 == "Horizontal slider window Wd"|checker1 == "Horizontal slider window Vl"){
#average cost per square meter in Euros
cost_per_square_meter_window = 120.96
window_area = 18.58
#Initial investment
data_base_new$initial_investment_window = cost_per_square_meter_window*window_area
# Includes VAT (10%)
data_base_new$initial_investment_window = data_base_new$initial_investment_window+(data_base_new$initial_investment_window*0.1)}
#summation of initial investment
data_base_new$initial_investment = data_base_new$initial_investment_floor+
data_base_new$initial_investment_wall+
data_base_new$initial_investment_roof+
data_base_new$initial_investment_window
if (checker1 == "Fixed window Wd"|checker1 == "Fixed window Vy"){
# Read the replacement cost m2 for over 30 years excel worksheet
replacement_costs <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 4)
present_values <- dplyr::mutate(energy,
# add new variable for replacement cost
replacement_cost_EUR=(replacement_costs$replacement_cost*41.3),
# add new variable for annual cost
annual_cost_EUR=((energy$YCE_EUR+replacement_cost_EUR)*discount_factors$discount_factor))
#Evaluation of the NPV for energy alone = (sum years 1 to 30)-(day 0)
NPV <- present_values$annual_cost_EUR
hold_sum <- sum(NPV [2:31])
hold_sub <- present_values$annual_cost_EUR[1]
NPV <- (hold_sum-hold_sub)
NPV
net_present_vaule <- NPV
}
if (checker1 == "Horizontal slider window Wd"|checker1 == "Horizontal slider window Vl"){
# Read the replacement cost for over 30 years excel worksheet
replacement_costs <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 5)
present_values <- dplyr::mutate(energy,
# add new variable for replacement cost
replacement_cost_EUR=(replacement_costs$replacement_cost*18.58),
# add new variable for annual cost
annual_cost_EUR=((energy$YCE_EUR+replacement_cost_EUR)*discount_factors$discount_factor))
#Evaluation of the NPV for energy alone = (sum years 1 to 30)-(day 0)
NPV <- present_values$annual_cost_EUR
hold_sum <- sum(NPV [2:31])
hold_sub <- present_values$annual_cost_EUR[1]
NPV <- (hold_sum-hold_sub)
NPV
net_present_vaule <- NPV
}
data_base$global_cost = data_base$initial_investment+(net_present_vaule - data_base$residual_value)
#global cost aggregate
data_base$global_cost = data_base$global_cost/190.55
#return global cost
data_base$global_cost
}
# set optimization objectives
ga$objective(combined_primary_energy,combined_global_cost, .dir = "min")
# specify how to mix solutions
ga$recombinator()
# specify how to change parts of one solution randomly
ga$mutator()
# specify how to select best solutions
ga$selector()
# specify the conditions when to terminate the computation
ga$terminator(max_gen = 100L)
# run optimization
ga$run(mu = 20, p_recomb = 0.7, p_mut = 0.2)
# get all population
population <- ga$population()
# get Pareto set
pareto <- ga$pareto_set()
# plot Pareto front
p_pareto <- ggplot() +
#geom_point(aes(basecase_primary_energy,basecase_global_cost), data_base_basecase, color = "red", size = 4,alpha = 1) +
geom_point(aes(combined_primary_energy,combined_global_cost), population, color = "darkblue", alpha = 0.5) +
geom_point(aes(combined_primary_energy,combined_global_cost), pareto, position= "jitter",color = "red", size = 2,alpha = 0.5) +
geom_line(aes(combined_primary_energy,combined_global_cost), pareto, position= "jitter",color = "red", linetype = 2) +
scale_x_continuous("Primary energy kWh/yr m² ", labels = scales::number_format(scale = 1)) +
scale_y_continuous("Global cost EUR/ m² ",labels = scales::number_format(big.mark = ","))
p_pareto
-- Initialization --------------------------------------------------------------------------------
* Create initial population
Checking if parameter(s) has been set ...
Checking if objective(s) has been set ...
Validating parameter function 'design_options' ...
Error in idf$save(outfile, overwrite = TRUE) :
attempt to apply non-function
set_type()
did not return an Idf
object, but a list. Actually, there is no need to assign idf back because an Idf
has reference semantics.
You can change the design_options
as follow:
# combine all measures into one
design_options <- function (idf,floor_insulation_thickness,
wall_insulation_thickness,
roof_insulation_thickness,
window_type){
set_insulation_floor(idf, floor_insulation_thickness)
set_insulation_wall(idf, wall_insulation_thickness)
set_insulation_roof(idf, roof_insulation_thickness)
set_type(idf, window_type )
idf
}
@hongyuanjia thanks for your suggestion once more. However, am now faced with a different error as shown below:
#load packages
library(eplusr)
library(tidyverse)
library(epluspar)
library(openxlsx)
library(ggplot2)
library(formattable)
#-------------------------------------------------------------------------------
# read weather file path
path_epw <- file.path(eplus_config(8.9)$dir, "WeatherData/Nicosia_hour.epw")
# read combined measure case file paths
path_idf <- system.file("extdata/1D 2F insulation_window_combine_case1.idf", package = "eplusr")
#Optimization section
# Create a GA optimization Job for floor insulation
ga <- gaoptim_job(path_idf, path_epw)
#define a measure to change the floor insulation thickness
set_insulation_floor <- function (idf, insul_tick1){
# set certain fields in different classes
idf$set(.("polystyrene eps floor") := list(Thickness = formattable(insul_tick1, digits= 3)))
idf}
#define a measure to change the wall insulation thickness
set_insulation_wall <- function (idf, insul_tick2){
# set certain fields in different classes
idf$set(.("polystyrene eps wall") := list(Thickness = formattable(insul_tick2, digits= 3)))
idf}
#define a measure to change the roof insulation thickness
set_insulation_roof <- function (idf, insul_tick3){
# set certain fields in different classes
idf$set(.("polystyrene eps roof") := list(Thickness = formattable(insul_tick3, digits= 3)))
idf}
#define a measure to change the operation of window
set_type <- function (idf,win1){
win1 <- as.character(win1)
#check for fixed window with wood frame
if(win1 == "Fixed window Wd"){
windowdata <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Fixed window Wd",
"U-Factor" = 2.706,
Solar_Heat_Gain_Coefficient =0.612,
Visible_Transmittance =0.643))
idf$add(windowdata)
idf$set(.("Exterior Window Fixed") := list(Outside_Layer= "Fixed window Wd"))
idf$set(.("Exterior Window fixed Shade") := list(Outside_Layer= "Fixed window Wd"))
windowdata1 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Dual action window Wd restrooms",
"U-Factor" = 2.706,
Solar_Heat_Gain_Coefficient =0.612,
Visible_Transmittance =0.643))
idf$add(windowdata1)
idf$set(.("Exterior Window restroom") := list(Outside_Layer= "Dual action window Wd restrooms"))
idf$set(.("Exterior Window restroom Shade") := list(Outside_Layer= "Dual action window Wd restrooms"))
}
#check for fixed window with PVC frame
if(win1 == "Fixed window Vy"){
windowdata2 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Fixed window Vy",
"U-Factor" = 2.592,
Solar_Heat_Gain_Coefficient =0.607,
Visible_Transmittance =0.643))
idf$add(windowdata2)
idf$set(.("Exterior Window Fixed") := list(Outside_Layer= "Fixed window Vy"))
idf$set(.("Exterior Window fixed Shade") := list(Outside_Layer= "Fixed window Vy"))
windowdata3 <- list(WindowMaterial_SimpleGlazingSystem := list(Name = "Dual action window Vy restrooms",
"U-Factor" = 2.592,
Solar_Heat_Gain_Coefficient =0.607,
Visible_Transmittance =0.643))
idf$add(windowdata3)
idf$set(.("Exterior Window restroom") := list(Outside_Layer= "Dual action window Vy restrooms"))
idf$set(.("Exterior Window restroom Shade") := list(Outside_Layer= "Dual action window Vy restrooms")) }
#check for horizontal slider window with wood frame
if(win1 == "Horizontal slider window Wd"){
windowdata4 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Horizontal slider window Wd",
"U-Factor" = 2.741,
Solar_Heat_Gain_Coefficient =0.0586,
Visible_Transmittance =0.610))
idf$add(windowdata4)
idf$set(.("Exterior Window horizontal slider") := list(Outside_Layer= "Horizontal slider window Wd"))
idf$set(.("Exterior Window horizontal Shade") := list(Outside_Layer= "Horizontal slider window Wd"))
windowdata5 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Dual action window Wd restrooms",
"U-Factor" = 2.706,
Solar_Heat_Gain_Coefficient =0.612,
Visible_Transmittance =0.643))
idf$add(windowdata5)
idf$set(.("Exterior Window restroom") := list(Outside_Layer= "Dual action window Wd restrooms"))
idf$set(.("Exterior Window restroom Shade") := list(Outside_Layer= "Dual action window Wd restrooms")) }
#check for horizontal slider window with PVC frame
if(win1 == "Horizontal slider window Vl"){
windowdata6 <- list(WindowMaterial_SimpleGlazingSystem = list(Name = "Horizontal slider window Vl",
"U-Factor" = 2.604,
Solar_Heat_Gain_Coefficient =0.580,
Visible_Transmittance =0.610))
idf$add(windowdata6)
idf$set(.("Exterior Window horizontal slider") := list(Outside_Layer= "Horizontal slider window Vl"))
idf$set(.("Exterior Window horizontal Shade") := list(Outside_Layer= "Horizontal slider window Vl")) }
}
#-------------------------------------------------------------------------------
# combine all measures into one
design_options <- function (idf,floor_insulation_thickness,
wall_insulation_thickness,
roof_insulation_thickness,
window_type){
set_insulation_floor(idf, floor_insulation_thickness)
set_insulation_wall(idf, wall_insulation_thickness)
set_insulation_roof(idf, roof_insulation_thickness)
set_type(idf, window_type )
idf
}
# specify design space of parameters
ga$apply_measure(design_options,floor_insulation_thickness = float_space(0,0.1),
wall_insulation_thickness = float_space(0,0.1),
roof_insulation_thickness = float_space(0,0.1),
window_type = choice_space(c("Fixed window Wd",
"Fixed window Vy",
"Horizontal slider window Wd",
"Horizontal slider window Vl")))
combined_primary_energy<- function (idf) {
primary_energy_per_square_meter <- as.double((idf$last_job()$tabular_data(
report_name = "AnnualBuildingUtilityPerformanceSummary",
table_name = "Site and Source Energy",
row_name = "Total Site Energy",
column_name = "Total Energy")
$value))
#converting annually consumed energy into primary energy then diving it by
# building floor area
primary_energy_per_square_meter <- ((primary_energy_per_square_meter*2.6)/190.55)
primary_energy_per_square_meter
}
combined_global_cost <- function(idf,param){
#Local definition of consumed energy kWh
hold_consumed_energy <- as.double(idf$last_job()$tabular_data(
report_name = "AnnualBuildingUtilityPerformanceSummary",
table_name = "Site and Source Energy",
row_name = "Total Site Energy",
column_name = "Total Energy")
$value)
#Insulation manipulation y = mx+c
#cost per square meter= 145.407061x-0.295028819
#Insulation present parameters for floor
present_thickness_insulation_floor <- param$floor_insulation_thickness
cost_per_square_meter_floor = ((145.407061*present_thickness_insulation_floor)-0.295028819)
#Insulation present parameters for wall
present_thickness_insulation_wall <- param$wall_insulation_thickness
cost_per_square_meter_wall = ((145.407061*present_thickness_insulation_wall)-0.295028819)
#Insulation present parameters for roof
present_thickness_insulation_roof <- param$roof_insulation_thickness
cost_per_square_meter_roof = ((145.407061*present_thickness_insulation_roof)-0.295028819)
# Read the Discount factors excel worksheet
discount_factors <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 1)
# Read the energy price over 30 years excel worksheet
energy_price <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 2)
# Evaluate present values for energy, note inflation is included
energy <- dplyr::mutate(energy_price,
# add new variable for yearly consumed_electricity kWh
consumed_electrcity_kWh = hold_consumed_energy,
# add new variable for yearly cost value of energy (YCE)
YCE_EUR = energy_price*hold_consumed_energy)
# Read the measures from excel worksheet
data_base <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 3)
data_base_new <- dplyr::mutate(data_base,
#Initial investment
initial_investment_floor = 0.00,
initial_investment_wall = 0.00,
initial_investment_roof = 0.00,
initial_investment_window = 0.00,
initial_investment = 0.00,
#Global cost of combined measures
global_cost_floor = 0.00,
global_cost_wall = 0.00,
global_cost_floor = 0.00,
global_cost_window = 0.00,
global_cost = 0.00)
#Initial investment floor
data_base_new$initial_investment_floor = cost_per_square_meter_floor*data_base_new$floor_area
# Includes VAT (10%)
data_base_new$initial_investment_floor = data_base_new$initial_investment_floor+(data_base_new$initial_investment_floor*0.1)
#Initial investment wall
data_base_new$initial_investment_wall = cost_per_square_meter_wall*data_base_new$wall_area
# Includes VAT (10%)
data_base_new$initial_investment_wall = data_base_new$initial_investment_wall+(data_base_new$initial_investment_wall*0.1)
#Initial investment roof
data_base_new$initial_investment_roof = cost_per_square_meter_roof*data_base_new$roof_area
# Includes VAT (10%)
data_base_new$initial_investment_roof = data_base_new$initial_investment_roof+(data_base_new$initial_investment_roof*0.1)
#Initial investment windows
checker1 <- as.character(param$window_type)
if (checker1 == "Fixed window Wd"|checker1 == "Fixed window Vy"){
#average cost per square meter in Euros
cost_per_square_meter_window = 85.98
window_area = 41.3
#Initial investment
data_base_new$initial_investment_window = cost_per_square_meter_window* window_area
# Includes VAT (10%)
data_base_new$initial_investment_window = data_base_new$initial_investment_window+(data_base_new$initial_investment_window*0.1)}
if (checker1 == "Horizontal slider window Wd"|checker1 == "Horizontal slider window Vl"){
#average cost per square meter in Euros
cost_per_square_meter_window = 120.96
window_area = 18.58
#Initial investment
data_base_new$initial_investment_window = cost_per_square_meter_window*window_area
# Includes VAT (10%)
data_base_new$initial_investment_window = data_base_new$initial_investment_window+(data_base_new$initial_investment_window*0.1)}
#summation of initial investment
data_base_new$initial_investment = data_base_new$initial_investment_floor+
data_base_new$initial_investment_wall+
data_base_new$initial_investment_roof+
data_base_new$initial_investment_window
if (checker1 == "Fixed window Wd"|checker1 == "Fixed window Vy"){
# Read the replacement cost m2 for over 30 years excel worksheet
replacement_costs <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 4)
present_values <- dplyr::mutate(energy,
# add new variable for replacement cost
replacement_cost_EUR=(replacement_costs$replacement_cost*41.3),
# add new variable for annual cost
annual_cost_EUR=((energy$YCE_EUR+replacement_cost_EUR)*discount_factors$discount_factor))
#Evaluation of the NPV for energy alone = (sum years 1 to 30)-(day 0)
NPV <- present_values$annual_cost_EUR
hold_sum <- sum(NPV [2:31])
hold_sub <- present_values$annual_cost_EUR[1]
NPV <- (hold_sum-hold_sub)
NPV
net_present_vaule <- NPV
}
if (checker1 == "Horizontal slider window Wd"|checker1 == "Horizontal slider window Vl"){
# Read the replacement cost for over 30 years excel worksheet
replacement_costs <- openxlsx::read.xlsx("EPBDcostsheet08.xlsx",
sheet = 5)
present_values <- dplyr::mutate(energy,
# add new variable for replacement cost
replacement_cost_EUR=(replacement_costs$replacement_cost*18.58),
# add new variable for annual cost
annual_cost_EUR=((energy$YCE_EUR+replacement_cost_EUR)*discount_factors$discount_factor))
#Evaluation of the NPV for energy alone = (sum years 1 to 30)-(day 0)
NPV <- present_values$annual_cost_EUR
hold_sum <- sum(NPV [2:31])
hold_sub <- present_values$annual_cost_EUR[1]
NPV <- (hold_sum-hold_sub)
NPV
net_present_vaule <- NPV
}
data_base$global_cost = data_base$initial_investment+(net_present_vaule - data_base$residual_value)
#global cost aggregate
data_base$global_cost = data_base$global_cost/190.55
#return global cost
data_base$global_cost
}
# set optimization objectives
ga$objective(combined_primary_energy,combined_global_cost, .dir = "min")
# specify how to mix solutions
ga$recombinator()
# specify how to change parts of one solution randomly
ga$mutator()
# specify how to select best solutions
ga$selector()
# specify the conditions when to terminate the computation
ga$terminator(max_gen = 100L)
# run optimization
ga$run(mu = 20, p_recomb = 0.7, p_mut = 0.2)
# get all population
population <- ga$population()
# get Pareto set
pareto <- ga$pareto_set()
# plot Pareto front
p_pareto <- ggplot() +
#geom_point(aes(basecase_primary_energy,basecase_global_cost), data_base_basecase, color = "red", size = 4,alpha = 1) +
geom_point(aes(combined_primary_energy,combined_global_cost), population, color = "darkblue", alpha = 0.5) +
geom_point(aes(combined_primary_energy,combined_global_cost), pareto, position= "jitter",color = "red", size = 2,alpha = 0.5) +
geom_line(aes(combined_primary_energy,combined_global_cost), pareto, position= "jitter",color = "red", linetype = 2) +
scale_x_continuous("Primary energy kWh/yr m² ", labels = scales::number_format(scale = 1)) +
scale_y_continuous("Global cost EUR/ m² ",labels = scales::number_format(big.mark = ","))
p_pareto
-- Initialization -------------------------------------------------------------------------------
* Create initial population
Checking if parameter(s) has been set ...
Checking if objective(s) has been set ...
Validating parameter function 'design_options' ...
Validating objective function(s)...
[1] 'combined_primary_energy' --> OK
Error in `$<-.data.frame`(`*tmp*`, "global_cost", value = numeric(0)) :
replacement has 0 rows, data has 1
Looks like the error comes from your objective function combined_global_cost()
from line
data_base$global_cost = data_base$initial_investment+(net_present_vaule - data_base$residual_value)
There is no way for me to debug as you did not provide a reproducible example. Please provide one if you want me to help.
At the same time, you can learn some debug techniques from the Advanced R book
@hongyuanjia I will really appreciate your help. Please how do I provide a reproducible example in GitHub?
You can learn what is a reproducible example here. Basically it is a code snippet that can run by others using their computer without any file dependencies on your computer.
Specifically for your case, you have to share with me the idf file and other dependencies that the combined_global_cost()
depends on, e.g. those Excel spreadsheets. Please do remember to remove any confidential information.
@hongyuanjia Thanks. Considering your willingness to help, am 100% ok sharing my Idf file and Excel spreadsheets with you. Is it possible to mail them over to you directly Sir? If yes, please kindly provide your email address.
You can send them to hongyuanjia@outlook.com
@hongyuanjia Ok Sir. Done.
Thanks. The error comes from lines:
data_base$global_cost = data_base$initial_investment+(net_present_vaule - data_base$residual_value)
#global cost aggregate
data_base$global_cost = data_base$global_cost/190.55
#return global cost
data_base$global_cost
You should use data_base_new
.
I revised your code to make it more modular. See below. Please install the latest version via remotes::install_github("hongyuanjia/epluspar")
since I fixed some bugs.
# load packages
library(dplyr)
library(epluspar)
library(ggplot2)
# read weather file path
path_epw <- "Nicosia_hour.epw"
# read combined measure case file paths
path_idf <- "1D 2F insulation_window_combine_case1.idf"
# create an IDF with all glazing systems
glzsys <- eplusr::empty_idf(8.9)
glzsys$add(
WindowMaterial_SimpleGlazingSystem = list(
Name = "Fixed window Wd",
U_Factor = 2.706,
Solar_Heat_Gain_Coefficient = 0.612,
Visible_Transmittance = 0.643
),
WindowMaterial_SimpleGlazingSystem = list(
Name = "Fixed window Vy",
U_Factor = 2.592,
Solar_Heat_Gain_Coefficient = 0.607,
Visible_Transmittance = 0.643
),
WindowMaterial_SimpleGlazingSystem = list(
Name = "Horizontal slider window Wd",
U_Factor = 2.741,
Solar_Heat_Gain_Coefficient = 0.0586,
Visible_Transmittance = 0.610
),
WindowMaterial_SimpleGlazingSystem = list(
Name = "Horizontal slider window Vl",
U_Factor = 2.604,
Solar_Heat_Gain_Coefficient = 0.580,
Visible_Transmittance = 0.610
),
WindowMaterial_SimpleGlazingSystem = list(
Name = "Dual action window Wd restrooms",
U_Factor = 2.706,
Solar_Heat_Gain_Coefficient = 0.612,
Visible_Transmittance = 0.643
),
WindowMaterial_SimpleGlazingSystem = list(
Name = "Dual action window Vy restrooms",
U_Factor = 2.592,
Solar_Heat_Gain_Coefficient = 0.607,
Visible_Transmittance = 0.643
),
WindowMaterial_SimpleGlazingSystem = list(
Name = "Dual action window Al restrooms",
U_Factor = 3.66,
Solar_Heat_Gain_Coefficient = 0.617,
Visible_Transmittance = 0.60
)
)
glzsys$save("glazing_system.idf", overwrite = TRUE)
# Optimization section
# Create a GA optimization Job for floor insulation
ga <- gaoptim_job(path_idf, path_epw)
# define a measure to change the floor insulation thickness
set_insulation_floor <- function(idf, insul_tick1) {
# set certain fields in different classes
idf$set("polystyrene eps floor" = list(Thickness = round(insul_tick1, digits = 3)))
}
# define a measure to change the wall insulation thickness
set_insulation_wall <- function(idf, insul_tick2) {
# set certain fields in different classes
idf$set("polystyrene eps wall" = list(Thickness = round(insul_tick2, digits = 3)))
}
# define a measure to change the roof insulation thickness
set_insulation_roof <- function(idf, insul_tick3) {
# set certain fields in different classes
idf$set("polystyrene eps roof" = list(Thickness = round(insul_tick3, digits = 3)))
}
# define a measure to change the operation of window
set_type <- function(idf, win1) {
glzsys <- eplusr::read_idf("glazing_system.idf")
win2 <- switch(win1,
"Fixed window Wd" = "Dual action window Wd restrooms",
"Fixed window Vy" = "Dual action window Vy restrooms",
"Horizontal slider window Wd" = "Dual action window Wd restrooms",
"Horizontal slider window Vl" = "Dual action window Al restrooms",
stop("Invalid window name found: ", sQuote(win1))
)
idf$insert(glzsys$objects(c(win1, win2)))
# get corresponding construction name per window
if (grepl("fixed", win1, ignore.case = TRUE)) {
const1_win1 <- "Exterior Window Fixed"
const2_win1 <- "Exterior Window fixed Shade"
} else if (grepl("horizontal", win1, ignore.case = TRUE)) {
const1_win1 <- "Exterior Window horizontal slider"
const2_win1 <- "Exterior Window horizontal Shade"
} else {
stop("Invalid window name found:", sQuote(win1))
}
# update construction
idf$set(
.(const1_win1) := list(Outside_Layer = win1),
.(const2_win1) := list(Outside_Layer = win1),
"Exterior Window restroom" = list(Outside_Layer = win2),
"Exterior Window restroom Shade" = list(Outside_Layer = win2)
)
}
#-------------------------------------------------------------------------------
# combine all measures into one
design_options <- function(idf,
floor_insulation_thickness,
wall_insulation_thickness,
roof_insulation_thickness,
window_type) {
set_insulation_floor(idf, floor_insulation_thickness)
set_insulation_wall(idf, wall_insulation_thickness)
set_insulation_roof(idf, roof_insulation_thickness)
set_type(idf, window_type)
idf
}
# specify design space of parameters
ga$apply_measure(design_options,
floor_insulation_thickness = float_space(1E-5, 0.1),
wall_insulation_thickness = float_space(1E-5, 0.1),
roof_insulation_thickness = float_space(1E-5, 0.1),
window_type = choice_space(c(
"Fixed window Wd", "Fixed window Vy",
"Horizontal slider window Wd", "Horizontal slider window Vl"
))
)
combined_primary_energy <- function(idf) {
primary_energy_per_square_meter <- as.double(idf$last_job()$tabular_data(
report_name = "AnnualBuildingUtilityPerformanceSummary",
table_name = "Site and Source Energy",
row_name = "Total Site Energy",
column_name = "Total Energy"
)
$value)
# converting annually consumed energy into primary energy then diving it by
# building floor area
primary_energy_per_square_meter * 2.6 / 190.55
}
# calculate the initial cost and add it to the "data base"
add_init_cost_to_db <- function(idf, param) {
# get thickness of floor, wall and roof
types <- c("floor", "wall", "roof")
thickness <- param[glue::glue("{types}_insulation_thickness")]
data_base <- as.list(readxl::read_excel("EPBDcostsheet08.xlsx", sheet = 3))
# get area of floor, wall and roof
area <- data_base[glue::glue("{types}_area")]
# insulation manipulation y = mx+c
# cost per square meter= 145.407061x-0.295028819
cost_per_m2 <- function(x) 145.407061 * x - 0.295028819
# calculate sum of initial cost of floor, wall and roof
initial_investment_envelope <- purrr::map2_dbl(thickness, area, ~cost_per_m2(.x) * .y * 1.1)
# calculate initial cost of windows
if (param$window_type %in% c("Fixed window Wd", "Fixed window Vy")) {
# average cost per square meter in Euros
cost_per_square_meter_window <- 85.98
window_area <- 41.3
} else if (param$window_type %in% c("Horizontal slider window Wd", "Horizontal slider window Vl")) {
# average cost per square meter in Euros
cost_per_square_meter_window <- 120.96
window_area <- 18.58
}
initial_investment_window <- cost_per_square_meter_window * window_area * 1.1
# summation of initial investment
initial_investment <- sum(initial_investment_envelope, initial_investment_window)
# add initial investment result to the data base
data_base$initial_investment <- initial_investment
# return the data base
data_base
}
# calculate the operation energy cost
cal_energy_cost <- function(idf, param) {
# Local definition of consumed energy kWh
hold_consumed_energy <- as.double(idf$last_job()$tabular_data(
report_name = "AnnualBuildingUtilityPerformanceSummary",
table_name = "Site and Source Energy",
row_name = "Total Site Energy",
column_name = "Total Energy"
)
$value)
# Read the energy price over 30 years excel worksheet
energy_price <- readxl::read_excel("EPBDcostsheet08.xlsx", sheet = 2)
# Evaluate present values for energy, note inflation is included
dplyr::mutate(energy_price,
# add new variable for yearly consumed_electricity kWh
consumed_electrcity_kWh = hold_consumed_energy,
# add new variable for yearly cost value of energy (YCE)
YCE_EUR = energy_price * hold_consumed_energy
)
}
# calcuate the net present value
cal_net_present_value <- function(idf, param) {
energy <- cal_energy_cost(idf, param)
# Read the Discount factors excel worksheet
discount_factors <- readxl::read_excel("EPBDcostsheet08.xlsx", sheet = 1)
# Read the replacement cost m2 for over 30 years excel worksheet
replacement_costs <- readxl::read_excel("EPBDcostsheet08.xlsx", sheet = 4)
if (param$window_type %in% c("Fixed window Wd", "Fixed window Vy")) {
window_area <- 41.3
} else if (param$window_type %in% c("Horizontal slider window Wd", "Horizontal slider window Vl")) {
window_area <- 18.58
}
present_values <- dplyr::left_join(energy, replacement_costs, c("years" = "year")) %>%
dplyr::left_join(discount_factors, "years") %>%
dplyr::mutate(
# add new variable for replacement cost
replacement_cost_EUR = replacement_cost * window_area,
# add new variable for annual cost
annual_cost_EUR = (YCE_EUR + replacement_cost_EUR) * discount_factors$discount_factor
)
# Evaluation of the NPV for energy alone = (sum years 1 to 30)-(day 0)
NPV <- present_values$annual_cost_EUR
hold_sum <- sum(NPV[2:31])
hold_sub <- present_values$annual_cost_EUR[1]
hold_sum - hold_sub
}
combined_global_cost <- function(idf, param) {
data_base <- add_init_cost_to_db(idf, param)
net_present_vaule <- cal_net_present_value(idf, param)
global_cost <- data_base$initial_investment + (net_present_vaule - data_base$residual_value)
# global cost aggregate
global_cost <- global_cost / 190.55
# return global cost
global_cost
}
# set optimization objectives
ga$objective(combined_primary_energy, combined_global_cost, .dir = "min")
# specify how to mix solutions
ga$recombinator()
# specify how to change parts of one solution randomly
ga$mutator()
# specify how to select best solutions
ga$selector()
# specify the conditions when to terminate the computation
ga$terminator(max_gen = 100L)
# run optimization
ga$run(mu = 20, p_recomb = 0.7, p_mut = 0.2)
# get all population
population <- ga$population()
# get Pareto set
pareto <- ga$pareto_set()
# plot Pareto front
p_pareto <- ggplot() +
# geom_point(aes(basecase_primary_energy,basecase_global_cost), data_base_basecase, color = "red", size = 4,alpha = 1) +
geom_point(aes(combined_primary_energy, combined_global_cost), population, color = "darkblue", alpha = 0.5) +
geom_point(aes(combined_primary_energy, combined_global_cost), pareto, position = "jitter", color = "red", size = 2, alpha = 0.5) +
geom_line(aes(combined_primary_energy, combined_global_cost), pareto, position = "jitter", color = "red", linetype = 2) +
scale_x_continuous("Primary energy kWh/yr m² ", labels = scales::number_format(scale = 1)) +
scale_y_continuous("Global cost EUR/ m² ", labels = scales::number_format(big.mark = ","))
@hongyuanjia Thank you so much, Sir. I will implement the changes on my own end, then I will let you know.
@hongyuanjia, I carefully adopted your suggestion, by updating my epluspar version. However, when I tried to run the codes (as modified by you), I encountered some warnings. Below are the executed codes displayed in R-Console, with the warnings ( not inside a Markdown code fence).
> # load packages
> library(dplyr)
> library(epluspar)
> library(ggplot2)
> # read weather file path
> path_epw <- "Nicosia_hour.epw"
> # read combined measure case file paths
> path_idf <- "1D 2F insulation_window_combine_case1.idf"
> # create an IDF with all glazing systems
> glzsys <- eplusr::empty_idf(8.9)
> glzsys$add(
+ WindowMaterial_SimpleGlazingSystem = list(
+ Name = "Fixed window Wd",
+ U_Factor = 2.706,
+ Solar_Heat_Gain_Coefficient = 0.612,
+ Visible_Transmittance = 0.643
+ ),
+ WindowMaterial_SimpleGlazingSystem = list(
+ Name = "Fixed window Vy",
+ U_Factor = 2.592,
+ Solar_Heat_Gain_Coefficient = 0.607,
+ Visible_Transmittance = 0.643
+ ),
+ WindowMaterial_SimpleGlazingSystem = list(
+ Name = "Horizontal slider window Wd",
+ U_Factor = 2.741,
+ Solar_Heat_Gain_Coefficient = 0.0586,
+ Visible_Transmittance = 0.610
+ ),
+ WindowMaterial_SimpleGlazingSystem = list(
+ Name = "Horizontal slider window Vl",
+ U_Factor = 2.604,
+ Solar_Heat_Gain_Coefficient = 0.580,
+ Visible_Transmittance = 0.610
+ ),
+ WindowMaterial_SimpleGlazingSystem = list(
+ Name = "Dual action window Wd restrooms",
+ U_Factor = 2.706,
+ Solar_Heat_Gain_Coefficient = 0.612,
+ Visible_Transmittance = 0.643
+ ),
+ WindowMaterial_SimpleGlazingSystem = list(
+ Name = "Dual action window Vy restrooms",
+ U_Factor = 2.592,
+ Solar_Heat_Gain_Coefficient = 0.607,
+ Visible_Transmittance = 0.643
+ ),
+ WindowMaterial_SimpleGlazingSystem = list(
+ Name = "Dual action window Al restrooms",
+ U_Factor = 3.66,
+ Solar_Heat_Gain_Coefficient = 0.617,
+ Visible_Transmittance = 0.60
+ )
+ )
$`Fixed window Wd`
<IdfObject: 'WindowMaterial:SimpleGlazingSystem'> [ID:2] `Fixed window Wd`
Class: <WindowMaterial:SimpleGlazingSystem>
+- 1*: "Fixed window Wd", !- Name
|- 2*: 2.706, !- U-Factor {W/m2-K}
|- 3*: 0.612, !- Solar Heat Gain Coefficient
\- 4 : 0.643; !- Visible Transmittance
$`Fixed window Vy`
<IdfObject: 'WindowMaterial:SimpleGlazingSystem'> [ID:3] `Fixed window Vy`
Class: <WindowMaterial:SimpleGlazingSystem>
+- 1*: "Fixed window Vy", !- Name
|- 2*: 2.592, !- U-Factor {W/m2-K}
|- 3*: 0.607, !- Solar Heat Gain Coefficient
\- 4 : 0.643; !- Visible Transmittance
$`Horizontal slider window Wd`
<IdfObject: 'WindowMaterial:SimpleGlazingSystem'> [ID:4] `Horizontal slider window Wd`
Class: <WindowMaterial:SimpleGlazingSystem>
+- 1*: "Horizontal slider window Wd", !- Name
|- 2*: 2.741, !- U-Factor {W/m2-K}
|- 3*: 0.0586, !- Solar Heat Gain Coefficient
\- 4 : 0.61; !- Visible Transmittance
$`Horizontal slider window Vl`
<IdfObject: 'WindowMaterial:SimpleGlazingSystem'> [ID:5] `Horizontal slider window Vl`
Class: <WindowMaterial:SimpleGlazingSystem>
+- 1*: "Horizontal slider window Vl", !- Name
|- 2*: 2.604, !- U-Factor {W/m2-K}
|- 3*: 0.58, !- Solar Heat Gain Coefficient
\- 4 : 0.61; !- Visible Transmittance
$`Dual action window Wd restrooms`
<IdfObject: 'WindowMaterial:SimpleGlazingSystem'> [ID:6] `Dual action window Wd restrooms`
Class: <WindowMaterial:SimpleGlazingSystem>
+- 1*: "Dual action window Wd restrooms", !- Name
|- 2*: 2.706, !- U-Factor {W/m2-K}
|- 3*: 0.612, !- Solar Heat Gain Coefficient
\- 4 : 0.643; !- Visible Transmittance
$`Dual action window Vy restrooms`
<IdfObject: 'WindowMaterial:SimpleGlazingSystem'> [ID:7] `Dual action window Vy restrooms`
Class: <WindowMaterial:SimpleGlazingSystem>
+- 1*: "Dual action window Vy restrooms", !- Name
|- 2*: 2.592, !- U-Factor {W/m2-K}
|- 3*: 0.607, !- Solar Heat Gain Coefficient
\- 4 : 0.643; !- Visible Transmittance
$`Dual action window Al restrooms`
<IdfObject: 'WindowMaterial:SimpleGlazingSystem'> [ID:8] `Dual action window Al restrooms`
Class: <WindowMaterial:SimpleGlazingSystem>
+- 1*: "Dual action window Al restrooms", !- Name
|- 2*: 3.66, !- U-Factor {W/m2-K}
|- 3*: 0.617, !- Solar Heat Gain Coefficient
\- 4 : 0.6; !- Visible Transmittance
> glzsys$save("glazing_system.idf", overwrite = TRUE)
Replace the existing IDF located at C:\Users\Toshiba\Documents\Always on Dr Murat + My pespective 2\glazing_system.idf.
> # Optimization section
> # Create a GA optimization Job for floor insulation
> ga <- gaoptim_job(path_idf, path_epw)
Taking input= as a system command ('1D 2F insulation_window_combine_case1.idf') and a variable has been used in the expression passed to input=
. Please use fread(cmd=...). There is a security concern if you are creating an app, and the app could have a malicious user, and the app is not running in a secure environment; e.g. the app is running as root. Please read item 5 in the NEWS file for v1.11.6 for more information and for the option to suppress this message.
'1D' is not recognized as an internal or external command,
operable program or batch file.
Error in !nrow(dt) : invalid argument type
In addition: Warning message:
In (if (.Platform$OS.type == "unix") system else shell)(paste0("(", :
'(1D 2F insulation_window_combine_case1.idf) > C:\Users\Toshiba\AppData\Local\Temp\Rtmpumn4Q6\file1f1017ad2c2a' execution failed with error code 1
Change path_idf
andpath_epw
to where your IDF and EPW is.
You probably should learn some basic R knowledge. This platform is mainly used for reporting software bugs related to epluspar.
Close now.
@hongyuanjia Thanks for your assistance. Will be in touch if I encounter any bug.
@hongyuanjia Please am trying to simultaneously consider the following:
# load packages
library(dplyr)
library(epluspar)
library(ggplot2)
path_epw <- "Nicosia_hour.epw"
path_idf <- "1D 2F insulation_window_combine_case2.idf"
glzsys <- eplusr::empty_idf(8.9) glzsys$add( WindowMaterial_SimpleGlazingSystem = list( Name = "Fixed window Al", U_Factor = 3.282, Solar_Heat_Gain_Coefficient = 0.659, Visible_Transmittance = 0.671 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Fixed window Wd", U_Factor = 2.706, Solar_Heat_Gain_Coefficient = 0.612, Visible_Transmittance = 0.643 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Fixed window Vy", U_Factor = 2.592, Solar_Heat_Gain_Coefficient = 0.607, Visible_Transmittance = 0.643 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Horizontal slider window Al", U_Factor = 3.441, Solar_Heat_Gain_Coefficient = 0.642, Visible_Transmittance = 0.643 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Horizontal slider window Wd", U_Factor = 2.741, Solar_Heat_Gain_Coefficient = 0.586, Visible_Transmittance = 0.610 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Horizontal slider window Vy", U_Factor = 2.604, Solar_Heat_Gain_Coefficient = 0.580, Visible_Transmittance = 0.610 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Casement window Al", U_Factor = 3.466, Solar_Heat_Gain_Coefficient = 0.637, Visible_Transmittance = 0.635 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Casement window Wd", U_Factor = 2.734, Solar_Heat_Gain_Coefficient = 0.579, Visible_Transmittance = 0.601 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Casement window Vy", U_Factor = 2.59, Solar_Heat_Gain_Coefficient = 0.579, Visible_Transmittance = 0.601 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Vertical slider window Al", U_Factor = 3.424, Solar_Heat_Gain_Coefficient = 0.642, Visible_Transmittance = 0.643 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Vertical slider window Wd", U_Factor = 2.726, Solar_Heat_Gain_Coefficient = 0.586, Visible_Transmittance = 0.610 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Vertical slider window Vy", U_Factor = 2.589, Solar_Heat_Gain_Coefficient = 0.58, Visible_Transmittance = 0.61 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Dual action window Al", U_Factor = 3.66, Solar_Heat_Gain_Coefficient = 0.617, Visible_Transmittance = 0.6 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Dual action window Wd", U_Factor = 2.769, Solar_Heat_Gain_Coefficient = 0.546, Visible_Transmittance = 0.558 ), WindowMaterial_SimpleGlazingSystem = list( Name = "Dual action window Vy", U_Factor = 2.595, Solar_Heat_Gain_Coefficient = 0.539, Visible_Transmittance = 0.558 ) )
glzsys$save("glazing_system.idf", overwrite = TRUE)
ga <- gaoptim_job(path_idf, path_epw)
set_insulation_floor <- function(idf, insul_tick1) {
idf$set("polystyrene eps floor" = list(Thickness = round(insul_tick1, digits = 3))) }
set_insulation_wall <- function(idf, insul_tick2) {
idf$set("polystyrene eps wall" = list(Thickness = round(insul_tick2, digits = 3))) }
set_insulation_roof <- function(idf, insul_tick3) {
idf$set("polystyrene eps roof" = list(Thickness = round(insul_tick3, digits = 3))) }
set_type <- function(idf, win1) { glzsys <- eplusr::read_idf("glazing_system.idf")
if (grepl("Al", win1, ignore.case = TRUE)) { win2 <- "Fixed window Al" win3 <- "Horizontal slider window Al" win4 <- "Casement window Al" win5 <- "Vertical slider window Al" win6 <- "Dual action window Al" } else if (grepl("Wd", win1, ignore.case = TRUE)) { win2 <- "Fixed window Wd" win3 <- "Horizontal slider window Wd" win4 <- "Casement window Wd" win5 <- "Vertical slider window Wd" win6 <- "Dual action window Wd" }else if (grepl("Vy", win1, ignore.case = TRUE)) { win2 <- "Fixed window Vy" win3 <- "Horizontal slider window Vy" win4 <- "Casement window Vy" win5 <- "Vertical slider window Vy" win6 <- "Dual action window Vy" }else { stop("Invalid window name found:", sQuote(win1)) }
idf$insert(glzsys$objects(c(win2, win3, win4, win5, win6)))
c(win2, win3, win4, win5, win6)
}
set_fixed_selector <- function(idf, param) { fix_hold <- set_type(idf, param)
# get corresponding construction name per window
const1_win2 <- "Exterior Window Fixed"
const2_win2 <- "Exterior Window fixed Shade"
if (grepl("Fixed", fix_hold, ignore.case = TRUE)){ idf$set( .(const1_win2) := list(Outside_Layer = win2), .(const2_win2) := list(Outside_Layer = win2) )} else if (grepl("Dual-action", fix_hold, ignore.case = TRUE)){ idf$set( .(const1_win2) := list(Outside_Layer = win6), .(const2_win2) := list(Outside_Layer = win6) )} }
set_horizontal_selector<-function(idf, param){ hor_hold <- set_type(idf, param)
# get corresponding construction name per window
const1_win3 <- "Exterior Window horizontal slider"
const2_win3 <- "Exterior Window horizontal Shade"
if (grepl("Horizontal slider", hor_hold, ignore.case = TRUE)){ idf$set( .(const1_win3) := list(Outside_Layer = win3), .(const2_win3) := list(Outside_Layer = win3) )}else if (grepl("Casement",hor_hold, ignore.case = TRUE)){ idf$set( .(const1_win3) := list(Outside_Layer = win4), .(const2_win3) := list(Outside_Layer = win4) )}else if (grepl("Dual-action", hor_hold, ignore.case = TRUE)){ idf$set( .(const1_win3) := list(Outside_Layer = win6), .(const2_win3) := list(Outside_Layer = win6) )}else if (grepl("Vertical silder", hor_hold, ignore.case = TRUE)){ idf$set( .(const1_win3) := list(Outside_Layer = win5), .(const2_win3) := list(Outside_Layer = win5) )} }
set_horizontal_selector2 <- function(idf,param){ hor_hold2 <- set_type(idf, param)
# get corresponding construction name per window
const1_win4 <- "Exterior Window horizontal slider with three options only"
const2_win4 <- "Exterior Window horizontal slider with three options only Shade"
if (grepl("Horizontal slider",hor_hold2, ignore.case = TRUE)){ idf$set( .(const1_win4) := list(Outside_Layer = win3), .(const2_win4) := list(Outside_Layer = win3) )}else if (grepl("Casement",hor_hold2, ignore.case = TRUE)){ idf$set( .(const1_win4) := list(Outside_Layer = win4), .(const2_win4) := list(Outside_Layer = win4) )}else if (grepl("Dual-action",hor_hold2, ignore.case = TRUE)){ idf$set( .(const1_win4) := list(Outside_Layer = win6), .(const2_win4) := list(Outside_Layer = win6) )} idf$set( "Exterior Window restroom" = list(Outside_Layer = win6), "Exterior Window restroom Shade" = list(Outside_Layer = win6) ) }
design_options <- function(idf, floor_insulation_thickness, wall_insulation_thickness, roof_insulation_thickness, window_frame, fixed_selector, horizontal_selector, horizontal_selector2) { set_insulation_floor(idf, floor_insulation_thickness) set_insulation_wall(idf, wall_insulation_thickness) set_insulation_roof(idf, roof_insulation_thickness) set_type(idf, window_frame) set_fixed_selector(idf, fixed_selector) set_horizontal_selector(idf, horizontal_selector) set_horizontal_selector2(idf, horizontal_selector2)
idf }
ga$apply_measure(design_options, floor_insulation_thickness = float_space(1E-5, 0.1), wall_insulation_thickness = float_space(1E-5, 0.1), roof_insulation_thickness = float_space(1E-5, 0.1), window_frame = choice_space(c("Al", "Wd", "Vy")), fixed_selector= choice_space(c("Fixed","Dual-action")), horizontal_selector= choice_space(c("Horizontal slider", "Casement", "Dual-action", "Vertical silder")), horizontal_selector2= choice_space(c("Horizontal slider", "Casement", "Dual-action")) )
combined_primary_energy <- function(idf) { primary_energy_per_square_meter <- as.double(idf$last_job()$tabular_data( report_name = "AnnualBuildingUtilityPerformanceSummary", table_name = "Site and Source Energy", row_name = "Total Site Energy", column_name = "Total Energy" ) $value)
primary_energy_per_square_meter * 2.6 / 190.55 }
add_init_cost_to_db <- function(idf, param) {
types <- c("floor", "wall", "roof")
thickness <- param[glue::glue("{types}_insulation_thickness")]
data_base <- as.list(readxl::read_excel("EPBDcostsheet08.xlsx", sheet = 3))
area <- data_base[glue::glue("{types}_area")]
cost_per_m2 <- function(x) 157.4992667 * x -0.449997905
initial_investment_envelope <- purrr::map2_dbl(thickness, area, ~cost_per_m2(.x) .y 1.1)
if (param$fixed_selector %in% c("Fixed")){
cost_per_square_meter_window1 <- 91.87
window_area1 <- 37.06
}else if (param$fixed_selector %in% c("Dual-action")) {
cost_per_square_meter_window2 <- 197.25
window_area2 <- 37.06
} if (param$horizontal_selector %in% c("Horizontal slider")) {
cost_per_square_meter_window3 <- 129.37
window_area3 <- 18.58
}else if (param$horizontal_selector %in% c("Casement")) {
cost_per_square_meter_window4 <- 160.87
window_area4 <- 18.58
}else if (param$horizontal_selector %in% c("Dual-action")) {
cost_per_square_meter_window5 <- 197.25
window_area5 <- 18.58
}else if (param$horizontal_selector %in% c("Vertical silder")) {
cost_per_square_meter_window6 <- 129.37
window_area6 <- 18.58
} if (param$horizontal_selector2 %in% c("Horizontal slider")) {
cost_per_square_meter_window7 <- 129.37
window_area7 <- 23.15
}else if (param$horizontal_selector2 %in% c("Casement")) {
cost_per_square_meter_window8 <- 160.87
window_area8 <- 23.15
}else if (param$horizontal_selector2 %in% c("Dual-action")) {
cost_per_square_meter_window9 <- 197.25
window_area9 <- 23.15
} total_cost_per_square_meter_window <- sum(cost_per_square_meter_window1, cost_per_square_meter_window2, cost_per_square_meter_window3, cost_per_square_meter_window4, cost_per_square_meter_window5, cost_per_square_meter_window6, cost_per_square_meter_window7, cost_per_square_meter_window8, cost_per_square_meter_window9)
total_window_area <- sum(window_area1, window_area2, window_area3, window_area4, window_area5, window_area6, window_area7, window_area8, window_area9)
initial_investment_window <- total_cost_per_square_meter_window total_window_area 1.1
initial_investment <- sum(initial_investment_envelope, initial_investment_window)
data_base$initial_investment <- initial_investment
data_base }
cal_energy_cost <- function(idf, param) {
hold_consumed_energy <- as.double(idf$last_job()$tabular_data( report_name = "AnnualBuildingUtilityPerformanceSummary", table_name = "Site and Source Energy", row_name = "Total Site Energy", column_name = "Total Energy" ) $value)
energy_price <- readxl::read_excel("EPBDcostsheet08.xlsx", sheet = 2)
dplyr::mutate(energy_price,
consumed_electrcity_kWh = hold_consumed_energy,
# add new variable for yearly cost value of energy (YCE)
YCE_EUR = energy_price * hold_consumed_energy
) }
cal_net_present_value <- function(idf, param) { energy <- cal_energy_cost(idf, param)
discount_factors <- readxl::read_excel("EPBDcostsheet08.xlsx", sheet = 1)
replacement_costs <- readxl::read_excel("EPBDcostsheet08.xlsx", sheet = 4)
if (param$fixed_selector %in% c("Fixed")) { window_area <- 37.06 }else if (param$fixed_selector %in% c("Dual-action")) { window_area2 <- 37.06 }
present_values <- dplyr::left_join(energy, replacement_costs, c("years" = "year")) %>% dplyr::left_join(discount_factors, "years") %>% dplyr::mutate(
replacement_cost_EUR = replacement_cost * window_area,
# add new variable for annual cost
annual_cost_EUR = (YCE_EUR + replacement_cost_EUR) * discount_factors$discount_factor
)
NPV <- present_values$annual_cost_EUR hold_sum <- sum(NPV[2:31]) hold_sub <- present_values$annual_cost_EUR[1] hold_sum - hold_sub }
combined_global_cost <- function(idf, param) { data_base <- add_init_cost_to_db(idf, param) net_present_vaule <- cal_net_present_value(idf, param)
global_cost <- data_base$initial_investment + (net_present_vaule - data_base$residual_value)
global_cost <- global_cost / 190.55
global_cost }
ga$objective(combined_primary_energy, combined_global_cost, .dir = "min")
ga$recombinator()
ga$mutator()
ga$selector()
ga$terminator(max_gen = 100L)
ga$run(mu = 20, p_recomb = 0.7, p_mut = 0.2)
Error
```r
-- Initialization ----------------------------------------------------------------------------------
* Create initial population
Checking if parameter(s) has been set ...
Checking if objective(s) has been set ...
Validating parameter function 'design_options' ...
Error in set_type(idf, param) : Invalid frame name found:‘Dual action’
This is not related to epluspar.
You did not pass valid arguments to set_type
when used in set_fixed_selector
and set_horizontal_selector
.
set_type <- function(idf, win1) {
......
}
set_fixed_selector <- function(idf, param) {
fix_hold <- set_type(idf, param)
......
}
set_horizontal_selector<-function(idf, param){
hor_hold <- set_type(idf, param)
......
}
Change them to:
set_fixed_selector <- function(idf, param) {
fix_hold <- set_type(idf, param$window_frame)
......
}
set_horizontal_selector<-function(idf, param){
hor_hold <- set_type(idf, param$window_frame)
......
}
@hongyuanjia, please bear with me for disorganizing this section a little bit. I implement the above suggestion, you made. However, am still faced with an error, as shown below.
-- Initialization ----------------------------------------------------------------------------------
* Create initial population
Checking if parameter(s) has been set ...
Checking if objective(s) has been set ...
Validating parameter function 'design_options' ...
Error in param$window_frame : $ operator is invalid for atomic vectors
You did not define your measure function correctly. param
is only available in objective functions. Just directly pass your parameter to set_fixed_selector
, set_horizontal_selector
and set_horizontal_selector2
set_fixed_selector <- function(idf, fix_hold) {
......
}
set_horizontal_selector<-function(idf, hor_hold){
......
}
set_horizontal_selector2 <- function(idf, hor_hold2){
......
}
design_options <- function(idf,
floor_insulation_thickness,
wall_insulation_thickness,
roof_insulation_thickness,
window_frame,
fixed_selector,
horizontal_selector,
horizontal_selector2) {
......
set_fixed_selector(idf, fixed_selector)
set_horizontal_selector(idf, horizontal_selector)
set_horizontal_selector2(idf, horizontal_selector2)
idf
}
@hongyuanjia Thanks for your recommendation.
I am trying to implement my code in a modular manner.
The function below checks for the frame type. However, the output of the function, can not be seen globally.
# define a measure to change window based on operation and frame
set_type <- function(idf, window_fr) {
glzsys <- eplusr::read_idf("glazing_system.idf")
if (grepl("Aluminum", window_fr, ignore.case = TRUE)) {
win2 <- "Fixed window Al"
win3 <- "Horizontal slider window Al"
win4 <- "Casement window Al"
win5 <- "Vertical slider window Al"
win6 <- "Dual action window Al"
} else if (grepl("Wood", window_fr, ignore.case = TRUE)) {
win2 <- "Fixed window Wd"
win3 <- "Horizontal slider window Wd"
win4 <- "Casement window Wd"
win5 <- "Vertical slider window Wd"
win6 <- "Dual action window Wd"
}else if (grepl("Vinyl", window_fr, ignore.case = TRUE)) {
win2 <- "Fixed window Vy"
win3 <- "Horizontal slider window Vy"
win4 <- "Casement window Vy"
win5 <- "Vertical slider window Vy"
win6 <- "Dual action window Vy"
}else {
stop("Invalid frame name found:", sQuote(window_fr))
}
idf$insert(glzsys$objects(c(win2, win3, win4, win5, win6)))
c(win2, win3, win4, win5, win6)
}
The following functions below, try to reference the function above.
set_fixed_selector <- function(idf, fix_hold) {
# update construction
if (grepl("Fixed window", fix_hold, ignore.case = TRUE)){
idf$set(
"Exterior Window Fixed" = list(Outside_Layer = win2),
"Exterior Window fixed Shade" = list(Outside_Layer = win2)
)} else if (grepl("Dual action", fix_hold, ignore.case = TRUE)){
idf$set(
"Exterior Window Fixed"= list(Outside_Layer = win6),
"Exterior Window fixed Shade" = list(Outside_Layer = win6)
)}
}
set_horizontal_selector <- function(idf, hor_hold){
# get corresponding construction name per window
# update construction
if (grepl("Horizontal slider", hor_hold, ignore.case = TRUE)){
idf$set(
"Exterior Window horizontal slider"= list(Outside_Layer = win3),
"Exterior Window horizontal Shade"= list(Outside_Layer = win3)
)}else if (grepl("Casement",hor_hold, ignore.case = TRUE)){
idf$set(
"Exterior Window horizontal slider"= list(Outside_Layer = win4),
"Exterior Window horizontal Shade" = list(Outside_Layer = win4)
)}else if (grepl("Dual action", hor_hold, ignore.case = TRUE)){
idf$set(
"Exterior Window horizontal slider"= list(Outside_Layer = win6),
"Exterior Window horizontal Shade"= list(Outside_Layer = win6)
)}else if (grepl("Vertical silder", hor_hold, ignore.case = TRUE)){
idf$set(
"Exterior Window horizontal slider"= list(Outside_Layer = win5),
"Exterior Window horizontal Shade"= list(Outside_Layer = win5)
)}
}
set_horizontal_selector2 <- function(idf,hor_hold2 ){
if (grepl("Horizontal slider",hor_hold2, ignore.case = TRUE)){
idf$set(
"Exterior Window horizontal slider with three options only"= list(Outside_Layer = win3),
"Exterior Window horizontal slider with three options only Shade" = list(Outside_Layer = win3)
)}else if (grepl("Casement",hor_hold2, ignore.case = TRUE)){
idf$set(
"Exterior Window horizontal slider with three options only"= list(Outside_Layer = win4),
"Exterior Window horizontal slider with three options only Shade"= list(Outside_Layer = win4)
)}else if (grepl("Dual action",hor_hold2, ignore.case = TRUE)){
idf$set(
"Exterior Window horizontal slider with three options only" = list(Outside_Layer = win6),
"Exterior Window horizontal slider with three options only Shade" = list(Outside_Layer = win6)
)}
idf$set(
"Exterior Window restroom" = list(Outside_Layer = win6),
"Exterior Window restroom Shade" = list(Outside_Layer = win6)
)
}
Error message
-- Initialization -------------------------------------------------------------------------------------
* Create initial population
Checking if parameter(s) has been set ...
Checking if objective(s) has been set ...
Validating parameter function 'design_options' ...
Error in eval(li, .env) : object 'win5' not found
Please, can you recommend an approach to this challenge?
If you want set_fixed_selector()
and others to use the results from set_type()
, just pass the results to them:
design_options <- function(idf,
floor_insulation_thickness,
wall_insulation_thickness,
roof_insulation_thickness,
window_frame,
fixed_selector,
horizontal_selector,
horizontal_selector2) {
# get results
types <- set_type(idf, window_frame)
# directly pass types to other functions
set_fixed_selector(idf, fixed_selector, types)
set_horizontal_selector(idf, horizontal_selector, types)
set_horizontal_selector2(idf, horizontal_selector2, types)
idf
}
set_type <- function(idf, win1) {
......
# do name the results
c(win2 = win2, win3 = win3, win4 = win4, win5 = win5, win6 = win6)
}
set_fixed_selector <- function(idf, fix_hold, types) {
if (grepl("Fixed window", fix_hold, ignore.case = TRUE)) {
idf$set("Exterior Window Fixed" = list(Outside_Layer = types["win2"]),
"Exterior Window fixed Shade" = list(Outside_Layer = types["win2"]))
} else if (grepl("Dual action", fix_hold, ignore.case = TRUE)) {
idf$set("Exterior Window Fixed"= list(Outside_Layer = types["win6"]),
"Exterior Window fixed Shade" = list(Outside_Layer = types["win6"]))
}
}
set_horizontal_selector <- function(idf, hor_hold, types) {
if (grepl("Horizontal slider", hor_hold, ignore.case = TRUE)) {
idf$set("Exterior Window horizontal slider"= list(Outside_Layer = types["win3"]),
"Exterior Window horizontal Shade"= list(Outside_Layer = types["win3"]))
} else if (...) {
......
}
}
set_horizontal_selector2 <- function(idf, hor_hold2, types) {
if (grepl("Horizontal slider",hor_hold2, ignore.case = TRUE)){
idf$set("Exterior Window horizontal slider with three options only"= list(Outside_Layer = types["win3"]),
"Exterior Window horizontal slider with three options only Shade" = list(Outside_Layer = types["win3"]))
} else if (...) {
......
}
}
Next time, you can validate your measure by passing an Idf
object and other arguments for testing. You should test each of them to make sure they all work properly. After that, do the $apply_measure()
step.
design_options(
# pass a cloned Idf in order to make the original Idf untouched
idf$clone(),
floor_insulation_thickness = 0.1,
wall_insulation_thickness = 0.1,
roof_insulation_thickness = 0.1,
window_frame = "Al",
fixed_selector= "Fixed",
horizontal_selector= "Casement",
horizontal_selector2= "Casement"
)
# Or test your small measure
set_horizontal_selector(idf$clone(), "Casement", "Al")
This step also applies to the objective functions.
# build up a test parameter input
param <- list(
floor_insulation_thickness = 0.1,
wall_insulation_thickness = 0.1,
roof_insulation_thickness = 0.1,
window_frame = "Al",
fixed_selector= "Fixed",
horizontal_selector= "Casement",
horizontal_selector2= "Casement"
)
# run a test simulation in the temporary directory
idf$run("Nicosia_hour.epw", tempdir())
combined_primary_energy(idf)
add_init_cost_to_db(idf, param)
cal_energy_cost(idf, param)
cal_net_present_value(idf, param)
combined_global_cost(idf, param)
After you test all your measure and objective functions, if you still encounter any errors, that is probably caused by a bug of epluspar package. Just open a new issue for it.
Hope the above helps.
@hongyuanjia, thanks once more for guidance. I was able to implement your recommendation that allows the passing of results to other functions. It was implemented as shown below:
# combine all measures into one
design_options <- function(idf, floor_insulation_thickness,
wall_insulation_thickness,
roof_insulation_thickness,
window_frame,
fixed_selector,
horizontal_selector,
horizontal_selector2) {
# get results
types <-set_type (idf, window_frame)
set_insulation_floor(idf, floor_insulation_thickness)
set_insulation_wall(idf, wall_insulation_thickness)
set_insulation_roof(idf, roof_insulation_thickness)
# function (idf, parameter, types)
set_fixed_selector(idf, fixed_selector, types)
set_horizontal_selector(idf, horizontal_selector, types)
set_horizontal_selector2(idf, horizontal_selector2, types)
idf
}
I spent some time trying to comprehend your recommendation, towards validating my measures. Unfortunately for me, it's still not clear. Upon adopting your recommendation, and running the codes. The first Objective seems ok. However, the second Objective returned the following error:
-- Initialization -------------------------------------------------------------------------------------
* Create initial population
Checking if parameter(s) has been set ...
Checking if objective(s) has been set ...
Validating parameter function 'design_options' ...
Validating objective function(s)...
[1] 'combined_primary_energy' --> OK
Error in add_init_cost_to_db(idf, param) :
object 'cost_per_square_meter_window2' not found
The fuction, generating the error is shown below
add_init_cost_to_db <- function(idf, param) {
# get thickness of floor, wall and roof
types <- c("floor", "wall", "roof")
thickness <- param[glue::glue("{types}_insulation_thickness")]
data_base <- as.list(readxl::read_excel("EPBDcostsheet08.xlsx", sheet = 3))
# get area of floor, wall and roof
area <- data_base[glue::glue("{types}_area")]
# insulation manipulation y = mx+c
# cost per square meter= 157.4992667x-0.449997905
cost_per_m2 <- function(x) 157.4992667 * x -0.449997905
# calculate sum of initial cost of floor, wall and roof
initial_investment_envelope <- purrr::map2_dbl(thickness, area, ~cost_per_m2(.x) * .y * 1.1)
# calculate initial cost of windows
if (param$fixed_selector %in% "Fixed"){
# average cost per square meter in Euros
cost_per_square_meter_window1 <- 91.87
window_area1 <- 37.06
}else if (param$fixed_selector %in% "Dual action") {
# average cost per square meter in Euros
cost_per_square_meter_window2 <- 197.25
window_area2 <- 37.06
}
if (param$horizontal_selector %in% "Horizontal slider") {
# average cost per square meter in Euros
cost_per_square_meter_window3 <- 129.37
window_area3 <- 18.58
}else if (param$horizontal_selector %in% "Casement") {
# average cost per square meter in Euros
cost_per_square_meter_window4 <- 160.87
window_area4 <- 18.58
}else if (param$horizontal_selector %in% "Dual action") {
# average cost per square meter in Euros
cost_per_square_meter_window5 <- 197.25
window_area5 <- 18.58
}else if (param$horizontal_selector %in% "Vertical silder") {
# average cost per square meter in Euros
cost_per_square_meter_window6 <- 129.37
window_area6 <- 18.58
}
if (param$horizontal_selector2 %in% "Horizontal slider") {
# average cost per square meter in Euros
cost_per_square_meter_window7 <- 129.37
window_area7 <- 23.15
}else if (param$horizontal_selector2 %in% "Casement") {
# average cost per square meter in Euros
cost_per_square_meter_window8 <- 160.87
window_area8 <- 23.15
}else if (param$horizontal_selector2 %in% "Dual action") {
# average cost per square meter in Euros
cost_per_square_meter_window9 <- 197.25
window_area9 <- 23.15
}
total_cost_per_square_meter_window <- sum(cost_per_square_meter_window1,
cost_per_square_meter_window2,
cost_per_square_meter_window3, cost_per_square_meter_window4,
cost_per_square_meter_window5, cost_per_square_meter_window6,
cost_per_square_meter_window7, cost_per_square_meter_window8,
cost_per_square_meter_window9)
total_window_area <- sum(window_area1, window_area2, window_area3,
window_area4, window_area5, window_area6,
window_area7, window_area8, window_area9)
initial_investment_window <- total_cost_per_square_meter_window * total_window_area * 1.1
# summation of initial investment
initial_investment <- sum(initial_investment_envelope, initial_investment_window)
# add initial investment result to the data base
data_base$initial_investment <- initial_investment
# return the data base
data_base
}
Please, Sir, do you have an idea of what argument I am not passing correctly?
The reason for this error is simple. It is not because you did not pass the argument, but because of the bad logic of defining values for window_area[1-9]
. What would the value of window_area_1
be if param$fixed_selector %in% "Fixed"
is not TRUE
? You should make sure all window_area[1-9]
are initialized.
# calculate initial cost of windows
if (param$fixed_selector %in% "Fixed"){
# average cost per square meter in Euros
cost_per_square_meter_window1 <- 91.87
window_area1 <- 37.06
......
@hongyuanjia Thank you so much for your guidance. All through the weekend.
I adopted your recommendation and initialized all window_area[1-9]
. Likewise, all cost_per_square_meter_window[1-9]
were also initialized.
The objective functions are both successfully validated as shown below.
-- Initialization ----------------------------------------------------------------------------------
* Create initial population
Checking if parameter(s) has been set ...
Checking if objective(s) has been set ...
Validating parameter function 'design_options' ...
Validating objective function(s)...
[1] 'combined_primary_energy' --> OK
[2] 'combined_global_cost' --> OK
All checks have been passed.
* Evaluate fitness values
Thanks once more, Sir.
Good day Sir. The code bellow, has been working perfectly for sometime. But after an update to my packages yesterday, It refused to work again.