Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cli package #238

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Depends:
R (>= 3.1.0)
Imports:
checkmate (>= 2.0.0),
cli,
data.table,
lgr,
methods,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export(transform_xdt_to_xss)
export(trm)
export(trms)
import(checkmate)
import(cli)
import(data.table)
import(mlr3misc)
import(paradox)
Expand Down
4 changes: 2 additions & 2 deletions R/Archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ Archive = R6Class("Archive",
#'
#' @param ... (ignored).
print = function() {
catf(format(self))
print(self$data[, setdiff(names(self$data), "x_domain"), with = FALSE], digits = 2)
cli_h1(sprintf("%s %s", class(self)[1L], if (is.na(self$label)) "" else paste0("- ", self$label)))
print(as.data.table(self, unnest = "x_domain"), digits = 1)
},

#' @description
Expand Down
14 changes: 7 additions & 7 deletions R/Objective.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ Objective = R6Class("Objective",
#' Print method.
#' @return `character()`.
print = function() {
catf(self$format())
catf("Domain:")
print(self$domain)
catf("Codomain:")
print(self$codomain)
cli_h1(class(self)[1L])
cli_li("Domain:")
print(as.data.table(self$domain)[, c("id", "class", "lower", "upper", "nlevels"), with = FALSE])
cli_li("Codomain:")
print(as.data.table(self$codomain)[, c("id", "class", "lower", "upper"), with = FALSE])
if (length(self$constants$values) > 0) {
catf("Constants:")
print(self$constants)
cli_li("Constants:")
print(as.data.table(self$constants)[, c("id", "class", "lower", "upper", "nlevels"), with = FALSE])
}
},

Expand Down
25 changes: 11 additions & 14 deletions R/OptimInstance.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,19 @@ OptimInstance = R6Class("OptimInstance",
#'
#' @param ... (ignored).
print = function(...) {

catf(format(self))
catf(str_indent("* State: ", if (is.null(private$.result)) "Not optimized" else "Optimized"))
catf(str_indent("* Objective:", format(self$objective)))
if (!self$search_space$length) {
catf("* Search Space: Empty")
} else {
catf("* Search Space:")
print(as.data.table(self$search_space)[, c("id", "class", "lower", "upper", "nlevels"), with = FALSE])
}
catf(str_indent("* Terminator:", format(self$terminator)))
cli_h1(class(self)[1L])
cli_li(sprintf("State: %s", if (is.null(private$.result)) "Not optimized" else "Optimized"))
cli_li(sprintf("Objective: %s", class(self$objective)[1]))
cli_li("Search Space:")
print(as.data.table(self$search_space)[, c("id", "class", "lower", "upper", "nlevels"), with = FALSE])
cli_li(sprintf("Terminator: %s %s", class(self$terminator)[1], if (length(self$terminator$param_set$values)) paste0("(", as_short_string(self$terminator$param_set$values), ")") else ""))
if (!is.null(private$.result)) {
catf("* Result:")
cli_li("Result:")
print(self$result[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
catf("* Archive:")
print(as.data.table(self$archive)[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
cli_li("Archive:")
tab = as.data.table(self$archive)
x_domain_ids = names(tab)[grepl("x_domain_" , names(tab))]
print(tab[, c(self$archive$cols_y, self$archive$cols_x, x_domain_ids), with = FALSE], digits = 1)
}
},

Expand Down
2 changes: 1 addition & 1 deletion R/OptimInstanceAsync.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ OptimInstanceAsync = R6Class("OptimInstanceAsync",
#' @param ... (ignored).
print = function(...) {
super$print()
catf(str_indent("* Workers:", self$rush$n_workers))
cli_li(sprintf("Workers: %i", self$rush$n_workers))
},

#' @description
Expand Down
10 changes: 5 additions & 5 deletions R/Optimizer.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ Optimizer = R6Class("Optimizer",
#'
#' @return (`character()`).
print = function() {
catn(format(self), if (is.na(self$label)) "" else paste0(": ", self$label))
catn(str_indent("* Parameters:", as_short_string(self$param_set$values)))
catn(str_indent("* Parameter classes:", self$param_classes))
catn(str_indent("* Properties:", self$properties))
catn(str_indent("* Packages:", self$packages))
cli_h1(sprintf("%s %s", class(self)[1L], if (is.na(self$label)) "" else paste0("- ", self$label)))
cli_li(sprintf("Parameters: %s", if (length(self$param_set$values)) as_short_string(self$param_set$values) else "-"))
cli_li(sprintf("Parameter classes: %s", paste(self$param_classes, collapse = ", ")))
cli_li(sprintf("Properties: %s", paste(self$properties, collapse = ", ")))
cli_li(sprintf("Packages: %s", paste(self$packages, collapse = ", ")))
},

#' @description
Expand Down
4 changes: 2 additions & 2 deletions R/Terminator.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ Terminator = R6Class("Terminator",
#'
#' @param ... (ignored).
print = function(...) {
catn(format(self), if (is.na(self$label)) "" else paste0(": ", self$label))
catn(str_indent("* Parameters:", as_short_string(self$param_set$values)))
cli_h1(sprintf("%s %s", class(self)[1L], if (is.na(self$label)) "" else paste0("- ", self$label)))
cli_li(sprintf("Parameters: %s", if(length(self$param_set$values)) as_short_string(self$param_set$values) else "-"))
},

#' @description
Expand Down
3 changes: 1 addition & 2 deletions R/TerminatorCombo.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ TerminatorCombo = R6Class("TerminatorCombo",
#' @param ... (ignored).
print = function(...) {
super$print(...)
catf(str_indent("* Terminators:", paste(map_chr(self$terminators, format),
collapse = ",")))
cli_li(sprintf("Terminators: %s", paste(map_chr(self$terminators, format), collapse = ", ")))
},

#' @description
Expand Down
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#' @import checkmate
#' @import paradox
#' @import mlr3misc
#' @import cli
#' @importFrom R6 R6Class
#' @importFrom utils capture.output head tail
#' @importFrom methods formalArgs
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/_snaps/ArchiveBatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

Code
a
Message

-- ArchiveBatch - Data Table Storage -------------------------------------------
Output
<ArchiveBatch>
Null data.table (0 rows and 0 cols)

52 changes: 28 additions & 24 deletions tests/testthat/_snaps/Objective.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,39 @@

Code
obj
Message

-- ObjectiveTestEval -----------------------------------------------------------
* Domain:
Output
<ObjectiveTestEval:f>
Domain:
<ParamSet(2)>
id class lower upper nlevels default value
<char> <char> <num> <num> <num> <list> <list>
1: x1 ParamDbl -1 1 Inf <NoDefault[0]> [NULL]
2: x2 ParamDbl -1 1 Inf <NoDefault[0]> [NULL]
Codomain:
<Codomain(1)>
id class lower upper nlevels default value
<char> <char> <num> <num> <num> <list> <list>
1: y ParamDbl -Inf Inf Inf <NoDefault[0]> [NULL]
id class lower upper nlevels
<char> <char> <num> <num> <num>
1: x1 ParamDbl -1 1 Inf
2: x2 ParamDbl -1 1 Inf
Message
* Codomain:
Output
id class lower upper
<char> <char> <num> <num>
1: y ParamDbl -Inf Inf

---

Code
obj
Message

-- ObjectiveTestEvalMany -------------------------------------------------------
* Domain:
Output
id class lower upper nlevels
<char> <char> <num> <num> <num>
1: x1 ParamDbl -1 1 Inf
2: x2 ParamDbl -1 1 Inf
Message
* Codomain:
Output
<ObjectiveTestEvalMany:f>
Domain:
<ParamSet(2)>
id class lower upper nlevels default value
<char> <char> <num> <num> <num> <list> <list>
1: x1 ParamDbl -1 1 Inf <NoDefault[0]> [NULL]
2: x2 ParamDbl -1 1 Inf <NoDefault[0]> [NULL]
Codomain:
<Codomain(1)>
id class lower upper nlevels default value
<char> <char> <num> <num> <num> <list> <list>
1: y ParamDbl -Inf Inf Inf <NoDefault[0]> [NULL]
id class lower upper
<char> <char> <num> <num>
1: y ParamDbl -Inf Inf

13 changes: 8 additions & 5 deletions tests/testthat/_snaps/OptimInstanceBatchMultiCrit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

Code
inst
Output
<OptimInstanceBatchMultiCrit>
* State: Not optimized
* Objective: <ObjectiveRFun:function>
Message

-- OptimInstanceBatchMultiCrit -------------------------------------------------
* State: Not optimized
* Objective: ObjectiveRFun
* Search Space:
Output
id class lower upper nlevels
<char> <char> <num> <num> <num>
1: x1 ParamDbl -1 1 Inf
2: x2 ParamDbl -1 1 Inf
* Terminator: <TerminatorEvals>
Message
* Terminator: TerminatorEvals (n_evals=20, k=0)

13 changes: 8 additions & 5 deletions tests/testthat/_snaps/OptimInstanceBatchSingleCrit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

Code
inst
Output
<OptimInstanceBatchSingleCrit>
* State: Not optimized
* Objective: <ObjectiveRFun:function>
Message

-- OptimInstanceBatchSingleCrit ------------------------------------------------
* State: Not optimized
* Objective: ObjectiveRFun
* Search Space:
Output
id class lower upper nlevels
<char> <char> <num> <num> <num>
1: x1 ParamDbl -1 1 Inf
2: x2 ParamDbl -1 1 Inf
* Terminator: <TerminatorEvals>
Message
* Terminator: TerminatorEvals (n_evals=20, k=0)

5 changes: 3 additions & 2 deletions tests/testthat/_snaps/OptimizerCmaes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

Code
z$optimizer
Output
<OptimizerBatchCmaes>: Covariance Matrix Adaptation Evolution Strategy
Message

-- OptimizerBatchCmaes - Covariance Matrix Adaptation Evolution Strategy -------
* Parameters: start_values=random
* Parameter classes: ParamDbl
* Properties: single-crit
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/_snaps/OptimizerDesignPoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

Code
z$optimizer
Output
<OptimizerBatchDesignPoints>: Design Points
Message

-- OptimizerBatchDesignPoints - Design Points ----------------------------------
* Parameters: batch_size=1, design=<data.table>
* Parameter classes: ParamLgl, ParamInt, ParamDbl, ParamFct, ParamUty
* Properties: dependencies, single-crit, multi-crit
Expand All @@ -13,8 +14,9 @@

Code
z$optimizer
Output
<OptimizerBatchDesignPoints>: Design Points
Message

-- OptimizerBatchDesignPoints - Design Points ----------------------------------
* Parameters: batch_size=1, design=<data.table>
* Parameter classes: ParamLgl, ParamInt, ParamDbl, ParamFct, ParamUty
* Properties: dependencies, single-crit, multi-crit
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/_snaps/OptimizerFocusSearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

Code
z$optimizer
Output
<OptimizerBatchFocusSearch>: Focus Search
Message

-- OptimizerBatchFocusSearch - Focus Search ------------------------------------
* Parameters: n_points=1, maxit=10
* Parameter classes: ParamLgl, ParamInt, ParamDbl, ParamFct
* Properties: dependencies, single-crit
Expand All @@ -13,8 +14,9 @@

Code
z$optimizer
Output
<OptimizerBatchFocusSearch>: Focus Search
Message

-- OptimizerBatchFocusSearch - Focus Search ------------------------------------
* Parameters: n_points=10, maxit=10
* Parameter classes: ParamLgl, ParamInt, ParamDbl, ParamFct
* Properties: dependencies, single-crit
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/_snaps/OptimizerGenSA.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

Code
z$optimizer
Output
<OptimizerBatchGenSA>: Generalized Simulated Annealing
* Parameters: list()
Message

-- OptimizerBatchGenSA - Generalized Simulated Annealing -----------------------
* Parameters: -
* Parameter classes: ParamDbl
* Properties: single-crit
* Packages: bbotk, GenSA
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/_snaps/OptimizerGridSearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

Code
z$optimizer
Output
<OptimizerBatchGridSearch>: Grid Search
Message

-- OptimizerBatchGridSearch - Grid Search --------------------------------------
* Parameters: batch_size=1, resolution=10
* Parameter classes: ParamLgl, ParamInt, ParamDbl, ParamFct
* Properties: dependencies, single-crit, multi-crit
Expand All @@ -13,8 +14,9 @@

Code
z$optimizer
Output
<OptimizerBatchGridSearch>: Grid Search
Message

-- OptimizerBatchGridSearch - Grid Search --------------------------------------
* Parameters: batch_size=1, resolution=10
* Parameter classes: ParamLgl, ParamInt, ParamDbl, ParamFct
* Properties: dependencies, single-crit, multi-crit
Expand Down
Loading
Loading