Skip to content

Commit

Permalink
fix bug in getLoggedValues yielding problems if logg.x = FALSE was se…
Browse files Browse the repository at this point in the history
…t for logging wrapper
  • Loading branch information
jakobbossek committed Sep 18, 2023
1 parent d652472 commit 3e2efee
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 8 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Description: Provides generators for a high number of both single- and multi-
objective test functions which are frequently used for the benchmarking of
(numerical) optimization algorithms. Moreover, it offers a set of convenient
functions to generate, plot and work with objective functions.
Version: 1.6.0.3
Date: 2023-03-06
Version: 1.6.0.4
Date: 2023-09-18
Authors@R: c(
person(
given = "Jakob",
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
smoof 1.6.0.4
=============

* Fixed bug in getLoggedValues when logging of x-values was set to FALSE in addLoggingWrapper

smoof 1.6.0.3
=============

Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# smoof 1.6.0.4

## Bugfixes

* Fixed bug in getLoggedValues when logging of x-values was set to FALSE in addLoggingWrapper


# smoof 1.6.0.3

## New features
Expand Down
3 changes: 2 additions & 1 deletion R/addLoggingWrapper.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ addLoggingWrapper = function(fn, logg.x = FALSE, logg.y = TRUE, size = 100L) {
# re-scale
if (curr.idx > max.idx) {
# extend data structures by another 'size' rows/columns
pars <<- rbind(pars, data.frame(matrix(NA, nrow = size, ncol = n.pars), stringsAsFactors = FALSE))
if (logg.x)
pars <<- rbind(pars, data.frame(matrix(NA, nrow = size, ncol = n.pars), stringsAsFactors = FALSE))
obj.vals <<- cbind(obj.vals, matrix(NA, nrow = n.obj, ncol = size))
max.idx <<- max.idx + size
}
Expand Down
15 changes: 10 additions & 5 deletions R/getLoggedValues.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@ getLoggedValues.smoof_logging_function = function(fn, compact = FALSE) {

env = environment(fn)
max.idx = env$curr.idx - 1L
pars = env$pars[seq_len(max.idx), , drop = FALSE]
colnames(pars) = getParamIds(ParamHelpers::getParamSet(fn), with.nr = TRUE, repeated = TRUE)
pars = NULL
if (env$logg.x) {
pars = env$pars[seq_len(max.idx), , drop = FALSE]
colnames(pars) = getParamIds(ParamHelpers::getParamSet(fn), with.nr = TRUE, repeated = TRUE)
}

obj.vals = env$obj.vals[, seq_len(max.idx), drop = FALSE]
obj.vals = NULL
if (env$logg.y)
obj.vals = env$obj.vals[, seq_len(max.idx), drop = FALSE]
# wrap everything up in a single data frame
if (compact) {
# if only the x-values are stored just return the data frame
if (is.null(obj.vals)) {
if (!env$logg.y) {
return(pars)
}
df = as.data.frame(t(obj.vals))
names(df) = paste0("y", seq(ncol(df)))
if (!is.null(pars)) {
if (env$logg.x) {
# append x-values if stored
df = cbind(pars, df)
}
Expand Down
34 changes: 34 additions & 0 deletions tests/testthat/test_logging.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ test_that("logging for functions with matrix input works well", {
expect_equal(ncol(res), 10L + 1L) # dim plus y
})

test_that("logging works without logging of objective space values", {
for (dimensions in c(1L, 2L, 5L, 10L)) {
fn = makeSphereFunction(dimensions = dimensions)

# add logger for both x and y values
fn = addLoggingWrapper(fn, logg.x = FALSE)

# now apply some evaluations
fn(runif(dimensions))

res = getLoggedValues(fn, compact = TRUE)
expect_true(is.data.frame(res))
expect_equal(colnames(res), "y1")
}
})

test_that("logging works without logging of decision space values", {
for (dimensions in c(1L, 2L, 5L, 10L)) {
fn = makeSphereFunction(dimensions = dimensions)
par.ids = getParamIds(getParamSet(fn), with.nr = TRUE, repeated = TRUE)

# add logger for both x and y values
fn = addLoggingWrapper(fn, logg.x = TRUE, logg.y = FALSE)

# now apply some evaluations
fn(runif(dimensions))

res = getLoggedValues(fn, compact = TRUE)
expect_true(is.data.frame(res))
expect_equal(ncol(res), dimensions)
expect_equal(colnames(res), par.ids)
}
})

test_that("logging for simple functions works well", {
# generate Sphere function
for (dimensions in c(1L, 2L, 5L, 10L)) {
Expand Down

0 comments on commit 3e2efee

Please sign in to comment.