diff --git a/.Rbuildignore b/.Rbuildignore index 9501323..c3ab1d3 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -4,4 +4,5 @@ ^Meta$ ^temp$ ^log$ +^vignettes/log$ ^\.github$ diff --git a/DESCRIPTION b/DESCRIPTION index 4c5e1b6..fa042d6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: simplaceUtil Title: Provides Utility Functions and ShinyApps to work with the modeling framework 'SIMPLACE' -Version: 0.6.8 -Date: 2024-06-20 +Version: 0.6.9 +Date: 2024-07-10 Authors@R: person("Gunther", "Krauss", , "guntherkrauss@uni-bonn.de", role = c("aut", "cre")) Description: Provides Utility Functions and ShinyApps to work with the modeling framework 'SIMPLACE'. It visualises components of a solution, runs simulations and displays results. diff --git a/NAMESPACE b/NAMESPACE index ae54902..873e20b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ export(componentsToGraph) export(createCodeStubsForSimVariables) export(createResourceStubsFromCsv) export(createResourceStubsFromXml) +export(enhanceFunctionWithBoundaries) export(fetchDescriptionFromWebsite) export(fetchSimComponentlistFromWebsite) export(fetchSimVariablesFromWebsite) diff --git a/NEWS b/NEWS index 3b34333..b9e6ae9 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +Version 0.6.9 +* output filenames vector named by output ids +* enhance functions to return penalty values when parameters outside boundaries + Version 0.6.8 * get output filenames * automatic facets for layered data plots diff --git a/R/get_solutions_elements.R b/R/get_solutions_elements.R index 4896ed6..324c82b 100644 --- a/R/get_solutions_elements.R +++ b/R/get_solutions_elements.R @@ -336,15 +336,19 @@ getMemoryOutputIds <- function(comp) { #' @param comp components dataframe #' @param variables variables dataframe #' @param additional additional variables as named vector c("var1"="value1", ...), useful for directory placeholder -#' @return character vector with the memory output ids +#' @return named character vector with the filenames #' @export getOutputFilenames <- function(comp,variables,additional=NULL) { refs <- comp[comp$type=="output" & !is.na(comp$ref) & substr(comp$ref,nchar(comp$ref)-7,nchar(comp$ref))!="[MEMORY]", "ref"] + ids <- comp[comp$type=="output" & !is.na(comp$ref) & substr(comp$ref,nchar(comp$ref)-7,nchar(comp$ref))!="[MEMORY]", "id"] refs <- gsub("[^\\[]+\\[(.+)\\]","\\1",refs) - replaceVariablesWithValues(refs, + + refs <- replaceVariablesWithValues(refs, variables, additional=additional ) + names(refs) <- ids + refs } diff --git a/R/optim_helper.R b/R/optim_helper.R new file mode 100644 index 0000000..2011d49 --- /dev/null +++ b/R/optim_helper.R @@ -0,0 +1,51 @@ +#' Modifies function to return a penalty value for parameters outside boundaries +#' +#' The method takes a function as well as values for lower and upper boundaries +#' and returns a modified function. The modified function returns the value of +#' the original function when the parameters are within boundaries and the penalty +#' value otherwise. +#' +#' Optionally an own function can be supplied to calculate whether the parameter +#' is valid. The boundary function must take 3 arguments: parameter, +#' lower boundary and upper boundary and must return TRUE or FALSE. +#' +#' A main use case of this method are optimisation / calibration tasks. If the +#' optimisation method and the function to optimise are both ignorant to boundaries +#' one can turn the function into a boundary sensitive one. +#' +#' @param fun function to be modified +#' @param l_bound vector with lower boundary values +#' @param u_bound vector with upper boundary values +#' @param penalty_value value if parameter outside boundaries +#' @param boundary_fun optional function for complex boundary conditions +#' @param ... arguments passed to original function +#' @param param_pos argument position of the parameter +#' @return a modified function that considers boundaries +#' @export +#' @examples +#' sqrt_bd <- enhanceFunctionWithBoundaries(sqrt, 0, 10) +#' sqrt_bd(-1) +#' sqrt_bd(1) +#' sqrt_bd(11) +#' +enhanceFunctionWithBoundaries <- function(fun, l_bound, u_bound, + penalty_value=Inf, + boundary_fun=NULL, + param_pos=1, + ...) { + function(...) { + invalid <- FALSE + if(!is.null(boundary_fun)) { + invalid <- !boundary_fun(...elt(param_pos),l_bound, u_bound) + } + else { + invalid <- !all(l_bound <= ...elt(param_pos) & ...elt(param_pos) <= u_bound) + } + if(invalid) { + penalty_value + } + else { + fun(...) + } + } +} diff --git a/R/simplaceUtil.R b/R/simplaceUtil.R index e546c17..7db97b1 100644 --- a/R/simplaceUtil.R +++ b/R/simplaceUtil.R @@ -20,4 +20,4 @@ #' package = "simplaceUtil")) #' DiagrammeR::render_graph(graph) #' @name simplaceUtil -NULL +"_PACKAGE" diff --git a/R/website_documentation.R b/R/website_documentation.R index fb006cd..8492a74 100644 --- a/R/website_documentation.R +++ b/R/website_documentation.R @@ -16,6 +16,9 @@ versionDir <- function(version="current") { vs } + + + #' Fetches the SimVariables table for a SimComponent from Simplace Website #' #' @param class class name of the SimComponent @@ -39,6 +42,21 @@ fetchSimVariablesFromWebsite <- function(class, version="current") { names(res) <- c("contenttype", "id", "description", "datatype", "unit", "min", "max", "default") res$component <- class res$componentname <- sapply(strsplit(res$component,'\\.'), \(x) x[length(x)]) + + res$min <- ifelse(!(res$datatype %in% c("CHAR","CHARARRAY")), + gsub("-","",trimws(res$min), fixed = TRUE), + trimws(res$min)) + res$max <- ifelse(!(res$datatype %in% c("CHAR","CHARARRAY")), + gsub("-","",trimws(res$max), fixed = TRUE), + trimws(res$max)) + res$default <- ifelse(!(res$datatype %in% c("CHAR","CHARARRAY")), + gsub("-","",trimws(res$default), fixed = TRUE), + trimws(res$default)) + + res$default <- ifelse(res$datatype %in% c("INTARRAY","DOUBLEARRAY"), + gsub(" ",",",res$default, fixed = TRUE), + res$default) + res } }, diff --git a/man/enhanceFunctionWithBoundaries.Rd b/man/enhanceFunctionWithBoundaries.Rd new file mode 100644 index 0000000..c21d66c --- /dev/null +++ b/man/enhanceFunctionWithBoundaries.Rd @@ -0,0 +1,56 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/optim_helper.R +\name{enhanceFunctionWithBoundaries} +\alias{enhanceFunctionWithBoundaries} +\title{Modifies function to return a penalty value for parameters outside boundaries} +\usage{ +enhanceFunctionWithBoundaries( + fun, + l_bound, + u_bound, + penalty_value = Inf, + boundary_fun = NULL, + param_pos = 1, + ... +) +} +\arguments{ +\item{fun}{function to be modified} + +\item{l_bound}{vector with lower boundary values} + +\item{u_bound}{vector with upper boundary values} + +\item{penalty_value}{value if parameter outside boundaries} + +\item{boundary_fun}{optional function for complex boundary conditions} + +\item{param_pos}{argument position of the parameter} + +\item{...}{arguments passed to original function} +} +\value{ +a modified function that considers boundaries +} +\description{ +The method takes a function as well as values for lower and upper boundaries +and returns a modified function. The modified function returns the value of +the original function when the parameters are within boundaries and the penalty +value otherwise. +} +\details{ +Optionally an own function can be supplied to calculate whether the parameter +is valid. The boundary function must take 3 arguments: parameter, +lower boundary and upper boundary and must return TRUE or FALSE. + +A main use case of this method are optimisation / calibration tasks. If the +optimisation method and the function to optimise are both ignorant to boundaries +one can turn the function into a boundary sensitive one. +} +\examples{ +sqrt_bd <- enhanceFunctionWithBoundaries(sqrt, 0, 10) +sqrt_bd(-1) +sqrt_bd(1) +sqrt_bd(11) + +} diff --git a/man/getOutputFilenames.Rd b/man/getOutputFilenames.Rd index 4584de7..6997581 100644 --- a/man/getOutputFilenames.Rd +++ b/man/getOutputFilenames.Rd @@ -14,7 +14,7 @@ getOutputFilenames(comp, variables, additional = NULL) \item{additional}{additional variables as named vector c("var1"="value1", ...), useful for directory placeholder} } \value{ -character vector with the memory output ids +named character vector with the filenames } \description{ Get filenames of file outputs diff --git a/man/simplaceUtil.Rd b/man/simplaceUtil.Rd index 140c92d..8a1c604 100644 --- a/man/simplaceUtil.Rd +++ b/man/simplaceUtil.Rd @@ -1,6 +1,8 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/simplaceUtil.R +\docType{package} \name{simplaceUtil} +\alias{simplaceUtil-package} \alias{simplaceUtil} \title{Package with utility function for working with Simplace} \description{