hongyuanjia / epluspar

Conduct parametric analysis on EnergyPlus models in R
https://hongyuanjia.github.io/epluspar
Other
9 stars 0 forks source link

Question related to storing design parameters and vital outputs using Epluspar #33

Open mdakyen opened 2 years ago

mdakyen commented 2 years ago

Good day Sir @hongyuanjia, Please I have been trying to store certain design parameters from my optimization process so I can access them afterwards. All my efforts have not been successful. Below is a sample of my code. Kindly assist me.

iteration <-  0
wall_insulation <- 0
roof_insulation <- 0
win_frame <- "" 
total_investment_opaque <- 0

#create a data frame
hold <- data.frame(iteration, wall_insulation, 
                             roof_insulation, win_frame,
                             total_investment_opaque) 

#-------------------------------------------------------------------------------
# combine all measures into one
design_options <- function(idf, wall_insulation_thickness,
                           roof_insulation_thickness,
                           window_frame,
                           ) {

  # get results
  types <- set_type (idf, window_frame)

  set_insulation_wall(idf, wall_insulation_thickness)
  set_insulation_roof(idf, roof_insulation_thickness)

   #set vital variables for data frame hold
  hold$wall_insulation <- wall_insulation_thickness
  hold$roof_insulation <- roof_insulation_thickness
  hold$win_frame <- window_frame

  idf
}
#-------------------------------------------------------------------------------
add_init_cost_to_db <- function(idf, param) {
initial_investment <- sum(initial_investment_envelope, 
                            initial_investment_floor)

  #set data frame hold
  hold$total_investment_opaque<- initial_investment
}

    wall_insulation <- hold$wall_insulation
    roof_insulation <- hold$roof_insulation
    win_frame <- hold$win_frame
     total_investment_opaque<- hold$total_investment_opaque

    iteration <<- iteration + 1

    vitalsimulationresults <- rbind( vitalsimulationresults, data.frame
                                                    (iteration, wall_insulation,
                                                     roof_insulation, win_frame, 
                                                     total_investment_opaque))
hongyuanjia commented 2 years ago

I did not get your point. Can you elaborate in details about what you want to achieve?

mdakyen commented 2 years ago

Good day sir, thank you for your swift response. Actually, I am trying to attain optimization for two objectives (global cost and primary energy). so far, my code works perfectly, but the only results i can see are those of my design options and objective functions. i wish to retrieve other parameters (such as initial investment replacement cost etc ) in a single data frame after my simulation. Please sir, I have will forward my code to your outlook account in the next 30mins tops

mdakyen commented 2 years ago

@hongyuanjia, Sir, I have sent the command code and IDF files to your outlook account.

mdakyen commented 2 years ago

@hongyuanjia please did you receive my mails Sir ?

mdakyen commented 2 years ago

@hongyuanjia, Dear Sir, kindly bear with my level of persistence at the moment. Honestly, my research progress now highly depends on your feedback.

hongyuanjia commented 2 years ago

Sorry, I have no time to debug your ~2000 lines of code. But I can give you some directions on how to do that.

The global variables stored in your current sessions can not be directly modified in your objective functions. This is because the fitness is evaluated in a separate R session. Therefore, it knows nothing about your current R session. You should make your measure function and object function self-contained, i.e., do not let them depend on other resources except the IDF itself.

One possible way to achieve data-sharing across R sessions is to use files. Below give you an example to do that. But before you do that, I recommend you perform all your calculations in the objective functions since you can access each parameter.

path_idf <- eplusr::path_eplus_example(8.9, "5Zone_Transformer.idf")
path_epw <- eplusr::path_eplus_weather(8.9, "USA_CA_San.Francisco.Intl.AP.724940_TMY3.epw")

ga <- epluspar::gaoptim_job(path_idf, path_epw)

case <- 0
north_axis <- 0
capacity <- NA_real_
df <- data.frame(case, north_axis, capacity)

# save data
data_save <- function(data, name = deparse(substitute(data)), dir = ".") {
    saveRDS(data, file.path(file.path(dir, name)))
}
# read data
data_read <- function(name, dir = ".") {
    readRDS(file.path(dir, name))
}

# init data
data_save(df)

mea <- function(idf, dir = 0) {
    idf$Building$North_Axis <- dir

    # read data
    df <- data_read("df")

    # update data
    new <- df[nrow(df), ]
    new$case <- max(df$case) + 1
    new$north_axis <- dir

    # save data
    df <- rbind(df, new)
    rownames(df) <- seq_len(nrow(df))
    data_save(df)

    idf
}

obj <- function(idf) {
    val <- as.double(idf$last_job()$tabular_data(
        report_name = "ComponentSizingSummary",
        table_name = "Chiller:Electric",
        column_name = "Design Size Nominal Capacity"
    )$value)

    # read data
    df <- data_read("df")

    # update data
    df[nrow(df), "capacity"] <- val

    # save data
    data_save(df)

    val
}

ga$apply_measure(mea, epluspar::float_space(0, 180, 0))
ga$objective(obj)

ga$recombinator()
ga$mutator()
ga$selector(survival = ecr::selGreedy)
ga$terminator(max_gen = 2L)
ga$run(mu = 2)

ga$population()
#    index_gen index_ind      dir      obj
#        <int>     <int>    <num>    <num>
# 1:         1         1 65.91481 40871.42
# 2:         1         2 65.91481 40871.42
# 3:         2         1 65.91481 40871.42
# 4:         2         2 65.91481 40871.42

data_read("df")
#   case north_axis capacity
# 1    0    0.00000       NA
# 2    1  101.55419 42244.54
# 3    2  101.55419 42244.54
# 4    3   65.91481 40871.42
# 5    4   65.91481 40871.42
# 6    5  101.55419 42244.54
# 7    6   65.91481 40871.42
# 8    7   65.91481 40871.42
mdakyen commented 2 years ago

Ok Sir, I will do my best in implementing your recommendations. Thank you so much for your time. I wish you a great week ahead.

mdakyen commented 2 years ago

Good day Sir, Your recommendation has been helpful so far. However, my optimization process, has been successful for only a small number of generations. Meaning that, when ever I increase the number of generations. My optimization process runs for awhile, then suddenly it crashes. Displaying the following error : Error in readRDS(file.path(dir, name)) : error reading from connection Below are functions from my optimization as you have earlier suggested sir.

# save data
data_save <- function(data, name = deparse(substitute(data)), dir = ".") {
  saveRDS(data, file.path(file.path(dir, name)))
}
# read data
data_read <- function(name, dir = ".") {
  readRDS(file.path(dir, name))
}

# init data
data_save(vitalsimulationresults)

Below is a screen shot explaining my issue in more detail sir.

image

mdakyen commented 2 years ago

Another sample of my error, sir

error II