Skip to content

Commit

Permalink
* R/generation.R: Remove duplicates when sampling.
Browse files Browse the repository at this point in the history
  • Loading branch information
MLopez-Ibanez committed Aug 4, 2023
1 parent c85d097 commit 3bb2412
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions R/generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,23 @@ generate_uniform <- function(parameters, nbConfigurations, repair = NULL)
### Uniform sampling for the initial generation
sampleUniform <- function(parameters, nbConfigurations, repair = NULL)
{
newConfigurations <- generate_uniform(parameters, nbConfigurations, repair)
forbidden <- parameters$forbidden
if (is.null(forbidden)) {
setDF(newConfigurations)
return(newConfigurations)
forbidden <- parameters$forbidden
retries <- 2L
needed <- nbConfigurations
newConfigurations <- NULL
repeat {
newConfigurations <- rbindlist(list(newConfigurations,
generate_uniform(parameters, needed, repair)))
newConfigurations <- unique(newConfigurations)
needed <- nbConfigurations - nrow(newConfigurations)
if (is.null(forbidden) && (needed == 0L || retries == 0L)) {
setDF(newConfigurations)
return(newConfigurations)
}
retries <- retries - 1L
}
retries <- 0L

retries <- 100L
repeat {
newConfigurations <- filter_forbidden(newConfigurations, forbidden)
needed <- nbConfigurations - nrow(newConfigurations)
Expand All @@ -158,8 +167,9 @@ sampleUniform <- function(parameters, nbConfigurations, repair = NULL)
}
newConfigurations <- rbindlist(list(newConfigurations,
generate_uniform(parameters, needed, repair = repair)))
retries <- retries + 1L
if (retries >= 100L) {
newConfigurations <- unique(newConfigurations)
retries <- retries - 1L
if (retries == 0L) {
irace.error("irace tried 100 times to sample from the model a configuration not forbidden without success, perhaps your constraints are too strict?")
}
}
Expand Down Expand Up @@ -269,15 +279,24 @@ sampleModel <- function(parameters, eliteConfigurations, model,
if (nbNewConfigurations <= 0) {
irace.error ("The number of configurations to generate appears to be negative or zero.")
}
newConfigurations <- sample_from_model(parameters, eliteConfigurations,
model, nbNewConfigurations, repair)
forbidden <- parameters$forbidden
if (is.null(forbidden)) {
setDF(newConfigurations)
return(newConfigurations)
retries <- 2L
needed <- nbNewConfigurations
newConfigurations <- NULL
repeat {
tmp <- sample_from_model(parameters, eliteConfigurations,
model, needed, repair)
newConfigurations <- rbindlist(list(newConfigurations, tmp))
newConfigurations <- unique(newConfigurations)
needed <- nbNewConfigurations - nrow(newConfigurations)
if (is.null(forbidden) && (needed == 0L || retries == 0L)) {
setDF(newConfigurations)
return(newConfigurations)
}
retries <- retries - 1L
}

retries <- 0L
retries <- 100L
repeat {
newConfigurations <- filter_forbidden(newConfigurations, forbidden)
needed <- nbNewConfigurations - nrow(newConfigurations)
Expand All @@ -287,8 +306,9 @@ sampleModel <- function(parameters, eliteConfigurations, model,
}
tmp <- sample_from_model(parameters, eliteConfigurations, model, needed, repair = repair)
newConfigurations <- rbindlist(list(newConfigurations, tmp))
retries <- retries + 1L
if (retries >= 100L) {
newConfigurations <- unique(newConfigurations)
retries <- retries - 1L
if (retries == 0L) {
irace.error("irace tried 100 times to sample from the model a configuration not forbidden without success, perhaps your constraints are too strict?")
}
}
Expand Down

0 comments on commit 3bb2412

Please sign in to comment.