├── codecov.yml ├── .ignore ├── LICENSE ├── todo-files ├── shiny │ ├── run.R │ ├── www │ │ └── index.html │ ├── ui.R │ └── server.R ├── outdated_rmds │ ├── multi_point_proposal.Rmd │ ├── index.Rmd │ ├── quickstart.Rmd │ ├── model_based_optimization.Rmd │ └── parallelization.Rmd ├── bench │ ├── mlr │ │ ├── data │ │ │ ├── semeion.RData │ │ │ ├── semeion.zip │ │ │ ├── vehicle.RData │ │ │ ├── optdigits.RData │ │ │ └── waveform-5000.RData │ │ ├── trainPredict.R │ │ ├── algoTemplate.R │ │ ├── reduceResults.R │ │ ├── defs.R │ │ ├── multiplexer.R │ │ ├── algos.R │ │ └── batchexps.R │ ├── soo │ │ ├── reduceResults.R │ │ └── defs.R │ └── mco │ │ ├── defs.R │ │ ├── ZDT.R │ │ ├── plot_eafs.R │ │ ├── bench_normalization.R │ │ ├── DTLZ.R │ │ └── testfunctionsMultiObjective.R ├── renderPCPPlot.R ├── crit_mq.R ├── test_plot_multiobj.R ├── generateMBODesign.R ├── multiobj_test.R ├── infillOptMultiObjRandom.R ├── plotOptPath.R ├── svetlana.R ├── mspot_vs_parego.R ├── ensembleSelector.r ├── crit_eipi.R ├── test_mbo_kmforrester.R ├── test_kmlocal.R ├── test_plotEAF.R ├── crit_aei.R ├── crit_eqi.R ├── makeEnsemble.r ├── aydin.R ├── clement │ └── test_clement.R ├── RLearner_regr_kmforrester.R ├── todo.txt └── fragen.txt ├── pkgdown └── favicon │ ├── favicon.ico │ ├── favicon-16x16.png │ ├── favicon-32x32.png │ ├── apple-touch-icon.png │ ├── apple-touch-icon-120x120.png │ ├── apple-touch-icon-152x152.png │ ├── apple-touch-icon-180x180.png │ ├── apple-touch-icon-60x60.png │ └── apple-touch-icon-76x76.png ├── man-roxygen ├── arg_control.R ├── arg_parset.R ├── arg_showinfo.R ├── arg_handle_violations.R └── arg_plot_MBO.R ├── R ├── getSupportedInfillOptFunctions.R ├── trainModels.R ├── getSupportedMultipointInfillOptFunctions.R ├── makeTasks.R ├── proposePointsRandom.R ├── calculateGap.R ├── getInfillOptFunction.R ├── doc_mlrMBO_examples.R ├── convertToDesign.R ├── makeMBOOptPath.R ├── proposePointsParallelCB.R ├── makeTaskSingleObj.R ├── evalFinalPoint.R ├── checkLearner.R ├── proposePointsParEGO.R ├── shouldTerminate.R ├── getEffectiveBestPoint.R ├── evalProposedPoints.R ├── chooseFinalPoint.R ├── makeTasksMultiObj.R ├── mboContinue.R ├── mboFinalize.R ├── proposePointsConstantLiar.R ├── proposePointsMSPOT.R ├── initOptProblem.R ├── checkDesign.R ├── doc_mbo_parallel.R ├── filterProposedPoints.R ├── getMBOInfillCrit.R ├── OptState_setter.R ├── makeProposal.R ├── renderExampleRunPlotSingleCrit.R ├── proposePointsDIB.R ├── doc_mbo_OptPath.R ├── proposePoints.R ├── utils.R ├── initCrit.R ├── plot_MBOResult.R ├── evalMBODesign.R ├── proposePointsHelpers.R ├── infillOptEA.R ├── mboTemplate.R ├── zzz.R ├── multiobj_helpers.R └── makeMBOTrafoFunction.R ├── tic.R ├── man ├── OptProblem.Rd ├── getSupportedInfillOptFunctions.Rd ├── OptState.Rd ├── print.MBOControl.Rd ├── OptResult.Rd ├── proposePoints.Rd ├── finalizeSMBO.Rd ├── getGlobalOpt.Rd ├── getSupportedMultipointInfillOptFunctions.Rd ├── mboFinalize.Rd ├── makeMBOTrafoFunction.Rd ├── mboContinue.Rd ├── plot.OptState.Rd ├── updateSMBO.Rd ├── getMBOInfillCrit.Rd ├── MBOMultiObjResult.Rd ├── trafos.Rd ├── plotMBOResult.Rd ├── initCrit.Rd ├── mbo_parallel.Rd ├── MBOSingleObjResult.Rd ├── mbo_OptPath.Rd └── initSMBO.Rd ├── .editorconfig ├── tests ├── testthat.R └── testthat │ ├── test_mbo_resample.R │ ├── test_multipoint_cl.R │ ├── test_multiobj_helpers.R │ ├── helper_zzz.R │ ├── test_infill_optimizers.R │ ├── test_filter_proposed.R │ ├── test_infill_opt_ea.R │ ├── test_smoof_wrappers.R │ ├── test_multiobj_mspot.R │ ├── test_initCrit.R │ ├── test_check_design.R │ ├── test_plot_MBOResult.R │ ├── test_multipoint_moimbo.R │ ├── test_initdesign.R │ ├── test_adaptive_infillcrits.R │ ├── test_multiobj_dib.R │ ├── test_optpath_extras.R │ ├── test_termination_criteria.R │ ├── test_exampleRunMultiobj.R │ ├── test_multipoint_cb.R │ └── test_different_learners.R ├── mlrMBO.Rproj ├── .Rbuildignore ├── inst ├── CITATION └── examples │ ├── ex_2d_numeric.R │ ├── ex_multiobj_plots.R │ ├── ex_multiobj.R │ ├── ex_1d_numeric.R │ ├── ex_2d_numeric_multipoint.R │ ├── ex_1d_numeric_multipoint.R │ ├── ex_mixed.R │ ├── ex_1d_noisy_numeric.R │ └── ex_1d_discrete_noisy.R ├── src ├── macros.h ├── init.c └── hv.h ├── NEWS.md ├── NAMESPACE ├── vignettes └── supplementary │ └── adaptive_infill_criteria.Rmd └── DESCRIPTION /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | -------------------------------------------------------------------------------- /.ignore: -------------------------------------------------------------------------------- 1 | man/ 2 | todo-files/ 3 | docs/ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2018 2 | COPYRIGHT HOLDER: Bernd Bischl 3 | -------------------------------------------------------------------------------- /todo-files/shiny/run.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | runApp(getwd()) -------------------------------------------------------------------------------- /todo-files/outdated_rmds/multi_point_proposal.Rmd: -------------------------------------------------------------------------------- 1 | Work in progress! 2 | -------------------------------------------------------------------------------- /pkgdown/favicon/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/pkgdown/favicon/favicon.ico -------------------------------------------------------------------------------- /man-roxygen/arg_control.R: -------------------------------------------------------------------------------- 1 | #' @param control [\code{\link{MBOControl}}]\cr 2 | #' Control object for mbo. 3 | -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/pkgdown/favicon/favicon-16x16.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/pkgdown/favicon/favicon-32x32.png -------------------------------------------------------------------------------- /man-roxygen/arg_parset.R: -------------------------------------------------------------------------------- 1 | #' @param par.set [\code{\link[ParamHelpers]{ParamSet}}]\cr 2 | #' See \code{\link{mbo}}. 3 | -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/pkgdown/favicon/apple-touch-icon.png -------------------------------------------------------------------------------- /todo-files/bench/mlr/data/semeion.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/todo-files/bench/mlr/data/semeion.RData -------------------------------------------------------------------------------- /todo-files/bench/mlr/data/semeion.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/todo-files/bench/mlr/data/semeion.zip -------------------------------------------------------------------------------- /todo-files/bench/mlr/data/vehicle.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/todo-files/bench/mlr/data/vehicle.RData -------------------------------------------------------------------------------- /todo-files/bench/mlr/data/optdigits.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/todo-files/bench/mlr/data/optdigits.RData -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-120x120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/pkgdown/favicon/apple-touch-icon-120x120.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-152x152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/pkgdown/favicon/apple-touch-icon-152x152.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-180x180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/pkgdown/favicon/apple-touch-icon-180x180.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-60x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/pkgdown/favicon/apple-touch-icon-60x60.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-76x76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/pkgdown/favicon/apple-touch-icon-76x76.png -------------------------------------------------------------------------------- /man-roxygen/arg_showinfo.R: -------------------------------------------------------------------------------- 1 | #' @param show.info [\code{logical(1)}]\cr 2 | #' Verbose output on console? 3 | #' Default is \code{TRUE}. 4 | -------------------------------------------------------------------------------- /todo-files/bench/mlr/data/waveform-5000.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlr-org/mlrMBO/HEAD/todo-files/bench/mlr/data/waveform-5000.RData -------------------------------------------------------------------------------- /todo-files/bench/mlr/trainPredict.R: -------------------------------------------------------------------------------- 1 | trainPredict = function(lrn, task, rin, iter) { 2 | rin2 = makeFixedHoldoutInstance(rin$train.inds[[iter]], rin$test.inds[[iter]], task$task.desc$size) 3 | resample(lrn, task, rin2) 4 | } 5 | -------------------------------------------------------------------------------- /R/getSupportedInfillOptFunctions.R: -------------------------------------------------------------------------------- 1 | #' @title Get names of supported infill-criteria optimizers. 2 | #' @description 3 | #' None. 4 | #' @return [\code{character}] 5 | #' @export 6 | getSupportedInfillOptFunctions = function() { 7 | c("cmaes", "focussearch", "ea", "nsga2") 8 | } 9 | -------------------------------------------------------------------------------- /man-roxygen/arg_handle_violations.R: -------------------------------------------------------------------------------- 1 | #' @param handle.violations [\code{character(1)}]\cr 2 | #' What should be done, if negative values occur? Setting this option to 3 | #' \dQuote{warn}, which is the default, shifts the function. \dQuote{error} 4 | #' stops the process immediately. 5 | -------------------------------------------------------------------------------- /tic.R: -------------------------------------------------------------------------------- 1 | do_package_checks(error_on = "warning", codecov = FALSE) 2 | 3 | if (ci_has_env("BUILD_PKGDOWN")) { 4 | get_stage("install") %>% 5 | add_step(step_install_github("mlr-org/mlr3pkgdowntemplate")) %>% 6 | add_step(step_install_cran("animation")) %>% 7 | add_step(step_install_cran("e1071")) 8 | do_pkgdown() 9 | } 10 | -------------------------------------------------------------------------------- /R/trainModels.R: -------------------------------------------------------------------------------- 1 | trainModels = function(learner, tasks, control) { 2 | 3 | models = vector("list", length(tasks)) 4 | secs = 0 5 | 6 | for (i in seq_along(models)) { 7 | secs = secs + measureTime({ 8 | models[[i]] = train(learner, tasks[[i]]) 9 | }) 10 | } 11 | 12 | list(models = models, train.time = secs) 13 | } 14 | -------------------------------------------------------------------------------- /todo-files/bench/mlr/algoTemplate.R: -------------------------------------------------------------------------------- 1 | algoTemplate = function(static, iter, tctrl) { 2 | 3 | task = static$task 4 | tdesc = task$task.desc 5 | rdesc = makeResampleDesc("CV", iters = 2L) 6 | tw = makeTuneWrapper(mplexer, resampling = rdesc, par.set = par.set, control = tctrl) 7 | trainPredict(tw, static$task, static$rin, iter) 8 | } 9 | 10 | -------------------------------------------------------------------------------- /R/getSupportedMultipointInfillOptFunctions.R: -------------------------------------------------------------------------------- 1 | #' @title Get names of supported multi-point infill-criteria optimizers. 2 | #' 3 | #' @description 4 | #' Returns all names of supported multi-point infill-criteria optimizers. 5 | #' 6 | #' @return [\code{character}] 7 | #' @export 8 | getSupportedMultipointInfillOptFunctions = function() { 9 | c("cl", "cb", "moimbo") 10 | } 11 | -------------------------------------------------------------------------------- /man/OptProblem.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/OptProblem.R 3 | \name{OptProblem} 4 | \alias{OptProblem} 5 | \title{OptProblem object.} 6 | \description{ 7 | The OptProblem contains all the constants values which define a OptProblem within our MBO Steps. 8 | It is an environment and is always pointed at by the OptState. 9 | } 10 | -------------------------------------------------------------------------------- /todo-files/bench/soo/reduceResults.R: -------------------------------------------------------------------------------- 1 | library(BBmisc) 2 | library(BatchExperiments) 3 | library(plyr) 4 | 5 | reg = loadRegistry("soo_bench-files") 6 | 7 | res = reduceResultsExperiments(reg, fun = function(job, res) res[c("y", "gap")]) 8 | 9 | aggr = ddply(res, getResultVars(res), summarise, y = mean(y), gap = mean(gap)) 10 | 11 | save2(res = res, aggr = aggr, file = "results.RData") 12 | 13 | 14 | -------------------------------------------------------------------------------- /todo-files/bench/soo/defs.R: -------------------------------------------------------------------------------- 1 | BBOB_FIDS = 1:2 2 | BBOB_DIMS = c(2) 3 | BUDGET = function(d) d * 100 4 | 5 | MBO_SURROGATE = "regr.km" 6 | MBO_SURROGATE_VALS = list() 7 | MBO_INFILL_CRIT = "ei" 8 | 9 | MBO_INIT_DES_SIZE = function(d) 10 * d 10 | MBO_ITERS = function(d) BUDGET(d) - MBO_INIT_DES_SIZE(d) 11 | 12 | MBO_FOCUSSEARCH_RESTARTS = 2L 13 | MBO_FOCUSSEARCH_ITERS = 2L 14 | MBO_FOCUSSEARCH_POINTS = 500L 15 | 16 | 17 | -------------------------------------------------------------------------------- /man/getSupportedInfillOptFunctions.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getSupportedInfillOptFunctions.R 3 | \name{getSupportedInfillOptFunctions} 4 | \alias{getSupportedInfillOptFunctions} 5 | \title{Get names of supported infill-criteria optimizers.} 6 | \usage{ 7 | getSupportedInfillOptFunctions() 8 | } 9 | \value{ 10 | [\code{character}] 11 | } 12 | \description{ 13 | None. 14 | } 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # See http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf8 6 | end_of_line = lf 7 | insert_final_newline = true 8 | indent_style = space 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | indent_size = 2 13 | 14 | [*.{r,R}] 15 | indent_size = 2 16 | 17 | [*.{c,h}] 18 | indent_size = 4 19 | 20 | [*.{cpp,hpp}] 21 | indent_size = 4 22 | 23 | [{NEWS,DESCRIPTION,LICENSE}] 24 | max_line_length = 80 25 | -------------------------------------------------------------------------------- /man/OptState.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/OptState.R 3 | \name{OptState} 4 | \alias{OptState} 5 | \title{OptState object.} 6 | \description{ 7 | The OptState is the central component of the mbo iterations. 8 | This environment contains every necessary information needed during optimization in MBO. 9 | It also links to the \code{\link{OptProblem}} and to the \code{\link{OptResult}}. 10 | } 11 | -------------------------------------------------------------------------------- /man/print.MBOControl.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/makeMBOControl.R 3 | \name{print.MBOControl} 4 | \alias{print.MBOControl} 5 | \title{Print mbo control object.} 6 | \usage{ 7 | \method{print}{MBOControl}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{[\code{\link{MBOControl}}]\cr 11 | Control object.} 12 | 13 | \item{...}{[any]\cr 14 | Not used.} 15 | } 16 | \description{ 17 | Print mbo control object. 18 | } 19 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | 3 | # the unit tests take pretty long, that can be a problem on WB and cran (and maybe annoying locally) 4 | # so we run all tests only on travis and if a certain user env var is set 5 | if (identical(Sys.getenv("TRAVIS"), "true") || identical(Sys.getenv("R_EXPENSIVE_TEST_OK"), "true") || identical(Sys.getenv("NOT_CRAN"), "true")) { 6 | test_check("mlrMBO") 7 | } else { 8 | test_check("mlrMBO", filter = "((mbo_rf)|(mbo_km))") 9 | } 10 | -------------------------------------------------------------------------------- /todo-files/renderPCPPlot.R: -------------------------------------------------------------------------------- 1 | 2 | renderPCPPlot = function(data, xnames, alpha, scale = "globalminmax") { 3 | requirePackages("GGally") 4 | 5 | # ggdata = data[, colnames, drop = FALSE] 6 | columns = match(xnames, colnames(data)) 7 | args = list(columns = columns, scale = scale) 8 | if (alpha) { 9 | data$.alpha = normalize(data$dob, "range", range = c(0, 1)) 10 | args$alphaLines = ".alpha" 11 | } 12 | args$data = data 13 | do.call(ggparcoord, args) 14 | } 15 | -------------------------------------------------------------------------------- /man/OptResult.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/OptResult.R 3 | \name{OptResult} 4 | \alias{OptResult} 5 | \title{OptResult object.} 6 | \description{ 7 | The OptResult stores all entities which are not needed while optimizing but are needed to build the final result. 8 | It can contains fitted surrogate models at certain times as well as resample objects. 9 | When the optimization ended it will contain the [\code{MBOResult}]. 10 | } 11 | -------------------------------------------------------------------------------- /mlrMBO.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^todo-files$ 2 | ^\.gitignore$ 3 | ^\.travis\.yml$ 4 | ^appveyor\.yml$ 5 | ^.*tar\.gz$ 6 | ^src/.*\.o$ 7 | ^src/.*\.so$ 8 | ^.*\.Rproj$ 9 | ^\.Rproj\.user$ 10 | ^man-roxygen$ 11 | ^\.editorconfig$ 12 | ^\.ignore$ 13 | ^codecov\.yml$ 14 | ^_pkgdown\.yml$ 15 | ^docs$ 16 | ^vignettes/.+_cache$ 17 | ^vignettes/.+_files$ 18 | ^vignettes/.+\.html$ 19 | .github 20 | .covrignore 21 | ^pkgdown$ 22 | ^tic\.R$ 23 | ^README_.*$ 24 | ^README.Rmd$ 25 | ^animation.gif$ 26 | ^\.ccache$ 27 | ^\.github$ 28 | -------------------------------------------------------------------------------- /man/proposePoints.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/proposePoints.R 3 | \name{proposePoints} 4 | \alias{proposePoints} 5 | \title{Propose candidates for the objective function} 6 | \usage{ 7 | proposePoints(opt.state) 8 | } 9 | \arguments{ 10 | \item{opt.state}{[\code{\link{OptState}}]} 11 | } 12 | \description{ 13 | Propose points for the objective function that should be evaluated according to the infill criterion and the recent evaluations. 14 | } 15 | -------------------------------------------------------------------------------- /man/finalizeSMBO.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/SMBO.R 3 | \name{finalizeSMBO} 4 | \alias{finalizeSMBO} 5 | \title{Finalizes the SMBO Optimization} 6 | \usage{ 7 | finalizeSMBO(opt.state) 8 | } 9 | \arguments{ 10 | \item{opt.state}{[\code{\link{OptState}}] 11 | The optimization state.} 12 | } 13 | \value{ 14 | [\code{\link{MBOSingleObjResult}} | \code{\link{MBOMultiObjResult}}] 15 | } 16 | \description{ 17 | Returns the common mlrMBO result object. 18 | } 19 | -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | bibentry( 2 | bibtype = "Manual", 3 | key = "mlrMBO", 4 | title = "{{{mlrMBO}}: {{A Modular Framework}} for {{Model}}-{{Based Optimization}} of {{Expensive Black}}-{{Box Functions}}}", 5 | author = c( 6 | person("Bernd", "Bischl"), 7 | person("Jakob", "Richter"), 8 | person("Jakob", "Bossek"), 9 | person("Daniel", "Horn"), 10 | person("Janek", "Thomas"), 11 | person("Michel", "Lang") 12 | ), 13 | year = "2017", 14 | url = "https://arxiv.org/abs/1703.03373" 15 | ) 16 | -------------------------------------------------------------------------------- /R/makeTasks.R: -------------------------------------------------------------------------------- 1 | #FIXME: Doku 2 | 3 | makeTasks = function(opt.state) { 4 | opt.problem = getOptStateOptProblem(opt.state) 5 | control = getOptProblemControl(opt.problem) 6 | opt.path = getOptStateOptPath(opt.state) 7 | if (control$n.objectives == 1L) { 8 | tasks = list(makeTaskSingleObj(opt.path, control)) 9 | } else { 10 | if (control$multiobj.method == "parego") 11 | tasks = makeTasksParEGO(opt.state) 12 | else 13 | tasks = makeTasksMultiObj(opt.path, control) 14 | } 15 | return(tasks) 16 | } 17 | -------------------------------------------------------------------------------- /todo-files/bench/mlr/reduceResults.R: -------------------------------------------------------------------------------- 1 | library(BBmisc) 2 | library(BatchExperiments) 3 | library(plyr) 4 | 5 | reg = loadRegistry("modelsel-files") 6 | 7 | res1 = reduceResultsExperiments(reg, fun = function(job, res) res$aggr) 8 | 9 | res2 = reduceResultsExperiments(reg, fun = function(job, res) 10 | c(as.list(res$aggr), res$extract[[1]]$x)) 11 | 12 | aggr = ddply(res1, setdiff(getResultVars(res1), "iter"), summarise, err = mean(mmce.test.mean)) 13 | 14 | save2(res1 = res1, res2 = res2, aggr = aggr, file = "results.RData") 15 | 16 | -------------------------------------------------------------------------------- /man/getGlobalOpt.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/exampleRun.R 3 | \name{getGlobalOpt} 4 | \alias{getGlobalOpt} 5 | \title{Helper function which returns the (estimated) global optimum.} 6 | \usage{ 7 | getGlobalOpt(run) 8 | } 9 | \arguments{ 10 | \item{run}{[\code{MBOExampleRun}]\cr 11 | Object of type \code{MBOExampleRun}.} 12 | } 13 | \value{ 14 | [\code{numeric(1)}]. (Estimated) global optimum. 15 | } 16 | \description{ 17 | Helper function which returns the (estimated) global optimum. 18 | } 19 | -------------------------------------------------------------------------------- /todo-files/bench/mlr/defs.R: -------------------------------------------------------------------------------- 1 | configureMlr(on.learner.error = "warn") 2 | 3 | CV_FOLDS_OUTER = 5L 4 | TUNE_BUDGET = 50L 5 | 6 | # IRACE_INNER_SPLIT = 0.8 7 | 8 | MBO_INIT_DES_SIZE = round(TUNE_BUDGET / 3) 9 | MBO_ITERS = TUNE_BUDGET - MBO_INIT_DES_SIZE 10 | # MBO_INIT_DES_SIZE = 20 11 | # MBO_ITERS = 1 12 | 13 | MBO_SURROGATE = "regr.randomForest" 14 | MBO_SURROGATE_VALS = list(ntree = 10L) 15 | MBO_INFILL_CRIT = "ei" 16 | 17 | MBO_FOCUSSEARCH_RESTARTS = 2L 18 | MBO_FOCUSSEARCH_ITERS = 2L 19 | MBO_FOCUSSEARCH_POINTS = 500L 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /man/getSupportedMultipointInfillOptFunctions.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getSupportedMultipointInfillOptFunctions.R 3 | \name{getSupportedMultipointInfillOptFunctions} 4 | \alias{getSupportedMultipointInfillOptFunctions} 5 | \title{Get names of supported multi-point infill-criteria optimizers.} 6 | \usage{ 7 | getSupportedMultipointInfillOptFunctions() 8 | } 9 | \value{ 10 | [\code{character}] 11 | } 12 | \description{ 13 | Returns all names of supported multi-point infill-criteria optimizers. 14 | } 15 | -------------------------------------------------------------------------------- /R/proposePointsRandom.R: -------------------------------------------------------------------------------- 1 | # Proposes n points randomly by calling generateRandomDesign. 2 | # crit.vals, propose.time and errors.model are all set to NA 3 | proposePointsRandom = function(opt.state) { 4 | opt.problem = getOptStateOptProblem(opt.state) 5 | control = getOptProblemControl(opt.problem) 6 | par.set = getOptProblemParSet(opt.problem) 7 | n = control$interleave.random.points 8 | makeProposal( 9 | control = control, 10 | prop.points = generateRandomDesign(par.set = par.set, n = n), 11 | prop.type = rep("random_interleave", n) 12 | ) 13 | } 14 | -------------------------------------------------------------------------------- /man/mboFinalize.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mboFinalize.R 3 | \name{mboFinalize} 4 | \alias{mboFinalize} 5 | \title{Finalizes an mbo run from a save-file.} 6 | \usage{ 7 | mboFinalize(file) 8 | } 9 | \arguments{ 10 | \item{file}{[\code{character(1)}]\cr 11 | File path of saved MBO state. 12 | See \code{save.on.disk.at} argument of \code{\link{MBOControl}} object.} 13 | } 14 | \value{ 15 | See \code{\link{mbo}}. 16 | } 17 | \description{ 18 | Useful if your optimization didn't terminate but you want a results nonetheless. 19 | } 20 | -------------------------------------------------------------------------------- /src/macros.h: -------------------------------------------------------------------------------- 1 | #ifndef MLRMBO_MACROS_H_ 2 | #define MLRMBO_MACROS_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define MAX(a,b) (((a)>(b))?(a):(b)) 9 | 10 | #define UNPACK_REAL_VECTOR(S, D, N) \ 11 | double *D = REAL(S); \ 12 | const R_len_t N = length(S); 13 | 14 | #define UNPACK_REAL_MATRIX(S, D, N, K) \ 15 | double *D = REAL(S); \ 16 | const R_len_t N = nrows(S); \ 17 | const R_len_t K = ncols(S); 18 | 19 | #define UNPACK_REAL_MATRIX_2(S, D, N) \ 20 | double *D = REAL(S); \ 21 | const R_len_t N = nrows(S); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /tests/testthat/test_mbo_resample.R: -------------------------------------------------------------------------------- 1 | context("mbo resample") 2 | 3 | test_that("mbo works with resampling", { 4 | learner = makeLearner("regr.randomForest", predict.type = "se", se.method = "sd") 5 | ctrl = makeMBOControl(resample.at = c(1, 3)) 6 | ctrl = setMBOControlTermination(ctrl, iters = 5L) 7 | ctrl = setMBOControlInfill(ctrl, opt.focussearch.points = 10L) 8 | or = mbo(testf.fsphere.2d, testd.fsphere.2d, learner, ctrl) 9 | x = or$resample.results 10 | expect_list(x, len = 2) 11 | expect_true(is.numeric(x[[1]]$aggr) && is.numeric(x[[2]]$aggr)) 12 | expect_equal(names(x), c("1", "3")) 13 | }) 14 | -------------------------------------------------------------------------------- /src/init.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // for NULL 4 | #include 5 | 6 | /* .Call calls */ 7 | extern SEXP c_eps_indicator(SEXP, SEXP); 8 | extern SEXP c_sms_indicator(SEXP, SEXP, SEXP, SEXP, SEXP); 9 | 10 | static const R_CallMethodDef CallEntries[] = { 11 | {"c_eps_indicator", (DL_FUNC) &c_eps_indicator, 2}, 12 | {"c_sms_indicator", (DL_FUNC) &c_sms_indicator, 5}, 13 | {NULL, NULL, 0} 14 | }; 15 | 16 | void R_init_mlrMBO(DllInfo *dll) { 17 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); 18 | R_useDynamicSymbols(dll, FALSE); 19 | } 20 | -------------------------------------------------------------------------------- /R/calculateGap.R: -------------------------------------------------------------------------------- 1 | # Calculates gap between best point and global optimum. 2 | # 3 | # Used in the exampleRun functions and printed in plots. 4 | # 5 | # @param design [\code{data.frame}]\cr 6 | # Initial design as data frame. 7 | # @param global.opt [\code{numeric(1)]\cr 8 | # Value of the known global optimum. 9 | # @param control [\code{\link{MBOControl}}]\cr 10 | # MBO control object. 11 | # @return [\code{numeric(1)}] 12 | calculateGap = function(design, global.opt, control) { 13 | best.y = if (control$minimize) min(design[, control$y.name]) else max(design[, control$y.name]) 14 | abs(best.y - global.opt) 15 | } 16 | -------------------------------------------------------------------------------- /R/getInfillOptFunction.R: -------------------------------------------------------------------------------- 1 | # Trivial dispatcher for infill optimizers. 2 | # 3 | # @note Keep in mind to update getSupportedInfillOptFunctions too, 4 | # if a new method is implemented. 5 | # 6 | # @param infill.opt [\code{character(1)}]\cr 7 | # String key for infill optimizer. 8 | # @return [\code{function}] 9 | getInfillOptFunction = function(infill.opt) { 10 | switch(infill.opt, 11 | cmaes = infillOptCMAES, 12 | focussearch = infillOptFocus, 13 | ea = infillOptEA, 14 | nsga2 = infillOptMultiObjNSGA2, 15 | # default: try to match the fun which is given as string 16 | match.fun(infill.opt) 17 | ) 18 | } 19 | -------------------------------------------------------------------------------- /tests/testthat/test_multipoint_cl.R: -------------------------------------------------------------------------------- 1 | context("multipoint constant liar") 2 | 3 | test_that("multipoint constant liar", { 4 | for (crit in list(crit.ei, crit.aei)) { 5 | ctrl = makeMBOControl(propose.points = 5L) 6 | ctrl = setMBOControlTermination(ctrl, iters = 2L) 7 | ctrl = setMBOControlInfill(ctrl, crit = crit) 8 | ctrl = setMBOControlMultiPoint(ctrl, method = "cl") 9 | res = mbo(testf.fsphere.1d, testd.fsphere.1d, control = ctrl) 10 | expect_is(res, "MBOResult", info = crit$id) 11 | expect_lt(res$y, 0.1) 12 | expect_equal(getOptPathDOB(res$opt.path), rep(0:2, each = 5), info = crit$ido) 13 | } 14 | }) 15 | -------------------------------------------------------------------------------- /R/doc_mlrMBO_examples.R: -------------------------------------------------------------------------------- 1 | #' @title mlrMBO examples 2 | #' 3 | #' @description 4 | #' Different scenarios of the usage of mlrMBO and visualizations. 5 | #' 6 | #' @example /inst/examples/ex_1d_numeric.R 7 | #' @example /inst/examples/ex_2d_numeric.R 8 | #' @example /inst/examples/ex_1d_numeric_multipoint.R 9 | #' @example /inst/examples/ex_2d_numeric_multipoint.R 10 | #' @example /inst/examples/ex_1d_noisy_numeric.R 11 | #' @example /inst/examples/ex_1d_discrete_noisy.R 12 | #' @example /inst/examples/ex_mixed.R 13 | #' @example /inst/examples/ex_multiobj.R 14 | #' @example /inst/examples/ex_multiobj_plots.R 15 | #' 16 | #' @name mlrMBO_examples 17 | NULL 18 | -------------------------------------------------------------------------------- /man/makeMBOTrafoFunction.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/makeMBOTrafoFunction.R 3 | \name{makeMBOTrafoFunction} 4 | \alias{makeMBOTrafoFunction} 5 | \title{Create a transformation function for MBOExampleRun.} 6 | \usage{ 7 | makeMBOTrafoFunction(name, fun) 8 | } 9 | \arguments{ 10 | \item{name}{[\code{character(1)}]\cr 11 | Name of the transformation.} 12 | 13 | \item{fun}{[\code{function}]\cr 14 | R function which expects a numeric vector.} 15 | } 16 | \value{ 17 | Object of type MBOTrafoFunction. 18 | } 19 | \description{ 20 | Creates a transformation function for MBOExampleRun. 21 | } 22 | \seealso{ 23 | \link{trafos} 24 | } 25 | -------------------------------------------------------------------------------- /tests/testthat/test_multiobj_helpers.R: -------------------------------------------------------------------------------- 1 | context("multi-objective helpers") 2 | 3 | test_that("multi-objective helpers", { 4 | # we had a bug here 5 | m = matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE) 6 | e = getWorstExtremePoint(m, minimize = c(TRUE, TRUE)) 7 | expect_equal(e, c(3, 4)) 8 | 9 | e = getWorstExtremePoint(m, minimize = c(FALSE, TRUE)) 10 | expect_equal(e, c(1, 4)) 11 | 12 | # we had a bug here 13 | m = matrix(c(1, 2, 3, 4, -10, 2), 3, 2, byrow = TRUE) 14 | 15 | e = getWorstExtremePoint(m, minimize = c(TRUE, TRUE)) 16 | expect_equal(e, c(3, 4)) 17 | e = getWorstExtremePoint(m, minimize = c(FALSE, FALSE)) 18 | expect_equal(e, c(-10, 2)) 19 | }) 20 | -------------------------------------------------------------------------------- /tests/testthat/helper_zzz.R: -------------------------------------------------------------------------------- 1 | # We want to run on cran with a fixed seed (1) so that tests will not fail stochastically 2 | # For local testing + Travis we want to detect and be able reproduce these so we sample a seed 3 | if (identical(Sys.getenv("TRAVIS"), "true") || identical(Sys.getenv("R_EXPENSIVE_TEST_OK"), "true")) { 4 | seed.val = sample(1:100, size = 1) 5 | set.seed(seed.val) 6 | # Only on Travis logs we want to see the used seed 7 | if (identical(Sys.getenv("TRAVIS"), "true")) { 8 | catf("Run test with seed: %i", seed.val) 9 | } 10 | } else { 11 | set.seed(1) 12 | } 13 | 14 | configureMlr(show.learner.output = FALSE) 15 | options(mlrMBO.show.info = FALSE, mlrMBO.debug.mode = TRUE) 16 | -------------------------------------------------------------------------------- /R/convertToDesign.R: -------------------------------------------------------------------------------- 1 | # Takes x.vals and y.vals from the opt.path, possibly imputes missing x.vals and 2 | # returns a data.frame. 3 | # 4 | # @param opt.state [OptState] 5 | # @param opt.path [\code{\link[ParamHelpers]{optPath}}]\cr 6 | # Optimization path. 7 | # Might be altered for multipoint proposals etc. 8 | # @param control [\code{\link{MBOControl}}]\cr 9 | # MBO control object. 10 | # Might be altered for multipoint proposals etc. 11 | # @return [\code{data.frame}] 12 | # With x and y columns 13 | 14 | convertToDesign = function(opt.path, control) { 15 | df = as.data.frame(opt.path, include.rest = FALSE) 16 | convertDataFrameCols(df, ints.as.num = TRUE, logicals.as.factor = TRUE) 17 | } 18 | -------------------------------------------------------------------------------- /R/makeMBOOptPath.R: -------------------------------------------------------------------------------- 1 | # Create the opt.path with MBO-specific defaults - 2 | # allways include error.message, exec.time and extra params, but never add transformed x. 3 | # 4 | # @param opt.problem [\code{OptProblem}]\cr 5 | # OptProblem Object 6 | # @return [\code{\link[ParamHelpers]{optPath}}] 7 | makeMBOOptPath = function(opt.problem) { 8 | par.set = getOptProblemParSet(opt.problem) 9 | control = getOptProblemControl(opt.problem) 10 | makeOptPathDF( 11 | par.set = par.set, 12 | y.names = control$y.name, 13 | minimize = control$minimize, 14 | add.transformed.x = FALSE, 15 | include.error.message = TRUE, 16 | include.exec.time = TRUE, 17 | include.extra = TRUE 18 | ) 19 | } 20 | -------------------------------------------------------------------------------- /todo-files/crit_mq.R: -------------------------------------------------------------------------------- 1 | # Minimal Quantil Criteria 2 | # FIXME: control$mq.beta in MBOControl! 3 | 4 | infillCritMQ = function(points, model, control, par.set, design) { 5 | maximize.mult = ifelse(control$minimize, 1, -1) 6 | y = maximize.mult * design[, control$y.name] 7 | p = predict(model, newdata = points)$data 8 | p.mu = maximize.mult * p$response 9 | p.se = p$se 10 | #y.min = min(y) 11 | 12 | if(is.null(control$mq.beta)==T){beta=0.9}else{beta=control$mq.quantile} 13 | q <- qnorm(beta) 14 | 15 | mq=p.mu+pnorm(beta)^(-1)*p.se 16 | 17 | # FIXME magic number 18 | # if se too low set 0 (numerical problems), negate due to minimization 19 | ifelse(p.se < 1e-6, 0, -mq) 20 | } 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /R/proposePointsParallelCB.R: -------------------------------------------------------------------------------- 1 | proposePointsParallelCB = function(opt.state) { 2 | 3 | opt.problem = getOptStateOptProblem(opt.state) 4 | control = getOptProblemControl(opt.problem) 5 | 6 | # draw lambdas from exp dist + create 1 control for each for single-objective with lambda-CB 7 | lambdas = rexp(control$propose.points) 8 | controls = createSinglePointControls(control, makeMBOInfillCritCB, crit.pars = .mapply(list, list(cb.lambda = lambdas), list())) 9 | 10 | props = parallelMap(proposePointsByInfillOptimization, control = controls, level = "mlrMBO.propose.points", 11 | more.args = list(opt.state = opt.state)) 12 | 13 | res = joinProposedPoints(props) 14 | res$multipoint.cb.lambdas = lambdas 15 | return(res) 16 | } 17 | -------------------------------------------------------------------------------- /man/mboContinue.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mboContinue.R 3 | \name{mboContinue} 4 | \alias{mboContinue} 5 | \title{Continues an mbo run from a save-file.} 6 | \usage{ 7 | mboContinue(opt.state) 8 | } 9 | \arguments{ 10 | \item{opt.state}{[\code{OptState} | \code{character(1)}]\cr 11 | Either the \code{save.state} slot of an \code{MBOResult} object, or a file path of a saved MBO state. 12 | See \code{save.on.disk.at} argument of \code{\link{MBOControl}} object.} 13 | } 14 | \value{ 15 | See \code{\link{mbo}}. 16 | } 17 | \description{ 18 | Useful if your optimization is likely to crash, 19 | so you can continue from a save point and will not lose too much information and runtime. 20 | } 21 | -------------------------------------------------------------------------------- /todo-files/test_plot_multiobj.R: -------------------------------------------------------------------------------- 1 | library(mco) 2 | library(devtools) 3 | library(checkmate) 4 | load_all() 5 | source("todo-files/plot_multiobjMBO.R") 6 | f = makeMBOFunction(zdt1) 7 | ps = makeNumericParamSet(len = 5L, lower = 0, upper = 1) 8 | 9 | set.seed(1) 10 | # Test normal run 11 | learner = makeLearner("regr.km", predict.type = "se") 12 | ctrl = makeMBOControl(iters = 10L, n.objectives = 2L, init.design.points = 8L, 13 | propose.points = 2L) 14 | ctrl = setMBOControlInfill(ctrl, crit = "ei", opt.focussearch.points = 1000L, 15 | opt.focussearch.maxit = 3L) 16 | ctrl = setMBOControlMultiObj(ctrl, method = "parego")#, dib.indicator = "eps") 17 | or = mbo(f, ps, learner = learner, control = ctrl) 18 | plot(or, alpha = TRUE, infill.crit = "ei", log.infill.crit = FALSE, pause = TRUE) 19 | -------------------------------------------------------------------------------- /todo-files/generateMBODesign.R: -------------------------------------------------------------------------------- 1 | 2 | generateMBODesign.OptProblem = function(opt.problem) { 3 | control = getOptProblemControl(opt.problem) 4 | par.set = getOptProblemParSet(opt.problem) 5 | design.x = generateDesign(control$init.design.points, par.set, fun = control$init.design.fun, fun.args = control$init.design.args, trafo = FALSE) 6 | points.diff = control$init.design.points - nrow(design.x) 7 | if (points.diff > 0L) { 8 | warningf("Could not generate enough points for init design: Only got %i / %i. Augmenting with %i random points now!", 9 | nrow(design.x), control$init.design.points, points.diff) 10 | design.x.rand = generateRandomDesign(points.diff, par.set, trafo = FALSE) 11 | design.x = rbind(design.x, design.x.rand) 12 | } 13 | return(design.x) 14 | } 15 | 16 | -------------------------------------------------------------------------------- /todo-files/multiobj_test.R: -------------------------------------------------------------------------------- 1 | 2 | library(soobench) 3 | 4 | load_all(".") 5 | 6 | set.seed(1) 7 | 8 | obj = generate_bbob2009_function(2L, fid=17L, iid=1) 9 | 10 | ctrl = makeMBOControl( 11 | noisy = FALSE, 12 | init.design.points = 10L, 13 | iters = 5L, 14 | propose.points = 5L, 15 | multipoint.method = "moimbo", 16 | multipoint.moimbo.objective = "mean.se.dist", 17 | multipoint.moimbo.eta = 15, 18 | multipoint.moimbo.p = 1, 19 | multipoint.moimbo.maxit = 200L, 20 | infill.crit="ei" 21 | ) 22 | 23 | lrn = makeLearner("regr.km", covtype="matern3_2", predict.type="se") 24 | # run = exampleRun(obj, learner=lrn, control=ctrl) 25 | print( 26 | autoplot(run, pause=T, trafo=list( 27 | y=logTrafo(), 28 | yhat=logTrafo(), 29 | se=logTrafo() 30 | ))$pl.all 31 | ) 32 | -------------------------------------------------------------------------------- /todo-files/infillOptMultiObjRandom.R: -------------------------------------------------------------------------------- 1 | # FIXME: For the moment excluded - talk about it 2 | # Random Search for the multi-objective optimization of all models 3 | #infillOptMultiObjRandom = function(infill.crit, models, control, par.set, opt.path, design, ...) { 4 | # requirePackages("emoa") 5 | # 6 | # newdesign = generateDesign(control$infill.opt.multiobj.randomsearch.points, par.set, 7 | # randomLHS) 8 | # 9 | # FUN.VALUE = rep(0, control$infill.opt.multiobj.randomsearch.points) 10 | # ys = vapply(models, infill.crit, FUN.VALUE = FUN.VALUE, points = newdesign, control = control, 11 | # par.set = par.set, design = design, ...) 12 | # 13 | # front.inds = !is_dominated(t(ys)) 14 | # return(list( 15 | # points = recodeTypes(newdesign[front.inds, , drop = FALSE], par.set), 16 | # crit.vals = ys[front.inds, , drop = FALSE]) 17 | # ) 18 | #} 19 | 20 | -------------------------------------------------------------------------------- /man/plot.OptState.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot_OptState.R 3 | \name{plot.OptState} 4 | \alias{plot.OptState} 5 | \title{Generate ggplot2 Object} 6 | \usage{ 7 | \method{plot}{OptState}(x, scale.panels = FALSE, points.per.dim = 100, ...) 8 | } 9 | \arguments{ 10 | \item{x}{[\code{OptState}]\cr 11 | The OptState.} 12 | 13 | \item{scale.panels}{[\code{logical(1)}]\cr 14 | If \code{TRUE} the values in each panel will be scaled to [0,1].} 15 | 16 | \item{points.per.dim}{[\code{integer}]\cr 17 | Number of (regular spaced) points at which to 18 | evaluate the surrogate per dimension. 19 | Default is 100.} 20 | 21 | \item{...}{[any] \cr 22 | Not used.} 23 | } 24 | \description{ 25 | Plots the values of the infill criterion for a 1- and 2-dimensional numerical search space for a given \code{\link{OptState}}. 26 | } 27 | -------------------------------------------------------------------------------- /todo-files/plotOptPath.R: -------------------------------------------------------------------------------- 1 | 2 | plotOptPath = function(opt.path, control) { 3 | assertClass(opt.path, "OptPath") 4 | ps = opt.path$par.set 5 | xnames = getParamIds(ps, rep = TRUE, with.nr = TRUE) 6 | opdf = as.data.frame(opt.path) 7 | 8 | opdf$dob = c(rep(0, 50), 1:58) 9 | yname = opt.path$y.names 10 | 11 | ggy0 = opdf[opdf$dob == 0L,] 12 | ggy1 = opdf[opdf$dob > 0L,] 13 | 14 | # p1 = ggplot(mapping = aes_string(x = "dob", y = yname)) 15 | # p1 = p1 + geom_boxplot(data = ggy0) 16 | # p1 = p1 + geom_point(data = ggy1) 17 | 18 | # ggcrit = opdf[opdf$dob > 0L,] 19 | # print(head(ggcrit)) 20 | 21 | # p2 = ggplot(data = ggcrit, mapping = aes_string(x = "dob", y = control$infill.crit)) 22 | # p2 = p2 + geom_point() 23 | 24 | p3 = renderPCPPlot(opdf, xnames, alpha = TRUE) 25 | # print(p3) 26 | 27 | # grid.arrange(p1, p2) 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /man/updateSMBO.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/SMBO.R 3 | \name{updateSMBO} 4 | \alias{updateSMBO} 5 | \title{Updates SMBO with the new observations} 6 | \usage{ 7 | updateSMBO(opt.state, x, y) 8 | } 9 | \arguments{ 10 | \item{opt.state}{[\code{\link{OptState}}] 11 | The optimization state. 12 | Generated by \code{\link{initSMBO}}, this function or an \code{\link{mbo}} run.} 13 | 14 | \item{x}{[\code{data.frame}] 15 | Named x values. 16 | One row per set of x values.} 17 | 18 | \item{y}{[\code{numeric|list}] 19 | Outcome of the optimization. 20 | For multiple results use a list. 21 | For a result of a multi-objective function use a numeric vector.} 22 | } 23 | \value{ 24 | [\code{\link{OptState}}] 25 | } 26 | \description{ 27 | After a function evaluation you want to update the \code{\link{OptState}} to get new proposals. 28 | } 29 | -------------------------------------------------------------------------------- /R/makeTaskSingleObj.R: -------------------------------------------------------------------------------- 1 | # Generates single-objective task. 2 | # 3 | # - only uses x and y columns of optpath 4 | # - converts data types for regr model 5 | # - imputes features 6 | 7 | # @param opt.path [\code{\link[ParamHelpers]{optPath}}]\cr 8 | # Optimization path. 9 | # @param control [\code{\link{MBOControl}}]\cr 10 | # MBO control object. 11 | # @return [\code{\link[mlr]{SupervisedTask}}] 12 | makeTaskSingleObj = function(opt.path, control) { 13 | data = convertToDesign(opt.path, control) 14 | data$dob = data$eol = NULL 15 | 16 | # user selected to (log)-transform the y-column 17 | trafo.y.fun = control$trafo.y.fun 18 | if (!is.null(trafo.y.fun)) { 19 | y.name = control$y.name 20 | # We stop the process if negative values occur 21 | data[[y.name]] = trafo.y.fun(data[[y.name]]) 22 | } 23 | 24 | makeRegrTask(target = control$y.name, data = data) 25 | } 26 | -------------------------------------------------------------------------------- /tests/testthat/test_infill_optimizers.R: -------------------------------------------------------------------------------- 1 | context("infill optimizers") 2 | 3 | test_that("infill optimizers", { 4 | mycontrol = function(opt, restarts) { 5 | ctrl = makeMBOControl() 6 | ctrl = setMBOControlTermination(ctrl, iters = 5L) 7 | ctrl = setMBOControlInfill(ctrl, opt = opt, 8 | opt.cmaes.control = list(stop.ons = list(stopOnMaxIters(30L)))) 9 | } 10 | mycheck = function(or) { 11 | expect_equal(getOptPathLength(or$opt.path), nrow(testd.fsphere.2d) + 5L) 12 | expect_number(or$y) 13 | expect_lt(or$y, 1) 14 | } 15 | 16 | learner = makeLearner("regr.km", predict.type = "se", nugget.estim = TRUE) 17 | ctrl = mycontrol("cmaes", 1L) 18 | or = mbo(testf.fsphere.2d, testd.fsphere.2d, learner, ctrl) 19 | mycheck(or) 20 | ctrl = mycontrol("cmaes", 2L) 21 | or = mbo(testf.fsphere.2d, testd.fsphere.2d, learner, ctrl) 22 | mycheck(or) 23 | }) 24 | -------------------------------------------------------------------------------- /R/evalFinalPoint.R: -------------------------------------------------------------------------------- 1 | #@arg opt.state [OptState] 2 | #@arg x.df [data.frame (1xp)] 3 | # The final point to be evaluated as a result from getOptPathX(op, best.index) 4 | evalFinalPoint = function(opt.state, x.df) { 5 | opt.problem = getOptStateOptProblem(opt.state) 6 | control = getOptProblemControl(opt.problem) 7 | n = control$final.evals 8 | # do some final evaluations and compute mean of target fun values 9 | # FIXME: Do we really want the resampling of the last point be part of the opt.path and thus be part of a new model fit if we restart the problem? 10 | showInfo(getOptProblemShowInfo(opt.problem), "Performing %i final evals", n) 11 | x.df[seq_len(n), ] = x.df 12 | prop = makeProposal( 13 | control = control, 14 | prop.points = x.df, 15 | prop.type = rep("final_eval", n) 16 | ) 17 | evalProposedPoints.OptState(opt.state, prop, train.time = NA_real_) 18 | } 19 | -------------------------------------------------------------------------------- /tests/testthat/test_filter_proposed.R: -------------------------------------------------------------------------------- 1 | context("filter proposed points") 2 | 3 | test_that("filter proposed points", { 4 | lrn = makeLearner("regr.km", predict.type = "se") 5 | 6 | # now check min dist, set to "inf" so we always replace 7 | ctrl = makeMBOControl(propose.points = 2L) 8 | ctrl = setMBOControlTermination(ctrl, iters = 1L) 9 | ctrl = setMBOControlInfill(ctrl, crit = crit.cb1, 10 | filter.proposed.points = TRUE, filter.proposed.points.tol = 1000, 11 | opt = "focussearch", opt.focussearch.points = 100L, opt.focussearch.maxit = 1L) 12 | ctrl = setMBOControlMultiPoint(ctrl, method = "cb") 13 | 14 | res = mbo(testf.fsphere.1d, testd.fsphere.1d, learner = lrn, control = ctrl) 15 | op = as.data.frame(res$opt.path) 16 | expect_true(all(op$prop.type[seq_row(testd.fsphere.1d)] != "random_filter")) 17 | expect_true(all(op$prop.type[-seq_row(testd.fsphere.1d)] == "random_filter")) 18 | }) 19 | -------------------------------------------------------------------------------- /todo-files/svetlana.R: -------------------------------------------------------------------------------- 1 | library(methods) 2 | library(testthat) 3 | library(devtools) 4 | library(BBmisc) 5 | library(mlr) 6 | library(soobench) 7 | library(mco) 8 | library(checkmate) 9 | library(ggplot2) 10 | library(gridExtra) 11 | 12 | load_all(".", reset=TRUE) 13 | 14 | source("todo-files/plotOptPath.R") 15 | source("todo-files/renderPCPPlot.R") 16 | 17 | set.seed(6) 18 | 19 | z =load2("~/Desktop/saveopt.RData") 20 | print(plotOptPath(z$opt.path, z$control)) 21 | 22 | 23 | # ps = z$par.set 24 | # opdf = as.data.frame(z$opt.path) 25 | # xy = as.data.frame(z$opt.path, include.rest = FALSE) 26 | # task = makeRegrTask(data = xy, target = "y") 27 | 28 | # z$learner$fix.factors = FALSE 29 | # mod = train(z$learner, task) 30 | # des = generateRandomDesign(1000L, z$opt.path$par.set) 31 | # pred = predict(mod, newdata = des)$data$response 32 | # idx = pred < 0.43 33 | # good = des[idx, ] 34 | # r= sapply(good, range) 35 | # print(r) 36 | 37 | -------------------------------------------------------------------------------- /inst/examples/ex_2d_numeric.R: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ### 3 | ### optimizing branin in 2D with mbo / EI ##### 4 | ### 5 | ##################################################### 6 | \dontrun{ 7 | library(mlrMBO) 8 | library(ggplot2) 9 | set.seed(1) 10 | configureMlr(show.learner.output = FALSE) 11 | 12 | obj.fun = makeBraninFunction() 13 | 14 | ctrl = makeMBOControl(propose.points = 1L) 15 | ctrl = setMBOControlTermination(ctrl, iters = 10L) 16 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritEI(), 17 | opt = "focussearch", opt.focussearch.points = 2000L) 18 | 19 | lrn = makeMBOLearner(ctrl, obj.fun) 20 | design = generateDesign(10L, getParamSet(obj.fun), fun = lhs::maximinLHS) 21 | 22 | run = exampleRun(obj.fun, design = design, learner = lrn, control = ctrl, 23 | points.per.dim = 50L, show.info = TRUE) 24 | 25 | print(run) 26 | 27 | plotExampleRun(run, gg.objects = list(theme_bw())) 28 | } 29 | -------------------------------------------------------------------------------- /R/checkLearner.R: -------------------------------------------------------------------------------- 1 | # check and create default learner 2 | checkLearner = function(learner, control, fun) { 3 | if (missing(learner) || is.null(learner)) { 4 | learner = makeMBOLearner(control, fun, config = list(show.learner.output = FALSE, on.learner.error = control$on.surrogate.error)) 5 | } else { 6 | assertClass(learner, "Learner") 7 | if (!is.null(learner$config$on.learner.error) && learner$config$on.learner.error != control$on.surrogate.error) { 8 | warningf("mlr setting on.learner.error = '%s' for the surrogate learner '%s' will be overwritten with mlrMBO setting on.surrogate.error = '%s'.", learner$config$on.learner.error, getLearnerId(learner), control$on.surrogate.error) 9 | } 10 | learner$config = insert(learner$config, list(on.learner.error = control$on.surrogate.error)) 11 | } 12 | # so we dont run into problems with focus search et al 13 | learner$fix.factors.prediction = TRUE 14 | return(learner) 15 | } 16 | -------------------------------------------------------------------------------- /man/getMBOInfillCrit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getMBOInfillCrit.R 3 | \name{getMBOInfillCrit} 4 | \alias{getMBOInfillCrit} 5 | \alias{getMBOInfillCritParams} 6 | \alias{getMBOInfillCritParam} 7 | \alias{getMBOInfillCritName} 8 | \alias{getMBOInfillCritId} 9 | \alias{hasRequiresInfillCritStandardError} 10 | \alias{getMBOInfillCritComponents} 11 | \title{Get properties of MBO infill criterion.} 12 | \usage{ 13 | getMBOInfillCritParams(x) 14 | 15 | getMBOInfillCritParam(x, par.name) 16 | 17 | getMBOInfillCritName(x) 18 | 19 | getMBOInfillCritId(x) 20 | 21 | hasRequiresInfillCritStandardError(x) 22 | 23 | getMBOInfillCritComponents(x) 24 | } 25 | \arguments{ 26 | \item{x}{[\code{\link{MBOInfillCrit}}]\cr 27 | Infill criterion.} 28 | 29 | \item{par.name}{[\code{character(1)}]\cr 30 | Parameter name.} 31 | } 32 | \description{ 33 | Returns properties of an infill criterion, e.g., name or id. 34 | } 35 | -------------------------------------------------------------------------------- /tests/testthat/test_infill_opt_ea.R: -------------------------------------------------------------------------------- 1 | context("infillopt ea") 2 | 3 | test_that("infillopt ea", { 4 | obj.fun = testf.fsphere.2d 5 | 6 | ctrl = makeMBOControl() 7 | ctrl = setMBOControlTermination(ctrl, iters = 4L) 8 | ctrl = setMBOControlInfill(ctrl, crit = crit.ei, opt = "ea", opt.restarts = 2L, 9 | opt.ea.maxit = 75L, opt.ea.lambda = 1L) 10 | 11 | des = testd.fsphere.2d 12 | res = mbo(obj.fun, des, control = ctrl) 13 | expect_true(res$y < 0.5, info = "real") 14 | 15 | obj.fun = smoof::makeSingleObjectiveFunction( 16 | fn = function(x) x$num1^2 + x$int1, 17 | par.set = makeParamSet( 18 | makeNumericParam("num1", lower = -1, upper = 1), 19 | makeIntegerParam("int1", lower = 0, upper = 2) 20 | ), 21 | has.simple.signature = FALSE 22 | ) 23 | 24 | des = generateTestDesign(10L, getParamSet(obj.fun)) 25 | res = mbo(obj.fun, des, control = ctrl) 26 | expect_true(res$y < 0.5, info = "integer mixed") 27 | }) 28 | -------------------------------------------------------------------------------- /R/proposePointsParEGO.R: -------------------------------------------------------------------------------- 1 | proposePointsParEGO = function(opt.state) { 2 | weight.mat = attr(getOptStateTasks(opt.state), "weight.mat") 3 | models = getOptStateModels(opt.state)$models 4 | designs = getOptStateDesigns(opt.state) 5 | # copy control and propose 1 point each, per scalar task 6 | control2 = getOptProblemControl(getOptStateOptProblem(opt.state)); 7 | control2$propose.points = 1L 8 | control2$n.objectives = 1L 9 | # scalar tasks are always constructed so they minimized 10 | control2$minimize = TRUE 11 | control2$multiobj.use.scalarized.y = TRUE 12 | control2$y.name = "y.scalar" 13 | 14 | props = list() 15 | props = parallelMap(proposePointsByInfillOptimization, models = models, designs = designs, level = "mlrMBO.propose.points", 16 | more.args = list(opt.state = opt.state, control = control2)) 17 | res = joinProposedPoints(props) 18 | res$crit.components = NULL 19 | res$weight.mat = weight.mat 20 | return(res) 21 | } 22 | -------------------------------------------------------------------------------- /R/shouldTerminate.R: -------------------------------------------------------------------------------- 1 | # Helper which checks termination criteria. 2 | # 3 | # @param opt.state [\code{OptState}]\cr 4 | shouldTerminate.OptState = function(opt.state) { 5 | opt.problem = getOptStateOptProblem(opt.state) 6 | control = getOptProblemControl(opt.problem) 7 | stop.conds = control$stop.conds 8 | progress = NULL 9 | 10 | for (i in seq_along(stop.conds)) { 11 | stop.cond = stop.conds[[i]] 12 | stop.obj = stop.cond(opt.state) 13 | if (isTRUE(names(stop.conds)[i] == control$use.for.adaptive.infill)) { 14 | progress = stop.obj$progress 15 | } 16 | if (stop.obj$term) { 17 | # if user-defined termination condition is active, set the code by hand 18 | if (is.null(stop.obj$code)) { 19 | stop.obj$code = "term.custom" 20 | } 21 | return(stop.obj) 22 | } 23 | } 24 | 25 | # "fallback" 26 | return(list(term = FALSE, message = NA_character_, code = NA_character_, progress = progress)) 27 | } 28 | -------------------------------------------------------------------------------- /inst/examples/ex_multiobj_plots.R: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ### 3 | ### optimizing multi objective function and plots 4 | ### 5 | ##################################################### 6 | \dontrun{ 7 | library(mlrMBO) 8 | library(ggplot2) 9 | set.seed(1) 10 | configureMlr(show.learner.output = FALSE) 11 | 12 | obj.fun = makeDTLZ1Function(dimensions = 5L, n.objectives = 2L) 13 | 14 | ctrl = makeMBOControl(n.objectives = 2L, 15 | propose.points = 2L) 16 | ctrl = setMBOControlTermination(ctrl, iters = 10L) 17 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritEI(), opt.focussearch.points = 1000L, 18 | opt.focussearch.maxit = 3L) 19 | ctrl = setMBOControlMultiObj(ctrl, method = "parego") 20 | lrn = makeMBOLearner(ctrl, obj.fun) 21 | 22 | design = generateDesign(8L, getParamSet(obj.fun), fun = lhs::maximinLHS) 23 | 24 | res = mbo(obj.fun, design = design, learner = lrn, control = ctrl, show.info = TRUE) 25 | 26 | plot(res) 27 | } 28 | -------------------------------------------------------------------------------- /tests/testthat/test_smoof_wrappers.R: -------------------------------------------------------------------------------- 1 | context("smoof wrappers") 2 | 3 | test_that("wrapped smoof function work", { 4 | f = makeSphereFunction(2L) 5 | fc = addCountingWrapper(f) 6 | fl = addLoggingWrapper(f) 7 | fcl = addCountingWrapper(addLoggingWrapper(f)) 8 | 9 | learner = makeLearner("regr.rpart") 10 | 11 | ctrl = makeMBOControl() 12 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritMeanResponse()) 13 | ctrl = setMBOControlTermination(ctrl, max.evals = 5) 14 | or.c = mbo(fc, learner = learner, control = ctrl) 15 | or.l = mbo(fl, learner = learner, control = ctrl) 16 | or.cl = mbo(fcl, learner = learner, control = ctrl) 17 | 18 | expect_equal(getOptPathLength(or.c$opt.path), getNumberOfEvaluations(fc)) 19 | expect_equal(getOptPathY(or.l$opt.path), getLoggedValues(fl)$obj.vals) 20 | expect_equal(getOptPathY(or.cl$opt.path), getLoggedValues(fcl)$obj.val) 21 | expect_equal(getOptPathLength(or.cl$opt.path), getNumberOfEvaluations(fcl)) 22 | }) 23 | -------------------------------------------------------------------------------- /R/getEffectiveBestPoint.R: -------------------------------------------------------------------------------- 1 | # small helper to select yhat(x) + c * s(x) from a design of noisy obs. huang style 2 | # @param design [\code{data.frame}]\cr 3 | # Design. 4 | # @param model [\code{\link[mlr]{Learner}}]\cr 5 | # Fitted surrogate model. 6 | # @param par.set [\code{param.set}]\cr 7 | # Parameter set. 8 | # @param control [\code{\link{MBOControl}}]\cr 9 | # MBO control object. 10 | # @return [\code{list}] 11 | getEffectiveBestPoint = function(design, model, par.set, control) { 12 | maximize.mult = ifelse(control$minimize, 1, -1) 13 | preds = predict(model, newdata = design) 14 | 15 | mu = preds$data$response 16 | se = preds$data$se 17 | 18 | # FIXME: add this constant to control? 19 | const = 1 20 | 21 | # minimize mu (if minimization of objective), large se is always penalized 22 | v = (maximize.mult * mu) + const * se 23 | j = getMinIndex(v) 24 | 25 | return(list(index = j, des = design[j,, drop = FALSE], mu = mu[[j]], se = se[[j]], val = v[[j]])) 26 | } 27 | -------------------------------------------------------------------------------- /tests/testthat/test_multiobj_mspot.R: -------------------------------------------------------------------------------- 1 | context("multi-objective: mspot") 2 | 3 | test_that("multi-objective mspot works", { 4 | # Test normal run 5 | learner = makeLearner("regr.km", nugget.estim = TRUE, predict.type = "se") 6 | ctrl = makeMBOControl(n.objectives = 2L) 7 | ctrl = setMBOControlTermination(ctrl, iters = 5L) 8 | ctrl = setMBOControlInfill(ctrl, crit = crit.ei, opt = "nsga2", opt.nsga2.generations = 1L, opt.nsga2.popsize = 12L) 9 | ctrl = setMBOControlMultiObj(ctrl, method = "mspot") 10 | or = mbo(testf.zdt1.2d, testd.zdt1.2d, learner = learner, control = ctrl) 11 | expect_output(print(or), "Optimization path") 12 | op = as.data.frame(or$opt.path) 13 | k = seq_row(testd.zdt1.2d) 14 | expect_true(all(is.na(op$ei.y_1[k]))) 15 | expect_true(all(is.na(op$ei.y_2[k]))) 16 | expect_numeric(op$ei.y_1[-k], any.missing = FALSE) 17 | expect_numeric(op$ei.y_2[-k], any.missing = FALSE) 18 | expect_matrix(or$pareto.front, mode = "numeric", any.missing = FALSE) 19 | }) 20 | -------------------------------------------------------------------------------- /todo-files/outdated_rmds/index.Rmd: -------------------------------------------------------------------------------- 1 | # mlrMBO Tutorial 2 | 3 | **mlrMBO** is a framework for the (sequential) Model Based parameter Optimization. 4 | The goal is to optimize numeric or discrete influence parameters 5 | of a non-linear black box function like an industrial simulator or a time-consuming algorithm. 6 | 7 | In the following we provide an in-depth introduction to **mlrMBO**. An introductory example serves as a quickstart guide. 8 | Note that our focus is on your comprehension of the basic functions and 9 | applications. For detailed technical information and manual pages, please refer to 10 | the package's [manual pages](http://mlr-org.github.io/mlrMBO/man/). They are regularly updated and reflect the documentation of the current packages on CRAN. 11 | 12 | 1. [Quickstart](quickstart.md) 13 | 2. [In Depth Introduction](in_depth_introduction.md) 14 | 3. Further advanced topics: 15 | - [Parallelization](parallelization.md) Make use of multicore CPUs and other distributed computing methods. 16 | -------------------------------------------------------------------------------- /inst/examples/ex_multiobj.R: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ### 3 | ### optimizing multi-objective function 4 | ### 5 | ##################################################### 6 | \dontrun{ 7 | library(mlrMBO) 8 | library(ggplot2) 9 | set.seed(1) 10 | configureMlr(show.learner.output = FALSE) 11 | 12 | obj.fun = makeZDT1Function(dimensions = 2L) 13 | 14 | ctrl = makeMBOControl(n.objectives = 2L, propose.points = 2L, save.on.disk.at = integer(0L)) 15 | ctrl = setMBOControlTermination(ctrl, iters = 5L) 16 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritDIB(), 17 | opt.focussearch.points = 10000L) 18 | ctrl = setMBOControlMultiObj(ctrl, parego.s = 100) 19 | learner = makeMBOLearner(ctrl, obj.fun) 20 | 21 | design = generateDesign(5L, getParamSet(obj.fun), fun = lhs::maximinLHS) 22 | 23 | run = exampleRunMultiObj(obj.fun, design = design, learner = learner, ctrl, points.per.dim = 50L, 24 | show.info = TRUE, nsga2.args = list()) 25 | 26 | plotExampleRun(run, gg.objects = list(theme_bw())) 27 | } 28 | -------------------------------------------------------------------------------- /todo-files/bench/mlr/multiplexer.R: -------------------------------------------------------------------------------- 1 | 2 | base.learners = list( 3 | # makeLearner("classif.rpart"), 4 | makeLearner("classif.ksvm"), 5 | makeLearner("classif.randomForest"), 6 | makeLearner("classif.gbm") 7 | # makeLearner("classif.lda") 8 | 9 | ) 10 | 11 | mplexer = makeModelMultiplexer(base.learners) 12 | 13 | 14 | par.set = makeModelMultiplexerParamSet(mplexer, 15 | 16 | classif.randomForest = makeParamSet( 17 | makeIntegerParam("ntree", lower = 10, upper = 500), 18 | makeIntegerParam("mtry", lower = 1, upper = 20) 19 | ), 20 | 21 | classif.gbm = makeParamSet( 22 | makeIntegerParam("n.trees", lower = 100L, upper = 5000L), 23 | makeIntegerParam("interaction.depth", lower = 1L, upper = 4L), 24 | makeNumericParam("shrinkage", lower = 1e-5, upper = 1e-1) 25 | ), 26 | 27 | classif.ksvm = makeParamSet( 28 | makeNumericParam("C", lower = -12, upper = 12, trafo = function(x) 2^x), 29 | makeNumericParam("sigma", lower = -12, upper = 12, trafo = function(x) 2^x) 30 | ) 31 | ) 32 | 33 | 34 | -------------------------------------------------------------------------------- /todo-files/mspot_vs_parego.R: -------------------------------------------------------------------------------- 1 | library(emoa) 2 | library(mco) 3 | library(devtools) 4 | load_all() 5 | f = makeMBOFunction(zdt1) 6 | ps = makeNumericParamSet(len = 5L, lower = 0, upper = 1) 7 | 8 | # Test normal run 9 | learner = makeLearner("regr.km", predict.typ = "se") 10 | ctrl = makeMBOControl(iters = 5L, n.objectives = 2L, init.design.points = 10L) 11 | ctrl = setMBOControlInfill(ctrl, crit = "ei") 12 | ctrl = setMBOControlMultiObj(ctrl, method = "parego") 13 | or.parego = mbo(f, ps, learner = learner, control = ctrl) 14 | 15 | ctrl = setMBOControlMultiObj(ctrl, method = "sms") 16 | ctrl = setMBOControlInfill(ctrl, crit = "sms") 17 | or.sms = mbo(f, ps, learner = learner, control = ctrl) 18 | 19 | ctrl = setMBOControlMultiObj(ctrl, method = "mspot") 20 | ctrl = setMBOControlInfill(ctrl, crit = "ei", opt = "nsga2", opt.nsga2.generations = 50L) 21 | or.mspot = mbo(f, ps, learner = learner, control = ctrl) 22 | 23 | 24 | plot(or.parego$pareto.front) 25 | points(or.sms$pareto.front, pch = 6) 26 | points(or.mspot$pareto.front, pch = 3) 27 | -------------------------------------------------------------------------------- /man/MBOMultiObjResult.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/MBOResult.R 3 | \name{MBOMultiObjResult} 4 | \alias{MBOMultiObjResult} 5 | \title{Multi-Objective result object.} 6 | \description{ 7 | \itemize{ 8 | \item{pareto.front [\code{matrix}]}{Pareto front of all evaluated points.} 9 | \item{pareto.set [\code{list} of \code{list}s]}{Pareto set of all evaluated points.} 10 | \item{pareto.inds [\code{numeric}]}{Indices of the Pareto-optimal points in the opt.path} 11 | \item{opt.path [\code{\link[ParamHelpers]{OptPath}}]}{Optimization path. 12 | Includes all evaluated points and additional information as documented in \link{mbo_OptPath}. 13 | You can convert it via \code{as.data.frame}.} 14 | \item{final.state [\code{character}] The final termination state. Gives information why the optimization ended} 15 | \item{models [List of \code{\link[mlr]{WrappedModel}}]}{List of saved regression models.} 16 | \item{control[\code{MBOControl}] Control object used in optimization} 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /R/evalProposedPoints.R: -------------------------------------------------------------------------------- 1 | # evals the points proposed by proposePoints 2 | # (called by mboTemplate) 3 | # 4 | # @param opt.state 5 | # @param prop: result of proposePoints 6 | # @return [\code{numeric} | \code{matrix}] Numeric vector of y-vals or matrix 7 | # (for multi-objective problems). 8 | # 9 | # gets the getExtras, converts point data.frame to a list, repairs points out-of-bounds 10 | # then call evalTargetFun 11 | 12 | evalProposedPoints.OptState = function(opt.state, prop, train.time = NULL) { 13 | if (is.null(train.time)) { 14 | train.time = getOptStateModels(opt.state)$train.time 15 | } 16 | opt.problem = getOptStateOptProblem(opt.state) 17 | par.set = getOptProblemParSet(opt.problem) 18 | extras = getExtras( 19 | n = nrow(prop$prop.points), 20 | prop = prop, 21 | train.time = train.time, 22 | control = getOptProblemControl(opt.problem) 23 | ) 24 | xs = dfRowsToList(prop$prop.points, par.set) 25 | xs = lapply(xs, repairPoint, par.set = par.set) 26 | evalTargetFun.OptState(opt.state, xs = xs, extras = extras) 27 | } 28 | -------------------------------------------------------------------------------- /todo-files/shiny/www/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |

See mlrMBO in action

12 | 13 |

14 |
15 | 19 |

20 | 21 |

22 |
23 | 24 |

25 | 26 |

27 |
28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /todo-files/ensembleSelector.r: -------------------------------------------------------------------------------- 1 | 2 | 3 | ensembleSelector = function(learners, ensemble.select, rt, par.set, control, opt.path, ...){ 4 | n.learners = length(learner) 5 | if(ensemble.select=="random") { 6 | sel.learner = sample(1:n.learners,1) 7 | } else if (ensemble.select=="best.proposed") { 8 | loop.models = list() 9 | loop.prop = list() 10 | for(learner in learners){ 11 | #mod = train(learner, rt) 12 | loop.models[[learner$id]] = train(learner, rt) 13 | loop.prop[[learner$id]] = proposePoints(loop.models[[learner$id]], par.set, control, opt.path) 14 | } 15 | best.learner <- names(which.min(sapply(loop.prop,FUN=function(d){d$y}))) 16 | model = loop.models[[best.learner]] 17 | learner = learners[[best.learner]] 18 | sel.learner = best.learner 19 | } else if (ensemble.select=="our.ensemble") { 20 | sel.learner = sample(1:n.learners,1, prob=action.probs[j,]) 21 | } 22 | list(sel.learner=sel.learner, learner=learner, model=model) 23 | } 24 | 25 | 26 | 27 | #list(learner, sel.learner=sel.learner) 28 | -------------------------------------------------------------------------------- /todo-files/bench/mlr/algos.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | addAlgorithm(reg, id = "random", fun = function(static, iter, learner.class, tune.measure) { 4 | tctrl = makeTuneControlRandom(maxit = TUNE_BUDGET) 5 | algoTemplate(static, iter, tctrl) 6 | }) 7 | 8 | addAlgorithm(reg, id = "mbo", fun = function(static, iter, learner.class, tune.measure) { 9 | library(mlrMBO) 10 | mbo.ctrl = makeMBOControl( 11 | init.design.points = MBO_INIT_DES_SIZE, 12 | iters = MBO_ITERS, 13 | save.on.disk.at = integer(0L), 14 | ) 15 | mbo.ctrl = setMBOControlInfill(mbo.ctrl, 16 | crit = MBO_INFILL_CRIT, 17 | opt.restarts = MBO_FOCUSSEARCH_RESTARTS, 18 | opt.focussearch.maxit = MBO_FOCUSSEARCH_ITERS, 19 | opt.focussearch.points = MBO_FOCUSSEARCH_POINTS 20 | ) 21 | surrogate = makeLearner(MBO_SURROGATE) 22 | surrogate = setHyperPars(surrogate, par.vals = MBO_SURROGATE_VALS) 23 | surrogate = setPredictType(surrogate, "se") 24 | tctrl = mlr:::makeTuneControlMBO(learner = surrogate, mbo.control = mbo.ctrl) 25 | algoTemplate(static, iter, tctrl) 26 | }) 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /todo-files/crit_eipi.R: -------------------------------------------------------------------------------- 1 | #expected improvement with Plugin 2 | # FIXME: control$plugin.beta in MBOControl 3 | infillCritEIPI = function(points, model, control, par.set, design) { 4 | maximize.mult = ifelse(control$minimize, 1, -1) 5 | y = maximize.mult * design[, control$y.name] 6 | p = predict(model, newdata = points)$data 7 | p.mu = maximize.mult * p$response 8 | p.se = p$se 9 | #y.min = min(y) 10 | 11 | if(is.null(control$plugin.beta)==T){beta=0}else{beta=control$plugin.quantile} 12 | q <- qnorm(beta) 13 | 14 | lower <- p.mu - q * sqrt(p.se) 15 | 16 | T_ei=min(lower) 17 | d = T_ei - p.mu 18 | xcr = d / p.se 19 | #FIXME: what is done in DiceOption::EI here for numerical reasons? 20 | #if (kriging.sd/sqrt(model@covariance@sd2) < 1e-06) { 21 | # res = 0 22 | # xcr = xcr.prob = xcr.dens = NULL 23 | # 24 | xcr.prob = pnorm(xcr) 25 | xcr.dens = dnorm(xcr) 26 | ei = d * xcr.prob + p.se * xcr.dens 27 | # FIXME magic number 28 | # if se too low set 0 (numerical problems), negate due to minimization 29 | ifelse(p.se < 1e-6, 0, -ei) 30 | } 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /inst/examples/ex_1d_numeric.R: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ### 3 | ### optimizing a simple sin(x) with mbo / EI 4 | ### 5 | ##################################################### 6 | \dontrun{ 7 | library(ggplot2) 8 | library(mlrMBO) 9 | configureMlr(show.learner.output = FALSE) 10 | set.seed(1) 11 | 12 | obj.fun = makeSingleObjectiveFunction( 13 | name = "Sine", 14 | fn = function(x) sin(x), 15 | par.set = makeNumericParamSet(lower = 3, upper = 13, len = 1), 16 | global.opt.value = -1 17 | ) 18 | 19 | ctrl = makeMBOControl(propose.points = 1) 20 | ctrl = setMBOControlTermination(ctrl, iters = 10L) 21 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritEI(), 22 | opt = "focussearch", opt.focussearch.points = 500L) 23 | 24 | lrn = makeMBOLearner(ctrl, obj.fun) 25 | 26 | design = generateDesign(6L, getParamSet(obj.fun), fun = lhs::maximinLHS) 27 | 28 | run = exampleRun(obj.fun, design = design, learner = lrn, 29 | control = ctrl, points.per.dim = 100, show.info = TRUE) 30 | 31 | plotExampleRun(run, densregion = TRUE, gg.objects = list(theme_bw())) 32 | } 33 | -------------------------------------------------------------------------------- /inst/examples/ex_2d_numeric_multipoint.R: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ### 3 | ### optimizing branin in 2D with multipoint proposal ##### 4 | ### 5 | ##################################################### 6 | \dontrun{ 7 | library(mlrMBO) 8 | library(ggplot2) 9 | set.seed(2) 10 | configureMlr(show.learner.output = FALSE) 11 | 12 | obj.fun = makeBraninFunction() 13 | 14 | ctrl = makeMBOControl(propose.points = 5L) 15 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritMeanResponse()) 16 | ctrl = setMBOControlTermination(ctrl, iters = 10L) 17 | ctrl = setMBOControlMultiPoint(ctrl, 18 | method = "moimbo", 19 | moimbo.objective = "ei.dist", 20 | moimbo.dist = "nearest.neighbor", 21 | moimbo.maxit = 200L 22 | ) 23 | 24 | lrn = makeLearner("regr.km", predict.type = "se") 25 | design = generateDesign(10L, getParamSet(obj.fun), fun = lhs::maximinLHS) 26 | 27 | run = exampleRun(obj.fun, design = design, learner = lrn, control = ctrl, 28 | points.per.dim = 50L, show.info = TRUE) 29 | 30 | print(run) 31 | 32 | plotExampleRun(run, gg.objects = list(theme_bw())) 33 | } 34 | -------------------------------------------------------------------------------- /tests/testthat/test_initCrit.R: -------------------------------------------------------------------------------- 1 | test_that("infill crit initialization", { 2 | skip_on_covr() #because the crit.cb*$fun field is modified by covr and therefore expect_equal will fail 3 | fn = makeSphereFunction(2) # 2d continous space 4 | 5 | crit.cb1_val = setValue(crit.cb1, "opt.direction", "minimize") 6 | crit.cb_init = initCrit(crit.cb, fn) 7 | expect_equal(crit.cb1_val, crit.cb_init) 8 | crit.ei_init = initCrit(crit.ei, fn) 9 | expect_equal(crit.ei, crit.ei_init) 10 | 11 | crit.cb3 = makeMBOInfillCritCB(3) 12 | 13 | crit.cb3_val = setValue(crit.cb3, "opt.direction", "minimize") 14 | crit.cb3_init = initCrit(crit.cb3, fn) 15 | expect_equal(crit.cb3_val, crit.cb3_init) 16 | 17 | fn = makeSwiler2014Function() # 2d continous space + 1 factor variable 18 | 19 | crit.cb2_val = setValue(crit.cb2, "opt.direction", "minimize") 20 | crit.cb_init = initCrit(crit.cb, fn) 21 | expect_equal(crit.cb2_val, crit.cb_init) 22 | 23 | crit.cb3_val = setValue(crit.cb3, "opt.direction", "minimize") 24 | crit.cb3_init = initCrit(crit.cb3, fn) 25 | expect_equal(crit.cb3_val, crit.cb3_init) 26 | 27 | 28 | }) 29 | -------------------------------------------------------------------------------- /R/chooseFinalPoint.R: -------------------------------------------------------------------------------- 1 | # Chooses the final point according to heuristic. 2 | # 3 | # @param fun [\code{function(x, ...)}}]\cr 4 | # Fitness function to optimize. 5 | # @param opt.path [\code{\link[ParamHelpers]{optPath}}]\cr 6 | # Optimization path. 7 | # @param model [\code{\link[mlr]{Learner}}]\cr 8 | # Fitted surrogate model. 9 | # @param task [\code{\link[mlr]{SupervisedTask}}]\cr 10 | # Fitted surrogate model. 11 | # @param control [\code{\link{MBOControl}}]\cr 12 | # MBO control object. 13 | # @return [\code{integer(1)}] Index of the final point. 14 | chooseFinalPoint = function(opt.state) { 15 | opt.problem = getOptStateOptProblem(opt.state) 16 | opt.path = getOptStateOptPath(opt.state) 17 | control = getOptProblemControl(opt.problem) 18 | switch (control$final.method, 19 | "last.proposed" = getOptPathLength(opt.path), 20 | "best.true.y" = getOptPathBestIndex(opt.path, ties = "random"), 21 | "best.predicted" = which(rank(ifelse(control$minimize, 1, -1) * 22 | predict(getOptStateModels(opt.state)$models[[1L]], task = getOptStateTasks(opt.state)[[1]])$data$response, ties.method = "random") == 1L)) 23 | } 24 | -------------------------------------------------------------------------------- /tests/testthat/test_check_design.R: -------------------------------------------------------------------------------- 1 | context("check design") 2 | 3 | test_that("test checks for initial design and new design", { 4 | par.set = makeParamSet( 5 | makeIntegerParam("v", lower = 1, upper = 2), 6 | makeDiscreteParam("w", values = c("a", "b", "e", "g")), 7 | makeDiscreteParam("x", values = c("a", "b", "c", "d"), requires = quote(w == "a")), 8 | makeNumericParam("y", lower = 1, upper = 2, requires = quote(w == "b")), 9 | makeNumericParam("z", lower = 1, upper = 2, requires = quote(w == "e")) 10 | ) 11 | 12 | design1 = generateDesign(8, par.set) 13 | expect_false(checkInitDesign(design1, par.set)) 14 | 15 | design2 = generateDesign(50, par.set) 16 | expect_true(checkInitDesign(design2, par.set)) 17 | 18 | checked = checkPredictionData(design2, design1) 19 | expect_true(any(checked)) 20 | expect_true(any(!checked)) 21 | 22 | design = data.frame(x = c("a", "b"), stringsAsFactors = TRUE) 23 | new.design = data.frame(x = c("a", "c", "b", "d", "a", NA), stringsAsFactors = TRUE) 24 | expect_equal(checkPredictionData(new.design, design), 25 | c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)) 26 | }) 27 | -------------------------------------------------------------------------------- /todo-files/test_mbo_kmforrester.R: -------------------------------------------------------------------------------- 1 | context("mbo kmforrester (reinterpolation)") 2 | 3 | test_that("mbo works with Kriging reinterpolation method ", { 4 | par.set = makeParamSet( 5 | makeNumericParam("x1", lower = -2, upper = 1), 6 | makeNumericParam("x2", lower = -1, upper = 2) 7 | ) 8 | f = makeSingleObjectiveFunction( 9 | fn = function(x) sum(x^2) + rnorm(1, sd = 0.5), 10 | par.set = par.set 11 | ) 12 | des = generateTestDesign(10, par.set = getParamSet(f)) 13 | des$y = vnapply(1:nrow(des), function(i) f(as.list(des[i, ]))) 14 | learner = makeLearner("regr.kmforrester", predict.type = "se") 15 | ctrl = makeMBOControl() 16 | ctrl = setMBOControlTermination(ctrl, iters = 5L) 17 | ctrl = setMBOControlInfill(ctrl, opt.focussearch.points = 100L) 18 | or = mbo(f, des, learner = learner, control = ctrl) 19 | expect_number(or$y) 20 | expect_equal(getOptPathLength(or$opt.path), 15L) 21 | df = as.data.frame(or$opt.path) 22 | expect_numeric(df$x1) 23 | expect_numeric(df$x2) 24 | expect_list(or$x) 25 | expect_set_equal(names(or$x), names(par.set$pars)) 26 | expect_set_equal(length(or$models[[1]]$subset), 15L) 27 | }) 28 | -------------------------------------------------------------------------------- /R/makeTasksMultiObj.R: -------------------------------------------------------------------------------- 1 | # Generates multi objective tasks - one for each target variable. 2 | # 3 | # - only uses x and y columns of optpath 4 | # - converts data types for regr model 5 | # - imputes features 6 | # 7 | # @param opt.path [\code{\link[ParamHelpers]{optPath}}]\cr 8 | # Optimization path. 9 | # @param control [\code{\link{MBOControl}}]\cr 10 | # MBO control object. 11 | # @return [\code{list(\link[mlr]{SupervisedTask}})] 12 | makeTasksMultiObj = function(opt.path, control) { 13 | data = convertToDesign(opt.path, control) 14 | 15 | # FIXME: trafo.y.fun should be a list of length y.name 16 | # user selected to (log)-transform the y-column 17 | trafo.y.fun = control$trafo.y.fun 18 | if (!is.null(trafo.y.fun)) { 19 | y.name = control$y.name 20 | # We stop the process if negative values occur 21 | data[, y.name] = trafo.y.fun(data[, y.name]) 22 | } 23 | 24 | tasks = vector(mode = "list", length = control$n.objectives) 25 | for (ind in seq_along(control$y.name)) { 26 | data.tmp = dropNamed(data, control$y.name[-ind]) 27 | tasks[[ind]] = makeRegrTask(target = control$y.name[ind], data = data.tmp) 28 | } 29 | return(tasks) 30 | } 31 | -------------------------------------------------------------------------------- /man/trafos.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/makeMBOTrafoFunction.R 3 | \name{trafos} 4 | \alias{trafos} 5 | \alias{trafoLog} 6 | \alias{trafoSqrt} 7 | \title{Transformation methods.} 8 | \format{ 9 | None 10 | } 11 | \usage{ 12 | trafoLog(base = 10, handle.violations = "warn") 13 | 14 | trafoSqrt(handle.violations = "warn") 15 | } 16 | \arguments{ 17 | \item{base}{[\code{numeric(1)}]\cr 18 | The base with respect to which logarithms are computed. 19 | Default is \code{10}.} 20 | 21 | \item{handle.violations}{[\code{character(1)}]\cr 22 | What should be done, if negative values occur? Setting this option to 23 | \dQuote{warn}, which is the default, shifts the function. \dQuote{error} 24 | stops the process immediately.} 25 | } 26 | \description{ 27 | \itemize{ 28 | \item{\bold{logTrafo}}{\cr Natural logarithm.} 29 | \item{\bold{sqrtTrafo}}{\cr Square root.} 30 | } 31 | If negative values occur and the trafo function can handle only positive values, 32 | a shift of the form x - min(x) + 1 is performed prior to the transformation if the 33 | argument \code{handle.violations} is set to \dQuote{warn} which is the default 34 | value. 35 | } 36 | -------------------------------------------------------------------------------- /R/mboContinue.R: -------------------------------------------------------------------------------- 1 | #' @title Continues an mbo run from a save-file. 2 | #' 3 | #' @description 4 | #' Useful if your optimization is likely to crash, 5 | #' so you can continue from a save point and will not lose too much information and runtime. 6 | #' 7 | #' @param opt.state [\code{OptState} | \code{character(1)}]\cr 8 | #' Either the \code{save.state} slot of an \code{MBOResult} object, or a file path of a saved MBO state. 9 | #' See \code{save.on.disk.at} argument of \code{\link{MBOControl}} object. 10 | #' @return See \code{\link{mbo}}. 11 | #' @export 12 | mboContinue = function(opt.state) { 13 | if (testCharacter(opt.state, len = 1L)) { 14 | opt.state = loadOptState(opt.state) 15 | } 16 | if (!inherits(opt.state, "OptState")) { 17 | stop("opt.state must be either an OptState object, or a file path of a saved OptState.") 18 | } 19 | state = getOptStateState(opt.state) 20 | if (state %nin% c("init", "iter")) { 21 | warningf("Tuning ended with %s. No need to continue. Simply returning stored result.", state) 22 | return(getOptResultMboResult(getOptStateOptResult(opt.state))) 23 | } 24 | final.opt.state = mboTemplate(opt.state) 25 | mboFinalize2(final.opt.state) 26 | } 27 | -------------------------------------------------------------------------------- /man/plotMBOResult.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot_MBOResult.R 3 | \name{plotMBOResult} 4 | \alias{plotMBOResult} 5 | \alias{plot.MBOSingleObjResult} 6 | \alias{plot.MBOMultiObjResult} 7 | \title{MBO Result Plotting} 8 | \usage{ 9 | \method{plot}{MBOSingleObjResult}(x, iters = NULL, pause = interactive(), ...) 10 | 11 | \method{plot}{MBOMultiObjResult}(x, iters = NULL, pause = interactive(), ...) 12 | } 13 | \arguments{ 14 | \item{x}{[\code{MBOResult}]\cr 15 | \code{MBOSingleObjResult} or \code{MBOMultiObjResult} object.} 16 | 17 | \item{iters}{[\code{integer}]\cr 18 | Iterations to be plotted, 0 indicates the initial design. Default is all iterations.} 19 | 20 | \item{pause}{[\code{logical(1)}]\cr 21 | Should the process be paused after each iteration? 22 | Default is \code{interactive()}.} 23 | 24 | \item{...}{Additional parameters for the \code{\link[ParamHelpers]{plotOptPath}} 25 | function in package \code{ParamHelpers}.} 26 | } 27 | \description{ 28 | Plots any MBO result objects. Plots for X-Space, Y-Space and any column in 29 | the optimization path are available. This function uses 30 | \code{\link[ParamHelpers]{plotOptPath}} from package \code{ParamHelpers}. 31 | } 32 | -------------------------------------------------------------------------------- /todo-files/test_kmlocal.R: -------------------------------------------------------------------------------- 1 | library(mlr) 2 | library(DiceOptim) 3 | library(ggplot2) 4 | library(devtools) 5 | load_all() 6 | source("todo-files/local_krig.R") 7 | 8 | set.seed(1) 9 | 10 | f = branin2 11 | d = 2L 12 | ps = makeNumericParamSet("x", len = d, lower = 0, upper = 1) 13 | 14 | # n = 200 15 | # x = generateDesign(n, ps) 16 | # y = apply(x, 1, f) 17 | # d = cbind(as.data.frame(x), y = y) 18 | # 19 | # task = makeRegrTask(data = d, target = "y") 20 | # lrn = makeLearner("regr.kmlocal", predict.type = "se") 21 | # m = train(lrn, task) 22 | # p = predict(m, task) 23 | # 24 | # mm = m$learner.model 25 | # 26 | 27 | surrogate = makeLearner("regr.kmlocal", predict.type = "se") 28 | 29 | ctrl = makeMBOControl(init.design.points = 8L, iters = 32L, on.learner.error = "stop", show.learner.output = TRUE) 30 | ctrl = setMBOControlInfill(ctrl, crit = "ei", opt = "cmaes", opt.restarts = 1L) 31 | 32 | res = mbo(makeMBOFunction(f), par.set = ps, learner = surrogate, control = ctrl) 33 | op = as.data.frame(res$opt.path) 34 | mm = res$models[[1L]]$learner.model 35 | 36 | ggdata = op 37 | ggdata$isc = FALSE 38 | ggdata$isc[mm$local.centers.i] = TRUE 39 | 40 | pl = ggplot(ggdata, aes(x = x1, y = x2, col = isc)) 41 | pl = pl + geom_point(size = 3) 42 | print(pl) -------------------------------------------------------------------------------- /inst/examples/ex_1d_numeric_multipoint.R: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ### 3 | ### optimizing a simple sin(x) with multipoint proposal 4 | ### 5 | ##################################################### 6 | \dontrun{ 7 | library(mlrMBO) 8 | library(ggplot2) 9 | set.seed(1) 10 | configureMlr(show.learner.output = FALSE) 11 | 12 | obj.fun = makeSingleObjectiveFunction( 13 | name = "Sine", 14 | fn = function(x) sin(x), 15 | par.set = makeNumericParamSet(lower = 3, upper = 13, len = 1L), 16 | global.opt.value = -1 17 | ) 18 | 19 | ctrl = makeMBOControl(propose.points = 2L) 20 | ctrl = setMBOControlTermination(ctrl, iters = 10L) 21 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritMeanResponse()) 22 | ctrl = setMBOControlMultiPoint( 23 | ctrl, 24 | method = "moimbo", 25 | moimbo.objective = "ei.dist", 26 | moimbo.dist = "nearest.neighbor", 27 | moimbo.maxit = 200L 28 | ) 29 | 30 | lrn = makeMBOLearner(ctrl, obj.fun) 31 | 32 | design = generateDesign(4L, getParamSet(obj.fun), fun = lhs::maximinLHS) 33 | 34 | run = exampleRun(obj.fun, design = design, learner = lrn, 35 | control = ctrl, points.per.dim = 100, show.info = TRUE) 36 | 37 | print(run) 38 | 39 | plotExampleRun(run, densregion = TRUE, gg.objects = list(theme_bw())) 40 | } 41 | -------------------------------------------------------------------------------- /R/mboFinalize.R: -------------------------------------------------------------------------------- 1 | #' @title Finalizes an mbo run from a save-file. 2 | #' 3 | #' @description 4 | #' Useful if your optimization didn't terminate but you want a results nonetheless. 5 | #' 6 | #' @param file [\code{character(1)}]\cr 7 | #' File path of saved MBO state. 8 | #' See \code{save.on.disk.at} argument of \code{\link{MBOControl}} object. 9 | #' @return See \code{\link{mbo}}. 10 | #' @export 11 | mboFinalize = function(file) { 12 | assertCharacter(file, len = 1L) 13 | opt.state = loadOptState(file) 14 | state = getOptStateState(opt.state) 15 | if (state %in% getOptStateValidTerminationStates()) { 16 | warningf("Optimization ended with %s. No need to finalize Simply returning stored result.", state) 17 | return(getOptResultMboResult(getOptStateOptResult(opt.state))) 18 | } 19 | setOptStateState(opt.state, "term.custom") 20 | mboFinalize2(opt.state) 21 | } 22 | 23 | mboFinalize2 = function(opt.state) { 24 | mbo.result = makeOptStateMboResult(opt.state) 25 | opt.problem = getOptStateOptProblem(opt.state) 26 | # save on disk routine 27 | if (getOptStateLoop(opt.state) %in% getOptProblemControl(opt.problem)$save.on.disk.at || is.finite(getOptProblemControl(opt.problem)$save.on.disk.at.time)) 28 | saveOptState(opt.state) 29 | 30 | return(mbo.result) 31 | } 32 | -------------------------------------------------------------------------------- /R/proposePointsConstantLiar.R: -------------------------------------------------------------------------------- 1 | proposePointsConstantLiar = function(opt.state) { 2 | 3 | opt.problem = getOptStateOptProblem(opt.state) 4 | model = getOptStateModels(opt.state)$models[[1L]] 5 | par.set = getOptProblemParSet(opt.problem) 6 | control = getOptProblemControl(opt.problem) 7 | opt.path = getOptStateOptPath(opt.state) 8 | 9 | npoints = control$propose.points 10 | liar = control$multipoint.cl.lie 11 | # copy control, and propose 1 point each 12 | control2 = control 13 | control2$propose.points = 1L 14 | # copy opt.path to store lies 15 | opt.path2 = deepCopyOptPath(opt.path) 16 | lie = liar(getOptPathY(opt.path, control$y.name)) 17 | dob = max(getOptPathDOB(opt.path)) + 1L 18 | props = list() 19 | for (i in seq_len(npoints)) { 20 | # propose point, add to opt.path2 with y = lie, then update model 21 | props[[i]] = proposePointsByInfillOptimization(opt.state, control = control2, opt.path = opt.path2, models = list(model)) 22 | if (i==npoints) break # we don't need to update the model when we aleady have the n-th proposal 23 | x = dfRowToList(props[[i]]$prop.points, par.set, 1) 24 | addOptPathEl(opt.path2, x = x, y = lie, dob = dob) 25 | rt = makeTaskSingleObj(opt.path2, control) 26 | model = train(model$learner, rt) 27 | } 28 | joinProposedPoints(props) 29 | } 30 | -------------------------------------------------------------------------------- /inst/examples/ex_mixed.R: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ### 3 | ### optimizing mixed space function 4 | ### 5 | ##################################################### 6 | \dontrun{ 7 | library(mlrMBO) 8 | library(ggplot2) 9 | set.seed(1) 10 | configureMlr(show.learner.output = FALSE) 11 | 12 | obj.fun = makeSingleObjectiveFunction( 13 | name = "Mixed functions", 14 | fn = function(x) { 15 | if (x$cat == "a") 16 | x$num^2 17 | else 18 | x$num^2 + 3 19 | }, 20 | par.set = makeParamSet( 21 | makeDiscreteParam("cat", values = c("a", "b")), 22 | makeNumericParam("num", lower = -5, upper = 5) 23 | ), 24 | has.simple.signature = FALSE, 25 | global.opt.value = -1 26 | ) 27 | 28 | ctrl = makeMBOControl(propose.points = 1L) 29 | ctrl = setMBOControlTermination(ctrl, iters = 10L) 30 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritEI(), 31 | opt = "focussearch", opt.focussearch.points = 500L) 32 | 33 | lrn = makeMBOLearner(ctrl, obj.fun) 34 | 35 | design = generateDesign(4L, getParamSet(obj.fun), fun = lhs::maximinLHS) 36 | 37 | run = exampleRun(obj.fun, design = design, learner = lrn, 38 | control = ctrl, points.per.dim = 100L, show.info = TRUE) 39 | 40 | print(run) 41 | 42 | plotExampleRun(run, densregion = TRUE, gg.objects = list(theme_bw())) 43 | } 44 | -------------------------------------------------------------------------------- /man/initCrit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/initCrit.R 3 | \name{initCrit} 4 | \alias{initCrit} 5 | \title{Initialize an MBO infill criterion.} 6 | \usage{ 7 | initCrit(crit, fun, design, learner, control) 8 | } 9 | \arguments{ 10 | \item{crit}{[\code{\link{MBOInfillCrit}}]\cr 11 | Uninitialized infill criterion.} 12 | 13 | \item{fun}{[\code{smoof_function}]\cr 14 | Fitness function to optimize.} 15 | 16 | \item{design}{Sampling plan.} 17 | 18 | \item{learner}{[\code{\link[mlr]{Learner}}]\cr 19 | Regression learner from mlr, which is used as a surrogate to model our fitness function.} 20 | 21 | \item{control}{[\code{\link{MBOControl}}]\cr 22 | MBO control object.} 23 | } 24 | \value{ 25 | [\code{\link{MBOInfillCrit}}] 26 | } 27 | \description{ 28 | Some infill criteria have parameters that are dependent on values in the parameter set, design, 29 | used learner or other control settings. 30 | To actually set these default values, this function is called, which returns a fully 31 | initialized [\code{\link{MBOInfillCrit}}]. 32 | This function is mainly for internal use. If a custom infill criterion is created, it may be 33 | required to create a separate method \code{initCrit.InfillCritID} where \code{ID} is the 34 | \code{id} of the custom \link{MBOInfillCrit}. 35 | } 36 | -------------------------------------------------------------------------------- /inst/examples/ex_1d_noisy_numeric.R: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ### 3 | ### optimizing a simple noisy sin(x) with mbo / EI 4 | ### 5 | ##################################################### 6 | \dontrun{ 7 | library(mlrMBO) 8 | library(ggplot2) 9 | set.seed(1) 10 | configureMlr(show.learner.output = FALSE) 11 | 12 | # function with noise 13 | obj.fun = makeSingleObjectiveFunction( 14 | name = "Some noisy function", 15 | fn = function(x) sin(x) + rnorm(1, 0, 0.1), 16 | par.set = makeNumericParamSet(lower = 3, upper = 13, len = 1L), 17 | noisy = TRUE, 18 | global.opt.value = -1, 19 | fn.mean = function(x) sin(x) 20 | ) 21 | 22 | ctrl = makeMBOControl( 23 | propose.points = 1L, 24 | final.method = "best.predicted", 25 | final.evals = 10L 26 | ) 27 | ctrl = setMBOControlTermination(ctrl, iters = 5L) 28 | 29 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritEI(), 30 | opt = "focussearch", opt.focussearch.points = 500L) 31 | 32 | lrn = makeMBOLearner(ctrl, obj.fun) 33 | 34 | design = generateDesign(6L, getParamSet(obj.fun), fun = lhs::maximinLHS) 35 | 36 | run = exampleRun(obj.fun, design = design, learner = lrn, 37 | control = ctrl, points.per.dim = 200L, noisy.evals = 50L, 38 | show.info = TRUE) 39 | 40 | print(run) 41 | 42 | plotExampleRun(run, densregion = TRUE, gg.objects = list(theme_bw())) 43 | } 44 | -------------------------------------------------------------------------------- /todo-files/test_plotEAF.R: -------------------------------------------------------------------------------- 1 | context("plotEAF") 2 | 3 | test_that("plotEAF works", { 4 | f = makeMultiObjectiveFunction( 5 | fn = function(x) c(1, -1) * x^2, 6 | par.set = makeNumericParamSet(len = 2L, lower = -2, upper = 1), 7 | n.objectives = 2L 8 | ) 9 | des = generateTestDesign(5L, getParamSet(f)) 10 | 11 | # Test normal run 12 | learner = makeLearner("regr.km", predict.type = "se", nugget.estim = TRUE) 13 | ctrl = makeMBOControl(n.objectives = 2L) 14 | ctrl = setMBOControlTermination(ctrl, iters = 2L) 15 | ctrl = setMBOControlInfill(ctrl, opt.focussearch.points = 10) 16 | ctrl = setMBOControlMultiObj(ctrl, method = "parego", parego.s = 100) 17 | 18 | # build aritificial list of opt pathes for two algorithms 19 | n.reps = 3L 20 | opt.pathes = lapply(seq(n.reps), function(i) { 21 | set.seed(i) 22 | res = mbo(f, des, learner = learner, control = ctrl) 23 | return(res$opt.path) 24 | }) 25 | opt.pathes = list("algo1" = opt.pathes, "algo2" = opt.pathes) 26 | 27 | # plot eaf and check returned data frame 28 | res = plotEAF(opt.pathes) 29 | 30 | algo.names = c("algo1", "algo2") 31 | 32 | expect_data_frame(res) 33 | expect_set_equal(colnames(res), c("y_1", "y_2", ".algo", ".repl")) 34 | expect_subset(as.character(res$.algo), algo.names) 35 | expect_true(all(res$.repl >= 1) && all(res$.repl <= n.reps)) 36 | }) 37 | -------------------------------------------------------------------------------- /R/proposePointsMSPOT.R: -------------------------------------------------------------------------------- 1 | proposePointsMSPOT = function(opt.state, ...) { 2 | 3 | opt.problem = getOptStateOptProblem(opt.state) 4 | models = getOptStateModels(opt.state)$models 5 | designs = getOptStateDesigns(opt.state) 6 | par.set = getOptProblemParSet(opt.problem) 7 | control = getOptProblemControl(opt.problem) 8 | opt.path = getOptStateOptPath(opt.state) 9 | iter = getOptStateLoop(opt.state) 10 | 11 | n = control$propose.points 12 | ch = checkFailedModels(models, par.set, n, control = control) 13 | if (!ch$ok) 14 | return(ch$prop) 15 | 16 | infill.crit.id = getMBOInfillCritId(control$infill.crit) 17 | infill.opt.fun = getInfillOptFunction(control$infill.opt) 18 | 19 | # store time to propose single point 20 | secs = measureTime({ 21 | prop.points = infill.opt.fun(control$infill.crit$fun, models, control, par.set, opt.path, designs, iter, ...) 22 | }) 23 | 24 | ppoints = prop.points$prop.points 25 | crit.vals = evalCritFunForMultiObjModels(control$infill.crit$fun, ppoints, models, control, 26 | par.set, designs, iter) 27 | crit.vals = cbind(crit.vals, prop.points$prop.hv.contrs) 28 | prop.type = rep(paste0("infill_", infill.crit.id), n) 29 | 30 | makeProposal( 31 | control = control, 32 | prop.points = ppoints, 33 | propose.time = secs, 34 | crit.vals = crit.vals, 35 | prop.type = prop.type 36 | ) 37 | } 38 | -------------------------------------------------------------------------------- /R/initOptProblem.R: -------------------------------------------------------------------------------- 1 | initOptProblem = function(fun, design, learner, control, show.info, more.args) { 2 | assertClass(fun, "smoof_function") 3 | 4 | par.set = getParamSet(fun) 5 | 6 | assertDataFrame(design, null.ok = TRUE) 7 | if (!is.null(design)) { 8 | assertSubset(getParamIds(par.set, repeated = TRUE, with.nr = TRUE), names(design)) 9 | } 10 | 11 | 12 | learner = checkLearner(learner, control, fun) 13 | 14 | if (is.null(control)) { 15 | control = makeMBOControl() 16 | } else { 17 | assertClass(control, "MBOControl") 18 | } 19 | 20 | n.params = sum(getParamLengths(par.set)) 21 | control$noisy = isNoisy(fun) 22 | control$minimize = shouldBeMinimized(fun) 23 | if (is.null(design)) 24 | design = generateDesign(n.params * 4L, par.set, fun = lhs::maximinLHS) 25 | else 26 | assertDataFrame(design, min.rows = 1L, min.cols = 1L) 27 | control = checkStuff(fun, design, learner, control) 28 | control$infill.crit = initCrit(control$infill.crit, fun, design, learner, control) 29 | 30 | loadPackages(control) 31 | 32 | # generate an OptProblem which gathers all necessary information to define the optimization problem in one environment. 33 | makeOptProblem( 34 | fun = fun, 35 | design = design, 36 | learner = learner, 37 | control = control, 38 | show.info = assertFlag(show.info), 39 | more.args = assertList(more.args, null.ok = TRUE)) 40 | } 41 | -------------------------------------------------------------------------------- /tests/testthat/test_plot_MBOResult.R: -------------------------------------------------------------------------------- 1 | context("plot MBO Result") 2 | 3 | test_that("plot single-objective", { 4 | f = makeSingleObjectiveFunction( 5 | fn = function(x) crossprod(x), 6 | par.set = makeNumericParamSet(len = 5L, lower = -1, upper = 1) 7 | ) 8 | learner = makeLearner("regr.km", predict.type = "se") 9 | des = generateTestDesign(8L, getParamSet(f)) 10 | ctrl = makeMBOControl(propose.points = 1L) 11 | ctrl = setMBOControlTermination(ctrl, iters = 2L) 12 | ctrl = setMBOControlInfill(ctrl, crit = crit.ei, opt.focussearch.points = 100L, 13 | opt.focussearch.maxit = 3L) 14 | or = mbo(f, des, learner = learner, control = ctrl) 15 | 16 | suppressWarnings(plot(or, iters = 0:2, pause = FALSE)) 17 | }) 18 | 19 | test_that("plot multi-objective", { 20 | # occassionally errors on windows 21 | skip_on_os("windows") 22 | 23 | f = smoof::makeZDT1Function(2L) 24 | learner = makeLearner("regr.km", predict.type = "se") 25 | des = generateTestDesign(8L, getParamSet(f)) 26 | ctrl = makeMBOControl(propose.points = 2L, n.objectives = 2L) 27 | ctrl = setMBOControlTermination(ctrl, iters = 2L) 28 | ctrl = setMBOControlInfill(ctrl, crit = crit.ei, opt.focussearch.points = 100L, 29 | opt.focussearch.maxit = 3L, opt = "nsga2") 30 | ctrl = setMBOControlMultiObj(ctrl, method = "mspot") 31 | or = mbo(f, des, learner = learner, control = ctrl) 32 | 33 | suppressWarnings(plot(or, iters = 0:2, pause = FALSE)) 34 | }) 35 | -------------------------------------------------------------------------------- /R/checkDesign.R: -------------------------------------------------------------------------------- 1 | # Check if the initial design is large enough. I.e.: 2 | # Every factor level is "visited" for factors 3 | # and every dependent feature has NAs and not-NAs 4 | checkInitDesign = function(design, par.set) { 5 | # discrete Params: all levels are visited 6 | disc.params = filterParams(par.set, type = "discrete") 7 | par.ids = getParamIds(disc.params) 8 | disc.params.values = lapply(getValues(disc.params), unlist, use.names = FALSE) 9 | not.visited = Map(setdiff, disc.params.values[par.ids], design[par.ids]) 10 | if (any(lengths(not.visited) > 0L)) 11 | return(FALSE) 12 | 13 | # if the param has requires, check if NA and normal level is visited 14 | has.requires = vlapply(par.set$pars, hasRequires) 15 | sum.na = lapply(design[has.requires], function(x) sum(is.na(x))) 16 | if (any(sum.na == 0 | sum.na == nrow(design))) 17 | return(FALSE) 18 | 19 | return(TRUE) 20 | } 21 | 22 | # Check if a line of the new design is able to predict atm 23 | # corresponding to the current design 24 | checkPredictionData = function(new.design, design) { 25 | ok = rep(TRUE, (nrow(new.design))) 26 | for (i in seq_col(new.design)) { 27 | # check factor level is visited 28 | if (is.factor(new.design[, i])) 29 | ok = ok & new.design[, i] %in% design[, i] 30 | feat.nas = is.na(new.design[, i]) 31 | design.nas = is.na(design[, i]) 32 | ok = ok & ifelse(feat.nas, any(design.nas), any(!design.nas)) 33 | } 34 | return(ok) 35 | } 36 | -------------------------------------------------------------------------------- /tests/testthat/test_multipoint_moimbo.R: -------------------------------------------------------------------------------- 1 | context("multipoint multi-objective") 2 | 3 | test_that("multipoint multi-objective", { 4 | f = makeBraninFunction() 5 | f = setAttribute(f, "par.set", makeNumericParamSet(len = 2L, lower = 0, upper = 1)) 6 | 7 | #FIXME: how can we test this better? 8 | for (obj in c("ei.dist", "mean.se", "mean.se.dist")) { 9 | for (dist in c("nearest.better", "nearest.neighbor")) { 10 | for (sel in c("hypervolume", "crowdingdist", "first", "last")) { 11 | 12 | des = generateTestDesign(10L, getParamSet(f)) 13 | ctrl = makeMBOControl(propose.points = 4L) 14 | ctrl = setMBOControlTermination(ctrl, iters = 1L) 15 | ctrl = setMBOControlMultiPoint(ctrl, 16 | method = "moimbo", 17 | moimbo.objective = obj, 18 | moimbo.dist = dist, 19 | moimbo.sel = sel, 20 | moimbo.maxit = 30L 21 | ) 22 | 23 | res = mbo(f, des, control = ctrl) 24 | expect_output(print(res), "Recommended parameters") 25 | 26 | gap = res$y - 0.3979 27 | # expect_lt(gap, 0.1) 28 | } 29 | } 30 | } 31 | 32 | #test that infill crit is ignored 33 | crits = list(makeMBOInfillCritCB(), makeMBOInfillCritEI(), makeMBOInfillCritMeanResponse()) 34 | for (i in seq_along(crits)) { 35 | ctrl = setMBOControlInfill(ctrl, crit = crits[[i]]) 36 | res = mbo(f, control = ctrl) 37 | expect_output(print(res), "Recommended parameters") 38 | } 39 | 40 | }) 41 | -------------------------------------------------------------------------------- /tests/testthat/test_initdesign.R: -------------------------------------------------------------------------------- 1 | context("init design") 2 | 3 | test_that("init design", { 4 | # without trafo 5 | obj.fun = smoof::makeSingleObjectiveFunction( 6 | fn = function(x) sum(x^2), 7 | par.set = makeParamSet( 8 | makeNumericParam("x1", lower = -2, upper = 1), 9 | makeNumericParam("x2", lower = -1, upper = 2) 10 | ) 11 | ) 12 | 13 | ctrl = makeMBOControl() 14 | ctrl = setMBOControlTermination(ctrl, iters = 1L) 15 | ctrl = setMBOControlInfill(ctrl, opt.focussearch.points = 10L) 16 | 17 | des = generateTestDesign(10L, par.set = getParamSet(obj.fun)) 18 | 19 | or = mbo(obj.fun, des, control = ctrl) 20 | expect_equal(getOptPathLength(or$opt.path), 11L) 21 | expect_number(or$y) 22 | 23 | # with precomputed y 24 | des$y = apply(des, 1L, obj.fun) 25 | or = mbo(obj.fun, des, control = ctrl) 26 | expect_equal(getOptPathLength(or$opt.path), 11L) 27 | expect_number(or$y) 28 | 29 | # with trafo 30 | obj.fun = setAttribute(obj.fun, "par.set", 31 | makeParamSet( 32 | makeNumericParam("x1", lower = -2, upper = 1), 33 | makeNumericParam("x2", lower = -1, upper = 2, trafo = function(x) x/2) 34 | ) 35 | ) 36 | 37 | des = generateTestDesign(10, getParamSet(obj.fun), trafo = TRUE) 38 | expect_error(mbo(obj.fun, des, control = ctrl)) 39 | 40 | # wrong col names in inir design 41 | des = data.frame(x1 = c(-0.49, 0.24, -1.47), z = c(-0.70, 1.29, 0.73)) 42 | expect_error(mbo(obj.fun, des, control = ctrl)) 43 | }) 44 | -------------------------------------------------------------------------------- /todo-files/crit_aei.R: -------------------------------------------------------------------------------- 1 | # augmented expected improvement, as designed by huang 2 | # useful for noisy 3 | infillCritAEI = function(points, model, control, par.set, design) { 4 | # FIXME: ugly! 5 | # FIXME check completely 6 | # design2 = imputeFeatures(design, par.set, control) 7 | # design2 = convertDfCols(design2, chars.as.factor = TRUE) 8 | 9 | #FIXME: generalize new.noise.var for all models 10 | maximize.mult = ifelse(control$minimize, 1, -1) 11 | p = predict(model, newdata = points)$data 12 | p.mu = maximize.mult * p$response 13 | p.se = p$se 14 | design2 = dropNamed(design2, c("dob", "eol")) 15 | # FIXME: what a mess! do we want to pass design or design2? 16 | ebs = getEffectiveBestPoint(design = design2, model = model, par.set = par.set, control = control) 17 | # calculate EI with plugin, plugin val is mean response at ebs solution 18 | d = ebs$mu - p.mu 19 | xcr = d / p.se 20 | xcr.prob = pnorm(xcr) 21 | xcr.dens = dnorm(xcr) 22 | 23 | #FIXME: how do we select here best? 24 | pure.noise.var = if (inherits(model$learnem, "regr.km")) 25 | pure.noise.var = model$learner.model@covariance@nugget 26 | else 27 | estimateResidualVariance(model, data = design2, target = control$y.name) 28 | 29 | tau = sqrt(pure.noise.var) 30 | #if (sk < sqrt(model@covariance@sd2)/1e+06) { 31 | #FIXME: What actually happens here. Find out in DiceOptim 32 | aei = ifelse(p.se < 1e-06, 0, 33 | (d * xcr.prob + p.se * xcr.dens) * (1 - tau / sqrt(tau^2 + p.se^2))) 34 | return(-aei) 35 | } 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /R/doc_mbo_parallel.R: -------------------------------------------------------------------------------- 1 | #' @title Parallelization in mlrMBO 2 | #' 3 | #' @description 4 | #' In mlrMBO you can parallelize the tuning on two different levels to speed up computation: 5 | #' \itemize{ 6 | #' \item{\code{mlrMBO.feval}}{Multiple evaluations of the target function.} 7 | #' \item{\code{mlrMBO.propose.points}}{Optimization of the infill criteria if multiple are used (e.g. ParEGO and ParallelLCB)} 8 | #' } 9 | #' Internally the evaluation of the target function is realized with the R package parallelMap. 10 | #' See the mlrMBO tutorial and the Github project pages of parallelMap for instructions on how to set up parallelization. 11 | #' The different levels of parallelization can be specified in \code{parallelStart*}. 12 | #' Details for the levels mentioned above are given below: 13 | #' \itemize{ 14 | #' \item{Evaluation of the objective function can be parallelized in cases multiple points are to be evaluated at once. These are: evaluation of the initial design, multiple proposed points per iteration and evaluation of the target function in \code{\link{exampleRun}}. (Level: \code{mlrMBO.feval})} 15 | #' \item{Model fitting / point proposal - in some cases where independent, expensive operations are performed. (Level: \code{mlrMBO.propose.points})} 16 | #' } 17 | #' Details regarding the latter: 18 | #' \describe{ 19 | #' \item{single-objective MBO with LCB multi-point}{Parallel optimization of LCBs for the lambda-values.} 20 | #' \item{Multi-objective MBO with ParEGO}{Parallel optimization of scalarization functions.} 21 | #' } 22 | #' 23 | #' @name mbo_parallel 24 | NULL 25 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # mlrMBO 1.1.5 2 | 3 | * Documentation fixes 4 | * Bugfix: Fix CRAN warnings. 5 | 6 | # mlrMBO 1.1.4 7 | 8 | * `plot.OptState()` now supports multiple categorical variables. 9 | * `crit.aei` works with wrapped learners. 10 | * Documentation fixes 11 | * `mbo()` can be called without control object. 12 | * Bugfix: MOIMBO works with integers 13 | 14 | # mlrMBO 1.1.3 15 | 16 | * Bugfix: `plot(opt.state)` now also works for Param Sets with transformations. 17 | * Bugfix: Infill optimization with CMAES now supports restarts. 18 | 19 | # mlrMBO 1.1.2 20 | 21 | * Adaptive infill criterions. Infill criterions now have to support an `progress` argument. Termination criterions now can supply a `progress` return value. 22 | * Fix for parEGO + EI (Issue #407) 23 | * `save.on.disk` now can take arbitrary numeric vectors to specify iterations, when to save on disk. 24 | * Spelling mistakes for infill criterions will now be cought. (Issue #417) 25 | 26 | # mlrMBO 1.1.1 27 | 28 | * `makeMBOControl()` has `on.surrogate.error` argument which enables random proposals if the surrogate model fails. 29 | * With `initSMBO()`, `updateSMBO()` and `finalizeSMBO()` it is now possible to do a human-in-the-loop MBO. 30 | * The result now contains the `final.opt.state`. 31 | * Plot method for `OptState` objects. 32 | 33 | # mlrMBO 1.1.0 34 | 35 | * Fixed bug in focus search that affected discrete search spaces. 36 | * Numerics will be auto converted to integers where integers are expected. 37 | * Package can now be cited with `citation("mlrMBO")`. 38 | 39 | # mlrMBO 1.0.0 40 | 41 | * Initial CRAN release 42 | -------------------------------------------------------------------------------- /todo-files/shiny/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(soobench) 3 | 4 | funs = list("Branin function" = "branin", 5 | "Ackley function 1d" = "ackley_1", 6 | "Ackley function 2d" = "ackley_2", 7 | "Rastrigin function 1d" = "rastrigin_1", 8 | "Rastrigin function 2d" = "rastrigin_2", 9 | "Rosenbrock function 2d" = "rosenbrock_2") 10 | 11 | trafos = list("none" = "none", "log" = "log", "log10" = "log10") 12 | 13 | 14 | shinyUI(pageWithSidebar( 15 | # Application title 16 | headerPanel("mlrMBO demo"), 17 | sidebarPanel( 18 | h3("Options"), 19 | p("Below you have the possibility to select one of the benchmarking function implemented in the current release of soobench R package by Olaf Mersmann."), 20 | selectInput("obj.fun", "Objective function:", funs, "Ackley function 1d"), 21 | 22 | sliderInput("iter", "Iterations:", 23 | min=1, max=10, value=2), 24 | 25 | p("Below you have the possibility to select transformations for different plots."), 26 | em("(Irrelevant trafos are not considered."), 27 | 28 | selectInput("trafo.y", 29 | "y-Trafo:", 30 | trafos), 31 | selectInput("trafo.yhat", 32 | "yhat-Trafo:", 33 | trafos), 34 | selectInput("trafo.crit", 35 | "Criterion-Trafo:", 36 | trafos), 37 | selectInput("trafo.se", 38 | "SE-Trafo:", 39 | trafos), 40 | p(em("Transformations currently applied only to 2D functions.")) 41 | ), 42 | mainPanel( 43 | h3(textOutput("obj.fun.name")), 44 | p("Be patient. This may take a while; especially in the case of", strong("2D functions"), "."), 45 | plotOutput("obj.fun.plot") 46 | ) 47 | )) -------------------------------------------------------------------------------- /man/mbo_parallel.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/doc_mbo_parallel.R 3 | \name{mbo_parallel} 4 | \alias{mbo_parallel} 5 | \title{Parallelization in mlrMBO} 6 | \description{ 7 | In mlrMBO you can parallelize the tuning on two different levels to speed up computation: 8 | \itemize{ 9 | \item{\code{mlrMBO.feval}}{Multiple evaluations of the target function.} 10 | \item{\code{mlrMBO.propose.points}}{Optimization of the infill criteria if multiple are used (e.g. ParEGO and ParallelLCB)} 11 | } 12 | Internally the evaluation of the target function is realized with the R package parallelMap. 13 | See the mlrMBO tutorial and the Github project pages of parallelMap for instructions on how to set up parallelization. 14 | The different levels of parallelization can be specified in \code{parallelStart*}. 15 | Details for the levels mentioned above are given below: 16 | \itemize{ 17 | \item{Evaluation of the objective function can be parallelized in cases multiple points are to be evaluated at once. These are: evaluation of the initial design, multiple proposed points per iteration and evaluation of the target function in \code{\link{exampleRun}}. (Level: \code{mlrMBO.feval})} 18 | \item{Model fitting / point proposal - in some cases where independent, expensive operations are performed. (Level: \code{mlrMBO.propose.points})} 19 | } 20 | Details regarding the latter: 21 | \describe{ 22 | \item{single-objective MBO with LCB multi-point}{Parallel optimization of LCBs for the lambda-values.} 23 | \item{Multi-objective MBO with ParEGO}{Parallel optimization of scalarization functions.} 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /todo-files/crit_eqi.R: -------------------------------------------------------------------------------- 1 | # Expected quantil improvement (EQI). 2 | # FIXME: control$eqi.beta in MBOControl! 3 | infillCritEQI = function(points, model, control, par.set, design) { 4 | 5 | if(is.null(control$eqi.beta)==T){beta=0.75}else{beta=control$eqi.beta} 6 | 7 | #FIXME: generalize new.noise.var for all models 8 | maximize.mult = ifelse(control$minimize, 1, -1) 9 | 10 | # q.min 11 | # FIXME: generalize model$learner.model@X for all models 12 | p.current.model <- predict(object=model, newdata=as.data.frame(model$learner.model@X))$data 13 | q.min <- min(model$learner.model*p.current.model$response + qnorm(beta) * p.current.model$se) 14 | 15 | 16 | p = predict(object=model, newdata = points)$data 17 | p.mu = p$response #m_k(x^(n+1)) 18 | p.se = p$se # #s_k(x^(n+1)) 19 | 20 | 21 | #FIXME: how do we select here best? 22 | pure.noise.var = if (inherits(model$learner, "regr.km")) 23 | pure.noise.var = model$learner.model@covariance@nugget 24 | else 25 | estimateResidualVariance(model, data = design2, target = control$y.name) 26 | tau = sqrt(pure.noise.var) 27 | 28 | # new.noise.var = model$learner.model@covariance@nugget 29 | # if(length(new.noise.var)==0) new.noise.var=0 # FIXME: nugget.estim=FALSE -> no noise, EQI does not work 30 | 31 | mq <- p.mu + qnorm(beta) * sqrt((tau * p.se^2)/(tau + p.se^2)) 32 | sq <- p.se^2/sqrt(new.noise.var + p.se^2) 33 | 34 | d = q.min - mq 35 | 36 | xcr <- d/sq 37 | xcr.prob <- pnorm(xcr) 38 | xcr.dens <- dnorm(xcr) 39 | 40 | eqi = ifelse(p.se < 1e-06, 0, 41 | (sq * (xcr * xcr.prob + xcr.dens))) 42 | 43 | return(-eqi) 44 | } 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /todo-files/makeEnsemble.r: -------------------------------------------------------------------------------- 1 | makeRLEnsemble = function(id = "RLEnsemble", base.learners) { 2 | assertCharacter(id, "character", len = 1L, any.missing = FALSE) 3 | assertList(base.learners) 4 | checkListElementClass(base.learners, "Learner") 5 | types = unique(extractSubList(base.learners, "type")) 6 | if (!identical(types, "regr")) 7 | stop("Base learners must all be regression learners!") 8 | pts = unique(extractSubList(base.learners, "predict.type")) 9 | 10 | lrn = structure(list( 11 | id = id, 12 | type = "regr", 13 | package = unique(extractSubList(base.learners, "package")), 14 | par.set = makeParamSet(), 15 | par.vals = list(), 16 | predict.type = "response", 17 | numerics = all(extractSubList(base.learners, "numerics")), 18 | factors = all(extractSubList(base.learners, "factors")), 19 | missings = all(extractSubList(base.learners, "missings")), 20 | weights = all(extractSubList(base.learners, "weights")), 21 | oneclass = FALSE, 22 | twoclass = FALSE, 23 | multiclass = FALSE, 24 | prob = FALSE, 25 | se = all(extractSubList(base.learners, "se")) 26 | ), class = c("RLEnsemble", "Learner")) 27 | lrn$base.learners = base.learners 28 | lrn$action.vals = matrix(NA, nrow=0, ncol=length(base.learners)) 29 | lrn$sel.learner = integer(0) 30 | return(lrn) 31 | } 32 | 33 | trainLearner.RLEnsemble = function(.learner, .task, .subset, ...) { 34 | bls = .learner$base.learners 35 | sl = sample(1:length(bls), 1, prob=gibbs(action.vals[nrow(action.vals),])) 36 | sel.learners[j] = sl 37 | trainLearner(bls[[sl]], .task, .subset, ...) 38 | } 39 | 40 | -------------------------------------------------------------------------------- /inst/examples/ex_1d_discrete_noisy.R: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ### 3 | ### optimizing 1D fun with 3 categorical level and 4 | ### noisy outout with random forest 5 | ### 6 | ##################################################### 7 | \dontrun{ 8 | library(mlrMBO) 9 | library(ggplot2) 10 | set.seed(1) 11 | configureMlr(show.learner.output = FALSE) 12 | 13 | obj.fun = makeSingleObjectiveFunction( 14 | name = "Mixed decision space function", 15 | fn = function(x) { 16 | if (x$foo == "a") { 17 | return(5 + x$bar^2 + rnorm(1)) 18 | } else if (x$foo == "b") { 19 | return(4 + x$bar^2 + rnorm(1, sd = 0.5)) 20 | } else { 21 | return(3 + x$bar^2 + rnorm(1, sd = 1)) 22 | } 23 | }, 24 | par.set = makeParamSet( 25 | makeDiscreteParam("foo", values = letters[1:3]), 26 | makeNumericParam("bar", lower = -5, upper = 5) 27 | ), 28 | has.simple.signature = FALSE, # function expects a named list of parameter values 29 | noisy = TRUE 30 | ) 31 | 32 | ctrl = makeMBOControl() 33 | ctrl = setMBOControlTermination(ctrl, iters = 10L) 34 | 35 | # we can basically do an exhaustive search in 3 values 36 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritEI(), 37 | opt.restarts = 1L, opt.focussearch.points = 3L, opt.focussearch.maxit = 1L) 38 | 39 | design = generateDesign(20L, getParamSet(obj.fun), fun = lhs::maximinLHS) 40 | 41 | lrn = makeMBOLearner(ctrl, obj.fun) 42 | 43 | run = exampleRun(obj.fun, design = design, learner = lrn, control = ctrl, 44 | points.per.dim = 50L, show.info = TRUE) 45 | 46 | print(run) 47 | plotExampleRun(run, densregion = TRUE, gg.objects = list(theme_bw())) 48 | } 49 | -------------------------------------------------------------------------------- /todo-files/aydin.R: -------------------------------------------------------------------------------- 1 | library(BBmisc) 2 | library(mlrMBO) 3 | library(parallelMap) 4 | library(emoa) 5 | source("todo-files/plot_multiobjMBO.R") 6 | set.seed(1) 7 | 8 | # create parameter space 9 | cost = makeNumericParam("cost", lower = -24, upper = 1, trafo = function(x) 2^x) 10 | gamma = makeNumericParam("gamma", lower = -24, upper = 1, trafo = function(x) 2^x) 11 | epsDualGap = makeNumericParam("epsilon", lower = -13, upper = -1, trafo = function(x) 2^x) 12 | ps= makeParamSet(cost, gamma, epsDualGap) 13 | 14 | f = function(x) { 15 | t = exp( (1.0 - x$cost)^2 + ( 1.0 - x$gamma)^2 + (0.1 - x$epsilon)^2) 16 | return(c(t, (0.1 - x$eps)^2)) 17 | } 18 | 19 | imputeFun = function(x, y, opt.path) { 20 | c(0.5 + rexp(1, 10), 60 * 480 + rexp(1, 1 / 100)) 21 | } 22 | 23 | # create control object 24 | learner = makeLearner("regr.km", predict.type = "se") 25 | control = makeMBOControl( 26 | n.objectives = 2L, 27 | minimize = c(TRUE, TRUE), 28 | iters = 5L, 29 | propose.points = 20L, 30 | init.design.points = 20L, 31 | y.name = c("exp", "eps"), 32 | # save.on.disk.at = 1:11, 33 | # save.file.path =paste(storagePath, "/saveFile.mboData", sep = ""), 34 | impute = imputeFun) 35 | control = setMBOControlInfill (control, 36 | crit = "lcb", 37 | filter.proposed.points = TRUE, 38 | filter.proposed.points.tol = 1e-2) 39 | control = setMBOControlMultiObj (control, 40 | method = "parego", 41 | parego.use.margin.points = c(TRUE, FALSE)) 42 | resMBO = mbo(f, ps, learner = learner, control = control) 43 | #plot(resMBO, infill.crit = "lcb") 44 | 45 | -------------------------------------------------------------------------------- /R/filterProposedPoints.R: -------------------------------------------------------------------------------- 1 | # tries to sequentially accept proposed points. points which are located too close to design 2 | # points or already accepted proposed points are dropped and replaced by random points. 3 | # 4 | # input: 5 | # prop [list] : list of proposed points, as generated by proposePoints 6 | # output [list] : same format as input, but some points replaced 7 | 8 | filterProposedPoints = function(prop, opt.state) { 9 | 10 | opt.problem = getOptStateOptProblem(opt.state) 11 | opt.path = getOptStateOptPath(opt.state) 12 | par.set = getOptProblemParSet(opt.problem) 13 | control = getOptProblemControl(opt.problem) 14 | 15 | # prepare stuff 16 | n = nrow(prop$prop.points) 17 | design = getOptPathX(opt.path) 18 | calcMaxMetric = function(x, y) max(abs(x - y)) 19 | to.delete = rep(FALSE, n) 20 | 21 | # look at min distance from i-point to current set (design + accepted) 22 | for (i in seq_len(n)) { 23 | pp = prop$prop.points[i, ] 24 | min.dist = min(apply(design, 1L, calcMaxMetric, y = pp)) 25 | # if too close, mark i-point, otherwise add it to set 26 | if (min.dist < control$filter.proposed.points.tol) 27 | to.delete[i] = TRUE 28 | else 29 | design = rbind(design, pp) 30 | } 31 | 32 | # for now replace removed design points with random points, 33 | # we leave all other data in prop like it is, we have prop.tye "random_filter" 34 | n.replace = sum(to.delete) 35 | 36 | if (n.replace > 0) { 37 | # FIXME: we might want to do something smarter here. how about augmenting the current design? 38 | prop$prop.points[to.delete, ] = generateRandomDesign(n.replace, par.set) 39 | prop$prop.type[to.delete] = "random_filter" 40 | } 41 | 42 | return(prop) 43 | } 44 | -------------------------------------------------------------------------------- /todo-files/clement/test_clement.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | library("mnormt") 4 | library("KrigInv") #because I use the "predict" function of KrigInv (which is now the 5 | #same as in DiceKriging) 6 | 7 | source("R/Scripts/multipointEI/ramunas/multEI_allfunctions.R") #change the source ! 8 | 9 | 10 | set.seed(8) 11 | n <- 6;T <- 80;d <- 2;r <- 2 12 | fun <- branin 13 | design <- data.frame(maximinLHS(n,k=2)) 14 | response <- fun(design) 15 | model <- km(formula=~1, design = design, response = response, 16 | covtype="matern3_2") 17 | 18 | xx <- runif(r*d);X.new <- matrix(xx,nrow=r) 19 | newdata <- as.data.frame(X.new); colnames(newdata) <- colnames(model@X) 20 | krig <- predict(object=model, newdata=newdata,type="UK",se.compute=TRUE, cov.compute=TRUE) 21 | mk <- krig$mean 22 | Sigma.r <- krig$cov 23 | 24 | t1 <- Sys.time() 25 | for(i in 1:100) aa <- multEI(mu=mk,sigma=Sigma.r,T=T) 26 | t2 <- Sys.time() 27 | difftime(t2,t1) 28 | 29 | ###### time for 100 calls 30 | #r = 2 : 0.09sec 31 | #r = 3 : 0.29sec 32 | #r = 4 : 1.18sec 33 | #r = 5 : 2.94sec 34 | #r = 6 : 5.64sec 35 | #r = 7 : 11.1sec 36 | #r = 8 : 18.3sec 37 | #r = 9 : 26.3sec 38 | #r = 10 :35sec 39 | 40 | 41 | #verification with a MC calculation 42 | mychol <- chol(Sigma.r) 43 | nsim <- 100000 44 | white.noise <- rnorm(n=nsim*r) 45 | corr.sim <- crossprod(mychol,matrix(white.noise,nrow=r)) + mk 46 | 47 | mean(apply(X=pmax(corr.sim-T,0),MARGIN=2,FUN=max)) 48 | multEI(mu=mk,sigma=Sigma.r,T=T) 49 | 50 | #example of maximisation of multEI 51 | 52 | obj <- multEI.nsteps(model=model,fun=branin,npoints=r,nsteps=1, 53 | lower=c(0,0),upper=c(1,1),minimization=TRUE) 54 | 55 | obj$par #r points ! 56 | 57 | -------------------------------------------------------------------------------- /src/hv.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | 3 | hv.h 4 | 5 | --------------------------------------------------------------------- 6 | 7 | Copyright (c) 2005, 2006 8 | Carlos Fonseca 9 | Manuel Lopez-Ibanez 10 | Luis Paquete 11 | 12 | This program is free software (software libre); you can redistribute 13 | it and/or modify it under the terms of the GNU General Public License 14 | as published by the Free Software Foundation; either version 2 of the 15 | License, or (at your option) any later version. 16 | 17 | This program is distributed in the hope that it will be useful, but 18 | WITHOUT ANY WARRANTY; without even the implied warranty of 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 | General Public License for more details. 21 | 22 | You should have received a copy of the GNU General Public License 23 | along with this program; if not, you can obtain a copy of the GNU 24 | General Public License at: 25 | http://www.gnu.org/copyleft/gpl.html 26 | or by writing to: 27 | Free Software Foundation, Inc., 59 Temple Place, 28 | Suite 330, Boston, MA 02111-1307 USA 29 | 30 | ---------------------------------------------------------------------- 31 | 32 | 33 | *************************************************************************/ 34 | #ifndef MLRMBO_HV_H_ 35 | #define MLRMBO_HV_H_ 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | extern int stop_dimension; 42 | double fpli_hv(double *data, int d, int n, const double *ref); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /todo-files/bench/mco/defs.R: -------------------------------------------------------------------------------- 1 | # GENERAL 2 | FEVALS = function(dimx) 40 * dimx 3 | REPLS = 20L 4 | INIT_DESIGN_POINTS = function(dimx) { 5 | return(4 * dimx) 6 | } 7 | PARALLEL_PROP_POINTS = c(1L, 4L) 8 | MBO_ITERS = function(dimx, prop.points) 9 | (FEVALS(dimx) - INIT_DESIGN_POINTS(dimx)) / prop.points 10 | 11 | # FOCUSSEARCH 12 | FOCUSSEARCH_POINTS = 1000L 13 | FOCUSSEARCH_MAXIT = 3L 14 | FOCUSSEARCH_RESTARTS = 3L 15 | 16 | # general algo stuff 17 | CB_PI = 0.5 # mspot_cb + dib (all) 18 | MULTICRIT_REFPOINT = "all" # mspot (all) + dib.sms 19 | MULTICRIT_REFPOINT_OFFSET = 1 # mspot (all) + dib.sms 20 | FILTER_PROPOSED_POINTS = TRUE 21 | FILTER_PROPOSED_POINTS_TOL = 1e-4 22 | 23 | # nsga2 baseline comparison 24 | BASELINE_NSGA2_POPSIZE = function(dimx) dimx * 4 # must be a multiple of 4, set like init design 25 | # the next lines assume that mco version >= 1.0??? is installed 26 | BASELINE_NSGA2_GENERATIONS1 = function(dimx) FEVALS(dimx) / BASELINE_NSGA2_POPSIZE(dimx) - 1L 27 | BASELINE_NSGA2_GENERATIONS2 = function(dimx) 10 * FEVALS(dimx) / BASELINE_NSGA2_POPSIZE(dimx) - 1L 28 | EXACT_NSGA2_POPSIZE = function(dimx) FEVALS(dimx) 29 | EXACT_NSGA2_GENERATIONS = function(dimx) 1000L 30 | BASELINE_NSGA2_cprob = function(dimx) 1 31 | BASELINE_NSGA2_cdist = function(dimx) 15 32 | BASELINE_NSGA2_mprob = function(dimx) 1 / dimx 33 | BASELINE_NSGA2_mdist = function(dimx) 20 34 | 35 | # randomSearch baseline comparison 36 | BASELINE_RANDOMSEARCH_BUDGET1 = function(dimx) FEVALS(dimx) - INIT_DESIGN_POINTS(dimx) 37 | BASELINE_RANDOMSEARCH_BUDGET2 = function(dimx) 10 * FEVALS(dimx) - INIT_DESIGN_POINTS(dimx) 38 | 39 | # parego 40 | PAREGO_RHO = 0.05 41 | PAREGO_SAMPLE_MORE_WEIGHTS = 5L 42 | #PAREGO_CRIT = "ei" 43 | 44 | # mspot 45 | MSPOT_NSGA2_GENERATIONS = 90L 46 | MSPOT_NSGA2_POPSIZE = 100L 47 | 48 | # dib: 49 | DIB_SMS_EPS = NULL # adaptive 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /todo-files/RLearner_regr_kmforrester.R: -------------------------------------------------------------------------------- 1 | #' @export 2 | makeRLearner.regr.kmforrester = function() { 3 | makeRLearnerRegr( 4 | cl = "regr.kmforrester", 5 | package = "DiceKriging", 6 | par.set = makeParamSet( 7 | makeDiscreteLearnerParam(id = "covtype", default = "matern5_2", 8 | values = list("gauss", "matern5_2", "matern3_2", "exp", "powexp")), 9 | makeNumericVectorLearnerParam(id = "noise.var"), 10 | makeDiscreteLearnerParam(id = "optim.method", default = "BFGS", 11 | values = list("BFGS", "gen")), 12 | makeNumericVectorLearnerParam(id = "lower"), 13 | makeNumericVectorLearnerParam(id = "upper"), 14 | makeUntypedLearnerParam(id = "control") 15 | ), 16 | properties = c("numerics", "se"), 17 | name = "Kriging-Reinterpolation", 18 | short.name = "kmforrester", 19 | note = "In predict, we currently always use type = 'SK'." 20 | ) 21 | } 22 | 23 | #' @export 24 | trainLearner.regr.kmforrester = function(.learner, .task, .subset, ...) { 25 | d = getTaskData(.task, .subset, target.extra = TRUE) 26 | # fit a regular Kriging-model with nugget effect estimation 27 | m = DiceKriging::km(design = d$data, response = d$target, nugget.estim = TRUE, ...) 28 | p = predict(m, d$data, type = "SK")$mean 29 | # now use the kriging predictions as the response for the second model 30 | m = DiceKriging::km(design = d$data, response = p, nugget.estim = FALSE, 31 | coef.trend = m@trend.coef, coef.var = m@covariance@sd2, coef.cov = m@covariance@range.val) 32 | return(m) 33 | } 34 | 35 | #' @export 36 | predictLearner.regr.kmforrester = function(.learner, .model, .newdata, ...) { 37 | se = (.learner$predict.type != "response") 38 | p = predict(.model$learner.model, newdata = .newdata, type = "SK", se.compute = se) 39 | if(!se) 40 | return(p$mean) 41 | else 42 | cbind(p$mean, p$sd) 43 | } 44 | -------------------------------------------------------------------------------- /tests/testthat/test_adaptive_infillcrits.R: -------------------------------------------------------------------------------- 1 | context("adaptive infill crits") 2 | 3 | test_that("adaptive infill crit works with all termination criteria", { 4 | 5 | f = testf.fsphere.1d 6 | f.slow = testf.fsphere.1d.slow 7 | f.max = convertToMaximization(f) 8 | design = testd.fsphere.1d 9 | design.max = design 10 | design$y = apply(design, 1, f) 11 | design.max$y = apply(design.max, 1, f.max) 12 | 13 | terminations = list( 14 | iters = list(iters = 4L), 15 | time.budget = list(time.budget = 2), 16 | exec.time.budget = list(exec.time.budget = 1.5, iters = 13, use.for.adaptive.infill = "exec.time.budget"), 17 | target.fun.value = list(target.fun.value = min(design$y)/3, iters = 13, use.for.adaptive.infill = "target.fun.value"), 18 | target.fun.value.max = list(target.fun.value = max(design.max$y)/3, iters = 13, use.for.adaptive.infill = "target.fun.value"), 19 | max.evals = list(max.evals = nrow(design) + 4L) 20 | ) 21 | 22 | ctrl = makeMBOControl() 23 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritAdaCB()) 24 | 25 | for (i in names(terminations)) { 26 | ctrl2 = do.call(setMBOControlTermination, c(list(control = ctrl), terminations[[i]])) 27 | if (i == "exec.time.budget") { 28 | fun = f.slow 29 | des = design 30 | } else if (i == "target.fun.value.max") { 31 | fun = f.max 32 | des = design.max 33 | } else { 34 | fun = f 35 | des = design 36 | } 37 | or = mbo(fun, des, control = ctrl2) 38 | expect_number(or$y, info = i) 39 | df = as.data.frame(or$opt.path) 40 | expect_true(any(df$lambda > 1), info = i) 41 | expect_true(length(unique(df$lambda)) >= 2, info = i) 42 | expect_true(all(diff(df$lambda[!is.na(df$lambda)])<=0), info = i) 43 | expect_true(all(df$prop.type %in% c("infill_adacb", "initdesign")), info = i) 44 | expect_numeric(df$adacb, info = i) 45 | } 46 | }) 47 | -------------------------------------------------------------------------------- /todo-files/bench/mlr/batchexps.R: -------------------------------------------------------------------------------- 1 | library(BBmisc) 2 | library(lhs) 3 | library(BatchExperiments) 4 | library(fail) 5 | # load_all("~/cos/mlr") 6 | # load_all("~/cos/mlrMBO") 7 | 8 | 9 | unlink("modelsel-files", recursive = TRUE) 10 | reg = makeExperimentRegistry("modelsel", packages = c("mlr"), src.files = c( 11 | "defs.R", 12 | "multiplexer.R", 13 | "trainPredict.R", 14 | "algoTemplate.R" 15 | )) 16 | 17 | fhandle = fail("data") 18 | 19 | # load data, create task and set pos class as min class, 20 | # then remove const features and create CV instance 21 | myAddProblem = function(reg, id, data, target) { 22 | y = data[, target] 23 | # remove crappy special chars in colnames 24 | cns = colnames(data) 25 | j = which(cns != target) 26 | cns[j] = sprintf("v%i", seq_along(j)) 27 | colnames(data) = cns 28 | data = droplevels(data) 29 | data = removeConstantFeatures(data) 30 | task = makeClassifTask(data = data, target = target) 31 | rdesc = makeResampleDesc("CV", iters = CV_FOLDS_OUTER, stratify = TRUE) 32 | rin = makeResampleInstance(rdesc, task = task) 33 | addProblem(reg, id = id, static = list( 34 | task = task, 35 | rin = rin 36 | )) 37 | } 38 | 39 | fs = fhandle$ls() 40 | # fs = fs[1] 41 | for (f in fs) { 42 | messagef("Adding data set: %s", f) 43 | myAddProblem(reg, id = f, data = fhandle$get(f), "class") 44 | } 45 | 46 | source("algos.R") 47 | 48 | addMyExps = function(id) { 49 | des = makeDesign(id, exhaustive = list( 50 | iter = 1:CV_FOLDS_OUTER) 51 | ) 52 | addExperiments(reg, algo.designs = des, skip.defined = TRUE) 53 | } 54 | 55 | addMyExps("random") 56 | # addMyExps("irace") 57 | addMyExps("mbo") 58 | 59 | #print(reg) 60 | #getJob(reg, 1) 61 | # testJob(reg, 1, external = FALSE) 62 | 63 | # submitJobs(reg) 64 | # waitForJobs(reg) 65 | # res = reduceResultsExperiments(reg, fun = function(job, res) res$aggr) 66 | # print(res) 67 | #testJob(reg, , external = TRUE) 68 | -------------------------------------------------------------------------------- /man/MBOSingleObjResult.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/MBOResult.R 3 | \name{MBOSingleObjResult} 4 | \alias{MBOSingleObjResult} 5 | \title{Single-Objective result object.} 6 | \description{ 7 | \itemize{ 8 | \item{x [\code{list}]}{Named list of proposed optimal parameters.} 9 | \item{y [\code{numeric(1)}]}{Value of objective function at \code{x}, 10 | either from evals during optimization or from requested final evaluations, 11 | if those were greater than 0.} 12 | \item{best.ind [\code{numeric(1)}]}{Index of \code{x} in the opt.path.} 13 | \item{opt.path [\code{\link[ParamHelpers]{OptPath}}]}{Optimization path. 14 | Includes all evaluated points and additional information as documented in \link{mbo_OptPath}. 15 | You can convert it via \code{as.data.frame}.} 16 | \item{resample.results [List of \code{\link[mlr]{ResampleResult}}]}{List of the desired \code{resample.results} if \code{resample.at} is set in \code{makeMBOControl}.} 17 | \item{final.state [\code{character}] The final termination state. Gives information why the optimization ended. Possible values are 18 | \describe{ 19 | \item{term.iter}{Maximal number of iterations reached.} 20 | \item{term.time}{Maximal running time exceeded.} 21 | \item{term.exectime}{Maximal execution time of function evaluations reached.} 22 | \item{term.yval}{Target function value reached.} 23 | \item{term.fevals}{maximal number of function evaluations reached.} 24 | \item{term.custom}{Terminated due to custom, user-defined termination condition.} 25 | } 26 | } 27 | \item{models [List of \code{\link[mlr]{WrappedModel}}]}{List of saved regression models if \code{store.model.at} is set in \code{makeMBOControl}. The default is that it contains the model generated after the last iteration.} 28 | \item{control [\code{MBOControl}] Control object used in optimization} 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/testthat/test_multiobj_dib.R: -------------------------------------------------------------------------------- 1 | context("multi-objective: dib") 2 | 3 | test_that("multi-objective dib works", { 4 | # Test normal run 5 | learner = makeLearner("regr.km", nugget.estim = TRUE, predict.type = "se") 6 | ctrl = makeMBOControl(n.objectives = 2L) 7 | ctrl = setMBOControlTermination(ctrl, iters = 5L) 8 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritDIB(cb.lambda = 1), 9 | opt.focussearch.points = 10L, opt.focussearch.maxit = 5L) 10 | ctrl = setMBOControlMultiObj(ctrl, method = "dib") 11 | or = mbo(testfmco1, testdesmco1, learner = learner, control = ctrl) 12 | expect_matrix(or$pareto.front, mode = "numeric", any.missing = FALSE) 13 | expect_output(print(or), "Optimization path") 14 | 15 | # reference point: 16 | expect_error(setMBOControlMultiObj(ctrl, method = "dib", ref.point.method = "const")) 17 | ctrl = setMBOControlMultiObj(ctrl, method = "dib", ref.point.method = "const", ref.point.val = c(11, 11)) 18 | or = mbo(testfmco1, testdesmco1, learner = learner, control = ctrl) 19 | expect_matrix(or$pareto.front, mode = "numeric", any.missing = FALSE) 20 | ctrl = setMBOControlMultiObj(ctrl, method = "dib", ref.point.method = "all", ref.point.offset = 2) 21 | or = mbo(testfmco1, testdesmco1, learner = learner, control = ctrl) 22 | expect_matrix(or$pareto.front, mode = "numeric", any.missing = FALSE) 23 | ctrl = setMBOControlMultiObj(ctrl, method = "dib", ref.point.method = "front") 24 | or = mbo(testfmco1, testdesmco1, learner = learner, control = ctrl) 25 | expect_matrix(or$pareto.front, mode = "numeric", any.missing = FALSE) 26 | 27 | ctrl = setMBOControlMultiObj(ctrl, method = "dib", dib.indicator = "eps") 28 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritDIB(cb.lambda = 1), opt.focussearch.points = 10) 29 | or = mbo(testfmco1, testdesmco1, learner = learner, control = ctrl) 30 | expect_matrix(or$pareto.front, mode = "numeric", any.missing = FALSE) 31 | }) 32 | -------------------------------------------------------------------------------- /R/getMBOInfillCrit.R: -------------------------------------------------------------------------------- 1 | #' @title Get properties of MBO infill criterion. 2 | #' 3 | #' @description 4 | #' Returns properties of an infill criterion, e.g., name or id. 5 | #' 6 | #' @param x [\code{\link{MBOInfillCrit}}]\cr 7 | #' Infill criterion. 8 | #' @param par.name [\code{character(1)}]\cr 9 | #' Parameter name. 10 | #' @name getMBOInfillCrit 11 | #' @rdname getMBOInfillCrit 12 | NULL 13 | 14 | #' @export 15 | #' @rdname getMBOInfillCrit 16 | getMBOInfillCritParams = function(x) { 17 | assertClass(x, "MBOInfillCrit") 18 | return(x$params) 19 | } 20 | 21 | #' @export 22 | #' @rdname getMBOInfillCrit 23 | getMBOInfillCritParam = function(x, par.name) { 24 | assertClass(x, "MBOInfillCrit") 25 | assertString(par.name) 26 | return(getMBOInfillCritParams(x)[[par.name]]) 27 | } 28 | 29 | #' @export 30 | #' @rdname getMBOInfillCrit 31 | getMBOInfillCritName = function(x) { 32 | assertClass(x, "MBOInfillCrit") 33 | return(x$name) 34 | } 35 | 36 | #' @export 37 | #' @rdname getMBOInfillCrit 38 | getMBOInfillCritId = function(x) { 39 | assertClass(x, "MBOInfillCrit") 40 | return(x$id) 41 | } 42 | 43 | #' @export 44 | #' @rdname getMBOInfillCrit 45 | hasRequiresInfillCritStandardError = function(x) { 46 | assertClass(x, "MBOInfillCrit") 47 | return(x$requires.se) 48 | } 49 | 50 | #' @export 51 | #' @rdname getMBOInfillCrit 52 | getMBOInfillCritComponents = function(x) { 53 | assertClass(x, "MBOInfillCrit") 54 | return(x$components) 55 | } 56 | 57 | getMBOInfillCritDummyComponents = function(x) { 58 | assertClass(x, "MBOInfillCrit") 59 | ns = getMBOInfillCritComponents(x) 60 | as.data.frame(BBmisc::namedList(ns, NA_real_)) 61 | } 62 | 63 | getMBOInfillCritMultiplier = function(x) { 64 | assertClass(x, "MBOInfillCrit") 65 | if (x$opt.direction == "minimize") 66 | return(1) 67 | else if (x$opt.direction == "maximize") 68 | return(-1) 69 | else 70 | stopf("The direction of the infill criterion is '%s' but should be 'minimize' or 'maximize' at this point.", x$opt.direction) 71 | } 72 | -------------------------------------------------------------------------------- /R/OptState_setter.R: -------------------------------------------------------------------------------- 1 | setOptStateModels = function(opt.state, models) { 2 | opt.state$models = models 3 | opt.state$models.loop = getOptStateLoop(opt.state) 4 | invisible() 5 | } 6 | 7 | setOptStateRandomSeed = function(opt.state) { 8 | opt.state$random.seed = getRandomSeed() 9 | invisible() 10 | } 11 | 12 | setOptStateTasks = function(opt.state, tasks) { 13 | opt.state$tasks = tasks 14 | opt.state$tasks.loop = getOptStateLoop(opt.state) 15 | invisible() 16 | } 17 | 18 | setOptStateTimeModel = function(opt.state, time.model) { 19 | opt.state$time.model = time.model 20 | invisible() 21 | } 22 | 23 | setOptStateLoop = function(opt.state, loop = NULL) { 24 | if (is.null(loop)) 25 | opt.state$loop = opt.state$loop + 1L 26 | else 27 | opt.state$loop = loop 28 | # save resampling and models in result routine 29 | setOptStateRandomSeed(opt.state) 30 | setOptStateTimeUsed(opt.state) 31 | invisible() 32 | } 33 | 34 | setOptStateTimeLastSaved = function(opt.state, time) { 35 | opt.state$time.last.saved = time 36 | invisible() 37 | } 38 | 39 | setOptStateLoopStarttime = function(opt.state) { 40 | opt.state$loop.starttime = Sys.time() 41 | invisible() 42 | } 43 | 44 | setOptStateTimeUsed = function(opt.state, time.used = NULL, time.add = NULL) { 45 | if (!is.null(time.used)) { 46 | opt.state$time.used = time.used 47 | } else if (!is.null(time.add)) { 48 | opt.state$time.used = getOptStateTimeUsed(opt.state) + time.add 49 | } else { 50 | opt.state$time.used = getOptStateTimeUsed(opt.state) + difftime(Sys.time(), getOptStateLoopStarttime(opt.state), units = "secs") 51 | setOptStateLoopStarttime(opt.state) 52 | } 53 | invisible() 54 | } 55 | 56 | setOptStateState = function(opt.state, state) { 57 | assertSubset(state, getOptStateValidStates()) 58 | opt.state$state = state 59 | invisible() 60 | } 61 | 62 | setOptStateProgress = function(opt.state, progress) { 63 | assertNumber(progress, null.ok = TRUE) 64 | opt.state$progress = progress 65 | invisible() 66 | } 67 | -------------------------------------------------------------------------------- /R/makeProposal.R: -------------------------------------------------------------------------------- 1 | # Generates the Proposal that is universally used internally 2 | # @param control [MBOcontrol] 3 | # @param prop.points [data.frame (nxk)] 4 | # columns: each parameter 5 | # rows: each point proposal (usually 1-row, multi-point: more rows) 6 | # @param propose.time [numeric (n)] 7 | # Vector with time it took for each proposal corresponding to each row (see above) 8 | # @param prop.type [character (n)] 9 | # Character telling us how this proposal was created 10 | # @param crit.vals [matrix (nxp)] 11 | # Values of the infill criterion 12 | # @param crit.components [data.frame (nxq)] 13 | # Components that generated the infill criterion 14 | # @errors.model [character] 15 | # Error message in case something went wrong. 16 | makeProposal = function(control, prop.points, propose.time = NULL, prop.type, crit.vals = NULL, crit.components = NULL, errors.model = NA_character_) { 17 | 18 | n.points = coalesce(nrow(prop.points), control$propose.points) 19 | 20 | if (is.null(propose.time)) { 21 | propose.time = rep(NA_real_, n.points) 22 | } 23 | 24 | 25 | if (is.null(crit.vals)) { 26 | if (control$n.objectives > 1L && control$multiobj.method == "mspot") 27 | # mspot is the special kid, that needs multiple crit vals 28 | ncol = control$n.objectives + 1 29 | else 30 | ncol = 1 31 | crit.vals = matrix(NA_real_, nrow = n.points, ncol = ncol) 32 | } 33 | 34 | if (is.null(crit.components) && !is.null(control$infill.crit$components)) { 35 | crit.components = getMBOInfillCritDummyComponents(control$infill.crit) 36 | # crit.components is a data.frame with one row 37 | # we need the dummy values for all n.points 38 | crit.components[seq_len(n.points), ] = crit.components[1, ] 39 | } 40 | 41 | res = list( 42 | prop.points = prop.points, 43 | propose.time = propose.time, 44 | prop.type = prop.type, 45 | crit.vals = crit.vals, 46 | crit.components = crit.components, 47 | errors.model = errors.model) 48 | addClasses(res, "Proposal") 49 | } 50 | -------------------------------------------------------------------------------- /R/renderExampleRunPlotSingleCrit.R: -------------------------------------------------------------------------------- 1 | # single-objective 2 | #' @export 3 | renderExampleRunPlot.MBOExampleRun = function(object, iter, densregion = TRUE, 4 | se.factor = 1, single.prop.point.plots = FALSE, xlim = NULL, ylim = NULL, 5 | point.size = 3, line.size = 1, trafo = NULL, colors = c("red", "blue", "green"), ...) { 6 | 7 | requirePackages("ggplot2") 8 | 9 | iters.max = object$control$iters 10 | iter = asInt(iter, lower = 1L, upper = iters.max) 11 | assertFlag(densregion) 12 | assertNumber(se.factor, lower = 0) 13 | assertNumber(point.size, lower = 1) 14 | assertNumber(line.size, lower = 1) 15 | assertCharacter(colors, len = 3L, any.missing = FALSE) 16 | 17 | if (!is.null(xlim)) { 18 | assertNumeric(xlim, len = 2L, any.missing = FALSE) 19 | } 20 | if (!is.null(ylim)) { 21 | assertNumeric(ylim, len = 2L, any.missing = FALSE) 22 | } 23 | 24 | n.params = object$n.params 25 | par.types = object$par.types 26 | par.set = object$par.set 27 | trafo = buildTrafoList(n.params, trafo) 28 | 29 | if (n.params == 1L) { 30 | if (par.types %nin% c("numeric", "numericvector", "discrete", "discretevector")) { 31 | stopf("For 1D function only plotting of numeric or discrete functions possible, but your function is '%s'.", par.types) 32 | } 33 | return(renderExampleRunPlot1d(object, iter = iter, xlim = xlim, ylim = ylim, se.factor = se.factor, pause = pause, 34 | point.size = point.size, line.size = line.size, trafo = trafo, densregion = densregion, 35 | colors = colors, ...)) 36 | } else if (n.params == 2L) { 37 | if (!hasNumeric(par.set)) { 38 | stopf("At least one parameter of the target function must be numeric!") 39 | } 40 | return(renderExampleRunPlot2d(object, iter = iter, xlim = xlim, ylim = ylim, se.factor = se.factor, pause = pause, 41 | point.size = point.size, line.size = line.size, trafo = trafo, 42 | colors = colors, ...)) 43 | } else { 44 | stopf("Functions with greater than 3 parameters are not supported.") 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /todo-files/todo.txt: -------------------------------------------------------------------------------- 1 | use makeS3Obj when new BBmisc on cran 2 | 3 | jakob: 4 | - überprüfen, ob Parameternamen mit den Standardparameternamen übereinstimmen 5 | - exampleRun_autoplot_1d in zwei Funktionen aufspalten (diskret/numerisch) 6 | - add se.factor param to parameter list 7 | - stat_contour kommt nicht klar mit zu kleinen EI Werten. Konturlinien für EI plot ausblenden? 8 | - add comments to autoplotExampleRun2d 9 | 10 | bernd: 11 | - dependent params 12 | - wie geht konfigurieren von algo auf instanzen? 13 | 14 | stefan: 15 | - EI varianten implementieren, siehe tobias comparison paper, 16 | besonders für noisy optimization 17 | - wir wollen vermutlich erstmal ohne repetitions 18 | an den selben punkten auskommen, sollten aber auch ein paar einfache 19 | verleichsprinzipien mit repetitions haben 20 | - nachdenken, was sonst noch für den noisy fall wichtig ist 21 | und implementieren 22 | 23 | klaus 24 | - multipoint EI implementieren 25 | 26 | nadja 27 | - tutorial 28 | - multipoint 29 | 30 | noch keiner: 31 | - wie optimiert man gut das infill crit, vielleicht auch mit diskreten par. 32 | - possibly scale parameters [0,1] before they go into the model? 33 | - better think about imputing in case of errors and NA. test and doc this. 34 | - think about how to handle FailureModels 35 | 36 | später: 37 | - tuning von surrogat modellen, wie machen wir es effizient? 38 | - wie nimmt man modelle die kein s(x) bieten? 39 | 40 | 41 | 1) multipoint proposal: nur das interface schaffen und eine dämliche methode rein 42 | 2) factor variablen müssen gehen (betrrift modelund model optimizer) 43 | 3) noisy optimization muss gehen (betrifft model, criterium, optimizer und punkte am ende) 44 | 4) dependent params müssen gehen: betrifft model, optimierer 45 | 46 | cases wo man mbo benutzen kann. was ist dann drin? 47 | - deterministisch / noisy 48 | - infill criteria 49 | - reellwertige X / gemischt (integer, real, factor) / dependent paramaters 50 | - single opoinmt / multi point 51 | - error handling 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /R/proposePointsDIB.R: -------------------------------------------------------------------------------- 1 | # proposes 1 or multi-points via direct multi-objective indicator optimization 2 | # the indicators we support are sms and epsilon 3 | # 4 | # 1-point proposal: we take the user-defined cb-lambda from the control, then simply optimize 5 | # 6 | # n-point proposal: we sample n random cb-lambdas, then propose a point normally, 7 | # then add it to the design (fake add) with the its cb-vector as fake output. then iterate. 8 | 9 | proposePointsDIB = function(opt.state) { 10 | 11 | opt.problem = getOptStateOptProblem(opt.state) 12 | models = getOptStateModels(opt.state)$models 13 | designs = getOptStateDesigns(opt.state) 14 | par.set = getOptProblemParSet(opt.problem) 15 | control = getOptProblemControl(opt.problem) 16 | opt.path = getOptStateOptPath(opt.state) 17 | iter = getOptStateLoop(opt.state) 18 | 19 | if (control$propose.points == 1L) { 20 | res = proposePointsByInfillOptimization(opt.state) 21 | } else { 22 | # copy opt.path and set prop.points = 1 23 | control2 = control 24 | control2$propose.points == 1L 25 | # Create CB crit fun 26 | cbFun = makeMBOInfillCritCB(cb.lambda = control2$infill.crit$params$cb.lambda)$fun 27 | props = list() 28 | # copy opt.path so we can store already proposed points in it 29 | opt.path2 = deepCopyOptPath(opt.path) 30 | dob = max(getOptPathDOB(opt.path)) + 1 31 | for (i in 1:control$propose.points) { 32 | prop = proposePointsByInfillOptimization(opt.state, control = control2, opt.path = opt.path2) 33 | cb = evalCritFunForMultiObjModels(cbFun, prop$prop.points, models, control2, 34 | par.set, designs, iter)[1L, ] 35 | x = dfRowToList(prop$prop.points, par.set, 1) 36 | addOptPathEl(opt.path2, x = x, y = cb, dob = dob) 37 | props[[i]] = prop 38 | } 39 | res = joinProposedPoints(props) 40 | # store extra info 41 | # DH: don't to this for now 42 | # FIXME: do we want to sample or adapt lamda here? decide 43 | #res$multipoint.cb.lambdas = z$lambdas 44 | } 45 | return(res) 46 | } 47 | -------------------------------------------------------------------------------- /todo-files/fragen.txt: -------------------------------------------------------------------------------- 1 | wie oft geht das model kaputt bei bbob 1 und 2 D bei kriging? 2 | was genau tun wir dann, wie wird ein punkt proposed? 3 | was machen wir beim plot? 4 | 5 | was machen wir wenn das infill crit komplett flach ist? 6 | 7 | kann man irgendwie mehr auf exploitation gehen wenn es mehr gegen ende des budgets geht? 8 | 9 | was machen wir, wenn das EI sehr klein wird? kann man bei fsphere provieren. 10 | bzw was wenn das se überall sehr klein wird? 11 | 12 | man kann sicherlich die diskrepanz zwischen aposteriori verteilung des modells und echten 13 | y-wert an der stelle x.proposed irgendwie als rückmeldung benutzen 14 | 15 | vielleicht kann man zwischendurch doch das modell resamplen und das irgendwie benutzen 16 | 17 | 18 | bei lcb stoppt irgendwann die exploration oder? wenn lambda zu klein ist? 19 | bei meinem phd sinus bsp kann man das sehen, dass man nur in ein global opr läuft 20 | 21 | es gibt sowohl im reinforcemnt learning als auch im seq. testen das problem, den 22 | besten unter k lösungen zu identifizieren. können wir das nutzen? 23 | 24 | 25 | wie kriegn wir raus ob wir in mbo die zielfunktion logarithmieren? 26 | 27 | kann man irgendwie bessere modelle in den knoten der trees fitten um "glattere" RF modelle bei numerics zu 28 | erzeugen? 29 | 30 | besser intensify oder nur auf ein "korrektes" infillcri setzen? 31 | 32 | welche regr modelle sind allgemein bei mbo sinnvoll? 33 | kann mann allgemein per bootstrap lokale uncertainty schätzen? 34 | 35 | wie ist exakt die definition der uncertainty bei kriging? 36 | das ist prinzipiell ein prediction intervall und kein conf. int. für die mean response? 37 | damit geht die unsicherheit nie gegen null, selbst wenn man unendlcih oft an einer stelle sampelt. das ist anders beim bootstrap oder? 38 | und was will man eigentlich für die infills? 39 | 40 | 41 | beim aei, noisy sphere konnte man ein Problem sehen, dass das crit fast komplett 0 42 | wurde. der optimierer stand dann immer auf einem Plateu 43 | Für das normale EI habe ich vermutlich eine Fortsetzungs-Formel 44 | 45 | -------------------------------------------------------------------------------- /R/doc_mbo_OptPath.R: -------------------------------------------------------------------------------- 1 | #' @title OptPath in mlrMBO 2 | #' 3 | #' @description 4 | #' In mlrMBO the \code{\link[ParamHelpers]{OptPath}} contains extra information next to the information documented in \code{\link[ParamHelpers]{OptPath}}. 5 | #' 6 | #' The extras are: 7 | #' \describe{ 8 | #' \item{train.time}{Time to train the model(s) that produced the points. Only the first slot of the vector is used (if we have multiple points), rest are NA.} 9 | #' \item{propose.time}{Time needed to propose the point. If we have individual timings from the proposal mechanism, we have one different value per point here. If all were generated in one go, we only have one timing, we store it in the slot for the first point, rest are NA.} 10 | #' \item{errors.model}{Possible Error Messages. If point-producing model(s) crashed they are replicated for all n points, if only one error message was passed we store it for the first point, rest are NA.} 11 | #' \item{prop.type}{Type of point proposal. Possible values are 12 | #' \describe{ 13 | #' \item{initdesign}{Points actually not proposed, but in the initial design.} 14 | #' \item{infill\_x}{Here x is a placeholder for the selected infill criterion, e.g., infill\_ei for expected improvement.} 15 | #' \item{random\_interleave}{Uniformly sampled points added additionally to the proposed points.} 16 | #' \item{random\_filtered}{If filtering of proposed points located too close to each other is active, these are replaced by random points.} 17 | #' \item{final\_eval}{If \code{final.evals} is set in \code{\link{makeMBOControl}}: Final evaluations of the proposed solution to reduce noise in y.} 18 | #' } 19 | #' } 20 | #' \item{parego.weight}{Weight vector sampled for multi-point ParEGO} 21 | #' \item{...}{Depending on the chosen infill criterion there will be additional columns, e.g. \code{se} and \code{mean} for the Expected Improvement)} 22 | #' } 23 | #' 24 | #' Moreover, the user may pass additional \dQuote{user extras} by appending a named list 25 | #' of scalar values to the return value of the objective function. 26 | #' 27 | #' @name mbo_OptPath 28 | NULL 29 | -------------------------------------------------------------------------------- /tests/testthat/test_optpath_extras.R: -------------------------------------------------------------------------------- 1 | context("test optpath extras") 2 | 3 | test_that("extras are logged a expected", { 4 | # Our test function stores additional stuff here via attributes 5 | f = makeSingleObjectiveFunction( 6 | fn = function(x) { 7 | res = sum(x^2) 8 | # here we append additional stuff to be logged in opt path 9 | extras = list(extra1 = runif(1), extra2 = letters[sample(1:10, 1)]) 10 | if (res > 2) extras = c(extras, list(.extra3 = "y bigger than two!")) 11 | res = setAttribute(res, "extras", extras) 12 | return(res) 13 | }, 14 | par.set = makeParamSet( 15 | makeNumericParam("x", lower = -2, upper = 2) 16 | ) 17 | ) 18 | des = generateTestDesign(10L, getParamSet(f)) 19 | 20 | learner = makeLearner("regr.km", predict.type = "se") 21 | control = makeMBOControl() 22 | control = setMBOControlTermination(control, iters = 2L) 23 | control = setMBOControlInfill(control, crit = crit.ei, opt = "focussearch") 24 | 25 | # run mbo and convert opt path to data frame 26 | result = mbo(f, des, learner = learner, control = control) 27 | extras.names = c("extra1", "extra2") 28 | 29 | opt.path = as.data.frame(result$opt.path) 30 | 31 | # check whether user extras are in fact logged 32 | expect_subset(extras.names, names(opt.path)) 33 | expect_numeric(opt.path[["extra1"]]) 34 | expect_subset(as.character(opt.path[["extra2"]]), letters[1:10]) 35 | 36 | # check if the dotted extra is logged 37 | expect_equal(getOptPathEl(result$opt.path, which.first(opt.path$y > 2))$extra$.extra3, "y bigger than two!") 38 | 39 | # check whether times (model fitting, execution, ...) are logged 40 | expect_numeric(opt.path[["exec.time"]], any.missing = FALSE) 41 | expect_numeric(opt.path[["train.time"]]) 42 | expect_numeric(opt.path[["propose.time"]]) 43 | 44 | #check whether infill.crit specific informations is logged 45 | expect_subset(c("se", "mean"), names(opt.path)) 46 | expect_numeric(opt.path[["se"]][11:12], any.missing = FALSE) 47 | expect_numeric(opt.path[["mean"]][11:12], any.missing = FALSE) 48 | }) 49 | -------------------------------------------------------------------------------- /todo-files/bench/mco/ZDT.R: -------------------------------------------------------------------------------- 1 | library(emoa) 2 | 3 | ## Set of the 6 DTLZ test functions for the case 2D -> 2D. 4 | ## For every function: x is a vector of length x such that 0 < x[i] < 1, i=1,...,n 5 | ## Output is a vector of length 2, too. 6 | 7 | zdt1 <- function (x) { 8 | f = numeric(2) 9 | n = length(x) 10 | f[1] = x[1] 11 | g = 1 + 9 * mean(x[2:n]) 12 | f[2] = g * (1 - sqrt(f[1] / g)) 13 | return(f) 14 | } 15 | 16 | zdt2 <- function (x) { 17 | f = numeric(2) 18 | n = length(x) 19 | f[1] = x[1] 20 | g = 1 + 9 * mean(x[2:n]) 21 | f[2] = g * (1 - (f[1] / g)^2) 22 | return(f) 23 | } 24 | 25 | 26 | zdt3 <- function (x) { 27 | f = numeric(2) 28 | n = length(x) 29 | f[1] = x[1] 30 | g = 1 + 9 * mean(x[2:n]) 31 | f[2] = g * (1 - sqrt(f[1]/g) - f[1]/g * sin(10 * pi * f[1])) 32 | return(f) 33 | } 34 | 35 | zdt4 = function(x){ 36 | f = numeric(2) 37 | n = length(x) 38 | ## transform x[2:n] to [-5, 5] 39 | x[2:n] = x[2:n] * 10 - 5 40 | f[1] = x[1] 41 | g = 1 + 10*(n-1) + sum(x[2:n]^2 - 10 * cos(4 * pi * x[2:n])) 42 | f[2] = g * (1 - sqrt(f[1] / g)) 43 | return(f) 44 | } 45 | 46 | zdt6 = function(x){ 47 | f = numeric(2) 48 | n = length(x) 49 | f[1] = 1 - exp(-4 * x[1]) * (sin (6 * pi * x[1]))^6 50 | g = 1 + 9 * mean(x[2:n])^0.25 51 | f[2] = g * (1 - (f[1]/g)^2) 52 | return(f) 53 | } 54 | 55 | ## Functions, which return l equally distributed points from the 56 | ## paretofront of the corresponding uf-function with in-dimension n 57 | 58 | zdt1pf = function(n, l){ 59 | X = matrix(0, ncol = l, nrow = 2) 60 | X[1,] = seq(0, 1, length.out = l) 61 | X[2,] = 1 - sqrt(X[1,]) 62 | X 63 | } 64 | 65 | zdt2pf = function(n, l){ 66 | X = matrix(0, ncol = l, nrow = 2) 67 | X[1,] = seq(0, 1, length.out = l) 68 | X[2,] = 1 - X[1,]^2 69 | X 70 | } 71 | 72 | zdt3pf = function(n, l){ 73 | X = matrix(0, ncol = 10*l, nrow = 2) 74 | X[1,] = seq(0, 1, length.out = 10*l) 75 | X[2,] = 1 - sqrt(X[1,]) - X[1,] * sin(10 * pi * X[1,]) 76 | X = nondominated_points(X) 77 | X[,sort(sample(ncol(X), l, replace = TRUE))] 78 | } 79 | 80 | zdt4pf = zdt1pf 81 | 82 | zdt6pf = zdt2pf -------------------------------------------------------------------------------- /man/mbo_OptPath.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/doc_mbo_OptPath.R 3 | \name{mbo_OptPath} 4 | \alias{mbo_OptPath} 5 | \title{OptPath in mlrMBO} 6 | \description{ 7 | In mlrMBO the \code{\link[ParamHelpers]{OptPath}} contains extra information next to the information documented in \code{\link[ParamHelpers]{OptPath}}. 8 | 9 | The extras are: 10 | \describe{ 11 | \item{train.time}{Time to train the model(s) that produced the points. Only the first slot of the vector is used (if we have multiple points), rest are NA.} 12 | \item{propose.time}{Time needed to propose the point. If we have individual timings from the proposal mechanism, we have one different value per point here. If all were generated in one go, we only have one timing, we store it in the slot for the first point, rest are NA.} 13 | \item{errors.model}{Possible Error Messages. If point-producing model(s) crashed they are replicated for all n points, if only one error message was passed we store it for the first point, rest are NA.} 14 | \item{prop.type}{Type of point proposal. Possible values are 15 | \describe{ 16 | \item{initdesign}{Points actually not proposed, but in the initial design.} 17 | \item{infill\_x}{Here x is a placeholder for the selected infill criterion, e.g., infill\_ei for expected improvement.} 18 | \item{random\_interleave}{Uniformly sampled points added additionally to the proposed points.} 19 | \item{random\_filtered}{If filtering of proposed points located too close to each other is active, these are replaced by random points.} 20 | \item{final\_eval}{If \code{final.evals} is set in \code{\link{makeMBOControl}}: Final evaluations of the proposed solution to reduce noise in y.} 21 | } 22 | } 23 | \item{parego.weight}{Weight vector sampled for multi-point ParEGO} 24 | \item{...}{Depending on the chosen infill criterion there will be additional columns, e.g. \code{se} and \code{mean} for the Expected Improvement)} 25 | } 26 | 27 | Moreover, the user may pass additional \dQuote{user extras} by appending a named list 28 | of scalar values to the return value of the objective function. 29 | } 30 | -------------------------------------------------------------------------------- /man-roxygen/arg_plot_MBO.R: -------------------------------------------------------------------------------- 1 | #' @param crit.plot [\code{logical(1)}]\cr 2 | #' Should the infill criterion versus the iteration be plotted? For multi objective 3 | #' and method = MSPOT multiple infill criterion values per iteration do exist, 4 | #' so a multi-dimensional plot is used. Default is TRUE 5 | #' @param hv.plot [\code{logical(1)}]\cr 6 | #' Should the hypervolume development be plotted? Default is FALSE for single 7 | #' objective and TRUE for multi objective. 8 | #' @param extra.measures [\code{character}]\cr 9 | #' Additional measures to be plotted versus the iteration. Must be the name 10 | #' of a column available in the optimization path. Notice that at most 4 plots 11 | #' can be rendered at the same time. Since X and Y Space plots will always be 12 | #' rendered, at most 2 addition plots are possible. You must deactivate crit.plot 13 | #' or hv.plot, if you want to add additional plots via extra.measure. 14 | #' @param ref.point [\code{numeric}]\cr 15 | #' Reference point used for the hypervolume development plot. Length of vector 16 | #' must equal the dimensionality of the optimization problem. Default is 17 | #' \code{NULL}, in this case the maximum / minimum values in the optimization 18 | #' path, that are alive in iteration iter, are used and increased / decreased 19 | #' by 0.1 (depending on minimization / maximization of the objective). 20 | #' @param lim.x [\code{list}], @param lim.y [\code{list}]\cr 21 | #' Axis limits for the plots. Must be a named list, so you can specify the 22 | #' axis limits for every plot. Every element of the list must be a numeric 23 | #' vector of length 2. Available names for elements are: 24 | #' XSpace - limits for the X-Space plot 25 | #' YSpaCe - limits for the Y-Space plot 26 | #' CritPlot - limits for the plot infill criterion plot 27 | #' HVPlot - limits for the Hypervolume Plit 28 | #' Extra1Plot - Limits for the Plot given by the first element of extra.measure 29 | #' Extra2Plot - Limits for the Plot given by the second element of extra.measure 30 | #' Default is an empty list - in this case limits are automatically set. 31 | -------------------------------------------------------------------------------- /todo-files/bench/mco/plot_eafs.R: -------------------------------------------------------------------------------- 1 | setwd("/home/bischl/cos/mlrMBO/") 2 | 3 | library(BatchExperiments) 4 | library(checkmate) 5 | load_all("/home/bischl/cos/ParamHelpers/todo-files/eaf/") 6 | 7 | source("/home/bischl/cos/ParamHelpers/todo-files/plotEAF.R") 8 | 9 | # reg = loadRegistry("/home/bischl/cos/mco_bench-files/", work.dir = "~/cos/mlrMBO/") 10 | 11 | job.info = getJobInfo(reg, pars = TRUE) 12 | job.info$algo2 = paste(job.info$algo, job.info$budget, job.info$prop.points, 13 | job.info$indicator, job.info$crit, sep = "-") 14 | job.info$algo2 = str_replace_all(job.info$algo2, "-NA", "") 15 | 16 | pids = c("GOMOP3_3D2M", "GOMOP_2D2M", "GOMOP_5D2M", "dtlz2_5D2M", "zdt1_5D2M", "zdt2_5D2M", "zdt3_5D2M") 17 | aids = c("nsga2-normal", "dib-1-eps", "dib-1-sms", "parego-1-ei", "parego-4-cb", "mspot-1-mean", "mspot-4-cb") 18 | 19 | getOptPaths = function(job.info, pids, aids) { 20 | ops = list() 21 | for (pid in pids) { 22 | for (aid in aids) { 23 | jids = job.info[job.info$prob == pid & job.info$algo2 == aid, "id"] 24 | assertInteger(jids, len = 20L, any.missing = FALSE) 25 | ops[[pid]][[aid]] = extractSubList(loadResults(reg, jids), "opt.path", simplify = FALSE) 26 | } 27 | names(ops[[pid]]) = paste(seq_along(aids), aids) 28 | } 29 | return(ops) 30 | } 31 | 32 | 33 | plotMMBOEAF = function(prob.id, opt.paths, file = NULL, logscale = FALSE, title = prob.id, xlim = NULL, ylim = NULL, ...) { 34 | # col = rainbow(length(aids)) 35 | if (!logscale) { 36 | log = ""; legend.pos = "topright" 37 | } else { 38 | log = "xy"; legend.pos = "bottomleft" 39 | } 40 | if (!is.null(file)) pdf(file) 41 | d = plotEAF(opt.paths[[prob.id]], legend.pos = "topright", log = log, main = title, 42 | xlab = "y1", ylab = "y2", ...) 43 | if (!is.null(file)) dev.off() 44 | return(d) 45 | } 46 | 47 | # opt.paths = getOptPaths(job.info, pids, aids) 48 | 49 | lty = c("dashed", "solid", "solid", "solid", "dashed", "solid", "dashed", "solid") 50 | col = c("red", "pink", "green", "yellow", "grey", "blue", "black", "orange") 51 | 52 | for (pid in pids) { 53 | plotMMBOEAF(pid, opt.paths, file = sprintf("eaf_%s.pdf", pid)) 54 | } 55 | -------------------------------------------------------------------------------- /R/proposePoints.R: -------------------------------------------------------------------------------- 1 | #' @title Propose candidates for the objective function 2 | #' @description Propose points for the objective function that should be evaluated according to the infill criterion and the recent evaluations. 3 | #' 4 | #' @param opt.state [\code{\link{OptState}}] 5 | #' @export 6 | # Propose infill points - simple dispatcher to real methods 7 | # 8 | # input: 9 | # opt.state 10 | # 11 | # output: 12 | # prop.points [data.frame] : the proposed points, 1 per row, with n rows 13 | # crit.vals [matrix(n, k)] : crit vals for proposed points. rows = points 14 | # for some methods, we have a cv for each objective. 15 | # in this case k > 1 and typically k = n.objectives 16 | # errors.models [character] : model errors, resulting in randomly proposed points. 17 | # length is one string PER PROPOSED POINT, not per element of 18 | # NA if the model was Ok, or the (first) error message if some model crashed 19 | proposePoints = function(opt.state) { 20 | opt.problem = getOptStateOptProblem(opt.state) 21 | control = getOptProblemControl(opt.problem) 22 | 23 | if (control$n.objectives == 1L) { 24 | if (is.null(control$multipoint.method)) { 25 | res = proposePointsByInfillOptimization(opt.state) 26 | } else { 27 | res = switch(control$multipoint.method, 28 | "cb" = proposePointsParallelCB(opt.state), 29 | "cl" = proposePointsConstantLiar(opt.state), 30 | "moimbo" = proposePointsMOIMBO(opt.state) 31 | ) 32 | } 33 | } else { 34 | res = switch(control$multiobj.method, 35 | "parego" = proposePointsParEGO(opt.state), 36 | "mspot" = proposePointsMSPOT(opt.state), 37 | "dib" = proposePointsDIB(opt.state) 38 | ) 39 | } 40 | 41 | if (control$interleave.random.points > 0L) 42 | res = joinProposedPoints(list(res, proposePointsRandom(opt.state))) 43 | 44 | if (!is.matrix(res$crit.vals)) 45 | res$crit.vals = matrix(res$crit.vals, ncol = 1L) 46 | 47 | if (control$filter.proposed.points) 48 | res = filterProposedPoints(res, opt.state) 49 | 50 | res 51 | } 52 | -------------------------------------------------------------------------------- /tests/testthat/test_termination_criteria.R: -------------------------------------------------------------------------------- 1 | context("termination criteria") 2 | 3 | test_that("termination criteria works", { 4 | 5 | f = testf.fsphere.1d 6 | f.slow = testf.fsphere.1d.slow 7 | f.max = convertToMaximization(f) 8 | design = testd.fsphere.1d 9 | design.max = design 10 | design$y = apply(design, 1, f) 11 | design.max$y = apply(design.max, 1, f.max) 12 | learner = NULL 13 | 14 | term.sets = list( 15 | list(arg = list(iters = 3L), state = "term.iter"), 16 | list(arg = list(time.budget = 3L), state = "term.time"), 17 | list(arg = list(exec.time.budget = 2), state = "term.exectime"), 18 | list(arg = list(target.fun.value = min(design$y) / 3, iters = 30L), state = "term.yval"), 19 | list(arg = list(max.evals = nrow(design) + 2L), state = "term.feval") 20 | ) 21 | 22 | for (term.set in term.sets) { 23 | ctrl = makeMBOControl() 24 | ctrl = do.call(setMBOControlTermination, c(list(control = ctrl), term.set$arg)) 25 | 26 | if (term.set$state == "term.exectime") { 27 | this.f = f.slow 28 | } else { 29 | this.f = f 30 | } 31 | 32 | or = mbo(this.f, design = design, control = ctrl, learner = learner) 33 | expect_equal(or$final.state, term.set$state, info = term.set$state) 34 | 35 | if (term.set$state == "term.iter") { 36 | expect_equal(getOptPathLength(or$opt.path), term.set$arg$iters + nrow(design), info = term.set$state) 37 | } 38 | if (term.set$state == "term.yval") { 39 | expect_true(or$y < term.set$arg$target.fun.value, info = term.set$state) 40 | term.set$arg$target.fun.value = term.set$arg$target.fun.value * (-1) 41 | ctrl = do.call(setMBOControlTermination, c(list(control = ctrl), term.set$arg)) 42 | or.max = mbo(f.max, design = design.max, control = ctrl, learner = learner) 43 | expect_true(getOptPathLength(or$opt.path) > nrow(design.max), info = term.set$state) 44 | expect_true(abs(getOptPathLength(or$opt.path)-getOptPathLength(or.max$opt.path)) < 3, info = term.set$state) 45 | } 46 | if (term.set$state == "term.feval") { 47 | expect_equal(getOptPathLength(or$opt.path), term.set$arg$max.evals, info = term.set$state) 48 | } 49 | } 50 | }) 51 | -------------------------------------------------------------------------------- /man/initSMBO.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/SMBO.R 3 | \name{initSMBO} 4 | \alias{initSMBO} 5 | \title{Initialize a manual sequential MBO run.} 6 | \usage{ 7 | initSMBO( 8 | par.set, 9 | design, 10 | learner = NULL, 11 | control, 12 | minimize = rep(TRUE, control$n.objectives), 13 | noisy = FALSE, 14 | show.info = getOption("mlrMBO.show.info", TRUE) 15 | ) 16 | } 17 | \arguments{ 18 | \item{par.set}{[\code{\link[ParamHelpers]{ParamSet}}]\cr 19 | See \code{\link{mbo}}.} 20 | 21 | \item{design}{[\code{data.frame}]\cr 22 | Initial design as data frame. 23 | If the y-values are not already present in design, mbo will evaluate the points. 24 | If the parameters have corresponding trafo functions, the design must not be transformed before it is passed! 25 | Functions to generate designs are available in \code{ParamHelpers}: \code{\link[ParamHelpers]{generateDesign}}, \code{\link[ParamHelpers]{generateGridDesign}}, \code{\link[ParamHelpers]{generateRandomDesign}}. 26 | Default is \code{NULL}, which means \code{\link[ParamHelpers]{generateDesign}} is called and a design of size 4 times number of all parameters is created 27 | The points are drawn via \code{\link[lhs]{maximinLHS}} to maximize the minimal distance between design points.} 28 | 29 | \item{learner}{[\code{\link[mlr]{Learner}}]\cr 30 | Regression learner from mlr, which is used as a surrogate to model our fitness function. 31 | If \code{NULL} (default), the default learner is determined as described here: \link{mbo_default_learner}.} 32 | 33 | \item{control}{[\code{\link{MBOControl}}]\cr 34 | Control object for mbo.} 35 | 36 | \item{minimize}{[\code{logical}]\cr 37 | Should objective values of the target functions be minimized? One value par objective. 38 | Default is \code{TRUE} for every objective.} 39 | 40 | \item{noisy}{[\code{logical(1)}]\cr 41 | Is the target function noisy? 42 | Default is \code{FALSE}.} 43 | 44 | \item{show.info}{[\code{logical(1)}]\cr 45 | Verbose output on console? 46 | Default is \code{TRUE}.} 47 | } 48 | \value{ 49 | [\code{\link{OptState}}] 50 | } 51 | \description{ 52 | When you want to run a human-in-the-loop MBO run you need to initialize it first. 53 | } 54 | -------------------------------------------------------------------------------- /tests/testthat/test_exampleRunMultiobj.R: -------------------------------------------------------------------------------- 1 | context("exampleRunMultiObj") 2 | 3 | test_that("exampleRunMultiObj", { 4 | 5 | doRun = function(method, crit, prop.points, indicator = "sms", trafo.y.fun = NULL) { 6 | # set nugget effect to small value for num stability in this unit test 7 | learner = makeLearner("regr.km", predict.type = "se", covtype = "matern3_2", nugget = 0.001) 8 | control = makeMBOControl(n.objectives = 2L, 9 | propose.points = prop.points, trafo.y.fun = trafo.y.fun) 10 | control = setMBOControlTermination(control, iters = 1L) 11 | if (method == "mspot") 12 | control = setMBOControlInfill(control, crit = crit, opt = "nsga2", opt.nsga2.popsize = 4L, 13 | opt.nsga2.generations = 1L) 14 | else 15 | control = setMBOControlInfill(control, crit = crit, opt = "focussearch", opt.restarts = 1L, 16 | opt.focussearch.points = 10L, opt.focussearch.maxit = 1L) 17 | control = setMBOControlMultiObj(control, method = method, dib.indicator = indicator) 18 | 19 | run = exampleRunMultiObj(testf.zdt1.2d, testd.zdt1.2d, learner = learner, control = control, points.per.dim = 4L, 20 | nsga2.args = list(popsize = 4L, generations = 2L)) 21 | expect_is(run, "MBOExampleRunMultiObj") 22 | res = renderExampleRunPlot(run, iter = 1L) 23 | expect_list(res, min.len = 1L, types = c("ggplot", "list")) 24 | } 25 | 26 | doRun(method = "parego", crit = crit.ei, prop.points = 1L) 27 | doRun(method = "parego", crit = crit.ei, prop.points = 2L) 28 | doRun(method = "parego", crit = crit.ei, prop.points = 2L, trafo.y.fun = trafoLog()) 29 | doRun(method = "parego", crit = crit.cb1, prop.points = 1L) 30 | doRun(method = "parego", crit = crit.cb1, prop.points = 2L) 31 | doRun(method = "mspot", crit = crit.ei, prop.points = 1L) 32 | doRun(method = "mspot", crit = crit.ei, prop.points = 4L) 33 | doRun(method = "mspot", crit = crit.cb1, prop.points = 1L) 34 | doRun(method = "mspot", crit = crit.cb1, prop.points = 2L) 35 | doRun(method = "dib", crit = crit.dib1, prop.points = 1L) 36 | doRun(method = "dib", crit = crit.dib1, prop.points = 2L) 37 | doRun(method = "dib", crit = crit.dib1, prop.points = 1L, indicator = "eps") 38 | doRun(method = "dib", crit = crit.dib1, prop.points = 2L, indicator = "eps") 39 | }) 40 | -------------------------------------------------------------------------------- /R/utils.R: -------------------------------------------------------------------------------- 1 | # show info without if-statement 2 | showInfo = function(show.info, ...) { 3 | if (show.info) 4 | messagef(...) 5 | } 6 | 7 | 8 | # load required extra packages 9 | loadPackages = function(control) { 10 | if (control$infill.opt == "cmaes") 11 | requirePackages("cmaesr", why = "proposePoints") 12 | if (control$n.objectives == 1L && control$propose.points > 1L && control$multipoint.method == "moimbo") 13 | requirePackages("emoa", why = "proposePoints") 14 | } 15 | 16 | 17 | # for Parego: calculate all integer vectors of length k with sum n 18 | combWithSum = function(n, k) { 19 | fun = function(n, k) { 20 | if (k == 1L) 21 | list(n) 22 | else 23 | unlist(lapply(0:n, function(i) Map(c, i, fun(n - i, k - 1L))), 24 | recursive = FALSE) 25 | } 26 | matrix(unlist(fun(n, k)), ncol = k, byrow = TRUE) 27 | } 28 | 29 | getFileBackupName = function(fn) { 30 | file.path(dirname(fn), sprintf(".~%s", basename(fn))) 31 | } 32 | 33 | getRandomSeed = function() { 34 | if (!exists(".Random.seed", .GlobalEnv)) 35 | set.seed(NULL) 36 | get(".Random.seed", .GlobalEnv) 37 | } 38 | 39 | measureTime = function(expr, ee = parent.frame()) { 40 | before = proc.time()[3L] 41 | force(expr) 42 | as.numeric(proc.time()[3L] - before) 43 | } 44 | 45 | # checks if a parameter or par.set is only numeric and has no dependencies/requires 46 | # FIXME: remove as soon as this is in ParamHelper 47 | isSimpleNumeric = function(par) { 48 | isNumeric(par, include.int = TRUE) && !hasRequires(par) 49 | } 50 | 51 | getHyperParsString2 = function(learner, show.missing.values = TRUE) { 52 | hps = getHyperPars(learner) 53 | ns = names(hps) 54 | pars = getParamSet(learner)$pars[ns] 55 | s = mapply(paramValueToString, pars, hps, MoreArgs = list(show.missing.values = show.missing.values)) 56 | paste(ns, s, sep = "=", collapse = ",") 57 | } 58 | 59 | 60 | getColorPalette = function() { 61 | # RColorBrewer::brewer.pal(11, "Spectral") 62 | c("#9E0142", "#D53D4F", "#F46D43", "#FDAE61", "#FEE08B", "#FFFFBF", "#E6F598", "#ABDDA4", "#66C2A5", "#3288BD", "#5E4FA2") 63 | } 64 | 65 | getLeafLearner = function(learner) { 66 | if (inherits(learner, "BaseWrapper")) { 67 | return(getLeafLearner(learner$next.learner)) 68 | } 69 | return(learner) 70 | } 71 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(initCrit,InfillCritAdaCB) 4 | S3method(initCrit,InfillCritCB) 5 | S3method(initCrit,default) 6 | S3method(plot,MBOMultiObjResult) 7 | S3method(plot,MBOSingleObjResult) 8 | S3method(plot,OptState) 9 | S3method(print,MBOControl) 10 | S3method(print,MBOExampleRun) 11 | S3method(print,MBOExampleRunMultiObj) 12 | S3method(print,MBOInfillCrit) 13 | S3method(print,MBOMultiObjResult) 14 | S3method(print,MBOResult) 15 | S3method(print,OptState) 16 | S3method(renderExampleRunPlot,MBOExampleRun) 17 | S3method(renderExampleRunPlot,MBOExampleRunMultiObj) 18 | export(crit.aei) 19 | export(crit.cb) 20 | export(crit.cb1) 21 | export(crit.cb2) 22 | export(crit.dib1) 23 | export(crit.ei) 24 | export(crit.eqi) 25 | export(crit.mr) 26 | export(crit.se) 27 | export(exampleRun) 28 | export(exampleRunMultiObj) 29 | export(finalizeSMBO) 30 | export(getGlobalOpt) 31 | export(getMBOInfillCritComponents) 32 | export(getMBOInfillCritId) 33 | export(getMBOInfillCritName) 34 | export(getMBOInfillCritParam) 35 | export(getMBOInfillCritParams) 36 | export(getSupportedInfillOptFunctions) 37 | export(getSupportedMultipointInfillOptFunctions) 38 | export(hasRequiresInfillCritStandardError) 39 | export(initCrit) 40 | export(initSMBO) 41 | export(makeMBOControl) 42 | export(makeMBOInfillCrit) 43 | export(makeMBOInfillCritAEI) 44 | export(makeMBOInfillCritAdaCB) 45 | export(makeMBOInfillCritCB) 46 | export(makeMBOInfillCritDIB) 47 | export(makeMBOInfillCritEI) 48 | export(makeMBOInfillCritEQI) 49 | export(makeMBOInfillCritMeanResponse) 50 | export(makeMBOInfillCritStandardError) 51 | export(makeMBOLearner) 52 | export(makeMBOTrafoFunction) 53 | export(mbo) 54 | export(mboContinue) 55 | export(mboFinalize) 56 | export(plotExampleRun) 57 | export(proposePoints) 58 | export(renderExampleRunPlot) 59 | export(setMBOControlInfill) 60 | export(setMBOControlMultiObj) 61 | export(setMBOControlMultiPoint) 62 | export(setMBOControlTermination) 63 | export(trafoLog) 64 | export(trafoSqrt) 65 | export(updateSMBO) 66 | import(BBmisc) 67 | import(ParamHelpers) 68 | import(checkmate) 69 | import(data.table) 70 | import(grDevices) 71 | import(mlr) 72 | import(parallelMap) 73 | import(smoof) 74 | import(stats) 75 | import(utils) 76 | importFrom(lhs,randomLHS) 77 | useDynLib(mlrMBO,c_eps_indicator) 78 | useDynLib(mlrMBO,c_sms_indicator) 79 | -------------------------------------------------------------------------------- /todo-files/bench/mco/bench_normalization.R: -------------------------------------------------------------------------------- 1 | library(BatchExperiments) 2 | library(mco) 3 | library(emoa) 4 | library(devtools) 5 | 6 | source("DTLZ.R") 7 | source("LZ09.R") 8 | source("UF.R") 9 | source("WFG.R") 10 | source("ZDT.R") 11 | 12 | source("defs.R") 13 | 14 | unlink("mco_bench-files", recursive = TRUE) 15 | reg = makeExperimentRegistry("mco_bench", packages = c( 16 | "mco", 17 | "emoa", 18 | "mlrMBO" 19 | ), src.files = c( 20 | "defs.R", 21 | "WFG.R" 22 | ) 23 | ) 24 | 25 | addMyProblem = function(id, objective, lower, upper) { 26 | addProblem(reg, id = id,, static = list( 27 | objective = objective, 28 | par.set = makeNumericParamSet("x", len = 5L, lower = lower, upper = upper), 29 | ny = 2L, 30 | ref = c(11, 11) 31 | )) 32 | } 33 | 34 | # test functions 35 | for (i in 1:9) { 36 | fname = sprintf("wfg%i", i) 37 | addMyProblem(fname, get(fname), lower = 0, upper = 1) 38 | } 39 | 40 | addAlgorithm(reg, "parego", fun = function(static, normalization) { 41 | par.set = static$par.set 42 | names.x = getParamIds(par.set, repeated = TRUE, with.nr = TRUE) 43 | 44 | learner = makeLearner("regr.km", predict.type = "se") 45 | iters = (FEVALS - INIT_DESIGN_POINTS) 46 | 47 | ctrl = makeMBOControl(n.objectives = static$ny, init.design.points = INIT_DESIGN_POINTS, 48 | iters = iters, propose.points = 1L, 49 | save.on.disk.at = integer(0L)) 50 | ctrl = setMBOControlInfill(ctrl, crit = "ei", opt.focussearch.points = 10000L, 51 | opt.restarts = 3L, opt.focussearch.maxit = 3L) 52 | ctrl = setMBOControlMultiObj(ctrl, parego.normalize = normalization) 53 | 54 | res = mbo(makeMBOFunction(static$objective), static$par.set, 55 | learner = learner, control = ctrl, show.info = TRUE) 56 | hv = dominated_hypervolume(t(res$pareto.front), ref = static$ref) 57 | cd = crowding_distance(t(res$pareto.front)) 58 | r2 = unary_r2_indicator(t(res$pareto.front), weights = WEIGHTS) 59 | list(paretoset = res$pareto.set, pareto.front = res$pareto.front, hv = hv, cd = cd, r2 = r2) 60 | }) 61 | 62 | des = makeDesign("parego", exhaustive = list( 63 | normalization = c("standard", "front") 64 | )) 65 | 66 | addExperiments(reg, algo.design = des, repls = 10L) 67 | 68 | submitJobs(reg, ids = getJobIds(reg), 69 | resources=list(walltime=1*3600, memory=2*1024), 70 | wait=function(retries) 1, max.retries=10) 71 | showStatus(reg) 72 | -------------------------------------------------------------------------------- /R/initCrit.R: -------------------------------------------------------------------------------- 1 | #' @title Initialize an MBO infill criterion. 2 | #' 3 | #' @description 4 | #' Some infill criteria have parameters that are dependent on values in the parameter set, design, 5 | #' used learner or other control settings. 6 | #' To actually set these default values, this function is called, which returns a fully 7 | #' initialized [\code{\link{MBOInfillCrit}}]. 8 | #' This function is mainly for internal use. If a custom infill criterion is created, it may be 9 | #' required to create a separate method \code{initCrit.InfillCritID} where \code{ID} is the 10 | #' \code{id} of the custom \link{MBOInfillCrit}. 11 | #' 12 | #' @param crit [\code{\link{MBOInfillCrit}}]\cr 13 | #' Uninitialized infill criterion. 14 | #' @param fun [\code{smoof_function}]\cr 15 | #' Fitness function to optimize. 16 | #' @param design 17 | #' Sampling plan. 18 | #' @param learner [\code{\link[mlr]{Learner}}]\cr 19 | #' Regression learner from mlr, which is used as a surrogate to model our fitness function. 20 | #' @param control [\code{\link{MBOControl}}]\cr 21 | #' MBO control object. 22 | #' @return [\code{\link{MBOInfillCrit}}] 23 | #' @export 24 | initCrit = function(crit, fun, design, learner, control) { 25 | UseMethod("initCrit") 26 | } 27 | 28 | #' @export 29 | initCrit.default = function(crit, fun, design, learner, control) { 30 | crit = initCritOptDirection(crit, fun) 31 | return(crit) 32 | } 33 | 34 | #' @export 35 | initCrit.InfillCritCB = function(crit, fun, design, learner, control) { 36 | cb.lambda = crit$params$cb.lambda 37 | if (is.null(cb.lambda)) 38 | cb.lambda = ifelse(isSimpleNumeric(getParamSet(fun)), 1, 2) 39 | crit = makeMBOInfillCritCB(cb.lambda) 40 | initCritOptDirection(crit, fun) 41 | } 42 | 43 | #' @export 44 | initCrit.InfillCritAdaCB = function(crit, fun, design, learner, control) { 45 | cb.lambda = crit$params$cb.lambda 46 | if (is.null(cb.lambda)) 47 | cb.lambda = ifelse(isSimpleNumeric(getParamSet(fun)), 1, 2) 48 | crit = makeMBOInfillCritAdaCB(2 * cb.lambda, 0.5 * cb.lambda) 49 | initCritOptDirection(crit, fun) 50 | } 51 | 52 | # sets the opt.direction to minimize or maximize depending on the object function. 53 | initCritOptDirection = function(crit, fun) { 54 | if (crit$opt.direction == "objective") { 55 | crit$opt.direction = ifelse(shouldBeMinimized(fun), "minimize", "maximize") 56 | } 57 | crit 58 | } 59 | -------------------------------------------------------------------------------- /R/plot_MBOResult.R: -------------------------------------------------------------------------------- 1 | #' @title MBO Result Plotting 2 | #' 3 | #' @description 4 | #' Plots any MBO result objects. Plots for X-Space, Y-Space and any column in 5 | #' the optimization path are available. This function uses 6 | #' \code{\link[ParamHelpers]{plotOptPath}} from package \code{ParamHelpers}. 7 | #' 8 | #' @name plotMBOResult 9 | #' @rdname plotMBOResult 10 | #' 11 | #' 12 | #' @param x [\code{MBOResult}]\cr 13 | #' \code{MBOSingleObjResult} or \code{MBOMultiObjResult} object. 14 | #' @param iters [\code{integer}]\cr 15 | #' Iterations to be plotted, 0 indicates the initial design. Default is all iterations. 16 | #' @param pause [\code{logical(1)}]\cr 17 | #' Should the process be paused after each iteration? 18 | #' Default is \code{interactive()}. 19 | #' @param ... 20 | #' Additional parameters for the \code{\link[ParamHelpers]{plotOptPath}} 21 | #' function in package \code{ParamHelpers}. 22 | #' 23 | NULL 24 | 25 | #' @rdname plotMBOResult 26 | #' @export 27 | plot.MBOSingleObjResult = function(x, iters = NULL, pause = interactive(), ...) { 28 | 29 | # extract and set params 30 | opt.path = x$opt.path 31 | control = x$control 32 | y.names = opt.path$y.names 33 | infill.crit.id = getMBOInfillCritId(control$infill.crit) 34 | 35 | if (is.null(iters)) 36 | iters = max(getOptPathDOB(opt.path)) 37 | 38 | args = list(...) 39 | args = insert(args, list(op = opt.path, iters = iters, pause = pause)) 40 | args = insert(list(y.over.time = list(y.names, infill.crit.id), title = "MBO Result"), args) 41 | 42 | do.call(plotOptPath, args) 43 | } 44 | 45 | #' @rdname plotMBOResult 46 | #' @export 47 | plot.MBOMultiObjResult = function(x, iters = NULL, pause = interactive(), ...) { 48 | 49 | # extract and set params 50 | opt.path = x$opt.path 51 | control = x$control 52 | y.names = opt.path$y.names 53 | infill.crit.id = getMBOInfillCritId(control$infill.crit) 54 | 55 | if (control$multiobj.method == "mspot") { 56 | infill.crit = paste(infill.crit.id, opt.path$y.names, sep = ".") 57 | } else { 58 | infill.crit = infill.crit.id 59 | } 60 | 61 | if (is.null(iters)) 62 | iters = max(getOptPathDOB(opt.path)) 63 | 64 | args = list(...) 65 | args = insert(args, list(op = opt.path, iters = iters, pause = pause)) 66 | args = insert(list(y.over.time = list(y.names, infill.crit), title = "MBO Result"), args) 67 | 68 | do.call(plotOptPath, args) 69 | } 70 | -------------------------------------------------------------------------------- /vignettes/supplementary/adaptive_infill_criteria.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Adaptive Infill Criteria" 3 | vignette: > 4 | %\VignetteIndexEntry{Infill criteria} 5 | %\VignetteEngine{knitr::rmarkdown} 6 | %\VignetteEncoding{UTF-8} 7 | --- 8 | 9 | ```{r setup, include=FALSE} 10 | library(mlrMBO) 11 | library(rgenoud) 12 | set.seed(1) 13 | knitr::opts_chunk$set(cache = TRUE, collapse = FALSE) 14 | knitr::knit_hooks$set(document = function(x){ 15 | gsub("```\n*```r*\n*", "", x) 16 | }) 17 | ``` 18 | 19 | ## Purpose 20 | 21 | This vignette gives a short example of the usage of the adaptive infill criteria in `mlrMBO`. 22 | 23 | ## Adaptive Infill Criteria 24 | 25 | An adaptive infill criterion can change its behaviour based on the progress of the optimization. 26 | The progress has to be supplied by the termination criterion. 27 | All integrated termination criteria support this feature. 28 | 29 | ## Exemplary Usage of the Adaptive CB 30 | 31 | To specify which infill criterion should be used, the `MBOControl` object has to be extended by calling `setMBOControlInfill()`. 32 | In addition to the criterion you can also set the parameters of the infill criterion optimization. 33 | The criterion itself is created with `makeMBOInfillCrit*()`. 34 | The most common infill criteria are predefined like `crit.ei` and `crit.cb2`. See `?MBOInfillCrit` for details. 35 | 36 | ```{r control} 37 | ctrl = makeMBOControl() 38 | ctrl = setMBOControlTermination(ctrl, iters = 10L) 39 | ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritAdaCB(cb.lambda.start = 4, cb.lambda.end = 0.1)) 40 | ``` 41 | 42 | This will lead to an CB infill criterion ($CB(x) = \hat{\mu}(x) \pm \lambda * \hat{s}(x)$) that will focus on uncertain regions in the beginning (`cb.lambda.start = 4`) and towards the end will have a stronger focus on areas close to the global minimum of the surrogate (`cb.lambda.end = 0.1`). 43 | 44 | ```{r fun} 45 | test.fun = makeSingleObjectiveFunction( 46 | fn = function(x) x[1]^2 * sin(3 * x[2]), 47 | par.set = makeNumericParamSet(lower = 0, upper = 1, len = 2L) 48 | ) 49 | ``` 50 | 51 | You can now start the optimization like usual: 52 | 53 | ```{r example, results = "hide", message = FALSE, warning = FALSE} 54 | res = mbo(test.fun, control = ctrl) 55 | ``` 56 | 57 | If we look at the OptPath we can see the different progress values and the resulting different values of lambda. 58 | 59 | ```{r res} 60 | tail(as.data.frame(res$opt.path)) 61 | ``` 62 | -------------------------------------------------------------------------------- /tests/testthat/test_multipoint_cb.R: -------------------------------------------------------------------------------- 1 | context("multipoint cb") 2 | 3 | test_that("multipoint cb", { 4 | ctrl = makeMBOControl(propose.points = 5L) 5 | ctrl = setMBOControlTermination(ctrl, iters = 2L) 6 | ctrl = setMBOControlInfill(ctrl, crit = crit.cb1, 7 | opt = "focussearch", opt.focussearch.points = 100L, opt.focussearch.maxit = 2L) 8 | ctrl = setMBOControlMultiPoint(ctrl, method = "cb") 9 | 10 | res = mbo(testf.fsphere.1d, testd.fsphere.1d, control = ctrl) 11 | expect_output(print(res), "Recommended parameters") 12 | op = as.data.frame(res$opt.path) 13 | expect_true(all(is.na(op$multipoint.cb.lambda[1:5]))) 14 | expect_numeric(op$multipoint.cb.lambda[6:15], any.missing = FALSE) 15 | expect_is(res, "MBOResult") 16 | expect_lt(res$y, 0.15) 17 | }) 18 | 19 | 20 | test_that("multipoint cb with random interleaved points", { 21 | ctrl = makeMBOControl(propose.points = 5L) 22 | ctrl = setMBOControlTermination(ctrl, iters = 1L) 23 | ctrl = setMBOControlInfill(ctrl, crit = crit.cb1, 24 | opt = "focussearch", opt.focussearch.points = 100L, 25 | opt.focussearch.maxit = 2L, interleave.random.points = 5L) 26 | ctrl = setMBOControlMultiPoint(ctrl, method = "cb") 27 | res = mbo(testf.fsphere.1d, testd.fsphere.1d, control = ctrl) 28 | expect_output(print(res), "Recommended parameters") 29 | op = as.data.frame(res$opt.path) 30 | op = tail(op, 10) 31 | 32 | # dob should be the same 33 | expect_true(all(op$dob == 1L)) 34 | 35 | # no error messages 36 | expect_true(all(is.na(op$error.message))) 37 | 38 | # cb should be NA for random points 39 | expect_true(all(!is.na(head(op$cb, 5)))) 40 | expect_true(all(is.na(tail(op$cb, 5)))) 41 | 42 | # propose.time is NA for random points 43 | expect_true(all(!is.na(head(op$propose.time, 5)))) 44 | expect_true(all(is.na(tail(op$propose.time, 5)))) 45 | 46 | ctrl = makeMBOControl(propose.points = 1L) 47 | ctrl = setMBOControlTermination(ctrl, iters = 1L) 48 | ctrl = setMBOControlInfill(ctrl, crit = crit.cb1, 49 | opt = "focussearch", opt.focussearch.points = 100L, 50 | opt.focussearch.maxit = 2L, interleave.random.points = 1L) 51 | ctrl = setMBOControlMultiPoint(ctrl, method = "cb") 52 | 53 | res = mbo(testf.fsphere.1d, testd.fsphere.1d, control = ctrl) 54 | op = as.data.frame(res$opt.path) 55 | op = tail(op, 2) 56 | expect_identical(is.na(op$cb), c(FALSE, TRUE)) 57 | expect_identical(is.na(op$train.time), c(FALSE, TRUE)) 58 | }) 59 | -------------------------------------------------------------------------------- /todo-files/outdated_rmds/quickstart.Rmd: -------------------------------------------------------------------------------- 1 | # Introductory example 2 | 3 | **Info:** this guide gives an overview of the typical optimization workflow with **mlrMBO** for those familiar with model based optimization. 4 | For a much more detailed introduction see [the next chapter](in_depth_introduction.md). 5 | 6 | In this example we aim to maximize a one dimensional mixed cosine function using model-based optimization. 7 | Instead of writing this function by hand, we make use of the smoof package, which offers a lot of common single objective optimization functions. 8 | 9 | ```{r} 10 | library(mlrMBO) 11 | obj.fun = makeCosineMixtureFunction(1) 12 | plot(obj.fun) 13 | ``` 14 | 15 | We decide to use kriging as our surrogate model and to do 10 sequential optimization steps. 16 | Furthermore we use Expected Improvement (EI) as the infill criterion. 17 | 18 | As a last step we have to generate an initial design on which we evaluate our model in the beginning. We use `ParamHelpers::generateDesign` to generate 10 points in a latin hypercube design. 19 | 20 | ```{r} 21 | learner = makeLearner("regr.km", predict.type = "se", covtype = "matern3_2", 22 | control = list(trace = FALSE)) 23 | control = makeMBOControl() 24 | control = setMBOControlTermination(control, iters = 10) 25 | control = setMBOControlInfill(control, crit = "ei") 26 | design = generateDesign(n = 10, par.set = getParamSet(obj.fun)) 27 | ``` 28 | 29 | Finally we start the optimization process and print the result object. 30 | 31 | ```{r} 32 | result = mbo(obj.fun, design = design, learner = learner, control = control, 33 | show.info = TRUE) 34 | print(result) 35 | ``` 36 | 37 | # Example run 38 | 39 | There is also the function `exampleRun`, which is useful to figure out how **MBO**works and to visualize the results. 40 | 41 | ```{r, results="hide"} 42 | ex = exampleRun(obj.fun, learner = learner, control = control, show.info = FALSE) 43 | ``` 44 | ```{r, fig.height=5, warning=FALSE} 45 | print(ex) 46 | plotExampleRun(ex, iters = c(1L, 3L, 10L)) 47 | ``` 48 | 49 | 50 | Or alternatively for a two dimensional function: 51 | 52 | ```{r, results="hide", warning=FALSE} 53 | obj.fun2 = makeCosineMixtureFunction(2L) 54 | plot(obj.fun2) 55 | ex2 = exampleRun(obj.fun2, learner = learner, control = control, show.info = FALSE) 56 | ``` 57 | ```{r, fig.height=5, warning=FALSE} 58 | print(ex2) 59 | plotExampleRun(ex2, iters = c(1L, 3L, 10L)) 60 | ``` 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /R/evalMBODesign.R: -------------------------------------------------------------------------------- 1 | # eval mbo design (if no y was passed by user) 2 | # the following is done: 3 | # 1) design is sanity checked a bit 4 | # - do column names match par.set names + y names? 5 | # - has the design been X-transformed? 6 | # 2) if y-values are there, just log all points to optpath 7 | # 3) if y-values are not there, eval points and log them to optpath 8 | 9 | evalMBODesign.OptState = function(opt.state) { 10 | opt.problem = getOptStateOptProblem(opt.state) 11 | design = getOptProblemDesign(opt.problem) 12 | control = getOptProblemControl(opt.problem) 13 | par.set = getOptProblemParSet(opt.problem) 14 | pids = getParamIds(par.set, repeated = TRUE, with.nr = TRUE) 15 | y.name = control$y.name 16 | 17 | # get dummy "extras object" for init design 18 | extras = getExtras(n = nrow(design), prop = NULL, train.time = NA_real_, control = control) 19 | 20 | # check that the provided design one seems ok 21 | # sanity check: are paramter values and colnames of design consistent? 22 | if (!setequal(setdiff(colnames(design), y.name), pids)) 23 | stop("Column names of design 'design' must match names of parameters in 'par.set'!") 24 | 25 | # sanity check: do not allow transformed designs 26 | # if no trafo attribute provided we act on the assumption that the design is not transformed 27 | if (!hasAttributes(design, "trafo")) { 28 | design = setAttribute(design, "trafo", FALSE) 29 | } else { 30 | if (attr(design, "trafo")) { 31 | stop("Design must not be transformed!") 32 | } 33 | } 34 | 35 | design.x = dropNamed(design, y.name) 36 | # reorder + create list of x-points 37 | design.x = design.x[, pids, drop = FALSE] 38 | xs = dfRowsToList(design.x, par.set) 39 | 40 | # either only log init design stuff to opt.path or eval y-values 41 | if (all(y.name %in% colnames(design))) { 42 | y = as.matrix(design[, y.name, drop = FALSE]) 43 | lapply(seq_along(xs), function(i) 44 | addOptPathEl(getOptStateOptPath(opt.state), x = xs[[i]], y = y[i, ], dob = 0L, 45 | error.message = NA_character_, exec.time = NA_real_, extra = extras[[i]]) 46 | ) 47 | } else if (all(y.name %nin% colnames(design))) { 48 | showInfo(getOptProblemShowInfo(opt.problem), "Computing y column(s) for design. Not provided.") 49 | evalTargetFun.OptState(opt.state, xs, extras) 50 | } else { 51 | stop("Only part of y-values are provided. Don't know what to do - provide either all or none.") 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /tests/testthat/test_different_learners.R: -------------------------------------------------------------------------------- 1 | context("different learner") 2 | 3 | test_that("mbo works with different learners", { 4 | 5 | # Test some possible learner on a simple problem with discrete and 6 | # numeric parameters 7 | ps1 = testp.mixed 8 | f1 = testf.mixed 9 | 10 | ps2 = testp.fsphere.2d 11 | f2 = testf.fsphere.2d 12 | 13 | ctrl = makeMBOControl() 14 | ctrl = setMBOControlTermination(ctrl, iters = 1L) 15 | 16 | ctrl = setMBOControlInfill(ctrl, opt.focussearch.points = 100L, 17 | opt.restarts = 1L, opt.focussearch.maxit = 1L) 18 | 19 | testit = function(fun, lrn, se, bagging = TRUE) { 20 | if (is.character(lrn)) 21 | lrn = makeLearner(lrn) 22 | if (se) { 23 | if (bagging) 24 | lrn = makeBaggingWrapper(lrn, bw.iters = 5L) 25 | lrn = setPredictType(lrn, "se") 26 | } 27 | else { 28 | ctrl$infill.crit = crit.mr 29 | } 30 | des = generateTestDesign(10L, getParamSet(fun)) 31 | res = mbo(fun, des, learner = lrn, control = ctrl) 32 | expect_class(res, "MBOResult") 33 | } 34 | 35 | # and now a huge number of runs 36 | testit(f1, "regr.lm", TRUE) 37 | testit(f1, "regr.lm", FALSE) 38 | testit(f2, "regr.lm", TRUE) 39 | testit(f2, "regr.lm", FALSE) 40 | testit(f2, "regr.km", TRUE, FALSE) 41 | testit(f2, "regr.km", FALSE) 42 | testit(f1, "regr.lm", TRUE, FALSE) 43 | testit(f1, "regr.lm", FALSE) 44 | testit(f2, "regr.lm", TRUE, FALSE) 45 | testit(f2, "regr.lm", FALSE) 46 | testit(f1, "regr.rpart", TRUE) 47 | testit(f1, "regr.rpart", FALSE) 48 | testit(f2, "regr.rpart", TRUE) 49 | testit(f2, "regr.rpart", FALSE) 50 | testit(f1, "regr.mob", TRUE) 51 | testit(f1, "regr.mob", FALSE) 52 | testit(f2, "regr.mob", TRUE) 53 | testit(f2, "regr.mob", FALSE) 54 | testit(f1, "regr.kknn", TRUE) 55 | testit(f1, "regr.kknn", FALSE) 56 | testit(f2, "regr.kknn", TRUE) 57 | testit(f2, "regr.kknn", FALSE) 58 | testit(f1, "regr.ksvm", TRUE) 59 | testit(f1, "regr.ksvm", FALSE) 60 | testit(f2, "regr.ksvm", TRUE) 61 | testit(f2, "regr.ksvm", FALSE) 62 | testit(f1, "regr.earth", TRUE) 63 | testit(f1, "regr.earth", FALSE) 64 | testit(f2, "regr.earth", TRUE) 65 | testit(f2, "regr.earth", FALSE) 66 | testit(f1, "regr.nnet", TRUE) 67 | testit(f1, "regr.nnet", FALSE) 68 | testit(f2, "regr.nnet", TRUE) 69 | testit(f2, "regr.nnet", FALSE) 70 | testit(f2, makeMBOLearner(ctrl, f1), TRUE, FALSE) 71 | testit(f2, makeMBOLearner(ctrl, f2), TRUE, FALSE) 72 | }) 73 | -------------------------------------------------------------------------------- /R/proposePointsHelpers.R: -------------------------------------------------------------------------------- 1 | # some functions call proposePointByInfillOptimization multiple times 2 | # this helpers joins these results to a single result 3 | joinProposedPoints = function(props) { 4 | makeProposal( 5 | prop.points = do.call(rbind, extractSubList(props, "prop.points", simplify = FALSE)), 6 | crit.vals = do.call(rbind, extractSubList(props, "crit.vals", simplify = FALSE)), 7 | propose.time = do.call(c, extractSubList(props, "propose.time", simplify = FALSE)), 8 | prop.type = do.call(c, extractSubList(props, "prop.type", simplify = FALSE)), 9 | errors.model = do.call(c, extractSubList(props, "errors.model", simplify = FALSE)), 10 | crit.components = do.call(rbind, extractSubList(props, "crit.components", simplify = FALSE)) 11 | ) 12 | } 13 | 14 | # generate a few random points if ANY model failed 15 | checkFailedModels = function(models, par.set, npoints, control) { 16 | isfail = vlapply(models, isFailureModel) 17 | if (any(isfail)) { 18 | # if error in any model, return first msg 19 | prop.points = generateDesign(npoints, par.set, randomLHS) 20 | prop.points = convertDataFrameCols(prop.points, ints.as.num = TRUE, logicals.as.factor = TRUE) 21 | prop = makeProposal( 22 | control = control, 23 | prop.points = prop.points, 24 | prop.type = "random_error", 25 | errors.model = getFailureModelMsg(models[[which.first(isfail)]]) 26 | ) 27 | } else { 28 | prop = NULL 29 | } 30 | 31 | return(list(ok = all(!isfail), prop = prop)) 32 | } 33 | 34 | 35 | # create control objects with random lamda values for parallel cb multi-point 36 | # @arg crit: MBOInfillCrit 37 | # @arg crit.pars: list of length propose.points. 38 | # Each list item contains a list with the arguments the infill crit should be initialized 39 | createSinglePointControls = function(control, crit, crit.pars = NULL) { 40 | if (is.null(crit.pars)) { 41 | crit.pars = replicate(control$propose.points, list()) 42 | } 43 | assertList(crit.pars, len = control$propose.points) 44 | lapply(crit.pars, function(crit.par) { 45 | ctrl = control; 46 | ctrl$propose.points = 1L 47 | ctrl$infill.crit = do.call(crit, crit.par) 48 | return(ctrl) 49 | }) 50 | } 51 | 52 | # perform a deep copy of an opt.path object 53 | # so we can store (temporary) stuff in it, without changing the real opt.path 54 | # needed in CL and DIB multi-point 55 | deepCopyOptPath = function(op) { 56 | op2 = op 57 | op2$env = new.env() 58 | op2$env$path = op$env$path 59 | op2$env$dob = op$env$dob 60 | op2$env$eol = op$env$eol 61 | return(op2) 62 | } 63 | -------------------------------------------------------------------------------- /todo-files/outdated_rmds/model_based_optimization.Rmd: -------------------------------------------------------------------------------- 1 | # Model Based Optimization 2 | 3 | The generic sequential model-based optimization (SMBO) procedure starts with an *initial design* of evaluation points. 4 | 5 | Subsequently, the following steps are performed iteratively until a *termination criterion* is met: 6 | 7 | 1. Fit a regression model to the design, based on the information gained from evaluating 8 | the black-box function at the points in the design 9 | 2. A new point is proposed by the surrogate model and the *infill criterion*. 10 | 3. Evaluate the black-box function at this point and add it to the design. 11 | 12 | ## Types of MBO supported in **mlrMBO** 13 | 14 | In addition to SMBO **mlrMBO** also supports *mulit-critera optimization* and *parallel optimization*. 15 | 16 | ## Surrogate Model 17 | 18 | The attribute `learner` of the `mbo()` function allows us to choose an appropriate surrogate model for the parameter optimization. 19 | Different learners can easily be created using the `makeLearner` function from the **mlr** package. 20 | A list of implemented learners can be seen using the `listlearners()` function or on the [mlr wiki](http://mlr-org.github.io/mlr-tutorial/release/html/integrated_learners/). 21 | 22 | The choice of the surrogate model depends on the parameter set of the objective function. 23 | While kriging models (gaussian processes) are advisable if all parameters are numeric, they cannot be used if the objective function contains categorical parameters. 24 | If at least one parameter is categorical, random forest models might be a good choice as surrogate models. 25 | The default kriging model is from the **DiceKriging** package and uses the `matern5_2`covariance kernel. 26 | 27 | ## Infill Criterion 28 | 29 | One of the most important questions is to define how the next design points in the sequential loop are chosen. 30 | With `setMBOControlInfill` a `MBOControl` object can be extended with infill criteria and infill optimizer options. 31 | 32 | ### Argument _crit_ 33 | 34 | 5 different possibilities can be set via the `crit` argument in `setMBOControlInfill`: 35 | 36 | * `mean`: mean response of the surrogate model 37 | * `ei`: expected improvement of the surrogate model 38 | * `aei`: augmented expected improvement, which is especially useful for noisy functions 39 | * `eqi`: expected quantile improvement 40 | * `cb`: confidence bound, which is the additive combination of mean response and mean standard error estimation of the surrogate model (response - lambda * standard.error) 41 | 42 | The parameters of the different criteria are set via further arguments (e.g. `crit.cb.lambds` for the lambda parameter if `crit = cb`) -------------------------------------------------------------------------------- /R/infillOptEA.R: -------------------------------------------------------------------------------- 1 | # Simple EA that uses operators from emoa functions 2 | # kind of mimics our multi-objective approach, so we can 3 | # compare more honestly. 4 | # See infillOptCMAES.R for interface explanation. 5 | infillOptEA = function(infill.crit, models, control, par.set, opt.path, designs, iter, ...) { 6 | requirePackages("emoa", why = "infillOptEA") 7 | 8 | # get constants and init shit 9 | repids = getParamIds(par.set, repeated = TRUE, with.nr = TRUE) 10 | 11 | # (mu + lambda) strategy 12 | mu = control$infill.opt.ea.mu 13 | lambda = control$infill.opt.ea.lambda 14 | mutate = emoa::pm_operator(control$infill.opt.ea.pm.eta, control$infill.opt.ea.pm.p, 15 | getLower(par.set), getUpper(par.set)) 16 | crossover = emoa::sbx_operator(control$infill.opt.ea.sbx.eta, control$infill.opt.ea.sbx.p, 17 | getLower(par.set), getUpper(par.set)) 18 | 19 | best.x = NULL 20 | best.y = Inf 21 | 22 | for (restart.iter in seq_len(control$infill.opt.restarts)) { 23 | 24 | # random inital population: 25 | X = generateDesign(mu, par.set, fun = randomLHS) 26 | y = infill.crit(X, models, control, par.set, designs, iter, ...) 27 | 28 | for (i in seq_len(control$infill.opt.ea.maxit)) { 29 | # Create new individual (mu + lambda) 30 | parentSet = replicate(lambda, sample(seq_len(mu), 2)) 31 | 32 | # get two kids from crossover, select 1 randomly and mutate 33 | for (i in seq_col(parentSet)) { 34 | parents = parentSet[, i] 35 | child = crossover(t(X[parents, , drop = FALSE])) 36 | child1 = child[, sample(c(1, 2), 1)] 37 | child1 = mutate(child1) 38 | if (hasInteger(par.set)) { 39 | # convert to list of parameter values (already rounded internally by dfRowToList) 40 | child1 = dfRowToList(as.data.frame(as.list(child1)), i = 1L, par.set = par.set) 41 | # possibly the values are out of bounds 42 | child1 = unlist(repairPoint(par.set, child1)) 43 | } 44 | # Add new individual: 45 | X[nrow(X) + 1, ] = child1 46 | child2 = setColNames(as.data.frame(as.list(child1)), repids) 47 | y[length(y) + 1] = infill.crit(child2, models, control, par.set, designs, iter, ...) 48 | } 49 | 50 | # get elements we want to remove from current population 51 | to.survive = head(order(y), mu) 52 | X = X[to.survive, , drop = FALSE] 53 | y = y[to.survive] 54 | } 55 | select = getMinIndex(y) 56 | y1 = y[select] 57 | x1 = X[select, , drop = FALSE] 58 | if (y1 < best.y) { 59 | best.x = x1 60 | best.y = y1 61 | } 62 | rownames(best.x) = NULL 63 | return(best.x) 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /todo-files/bench/mco/DTLZ.R: -------------------------------------------------------------------------------- 1 | ## Set of the 9 DTLZ test functions for the case 2D -> 2D. 2 | ## For every function: x is a vector of length n such that 0 < x[i] < 1, i=1,...,n 3 | ## Output is a vector of length 2 4 | 5 | dtlz1 = function(x) { 6 | f = numeric(2) 7 | n = length(x) 8 | g = 100 * (n - 1 + sum((x[2:n] - 0.5)^2 - cos(20 * pi * (x[2:n] - 0.5)))) 9 | f[1] = 1/2 * x[1] * (1 + g) 10 | f[2] = 1/2 * (1 - x[1]) * (1 + g) 11 | return(f) 12 | } 13 | 14 | dtlz2 = function(x) { 15 | f = numeric(2) 16 | n = length(x) 17 | g = sum((x[2:n] - 0.5)^2) 18 | f[1] = (1 + g) * cos(x[1] * pi / 2) 19 | f[2] = (1 + g) * sin(x[1] * pi / 2) 20 | return(f) 21 | } 22 | 23 | dtlz3 = function(x){ 24 | f = numeric(2) 25 | n = length(x) 26 | g = 100 * (n - 1 + sum((x[2:n] - 0.5)^2 - cos(20 * pi * (x[2:n] - 0.5)))) 27 | f[1] = (1 + g) * cos(x[1] * pi / 2) 28 | f[2] = (1 + g) * sin(x[1] * pi / 2) 29 | return(f) 30 | } 31 | 32 | dtlz4 = function(x){ 33 | f = numeric(2) 34 | n = length(x) 35 | x = x^100 36 | g = sum((x[2:n] - 0.5)^2) 37 | f[1] = (1 + g) * cos(x[1] * pi / 2) 38 | f[2] = (1 + g) * sin(x[1] * pi / 2) 39 | return(f) 40 | } 41 | 42 | dtlz5 <- function(x) { 43 | f <- numeric(2) 44 | xm <- x[2:length(x)] 45 | g <- sum(xm^0.1) 46 | t <- pi / (4 * (1 + g)) 47 | theta <- x[1] * pi / 2 48 | f[2] <- (1 + g) * sin(theta) 49 | f[1] <- (1 + g) * cos(theta) 50 | return(f) 51 | } 52 | 53 | 54 | dtlz6 = function(x){ 55 | f = numeric(2) 56 | n = length(x) 57 | x = x * 2.116426807 58 | f[1] = x[1] 59 | g = 1 + 9 * mean(x[2:n]) 60 | h = 2 - f[1] / (1 + g) * (1 + sin(3 * pi * f[1])) 61 | f[2] = (1+g)*h 62 | return(f) 63 | } 64 | 65 | ## Functions, which return l equally distributed points from the 66 | ## paretofront of the corresponding uf-function with in-dimension n 67 | 68 | dtlz1pf = function(n, l){ 69 | X = matrix(0, ncol = l, nrow = 2) 70 | X[1,] = seq(0, 0.5, length.out = l) 71 | X[2,] = 0.5 - X[1,] 72 | X 73 | } 74 | 75 | dtlz2pf = function(n, l){ 76 | x = seq(0, 1, length.out = l) 77 | X = matrix(0, ncol = l, nrow = 2) 78 | X[1,] = sin(x * pi / 2) 79 | X[2,] = cos(x * pi / 2) 80 | X 81 | } 82 | 83 | dtlz3pf = dtlz2pf 84 | 85 | dtlz4pf = dtlz2pf 86 | 87 | dtlz5pf = dtlz2pf 88 | 89 | dtlz6pf = function(n, l){ 90 | X = matrix(0, ncol = l, nrow = 2) 91 | X[1,] = c(seq(0, 0.2514118360, length.out = floor(l/4)), 92 | seq(0.6316265307, 0.8594008566, length.out = ceiling(l/4)), 93 | seq(1.3596178367, 1.5148392681, length.out = floor(l/4)), 94 | seq(2.0518383519, 2.116426807, length.out = ceiling(l/4))) 95 | X[2,] = 4 - X[1,] * (1 + sin(3 * pi * X[1,])) 96 | X 97 | } -------------------------------------------------------------------------------- /R/mboTemplate.R: -------------------------------------------------------------------------------- 1 | # magic mboTemplate - in this function the mbo magic for all our mbo approaches 2 | # does happen - model fitting and point proposal in a generall way. the respective 3 | # mbo algorithms differ in the subfunctions. 4 | # - usually the mboTemplate is started from an OptProblem which is an environment. 5 | # - mboTemplate can also be called from mboContinue from a saved OptState 6 | # - The opt.state is also en environment linking to the main Objects 7 | # - OptProblem (constant; stores the information which define the problem) 8 | # - OptPath (stores all information about function evaluations) 9 | # - OptResult (stores information which should be part of the later constructed mboResult) 10 | # - (see respective source files for further information) 11 | 12 | mboTemplate = function(obj) { 13 | UseMethod("mboTemplate") 14 | } 15 | 16 | # Creates the initial OptState and then runs the template on it 17 | mboTemplate.OptProblem = function(obj) { 18 | opt.state = makeOptState(obj) 19 | # evaluate initial design (if y not given) and log to optpath 20 | evalMBODesign.OptState(opt.state) 21 | finalizeMboLoop(opt.state) 22 | mboTemplate(opt.state) 23 | } 24 | 25 | # Runs the mbo iterations on any given OptState until termination criterion is fulfilled 26 | mboTemplate.OptState = function(obj) { 27 | opt.state = obj 28 | setOptStateLoopStarttime(opt.state) 29 | # check if budget is already exceeded after intitial design creation 30 | terminate = getOptStateTermination(opt.state) 31 | if (terminate$term) { 32 | opt.problem = getOptStateOptProblem(opt.state) 33 | showInfo(getOptProblemShowInfo(opt.problem), "%s. The termination conditions 34 | was satisfied right after the creation of the initial design!", terminate$message) 35 | return(opt.state) 36 | } 37 | repeat { 38 | prop = proposePoints(opt.state) 39 | evalProposedPoints.OptState(opt.state, prop) 40 | finalizeMboLoop(opt.state) 41 | terminate = getOptStateTermination(opt.state) 42 | if (terminate$term) { 43 | break 44 | } 45 | } 46 | opt.state 47 | } 48 | 49 | finalizeMboLoop = function(opt.state) { 50 | # put resampling of surrogate learner and the model itself in the result environment 51 | opt.result = getOptStateOptResult(opt.state) 52 | setOptResultResampleResults(opt.result, opt.state) 53 | setOptResultStoredModels(opt.result, opt.state) 54 | # Indicate, that we are finished by increasing the loop by one 55 | setOptStateLoop(opt.state) 56 | # save on disk routine 57 | # save with increased loop so we can directly start from here again 58 | if (getOptStateShouldSave(opt.state)) 59 | saveOptState(opt.state) 60 | invisible() 61 | } 62 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | #' @import BBmisc 2 | #' @import checkmate 3 | #' @import grDevices 4 | #' @import mlr 5 | #' @import parallelMap 6 | #' @import ParamHelpers 7 | #' @import smoof 8 | #' @import stats 9 | #' @import utils 10 | #' @import data.table 11 | #' @importFrom lhs randomLHS 12 | #' @useDynLib mlrMBO c_sms_indicator c_eps_indicator 13 | NULL 14 | 15 | .onLoad = function(libname, pkgname) { # nocov start 16 | backports::import(pkgname) 17 | parallelRegisterLevels(package = "mlrMBO", levels = c("propose.points", "feval")) 18 | } # nocov end 19 | 20 | # some shortcuts for infill criteria 21 | 22 | #' @rdname MBOInfillCrit 23 | #' @section Predefined standard infill criteria: 24 | #' \describe{ 25 | #' \item{crit.ei}{Expected Improvement} 26 | #' \item{crit.mr}{Mean response} 27 | #' \item{crit.se}{Standard error} 28 | #' \item{crit.cb}{Confidence bound with lambda automatically chosen, see \code{\link{infillcrits}}} 29 | #' \item{crit.cb1}{Confidence bound with lambda=1} 30 | #' \item{crit.cb2}{Confidence bound with lambda=2} 31 | #' \item{crit.aei}{Augmented expected improvement} 32 | #' \item{crit.eqi}{Expected quantile improvement} 33 | #' \item{crit.dib1}{Direct indicator-based with lambda=1} 34 | #' } 35 | #' @export 36 | #' @usage NULL 37 | #' @docType NULL 38 | #' @format NULL 39 | #' @keywords NULL 40 | crit.ei = makeMBOInfillCritEI() 41 | #' @rdname MBOInfillCrit 42 | #' @export 43 | #' @usage NULL 44 | #' @docType NULL 45 | #' @format NULL 46 | #' @keywords NULL 47 | crit.mr = makeMBOInfillCritMeanResponse() 48 | #' @rdname MBOInfillCrit 49 | #' @export 50 | #' @usage NULL 51 | #' @docType NULL 52 | #' @format NULL 53 | #' @keywords NULL 54 | crit.se = makeMBOInfillCritStandardError() 55 | #' @rdname MBOInfillCrit 56 | #' @export 57 | #' @usage NULL 58 | #' @docType NULL 59 | #' @format NULL 60 | #' @keywords NULL 61 | crit.cb = makeMBOInfillCritCB() 62 | #' @rdname MBOInfillCrit 63 | #' @export 64 | #' @usage NULL 65 | #' @docType NULL 66 | #' @format NULL 67 | #' @keywords NULL 68 | crit.cb1 = makeMBOInfillCritCB(cb.lambda = 1) 69 | #' @rdname MBOInfillCrit 70 | #' @export 71 | #' @usage NULL 72 | #' @docType NULL 73 | #' @format NULL 74 | #' @keywords NULL 75 | crit.cb2 = makeMBOInfillCritCB(cb.lambda = 2) 76 | #' @rdname MBOInfillCrit 77 | #' @export 78 | #' @usage NULL 79 | #' @docType NULL 80 | #' @format NULL 81 | #' @keywords NULL 82 | crit.aei = makeMBOInfillCritAEI() 83 | #' @rdname MBOInfillCrit 84 | #' @export 85 | #' @usage NULL 86 | #' @docType NULL 87 | #' @format NULL 88 | #' @keywords NULL 89 | crit.eqi = makeMBOInfillCritEQI() 90 | #' @rdname MBOInfillCrit 91 | #' @export 92 | #' @usage NULL 93 | #' @docType NULL 94 | #' @format NULL 95 | #' @keywords NULL 96 | crit.dib1 = makeMBOInfillCritDIB(cb.lambda = 1) 97 | -------------------------------------------------------------------------------- /R/multiobj_helpers.R: -------------------------------------------------------------------------------- 1 | # operates on row-stored-points. but more importanty: can deal with maximization 2 | getDominatedHV = function(points, ref.point, minimize) { 3 | mults = ifelse(minimize, 1, -1) 4 | points2 = t(as.matrix(points) %*% diag(mults)) 5 | emoa::dominated_hypervolume(points2, ref.point) 6 | } 7 | 8 | # returns logical index 9 | isDominated = function(points, minimize) { 10 | mults = ifelse(minimize, 1, -1) 11 | points2 = t(as.matrix(points) %*% diag(mults)) 12 | emoa::is_dominated(points2) 13 | } 14 | 15 | # get subset of point which are non-dominated 16 | getNonDominatedPoints = function(points, minimize) { 17 | d = isDominated(points, minimize) 18 | points[!d, , drop = FALSE] 19 | } 20 | 21 | # returns max (or min) of all coords, depending on minimization (or maximization) 22 | getWorstExtremePoint = function(points, minimize) { 23 | mults = ifelse(minimize, 1, -1) 24 | apply(as.matrix(points) %*% diag(mults), 2L, max) * mults 25 | } 26 | 27 | # return the hypervolume contribution of each elemt (row) of xs (matrix with length 28 | # n.objectives cols) with respect to the ys (matrix with number.of.target cols) 29 | # ref is the used reference point for hv calculation 30 | # return vector of hypervolume contributions 31 | getHypervolumeContributions = function(xs, ys, ref.point, minimize) { 32 | hv.old = getDominatedHV(points = ys, ref.point = ref.point, minimize) 33 | hv.new = apply(xs, 1, function(x) 34 | getDominatedHV(rbind(ys, x), ref.point, minimize)) 35 | return(hv.new - hv.old) 36 | } 37 | 38 | # determines the reference point for multi-objective optimization 39 | # Returns reference-point, numeric vector of length n.objectives 40 | getMultiObjRefPoint = function (ys, control, minimize = control$minimize) { 41 | switch(control$multiobj.ref.point.method, 42 | const = control$multiobj.ref.point.val, 43 | all = getWorstExtremePoint(ys, minimize) + control$multiobj.ref.point.offset, 44 | front = { 45 | front = getNonDominatedPoints(ys, minimize) 46 | getWorstExtremePoint(front, control$minimize) + control$multiobj.ref.point.offset 47 | } 48 | ) 49 | } 50 | 51 | # evaluate an infill crit on multiple models (one per objective in multi-objective) 52 | # returns matrix of crit vals, rows = points, cols = crits 53 | evalCritFunForMultiObjModels = function(infill.crit.fun, points, models, control, par.set, designs, iter) { 54 | asMatrixCols(lapply(seq_along(models), function(i) { 55 | # we need to make sure mininimize in control is a scalar, so we can multiply it in infill crits... 56 | control$minimize = control$minimize[i] 57 | control$y.name = control$y.name[i] 58 | infill.crit.fun(points, models[i], control, par.set, designs[i], iter) 59 | })) 60 | } 61 | -------------------------------------------------------------------------------- /todo-files/bench/mco/testfunctionsMultiObjective.R: -------------------------------------------------------------------------------- 1 | library(mco) 2 | 3 | source("bench/mco/testfunctionsSingleObjective.R") 4 | 5 | 6 | # 2D -> 2M 7 | GOMOP_2D2M = function(x) { 8 | y = numeric(2) 9 | y[1] = branin(x) 10 | y[2] = camel3(x) 11 | return(y) 12 | } 13 | 14 | # 5D -> 2M 15 | GOMOP_5D2M = function(x) { 16 | y = numeric(2) 17 | y[1] = hartman(x) 18 | y[2] = rastrigin(x) 19 | return(y) 20 | } 21 | 22 | # 2D -> 5M 23 | GOMOP_2D5M = function(x) { 24 | y = numeric(5) 25 | y[1] = branin(x) 26 | y[2] = hartman(x) 27 | y[3] = goldsteinPrice(x) 28 | y[4] = camel3(x) 29 | y[5] = camel6(x) 30 | return(y) 31 | } 32 | 33 | # 5D -> 5M 34 | GOMOP_5D5M = function(x) { 35 | y = numeric(5) 36 | y[1] = hartman(x) 37 | y[2] = rosenbrock(x) 38 | y[3] = rastrigin(x) 39 | y[4] = zakharov(x) 40 | y[5] = powell(x) 41 | return(y) 42 | } 43 | 44 | # GOMOP from Wagner Diss 45 | 46 | GOMOP2_2D3M = function(x) { 47 | y = numeric(3) 48 | y[1] = branin(x) 49 | y[2] = hartman(x) 50 | y[3] = goldsteinPrice(x) 51 | return(y) 52 | } 53 | 54 | GOMOP3_3D2M = function(x) { 55 | y = numeric(2) 56 | y[1] = hartman(x) 57 | y[2] = rosenbrock(x) 58 | return(y) 59 | } 60 | 61 | # ZDT - functions 62 | # 5D -> 2M 63 | # functions zdt1(), zdt2() and zdt3() from mco package 64 | 65 | 66 | # DTLZ - functions 67 | 68 | # dtlz1: 5D -> 5M 69 | dtlz1_5D5M = function(x) { 70 | stopifnot(length(x) >= 5) 71 | y = numeric(5) 72 | n = length(x) 73 | g = 100 * (n - 4 + sum((x[5:n] - 0.5) ^ 2 - cos(20 * pi * (x[5:n] - 0.5)))) 74 | y[1] = 1/2 * x[1] * x[2] * x[3] * x[4] * (1 + g) 75 | y[2] = 1/2 * x[1] * x[2] * x[3] * (1 - x[4]) * (1 + g) 76 | y[3] = 1/2 * x[1] * x[2] * (1 - x[3]) * (1 + g) 77 | y[4] = 1/2 * x[1] * (1 - x[2]) * (1 + g) 78 | y[5] = 1/2 * (1 - x[1]) * (1 + g) 79 | return(y) 80 | } 81 | 82 | # dtlz2: 5D -> 2M 83 | dtlz2_5D2M = function(x) { 84 | f = numeric(2) 85 | n = length(x) 86 | g = sum((x[2:n] - 0.5)^2) 87 | f[1] = (1 + g) * cos(x[1] * pi / 2) 88 | f[2] = (1 + g) * sin(x[1] * pi / 2) 89 | return(f) 90 | } 91 | 92 | 93 | # dtlz2: 5D -> 5M 94 | dtlz2_5D5M = function(x) { 95 | stopifnot(length(x) >= 5) 96 | y = numeric(5) 97 | n = length(x) 98 | g = sum((x[5:n] - 0.5)^2) 99 | y[1] = (1 + g) * cos(x[1] * pi / 2) * cos(x[2] * pi / 2) * cos(x[3] * pi / 2) * cos(x[4] * pi / 2) 100 | y[2] = (1 + g) * cos(x[1] * pi / 2) * cos(x[2] * pi / 2) * cos(x[3] * pi / 2) * sin(x[4] * pi / 2) 101 | y[3] = (1 + g) * cos(x[1] * pi / 2) * cos(x[2] * pi / 2) * sin(x[3] * pi / 2) 102 | y[4] = (1 + g) * cos(x[1] * pi / 2) * sin(x[2] * pi / 2) 103 | y[5] = (1 + g) * sin(x[1] * pi / 2) 104 | return(y) 105 | } 106 | 107 | -------------------------------------------------------------------------------- /R/makeMBOTrafoFunction.R: -------------------------------------------------------------------------------- 1 | #' @title Create a transformation function for MBOExampleRun. 2 | #' 3 | #' @description 4 | #' Creates a transformation function for MBOExampleRun. 5 | #' @param name [\code{character(1)}]\cr 6 | #' Name of the transformation. 7 | #' @param fun [\code{function}]\cr 8 | #' R function which expects a numeric vector. 9 | #' @return Object of type MBOTrafoFunction. 10 | #' @seealso \link{trafos} 11 | #' @export 12 | makeMBOTrafoFunction = function(name, fun) { 13 | assertString(name, na.ok = FALSE) 14 | assertFunction(fun) 15 | fun = addClasses(fun, "MBOTrafoFunction") 16 | fun = setAttribute(fun, "name", name) 17 | return(fun) 18 | } 19 | 20 | #' Transformation methods. 21 | #' 22 | #' \itemize{ 23 | #' \item{\bold{logTrafo}}{\cr Natural logarithm.} 24 | #' \item{\bold{sqrtTrafo}}{\cr Square root.} 25 | #' } 26 | #' If negative values occur and the trafo function can handle only positive values, 27 | #' a shift of the form x - min(x) + 1 is performed prior to the transformation if the 28 | #' argument \code{handle.violations} is set to \dQuote{warn} which is the default 29 | #' value. 30 | #' @template arg_handle_violations 31 | #' @format None 32 | #' @name trafos 33 | #' @rdname trafos 34 | NULL 35 | 36 | #' @export 37 | #' @rdname trafos 38 | #' @param base [\code{numeric(1)}]\cr 39 | #' The base with respect to which logarithms are computed. 40 | #' Default is \code{10}. 41 | trafoLog = function(base = 10, handle.violations = "warn") { 42 | assertNumber(base, na.ok = FALSE, lower = 2L) 43 | assertChoice(handle.violations, c("warn", "error")) 44 | force(handle.violations) 45 | makeMBOTrafoFunction( 46 | name = "log", 47 | fun = function(x) { 48 | x = checkAndRepairNegativeValues(x, handle.violations) 49 | log(x, base = base) 50 | } 51 | ) 52 | } 53 | 54 | #' @export 55 | #' @rdname trafos 56 | trafoSqrt = function(handle.violations = "warn") { 57 | force(handle.violations) 58 | makeMBOTrafoFunction( 59 | name = "sqrt", 60 | fun = function(x) { 61 | x = checkAndRepairNegativeValues(x, handle.violations) 62 | sqrt(x) 63 | } 64 | ) 65 | } 66 | 67 | # Helper function which shifts function values to makes trafo functions work. 68 | # 69 | # @param x [\code{numeric}]\cr 70 | # Source vector. 71 | # @param handle.violations [\code{character}]\cr 72 | # How to handle violations? Either shift them or throw an error. 73 | #@ return [\code{numeric}] 74 | checkAndRepairNegativeValues = function(x, handle.violations) { 75 | if (any(x < 0)) { 76 | if (handle.violations == "error") 77 | stopf("Negative function values occurred during transformation.") 78 | warning("Negative function values. Shifting function.") 79 | return(x - min(x) + 1) 80 | } 81 | return(x) 82 | } 83 | -------------------------------------------------------------------------------- /todo-files/shiny/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(grid) 3 | library(gridExtra) 4 | library(soobench) 5 | library(mlrMBO) 6 | 7 | # FIXME: this is ugly 8 | fun.map = list("branin" = list("fun" = generate_branin_function), 9 | "ackley_1" = list("fun" = generate_ackley_function, "dimension" = 1), 10 | "ackley_2" = list("fun" = generate_ackley_function, "dimension" = 2), 11 | "rastrigin_1" = list("fun" = generate_rastrigin_function, "dimension" = 1), 12 | "rastrigin_2" = list("fun" = generate_rastrigin_function, "dimension" = 2), 13 | "rosenbrock_2" = list("fun" = generate_rosenbrock_function, "dimension" = 2)) 14 | 15 | trafo = function(x) { 16 | trafo = NULL 17 | switch(x, 18 | "none" = { trafo=NULL }, 19 | "log" = { trafo=mlrMBO:::logTrafo() }, 20 | "log10" = { trafo=mlrMBO:::log10Trafo() }) 21 | return(trafo) 22 | } 23 | 24 | # all the shiny magic 25 | shinyServer(function(input, output) { 26 | # server logic comes here 27 | 28 | # return objective function we wish to optimize 29 | fun = reactive({ 30 | name.fun = input$obj.fun 31 | fun.spec = fun.map[[name.fun]] 32 | if (!"dimension" %in% names(fun.spec)) { 33 | fun.spec$fun() 34 | } else { 35 | fun.spec$fun(fun.spec$dimension) 36 | } 37 | }) 38 | 39 | # returns the objective function name as a string 40 | objFunName = reactive({ 41 | obj.fun = fun() 42 | paste(function_name(obj.fun)) 43 | }) 44 | 45 | # render objective fun 46 | output$obj.fun.name = renderText({ 47 | objFunName() 48 | }) 49 | 50 | #render plot 51 | output$obj.fun.plot = renderPlot({ 52 | # get data 53 | obj.fun = fun() 54 | iter = input$iter 55 | 56 | # determine desired z-transformation 57 | trafo.y = trafo(input$trafo.y) 58 | trafo.yhat = trafo(input$trafo.yhat) 59 | trafo.crit = trafo(input$trafo.crit) 60 | trafo.se = trafo(input$trafo.se) 61 | 62 | trafo = list( 63 | "y" = trafo.y, 64 | "yhat" = trafo.yhat, 65 | "crit" = trafo.crit, 66 | "se" = trafo.se) 67 | 68 | # build file name 69 | hash = function_name(obj.fun) 70 | file.name = paste(hash, ".Rdata", sep="") 71 | 72 | # load optimization 73 | # load mbo result object from file 74 | if (file.exists(file.name)) { 75 | load(file.name) 76 | } else { 77 | # otherwise apply learner and do example run 78 | ctrl = makeMBOControl(init.design.points=10, iters=10, propose.points=1, 79 | infill.crit="ei", infill.opt="focussearch", infill.opt.focussearch.points=2000) 80 | 81 | lrn = makeLearner("regr.km", predict.type="se", covtype="matern3_2") 82 | 83 | run = mlrMBO:::exampleRun(obj.fun, learner=lrn, control=ctrl, points.per.dim=50) 84 | save(file=file.name, list=c("run")) 85 | } 86 | 87 | print(str(trafo)) 88 | pl = autoplot(run, iters=iter, pause=FALSE, trafo=trafo) 89 | 90 | pl$pl.all 91 | }) 92 | }) 93 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: mlrMBO 2 | Title: Bayesian Optimization and Model-Based Optimization of Expensive 3 | Black-Box Functions 4 | Version: 1.1.5-9000 5 | Authors@R: c( 6 | person("Bernd", "Bischl", , "bernd_bischl@gmx.net", role = "aut", 7 | comment = c(ORCID = "0000-0001-6002-6980")), 8 | person("Jakob", "Richter", , "code@jakob-r.de", role = c("aut", "cre"), 9 | comment = c(ORCID = "0000-0003-4481-5554")), 10 | person("Jakob", "Bossek", , "j.bossek@gmail.com", role = "aut", 11 | comment = c(ORCID = "0000-0002-4121-4668")), 12 | person("Daniel", "Horn", , "daniel.horn@tu-dortmund.de", role = "aut"), 13 | person("Michel", "Lang", , "michellang@gmail.com", role = "aut", 14 | comment = c(ORCID = "0000-0001-9754-0393")), 15 | person("Janek", "Thomas", , "janek.thomas@stat.uni-muenchen.de", role = "aut", 16 | comment = c(ORCID = "0000-0003-4511-6245")) 17 | ) 18 | Description: Flexible and comprehensive R toolbox for model-based 19 | optimization ('MBO'), also known as Bayesian optimization. It 20 | implements the Efficient Global Optimization Algorithm and is designed 21 | for both single- and multi- objective optimization with mixed 22 | continuous, categorical and conditional parameters. The machine 23 | learning toolbox 'mlr' provide dozens of regression learners to model 24 | the performance of the target algorithm with respect to the parameter 25 | settings. It provides many different infill criteria to guide the 26 | search process. Additional features include multi-point batch 27 | proposal, parallel execution as well as visualization and 28 | sophisticated logging mechanisms, which is especially useful for 29 | teaching and understanding of algorithm behavior. 'mlrMBO' is 30 | implemented in a modular fashion, such that single components can be 31 | easily replaced or adapted by the user for specific use cases. 32 | License: BSD_2_clause + file LICENSE 33 | URL: https://github.com/mlr-org/mlrMBO 34 | BugReports: https://github.com/mlr-org/mlrMBO/issues 35 | Depends: 36 | mlr (>= 2.10), 37 | ParamHelpers (>= 1.10), 38 | smoof (>= 1.5.1) 39 | Imports: 40 | backports (>= 1.1.0), 41 | BBmisc (>= 1.11), 42 | checkmate (>= 1.8.2), 43 | data.table, 44 | lhs, 45 | parallelMap (>= 1.3) 46 | Suggests: 47 | akima, 48 | cmaesr (>= 1.0.3), 49 | covr, 50 | DiceKriging, 51 | earth, 52 | emoa, 53 | GGally, 54 | ggplot2, 55 | gridExtra, 56 | interp, 57 | kernlab, 58 | kknn, 59 | knitr, 60 | mco, 61 | nnet, 62 | party, 63 | randomForest, 64 | reshape2, 65 | rgenoud, 66 | rmarkdown, 67 | rpart, 68 | testthat 69 | VignetteBuilder: 70 | knitr 71 | ByteCompile: yes 72 | Encoding: UTF-8 73 | RoxygenNote: 7.2.1 74 | -------------------------------------------------------------------------------- /todo-files/outdated_rmds/parallelization.Rmd: -------------------------------------------------------------------------------- 1 | # Work in progress! 2 | 3 | # Parallelization 4 | 5 | It is possible to parallelize the evaluation of the target function to speed up the computation. Internally the 6 | evaluation of the target function is realized with the R package `parallelMap`. This package offers simple parallelization with various different backends. For details on the usage see the [parallelMap github page](https://github.com/mlr-org/parallelMap#parallelmap), which offers a nice tutorial and describes all possible backends thorougly. For our usage we use a *multicore* backend. Note, that the multicore parallelization does _not_ work on windows machines. 7 | 8 | ```{r eval=FALSE} 9 | library(mlrMBO) 10 | library(parallelMap) 11 | 12 | obj.fun = makeSphereFunction(1) 13 | learner = makeLearner("regr.km", predict.type = "se", covtype = "matern3_2") 14 | control = makeMBOControl() 15 | control = setMBOControlTermination(control, iters = 3) 16 | design = generateDesign(n = 6, par.set = getParamSet(obj.fun)) 17 | 18 | parallelStartMulticore(cpus = 2) # use 2 CPUs 19 | res = mbo(obj.fun, learner = learner, control = control) 20 | parallelStop() 21 | ``` 22 | 23 | 24 | 25 | Since mbo is a sequential method we cannot gain any improvement in the running time in this setup. But there are multiple two situations in which parallelization can be utilized _noisy functions_ and _multi point proposals_. 26 | 27 | 28 | ## Noisy functions 29 | 30 | If our target function is not deterministic (e.g. the accuracy of a machine learning model) it is advisable to take the crossvalidation error. For the crossvalidation error, the model is fitted multuple times on slightly different training data. This process can be easily parallelized with `parallelMap`. 31 | 32 | ```{r, eval = FALSE} 33 | library(mlr) 34 | 35 | fn = function(x) { 36 | lrn = makeLearner("classif.ksvm", par.vals = x) 37 | 38 | rdesc = makeResampleDesc("CV", iters = 10L) 39 | 40 | res = resample(learner = lrn, iris.task, rdesc, show.info = FALSE) 41 | 42 | res$aggr 43 | } 44 | 45 | 46 | par.set = filterParams(mlr::getParamSet("classif.ksvm"), ids = c("C", "sigma")) 47 | 48 | par.set = makeParamSet(makeNumericParam("C", lower = -10, upper = 10, trafo = function(x) 2^x), 49 | makeNumericParam("sigma", lower = -10, upper = 10, trafo = function(x) 2^x)) 50 | 51 | obj.fun = makeSingleObjectiveFunction(name = "noisy_example", 52 | fn = fn, 53 | has.simple.signature = FALSE, 54 | par.set = par.set) 55 | 56 | 57 | control = makeMBOControl() 58 | control = setMBOControlTermination(control, iters = 10L) 59 | 60 | design = generateDesign(n = 10, getParamSet(obj.fun), trafo = TRUE) 61 | 62 | parallelStartMulticore(cpus = 4L) 63 | exampleRun(obj.fun, design = design, control = control) 64 | parallelStop() 65 | 66 | ``` 67 | 68 | --------------------------------------------------------------------------------