Skip to content

Commit

Permalink
Version 0.6.9 - small improvements, method to enhance functions with …
Browse files Browse the repository at this point in the history
…boundaries
  • Loading branch information
gk-crop committed Jul 10, 2024
1 parent a484128 commit 10fabca
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 6 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
^Meta$
^temp$
^log$
^vignettes/log$
^\.github$
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", 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.
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(componentsToGraph)
export(createCodeStubsForSimVariables)
export(createResourceStubsFromCsv)
export(createResourceStubsFromXml)
export(enhanceFunctionWithBoundaries)
export(fetchDescriptionFromWebsite)
export(fetchSimComponentlistFromWebsite)
export(fetchSimVariablesFromWebsite)
Expand Down
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 6 additions & 2 deletions R/get_solutions_elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
51 changes: 51 additions & 0 deletions R/optim_helper.R
Original file line number Diff line number Diff line change
@@ -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(...)
}
}
}
2 changes: 1 addition & 1 deletion R/simplaceUtil.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
#' package = "simplaceUtil"))
#' DiagrammeR::render_graph(graph)
#' @name simplaceUtil
NULL
"_PACKAGE"
18 changes: 18 additions & 0 deletions R/website_documentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
},
Expand Down
56 changes: 56 additions & 0 deletions man/enhanceFunctionWithBoundaries.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/getOutputFilenames.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/simplaceUtil.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 10fabca

Please sign in to comment.