-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.R
467 lines (434 loc) · 18.3 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# =========================================================================
# irace: An implementation in R of Iterated Race.
# -------------------------------------------------------------------------
#
# Copyright (C) 2010-2025
# Manuel López-Ibáñez <manuel.lopez-ibanez@manchester.ac.uk>
# Jérémie Dubois-Lacoste <jeremie.dubois-lacoste@ulb.ac.be>
# Leslie Perez Caceres <leslie.perez.caceres@ulb.ac.be>
#
# -------------------------------------------------------------------------
# This program is free software (software libre); you can redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can obtain a copy of the GNU
# General Public License at:
# http://www.gnu.org/copyleft/gpl.html
# or by writing to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
# -------------------------------------------------------------------------
# $Revision$
# =========================================================================
#' irace_license
#'
#' A character string containing the license information of \pkg{irace}.
#'
#' @export
## __VERSION__ below will be replaced by the version defined in R/version.R
## This avoids constant conflicts within this file.
irace_license <-
'#------------------------------------------------------------------------------
# irace: An implementation in R of (Elitist) Iterated Racing
# Version: __VERSION__
# Copyright (C) 2010-2025
# Manuel Lopez-Ibanez <manuel.lopez-ibanez@manchester.ac.uk>
# Jeremie Dubois-Lacoste
# Leslie Perez Caceres <leslie.perez.caceres@ulb.ac.be>
#
# This is free software, and you are welcome to redistribute it under certain
# conditions. See the GNU General Public License for details. There is NO
# WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# irace builds upon previous code from the race package:
# race: Racing methods for the selection of the best
# Copyright (C) 2003 Mauro Birattari
#------------------------------------------------------------------------------
'
cat_irace_license <- function()
cat(sub("__VERSION__", irace_version, irace_license, fixed=TRUE))
#' Higher-level interface to launch irace.
#'
#' @inheritParams defaultScenario
#'
#' @param output.width `integer(1)`\cr The width used for the screen
#' output.
#'
#' @details This function checks the correctness of the scenario, reads the
#' parameter space from \code{scenario$parameterFile}, invokes [irace()],
#' prints its results in various formatted ways, (optionally) calls
#' [psRace()] and, finally, evaluates the best configurations on the test
#' instances (if provided). If you want a lower-level interface that just
#' runs irace, please see function [irace()].
#'
#' @templateVar return_invisible TRUE
#' @template return_irace
#' @seealso
#' \describe{
#' \item{[irace_cmdline()]}{a command-line interface to [irace()].}
#' \item{[readScenario()]}{for reading a configuration scenario from a file.}
#' \item{[readParameters()]}{read the target algorithm parameters from a file.}
#' \item{[defaultScenario()]}{returns the default scenario settings of \pkg{irace}.}
#' }
#' @author Manuel López-Ibáñez and Jérémie Dubois-Lacoste
#' @concept running
#' @export
irace_main <- function(scenario, output.width = 9999L)
irace_common(scenario = scenario, simple=FALSE, output.width = output.width)
#' Test configurations given in the logfile (typically `irace.Rdata`) produced by \pkg{irace}.
#'
#' `testing_fromlog` executes the testing of the target algorithm configurations
#' found by an \pkg{irace} execution.
#'
#' @param logFile `character(1)`\cr Path to the logfile (typically `irace.Rdata`) produced by \pkg{irace}.
#'
#' @param testNbElites Number of (final) elite configurations to test. Overrides
#' the value found in `logFile`.
#'
#' @param testIterationElites `logical(1)`\cr If `FALSE`, only the final
#' `testNbElites` configurations are tested; otherwise, also test the best
#' configurations of each iteration. Overrides the value found in `logFile`.
#'
#' @param testInstancesDir Directory where testing instances are located, either absolute or relative to current directory.
#'
#' @param testInstancesFile File containing a list of test instances and optionally additional parameters for them.
#'
#' @param testInstances Character vector of the instances to be used in the `targetRunner` when executing the testing.
#'
#' @return `logical(1)`\cr `TRUE` if the testing ended successfully otherwise, `FALSE`.
#'
#' @details The function `testing_fromlog` loads the `logFile` and obtains the
#' testing setup and configurations to be tested. Within the `logFile`, the
#' variable `scenario$testNbElites` specifies how many final elite
#' configurations to test and `scenario$testIterationElites` indicates
#' whether test the best configuration of each iteration. The values may be
#' overridden by setting the corresponding arguments in this function. The
#' set of testing instances must appear in `scenario[["testInstances"]]`.
#'
#' @seealso [defaultScenario()] to provide a default scenario for \pkg{irace}.
#' [testing_fromfile()] provides a different interface for testing.
#'
#' @author Manuel López-Ibáñez and Leslie Pérez Cáceres
#' @concept running
#' @export
testing_fromlog <- function(logFile, testNbElites, testIterationElites,
testInstancesDir, testInstancesFile, testInstances)
{
if (is.null.or.empty(logFile)) {
irace_note("No logFile provided to perform the testing of configurations. Skipping testing.\n")
return(FALSE)
}
iraceResults <- read_logfile(logFile)
scenario <- iraceResults[["scenario"]]
instances_changed <- FALSE
if (!missing(testNbElites))
scenario[["testNbElites"]] <- testNbElites
if (!missing(testIterationElites))
scenario$testIterationElites <- testIterationElites
if (!missing(testInstances))
scenario[["testInstances"]] <- testInstances
if (!missing(testInstancesDir)) {
scenario$testInstancesDir <- testInstancesDir
instances_changed <- TRUE
}
if (!missing(testInstancesFile)) {
scenario$testInstancesFile <- testInstancesFile
instances_changed <- TRUE
}
cat("\n\n# Testing of elite configurations:", scenario$testNbElites,
"\n# Testing iteration configurations:", scenario$testIterationElites,"\n")
if (scenario$testNbElites <= 0)
return (FALSE)
# If they are already setup, don't change them.
if (instances_changed || is.null.or.empty(scenario[["testInstances"]])) {
scenario <- setup_test_instances(scenario)
if (is.null.or.empty(scenario[["testInstances"]])) {
irace_note("No test instances, skip testing\n")
return(FALSE)
}
}
# Get configurations that will be tested
if (scenario$testIterationElites)
testing_id <- sapply(iraceResults$allElites,
function(x) x[seq_len(min(length(x), scenario$testNbElites))])
else {
tmp <- iraceResults$allElites[[length(iraceResults$allElites)]]
testing_id <- tmp[seq_len(min(length(tmp), scenario$testNbElites))]
}
testing_id <- unique.default(unlist(testing_id))
configurations <- iraceResults$allConfigurations[testing_id, , drop=FALSE]
irace_note ("Testing configurations (in no particular order): ", paste(testing_id, collapse=" "), "\n")
testing_common(configurations, scenario, iraceResults)
return(TRUE)
}
#' Test configurations given an explicit table of configurations and a scenario file
#'
#' Executes the testing of an explicit list of configurations given in
#' `filename` (same format as in [readConfigurationsFile()]). A `logFile` is
#' created unless disabled in `scenario`. This may overwrite an existing one!
#'
#' @param filename `character(1)`\cr Path to a file containing configurations: one configuration
#' per line, one parameter per column, parameter names in header.
#'
#' @inheritParams defaultScenario
#'
#' @return iraceResults
#'
#' @seealso [testing_fromlog()] provides a different interface for testing.
#'
#' @author Manuel López-Ibáñez
#' @concept running
#' @export
testing_fromfile <- function(filename, scenario)
{
irace_note ("Checking scenario.\n")
scenario <- checkScenario(scenario)
if (!scenario$quiet) printScenario(scenario)
configurations <- readConfigurationsFile(filename, scenario$parameters)
configurations <- cbind(.ID. = seq_nrow(configurations), configurations, .PARENT. = NA_integer_)
rownames(configurations) <- configurations[[".ID."]]
num <- nrow(configurations)
configurations <- checkForbidden(configurations, scenario$parameters$forbidden)
if (nrow(configurations) < num) {
irace_warning("Some of the configurations in the configurations file were forbidden",
"and, thus, discarded.")
}
# To save the logs
iraceResults <- list(scenario = scenario,
irace_version = irace_version,
allConfigurations = configurations)
irace_note ("Testing configurations (in the order given as input): \n")
testing_common(configurations, scenario, iraceResults)
}
testing_common <- function(configurations, scenario, iraceResults)
{
verbose <- !scenario$quiet
if (verbose) configurations_print(configurations)
iraceResults$testing <- testConfigurations(configurations, scenario)
save_irace_logfile(iraceResults, logfile = scenario$logFile)
irace_note ("Testing results (column number is configuration ID in no particular order):\n")
if (verbose) print(cbind(seeds = iraceResults$testing$seeds,
as.data.frame(iraceResults$testing$experiments)))
irace_note ("Finished testing\n")
iraceResults
}
#' Test that the given irace scenario can be run.
#'
#' Test that the given irace scenario can be run by checking the scenario
#' settings provided and trying to run the target-algorithm.
#'
#' @inheritParams defaultScenario
#'
#' @return returns `TRUE` if successful and gives an error and returns `FALSE`
#' otherwise.
#'
#' @details If the `parameters` argument is missing, then the parameters
#' will be read from the file `parameterFile` given by `scenario`. If
#' `parameters` is provided, then `parameterFile` will not be read. This function will
#' try to execute the target-algorithm.
#'
#' @seealso
#' \describe{
#' \item{\code{\link{readScenario}}}{for reading a configuration scenario from a file.}
#' \item{\code{\link{printScenario}}}{prints the given scenario.}
#' \item{\code{\link{defaultScenario}}}{returns the default scenario settings of \pkg{irace}.}
#' \item{\code{\link{checkScenario}}}{to check that the scenario is valid.}
#' }
#' @author Manuel López-Ibáñez and Jérémie Dubois-Lacoste
#' @export
checkIraceScenario <- function(scenario)
{
irace_note ("Checking scenario\n")
scenario$debugLevel <- 2L
scenario <- checkScenario(scenario)
if (!scenario$quiet) printScenario(scenario)
irace_note("Checking target runner.\n")
if (checkTargetFiles(scenario = scenario)) {
irace_note("Check successful.\n")
return(TRUE)
}
irace_error("Check unsuccessful.\n")
return(FALSE)
}
init <- function()
{
irace_note("Initializing working directory...\n")
libPath <- system.file(package = "irace")
tmplFiles <- list.files(file.path(libPath, "templates"))
for (file in tmplFiles) {
if (grepl(".tmpl", file) && (file != "target-evaluator.tmpl")) {
newFile <- gsub(".tmpl", "", file)
if (file == "target-runner.tmpl" && .Platform$OS.type == 'windows') {
file.copy(file.path(libPath, "templates", "windows", "target-runner.bat"), file.path(getwd(), "target-runner.bat"), overwrite = FALSE)
} else {
file.copy(file.path(libPath, "templates", file), file.path(getwd(), newFile), overwrite = FALSE)
}
}
}
}
#' Launch `irace` with command-line options.
#'
#' Calls [irace_main()] using command-line options, maybe parsed from the
#' command line used to invoke R.
#'
#' @param argv `character()`\cr The arguments
#' provided on the R command line as a character vector, e.g.,
#' `c("--scenario", "scenario.txt", "-p", "parameters.txt")`.
#' Using the default value (not providing the parameter) is the
#' easiest way to call [irace_cmdline()].
#'
#' @details The function reads the parameters given on the command line
#' used to invoke R, finds the name of the scenario file,
#' initializes the scenario from the file (with the function
#' [readScenario()]) and possibly from parameters passed in
#' the command line. It finally starts \pkg{irace} by calling
#' [irace_main()].
#'
#' List of command-line options:
#' ```{r echo=FALSE,comment=NA}
#' cmdline_usage(.irace.params.def)
#' ```
#'
#' @templateVar return_invisible TRUE
#' @template return_irace
#'
#' @seealso
#' [irace_main()] to start \pkg{irace} with a given scenario.
#' @examples
#' irace_cmdline("--version")
#' @author Manuel López-Ibáñez and Jérémie Dubois-Lacoste
#' @concept running
#' @export
irace_cmdline <- function(argv = commandArgs(trailingOnly = TRUE))
{
parser <- CommandArgsParser$new(argv = argv, argsdef = .irace.params.def)
quiet <- !is.null(parser$readArg (short = "-q", long = "--quiet"))
if (quiet) {
op <- options(.irace.quiet = TRUE)
on.exit(options(op))
} else {
cat_irace_license()
cat("# installed at: ", system.file(package="irace"), "\n",
"# called with: ", paste(argv, collapse = " "), "\n", sep = "")
}
if (!is.null(parser$readArg(short = "-h", long = "--help"))) {
parser$cmdline_usage()
return(invisible(NULL))
}
if (!is.null(parser$readArg(short = "-v", long = "--version"))) {
print(utils::citation(package="irace"))
return(invisible(NULL))
}
if (!is.null(parser$readArg(short = "-i", long = "--init"))) {
init()
return(invisible(NULL))
}
# Read the scenario file and the command line
scenarioFile <- parser$readCmdLineParameter ("scenarioFile", default = "")
scenario <- readScenario(scenarioFile)
for (param in .irace.params.names) {
scenario[[param]] <-
parser$readCmdLineParameter(paramName = param, default = scenario[[param]])
}
if (quiet) scenario$quiet <- TRUE
# Check scenario
if (!is.null(parser$readArg (short = "-c", long = "--check"))) {
checkIraceScenario(scenario)
return(invisible(NULL))
}
# Only do testing
testFile <- parser$readArg (long = "--only-test")
if (!is.null(testFile)) {
return(invisible(testing_fromfile(testFile, scenario)))
}
if (length(parser$argv))
irace_error ("Unknown command-line options: ", paste(parser$argv, collapse = " "))
irace_common(scenario = scenario, simple=FALSE)
}
#' @rdname irace_cmdline
#' @export
irace.cmdline <- function(argv = commandArgs(trailingOnly = TRUE))
{
.Deprecated("irace.cmdline")
irace_cmdline(argv = argv)
}
## Check targetRunner execution
checkTargetFiles <- function(scenario)
{
## Create two random configurations
configurations <- sampleUniform(scenario$parameters, 2L,
repair = scenario$repairConfiguration)
set(configurations, j = ".ID.", value = seq_nrow(configurations))
setcolorder(configurations, ".ID.", before = 1L)
# Read initial configurations provided by the user.
initConfigurations <- allConfigurationsInit(scenario)
setDT(initConfigurations)
if (nrow(initConfigurations) > 0L) {
irace_assert(all(colnames(configurations) == colnames(initConfigurations)))
configurations <- rbindlist(list(initConfigurations, configurations))
set(configurations, j = ".ID.", value = seq_nrow(configurations))
}
bounds <- rep(scenario$boundMax, nrow(configurations))
instances_ID <- if (scenario$sampleInstances)
sample.int(length(scenario$instances), 1L) else 1L
setDF(configurations)
experiments <- createExperimentList(
configurations, scenario$parameters, instances = scenario$instances,
instances_ID = instances_ID, seeds = 1234567L, bounds = bounds)
race_state <- RaceState$new(scenario)
race_state$start_parallel(scenario)
on.exit(race_state$stop_parallel(), add = TRUE)
# FIXME: Create a function try.call(err.msg,warn.msg, fun, ...)
# Executing targetRunner
irace_note("Executing targetRunner (", nrow(configurations), " times)...\n")
result <- TRUE
# We cannot let targetRunner or targetEvaluator modify our random seed, so we save it.
withr::local_preserve_seed()
output <- withCallingHandlers(
tryCatch(execute_experiments(race_state, experiments, scenario),
error = function(e) {
cat(sep = "\n",
"\n# Error occurred while executing targetRunner:",
paste0(conditionMessage(e), collapse="\n"))
result <<- FALSE
NULL
}), warning = function(w) {
cat(sep = "\n",
"\n# Warning occurred while executing targetRunner:",
paste0(conditionMessage(w), collapse="\n"))
invokeRestart("muffleWarning")})
if (scenario$debugLevel >= 1L) {
cat("# targetRunner returned:\n")
print(output, digits = 15L)
}
irace_assert(is.null(scenario$targetEvaluator) == is.null(race_state$target_evaluator))
if (!result) return(FALSE)
if (!is.null(scenario$targetEvaluator)) {
irace_note("Executing targetEvaluator...\n")
output <- withCallingHandlers(
tryCatch(execute_evaluator(race_state$target_evaluator, experiments, scenario, output),
error = function(e) {
cat(sep = "\n",
"\n# Error ocurred while executing targetEvaluator:",
paste0(conditionMessage(e), collapse="\n"))
result <<- FALSE
NULL
}), warning = function(w) {
cat(sep = "\n",
"\n# Warning ocurred while executing targetEvaluator:",
paste0(conditionMessage(w), collapse="\n"))
invokeRestart("muffleWarning")})
if (scenario$debugLevel >= 1L) {
cat("# targetEvaluator returned:\n")
print(output, digits = 15L)
}
}
result
}