Title: | MASCOTNUM Algorithms Template Tools |
---|---|
Description: | Helper functions for MASCOTNUM algorithm template, for design of numerical experiments practice: algorithm template parser to support MASCOTNUM specification <https://www.gdr-mascotnum.fr/template.html>, 'ask & tell' decoupling injection (inspired by <https://search.r-project.org/CRAN/refmans/sensitivity/html/decoupling.html>) to use "crimped" algorithms (like uniroot(), optim(), ...) from outside R, basic template examples: Brent algorithm for 1 dim root finding and L-BFGS-B from base optim(). |
Authors: | Yann Richet [aut, cre]
|
Maintainer: | Yann Richet <[email protected]> |
License: | Apache License (>= 2) |
Version: | 0.2-0 |
Built: | 2025-03-12 04:14:06 UTC |
Source: | https://github.com/mascotnum/templr |
ask&tell component function to 'ask' where objective function gradient evaluation is required.
ask_dX( id = 0, dX.tmp = "dX.todo", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), sleep_step = 0.1, sleep_init = 0, timeout = 360000, trace = function(...) cat(paste0(..., "\n")), clean = TRUE )
ask_dX( id = 0, dX.tmp = "dX.todo", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), sleep_step = 0.1, sleep_init = 0, timeout = 360000, trace = function(...) cat(paste0(..., "\n")), clean = TRUE )
id |
unique identifier for this asktell loop (default: "0") |
dX.tmp |
temporary "X" values file (default: "dX.todo") |
tmp_path |
temporary directory to store X.tmp & Y.tmp (default: 'tempdir()/../asktell.tmp') |
sleep_step |
delay between checking X.tmp and Y.tmp (default: 0.1 sec.) |
sleep_init |
initial delay before checking X.tmp and Y.tmp (default: 0 sec.) |
timeout |
maximum delay before breaking loop if X.tmp or Y.tmp doesn't appear (default: 36000 sec. = 10 min.) . |
trace |
function to display asktell loop status (default : 'cat') |
clean |
should we cleanup temporary files after reading ? (default: TRUE) |
'ask&tell' injection loop to call an external objective function within an inline algorithm (like optim(...)) Main idea: pass 'ask_Y' as objectve function argument of algorithm, which will wait until you call 'tell_Y' in another R process. In this secondary process, you can read what X is called using 'ask_X', and when you know what values returns from the external objective, just call 'tell_Y' to give it.
input values of objective function to compute externally
Y. Richet, discussions with D. Sinoquet. Async IO principle was defined by G. Pujol.
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_dY(x=123) ## In another R session ask_dX() # returns 123 tell_dY(y=456) ## Then ask_dY in main R session returns with value '456' ## End(Not run)
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_dY(x=123) ## In another R session ask_dX() # returns 123 tell_dY(y=456) ## Then ask_dY in main R session returns with value '456' ## End(Not run)
ask&tell component function to 'ask' objective function gradient evaluation using finite difference.
ask_dY( x, dX = 0.001, id = 0, dX.tmp = "dX.todo", dY.tmp = "dY.done", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), sleep_step = 0.1, sleep_init = 0, timeout = 360000, trace = function(...) cat(paste0(..., "\n")), clean = TRUE, force_cleanup = FALSE )
ask_dY( x, dX = 0.001, id = 0, dX.tmp = "dX.todo", dY.tmp = "dY.done", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), sleep_step = 0.1, sleep_init = 0, timeout = 360000, trace = function(...) cat(paste0(..., "\n")), clean = TRUE, force_cleanup = FALSE )
x |
input values of objective function gradient to compute |
dX |
finite difference applied to input values to compute gradient |
id |
unique identifier for this asktell loop (default: "0") |
dX.tmp |
temporary "X" values file (default: "dX.todo") |
dY.tmp |
temporary "Y" values file (default: "dY.done") |
tmp_path |
temporary directory to store X.tmp & Y.tmp (default: 'tempdir()/../asktell.tmp') |
sleep_step |
delay between checking X.tmp and Y.tmp (default: 0.1 sec.) |
sleep_init |
initial delay before checking X.tmp and Y.tmp (default: 0 sec.) |
timeout |
maximum delay before breaking loop if X.tmp or Y.tmp doesn't appear (default: 36000 sec. = 10 min.) . |
trace |
function to display asktell loop status (default : 'cat') |
clean |
should we cleanup temporary files after reading ? (default: TRUE) |
force_cleanup |
should we cleanup temporary files before writing (possible conflicting asktell calls) ? (default: FALSE) |
'ask&tell' injection loop to call an external objective function within an inline algorithm (like optim(...)) Main idea: pass 'ask_Y' as objectve function argument of algorithm, which will wait until you call 'tell_Y' in another R process. In this secondary process, you can read what X is called using 'ask_X', and when you know what values returns from the external objective, just call 'tell_Y' to give it.
output value of objective function gradient, as given by tell_dY() call in parallel session
Y. Richet, discussions with D. Sinoquet. Async IO principle was defined by G. Pujol.
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_dY(x=123) ## In another R session ask_dX() # returns 123 tell_dY(y=456) ## Then ask_dY in main R session returns with value '456' ## End(Not run)
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_dY(x=123) ## In another R session ask_dX() # returns 123 tell_dY(y=456) ## Then ask_dY in main R session returns with value '456' ## End(Not run)
ask&tell component function to 'ask' where objective function evaluation is required.
ask_X( id = 0, X.tmp = "X.todo", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), sleep_step = 0.1, sleep_init = 0.1, timeout = 360000, trace = function(...) cat(paste0(..., "\n")), clean = TRUE )
ask_X( id = 0, X.tmp = "X.todo", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), sleep_step = 0.1, sleep_init = 0.1, timeout = 360000, trace = function(...) cat(paste0(..., "\n")), clean = TRUE )
id |
unique identifier for this asktell loop (default: "0") |
X.tmp |
temporary "X" values file (default: "X.todo") |
tmp_path |
temporary directory to store X.tmp & Y.tmp (default: 'tempdir()/../asktell.tmp') |
sleep_step |
delay between checking X.tmp and Y.tmp (default: 0.1 sec.) |
sleep_init |
initial delay before checking X.tmp and Y.tmp (default: 0 sec.) |
timeout |
maximum delay before breaking loop if X.tmp or Y.tmp doesn't appear (default: 36000 sec. = 10 min.) . |
trace |
function to display asktell loop status (default : 'cat') |
clean |
should we cleanup temporary files after reading ? (default: TRUE) |
'ask&tell' injection loop to call an external objective function within an inline algorithm (like optim(...)) Main idea: pass 'ask_Y' as objectve function argument of algorithm, which will wait until you call 'tell_Y' in another R process. In this secondary process, you can read what X is called using 'ask_X', and when you know what values returns from the external objective, just call 'tell_Y' to give it.
input value of objective function to compute externally
Y. Richet, discussions with D. Sinoquet. Async IO principle was defined by G. Pujol.
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_Y(x=123) ## In another R session ask_X() # returns 123 tell_Y(y=456) ## Then ask_dY in main R session returns with value '456' ## End(Not run)
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_Y(x=123) ## In another R session ask_X() # returns 123 tell_Y(y=456) ## Then ask_dY in main R session returns with value '456' ## End(Not run)
ask&tell component function to 'ask' objective function evaluation.
ask_Y( x, id = 0, X.tmp = "X.todo", Y.tmp = "Y.done", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), sleep_step = 0.1, sleep_init = 0, timeout = 360000, trace = function(...) cat(paste0(..., "\n")), clean = TRUE, force_cleanup = FALSE )
ask_Y( x, id = 0, X.tmp = "X.todo", Y.tmp = "Y.done", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), sleep_step = 0.1, sleep_init = 0, timeout = 360000, trace = function(...) cat(paste0(..., "\n")), clean = TRUE, force_cleanup = FALSE )
x |
input values of objective function to compute |
id |
unique identifier for this asktell loop (default: "0") |
X.tmp |
temporary "X" values file (default: "X.todo") |
Y.tmp |
temporary "Y" values file (default: "Y.done") |
tmp_path |
temporary directory to store X.tmp & Y.tmp (default: 'tempdir()/../asktell.tmp') |
sleep_step |
delay between checking X.tmp and Y.tmp (default: 0.1 sec.) |
sleep_init |
initial delay before checking X.tmp and Y.tmp (default: 0 sec.) |
timeout |
maximum delay before breaking loop if X.tmp or Y.tmp doesn't appear (default: 36000 sec. = 10 min.) . |
trace |
function to display asktell loop status (default : 'cat') |
clean |
should we cleanup temporary files after reading ? (default: TRUE) |
force_cleanup |
should we cleanup temporary files before writing (possible conflicting asktell calls) ? (default: FALSE) |
'ask&tell' injection loop to call an external objective function within an inline algorithm (like optim(...)) Main idea: pass 'ask_Y' as objectve function argument of algorithm, which will wait until you call 'tell_Y' in another R process. In this secondary process, you can read what X is called using 'ask_X', and when you know what values returns from the external objective, just call 'tell_Y' to give it.
output value of objective function, as given by tell_Y() call in parallel session
Y. Richet, discussions with D. Sinoquet. Async IO principle was defined by G. Pujol.
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_Y(x=123) ## In another R session ask_X() # returns 123 tell_Y(y=456) ## Then ask_Y in main R session returns with value '456' ## End(Not run)
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_Y(x=123) ## In another R session ask_X() # returns 123 tell_Y(y=456) ## Then ask_Y in main R session returns with value '456' ## End(Not run)
Helper function to scale from [0,1] to [min,max]
from01(X, inp)
from01(X, inp)
X |
values to scale |
inp |
list containing 'min' and 'max' values |
X scaled in [inp$min, inp$max]
from01(data.frame(x=matrix(runif(10))),list(x=list(min=10,max=20)))
from01(data.frame(x=matrix(runif(10))),list(x=list(min=10,max=20)))
Dependencies loader, supports many protocols like github:, gitlab:, ... using remotes::instal_... Will create a local '.lib' directory to store packages installed
import(..., lib.loc = NULL, trace = function(...) cat(paste0(..., "\n")))
import(..., lib.loc = NULL, trace = function(...) cat(paste0(..., "\n")))
... |
dependencies/libraries/packages to load |
lib.loc |
use to setup a dedicated libPath directory to install packages |
trace |
display info |
(list of) load status of packages (TRUE/FALSE)
if(interactive()){ import('VGAM') }
if(interactive()){ import('VGAM') }
Parse algorithm string result in R list
list.results(result)
list.results(result)
result |
templated algorithm result string |
list of string parsed: extract XML or JSON content
list.results(paste0( "<HTML name='minimum'>minimum is 0.523431237543406 found at ...</HTML>", "<min> 0.523431237543406 </min>", "<argmin>[ 0.543459029033452,0.173028395040855 ]</argmin>"))
list.results(paste0( "<HTML name='minimum'>minimum is 0.523431237543406 found at ...</HTML>", "<min> 0.523431237543406 </min>", "<argmin>[ 0.543459029033452,0.173028395040855 ]</argmin>"))
Helper function to get $max from 'input' list
max_input(inp)
max_input(inp)
inp |
lst of objects containing 'max' field (as list) |
array of inp$...$max values
max_input(list(x1=list(min=0,max=1),x2=list(min=2,max=3)))
max_input(list(x1=list(min=0,max=1),x2=list(min=2,max=3)))
Helper function to get $min from 'input' list
min_input(inp)
min_input(inp)
inp |
lst of objects containing 'min' field (as list) |
array of inp$...$min values
min_input(list(x1=list(min=0,max=1),x2=list(min=2,max=3)))
min_input(list(x1=list(min=0,max=1),x2=list(min=2,max=3)))
Parse algorithm file and returns its (header) indos and methods
parse.algorithm(file)
parse.algorithm(file)
file |
Template algorithm file to parse |
list of header infos and environment containing methods <constructor>,getInitialDesign,getNextDesign,displayResults
parse.algorithm(system.file("Brent.R", package="templr"))
parse.algorithm(system.file("Brent.R", package="templr"))
Read algorithm file and returns one header info
read.algorithm(file, info = "help")
read.algorithm(file, info = "help")
file |
Template algorithm file to read |
info |
header info to return |
list of header infos
read.algorithm(system.file("Brent.R", package="templr"),"help")
read.algorithm(system.file("Brent.R", package="templr"),"help")
Apply a template algorithm file to an objective function
run.algorithm( algorithm_file, objective_function, input, output = NULL, options = NULL, work_dir = ".", trace = function(...) cat(paste0(..., "\n")), silent = FALSE, save_data = TRUE )
run.algorithm( algorithm_file, objective_function, input, output = NULL, options = NULL, work_dir = ".", trace = function(...) cat(paste0(..., "\n")), silent = FALSE, save_data = TRUE )
algorithm_file |
templated algorithm file |
objective_function |
function to apply algorithm on |
input |
list of input arguments of function (eg. list(x1=list(min=0,max=1),x2=list(min=10,max=20))) |
output |
list of output names |
options |
algorithm options to overload default ones |
work_dir |
working directory to run algorithm. will store output files, images, .. |
trace |
display running info |
silent |
quietness |
save_data |
enable (by default) saving of data (in .Rds) along algorithm iterations. |
algorithm result (and algorithm object & files as attributes)
run.algorithm( system.file("Brent.R", package="templr"), function(x) sin(x)-0.75, list(x=list(min=0,max=pi/2)), work_dir=tempdir() )
run.algorithm( system.file("Brent.R", package="templr"), function(x) sin(x)-0.75, list(x=list(min=0,max=pi/2)), work_dir=tempdir() )
ask&tell component function to 'tell' objective function value to waiting 'ask_Y' call in another R session.
tell_dY( dy, id = 0, dY.tmp = "dY.done", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), trace = function(...) cat(paste0(..., "\n")), force_cleanup = FALSE )
tell_dY( dy, id = 0, dY.tmp = "dY.done", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), trace = function(...) cat(paste0(..., "\n")), force_cleanup = FALSE )
dy |
output value of objective function gradient to return |
id |
unique identifier for this asktell loop (default: "0") |
dY.tmp |
temporary "Y" values file (default: "dY.done") |
tmp_path |
temporary directory to store X.tmp & Y.tmp (default: 'tempdir()/../asktell.tmp') |
trace |
function to display asktell loop status (default : 'cat') |
force_cleanup |
should we cleanup temporary files before writing (possible conflicting asktell calls) ? (default: FALSE) |
'ask&tell' injection loop to call an external objective function within an inline algorithm (like optim(...)) Main idea: pass 'ask_Y' as objectve function argument of algorithm, which will wait until you call 'tell_Y' in another R process. In this secondary process, you can read what X is called using 'ask_X', and when you know what values returns from the external objective, just call 'tell_Y' to give it.
input value of objective function to compute externally
Y. Richet, discussions with D. Sinoquet. Async IO principle was defined by G. Pujol.
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_dY(x=123) ## In another R session ask_dX() # returns c(123, 123.123) tell_dY(dy=c(456,456.123)) ## Then ask_dY in main R session returns with value '1' ## End(Not run)
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_dY(x=123) ## In another R session ask_dX() # returns c(123, 123.123) tell_dY(dy=c(456,456.123)) ## Then ask_dY in main R session returns with value '1' ## End(Not run)
ask&tell component function to 'tell' objective function value to waiting 'ask_Y' call in another R session.
tell_Y( y, id = 0, Y.tmp = "Y.done", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), trace = function(...) cat(paste0(..., "\n")), force_cleanup = FALSE )
tell_Y( y, id = 0, Y.tmp = "Y.done", tmp_path = file.path(tempdir(), "..", "asktell.tmp"), trace = function(...) cat(paste0(..., "\n")), force_cleanup = FALSE )
y |
output value of objective function to return |
id |
unique identifier for this asktell loop (default: "0") |
Y.tmp |
temporary "Y" values file (default: "Y.done") |
tmp_path |
temporary directory to store X.tmp & Y.tmp (default: 'tempdir()/../asktell.tmp') |
trace |
function to display asktell loop status (default : 'cat') |
force_cleanup |
should we cleanup temporary files before writing (possible conflicting asktell calls) ? (default: FALSE) |
'ask&tell' injection loop to call an external objective function within an inline algorithm (like optim(...)) Main idea: pass 'ask_Y' as objectve function argument of algorithm, which will wait until you call 'tell_Y' in another R process. In this secondary process, you can read what X is called using 'ask_X', and when you know what values returns from the external objective, just call 'tell_Y' to give it.
input value of objective function to compute externally
Y. Richet, discussions with D. Sinoquet. Async IO principle was defined by G. Pujol.
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_Y(x=123) ## In another R session ask_X() # returns 123 tell_Y(y=456) ## Then ask_dY in main R session returns with value '456' ## End(Not run)
## Not run: ### Assumes you can use two independent R sessions ## In main R session ask_Y(x=123) ## In another R session ask_X() # returns 123 tell_Y(y=456) ## Then ask_dY in main R session returns with value '456' ## End(Not run)
Helper function to scale from [min,max] to [0,1]
to01(X, inp)
to01(X, inp)
X |
values to scale |
inp |
list containing 'min' and 'max' values |
X scaled in [0,1]
to01(10+10*data.frame(x=matrix(runif(10))),list(x=list(min=10,max=20)))
to01(10+10*data.frame(x=matrix(runif(10))),list(x=list(min=10,max=20)))