Skip to content

Commit

Permalink
Fix #47: detect domains close to zero not representable with the give…
Browse files Browse the repository at this point in the history
…n digits.
  • Loading branch information
MLopez-Ibanez committed Nov 25, 2022
1 parent d0883a8 commit 2b1ff80
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
14 changes: 6 additions & 8 deletions R/generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ getDependentBound <- function(parameters, param, configuration)
}
}

return(values)
values
}

### Uniform sampling for the initial generation
Expand Down Expand Up @@ -247,22 +247,22 @@ sampleModel <- function (parameters, eliteConfigurations, model,
irace.error("irace tried 100 times to sample from the model a configuration not forbidden without success, perhaps your constraints are too strict?")
}
}
return (newConfigurations)
newConfigurations
}

transform.from.log <- function(x, transf, lowerBound, upperBound)
{
trLower <- attr(transf, "lower")
trUpper <- attr(transf, "upper")
x <- exp(trLower + (trUpper - trLower) * x)
return(x)
x <- min(max(x, lowerBound), upperBound)
}

transform.to.log <- function(x, transf, lowerBound, upperBound)
{
trLower <- attr(transf, "lower")
trUpper <- attr(transf, "upper")
return((log(x) - trLower)/(trUpper - trLower))
(log(x) - trLower)/(trUpper - trLower)
}
## How to sample integer values?
#
Expand Down Expand Up @@ -344,8 +344,7 @@ sample_unif <- function(type, domain, transf, digits)
value <- runif(1, min = lowerBound, max = upperBound)
}
# We use original upperBound, not the +1L for 'i'.
value <- numeric_value_round(type, value, lowerBound, upperBound = domain[2], digits)
return(value)
numeric_value_round(type, value, lowerBound, upperBound = domain[2], digits)
}

sample_norm <- function(mean, sd, type, domain, transf, digits)
Expand All @@ -372,6 +371,5 @@ sample_norm <- function(mean, sd, type, domain, transf, digits)
value <- rtnorm(1, mean, sd, lowerBound, upperBound)
}
# We use original upperBound, not the +1L for 'i'.
value <- numeric_value_round(type, value, lowerBound, upperBound = domain[2], digits)
return(value)
numeric_value_round(type, value, lowerBound, upperBound = domain[2], digits)
}
5 changes: 3 additions & 2 deletions R/readParameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,10 @@ readParameters <- function (file, digits = 4, debugLevel = 0, text)
}

valid_real_bound <- function(x, digits) {
if (is.na(x)) return(TRUE)
if (is.na(x) || x == 0) return(TRUE)
rx <- round(x, digits = digits)
(abs(rx - x) <= .irace_tolerance * max(1, abs(x)))
((abs(rx - x) <= .irace_tolerance * max(1, abs(x)))
&& digits >= -log10(abs(x)))
}

if (param.type == "r") {
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-bug-44.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ context("bug")
# https://github.com/MLopez-Ibanez/irace/issues/44
withr::with_output_sink("test-bug-44.Rout", {
readParameters(text='a "a" r (0.01, 0.99)', digits=2)
readParameters(text='a "a" r (0, 1)', digits=1)
expect_error(readParameters(text='a "a" r (0.01, 0.99)', digits=1), "must be representable")
expect_error(readParameters(text='a "a" r,log (1e-8, 1)', digits=4), "must be representable")
})

0 comments on commit 2b1ff80

Please sign in to comment.