practical computer experiments...u5=1.2e-03 u6=3.49e-05 u8=2.4e-02-> cpu #2 1. super-impose a...
TRANSCRIPT
Practical computerexperiments... in R
Yann Richet, Miguel Munoz ZunigaIRSN (French Nuclear Safety and Radiation shielding Institute)
Computer experimentsWhat is a computer experiment ? [worst case]
Try to simulate true physics/biology/... ("mechanistic" simulation)
High resources consuming: CPU, memory
Long run time: > 1 hour
Remote access & platform dependant: unix/Linux, GPU, FPGA
Proprietary software
Dedicated input modeling: syntax, mesh
·
·
·
·
·
·
2/30
Computer experimentsWhat is a computer experiment ? [worst case]
Examples: CFD, particle physics, multiphysics, ...
Try to simulate true physics/biology/... ("mechanistic" simulation)
High resources consuming: CPU, memory
Long run time: > 1 hour
Remote access & platform dependant: unix/Linux, GPU, FPGA
Proprietary software
Dedicated input modeling: syntax, mesh
·
·
·
·
·
·
3/30
Computer experimentsWhat is a computer experiment ? [worst case]
Examples: CFD, particle physics, multiphysics, ...
... and no wrapper from R
Try to simulate true physics/biology/... ("mechanistic" simulation)
High resources consuming: CPU, memory
Long run time: > 1 hour
Remote access & platform dependant: unix/Linux, GPU, FPGA
Proprietary software
Dedicated input modeling: syntax, mesh
·
·
·
·
·
·
4/30
Computer experimentsWhat do we want to do ? [best case]
CRAN Task View: Design of Experiments (DoE) / Experimental designs for
computer experiments
Also available: lot of algorithms optim/optimx, CMA-ES, PSO, uniroot
Extended processing of I/O data: visualization, analysis
·
input sampling (lhs, DiceDesign)
input/output sensitivity analysis (see Stat. Modelling I about
Polynomial Chaos Expansion)
surrogate (Gaussian process: DiceKriging, tgp) based strategies:
optimization (DiceOptim), inversion (KrigInv)
-
-
-
·
·
5/30
How ?
6/30
1. Quick & dirty: "wait for" wrapperBasic idea: "wait for" function to let user to perform experiments:
optim(fn=waitFor, ...)
·
7/30
1. Quick & dirty: "wait for" wrapperBasic idea: "wait for" function to let user to perform experiments:
optim(fn=waitFor, ...)
User have to read & write the file:
·
·
design to perform: X_todo.txt
results obtained: Y_done.txt
-
-
8/30
1. Quick & dirty: "wait for" wrapperalgorithm R session:
optim(fn=waitFor.Y, ...)
·
waitFor.Y <- function(x,sleep.step=1) {
write.csv(x, "X_todo.txt")
while (!file.exists("Y_done.txt")) Sys.sleep(sleep.step)
y = read.csv("Y_done.txt")
file.remove("Y_done.txt")
return(y)
}
9/30
1. Quick & dirty: "wait for" wrappercode IO R session:
waitFor.X()...tell.Y(y=...)...waitFor.X()...tell.Y(y=...)...
·
tell.Y <- function(y) {
write.csv(y, "Y_done.txt")
}
waitFor.X <- function(sleep.step=1) {
while (!file.exists("X_todo.txt")) Sys.sleep(sleep.step)
x = read.csv("X_todo.txt")
file.remove("X_todo.txt")
return(x)
}
10/30
1. Quick & dirty: "wait for" wrapper
I never used this wrapper in production:
algorithm R session:
optim(fn=waitFor.Y, ...)
code IO R session:
waitFor.X()...tell.Y(y=...)...waitFor.X()...tell.Y(y=...)...
·
·
quite painful
not easy to avoid mistakes !
·
·
11/30
1. Quick & dirty: "wait for" wrapper
I never used this wrapper in production:
Let's try something else: ~ MapReduce
algorithm R session:
optim(fn=waitFor.Y, ...)
code IO R session:
waitFor.X()...tell.Y(y=...)...waitFor.X()...tell.Y(y=...)...
·
·
quite painful
not easy to avoid mistakes !
·
·
12/30
2. MapReduce-like function
13/30
2. MapReduce-like function
easy to use from R:
run("code_name","input_data_file",x1=...,x2=....)$y
resources invasive
fail-over
hot plug/unplug computing CPU
·
·
·
14/30
2. MR-like function: Promethee projectCode input/output in text files: "output.txt"
...
reactivity = 0.93098
...
15/30
2. MR-like function: Promethee projectCode input/output in text files: "input.dat"
TOPT.'STMIL'.nom_mil.'U234 ' = 7.11015E-06
TOPT.'STMIL'.nom_mil.'U235 ' = 1.11058E-03
TOPT.'STMIL'.nom_mil.'U236 ' = 3.17988E-05
TOPT.'STMIL'.nom_mil.'U238 ' = 2.20106E-02
TOPT.'STMIL'.nom_mil.'O16 ' = 4.63202E-02
TOPT.'STMIL'.nom_mil.'AL27 ' = 4.17014E-06
TOPT.'STMIL'.nom_mil.'FENAT ' = 9.51403E-06
TOPT.'STMIL'.nom_mil.'SINAT ' = 2.24794E-05
TOPT.'STMIL'.nom_mil.'TEMPERATURE' = 21.
16/30
2. MR-like function: Promethee projectsuper-impose a "parameter" syntax on code input data syntax:1.
TOPT.'STMIL'.nom_mil.'U234 ' = $u4
TOPT.'STMIL'.nom_mil.'U235 ' = $u5
TOPT.'STMIL'.nom_mil.'U236 ' = $u6
TOPT.'STMIL'.nom_mil.'U238 ' = $u8
TOPT.'STMIL'.nom_mil.'O16 ' = $o
TOPT.'STMIL'.nom_mil.'AL27 ' = 4.17014E-06
TOPT.'STMIL'.nom_mil.'FENAT ' = 9.51403E-06
TOPT.'STMIL'.nom_mil.'SINAT ' = 2.24794E-05
TOPT.'STMIL'.nom_mil.'TEMPERATURE' = $temp
17/30
2. MR-like function: Promethee project
u4=7.1E-06
u5=1.1E-03
u6=3.18E-05
u8=2.2E-02
o=4.6E-02
temp=21
u4=7.8E-06
u5=1.2E-03
u6=3.49E-05
u8=2.4E-02
o=5.0E-02
temp=23
super-impose a "parameter" syntax on code input data syntax:1.
compile (search/replace) parametric dataset for given parameters values2.
TOPT.'STMIL'.nom_mil.'U234 ' = 7.1E-06
TOPT.'STMIL'.nom_mil.'U235 ' = 1.1E-03
TOPT.'STMIL'.nom_mil.'U236 ' = 3.18E-05
TOPT.'STMIL'.nom_mil.'U238 ' = 2.2E-02
TOPT.'STMIL'.nom_mil.'O16 ' = 4.6E-02
TOPT.'STMIL'.nom_mil.'AL27 ' = 4.17014E-06
TOPT.'STMIL'.nom_mil.'FENAT ' = 9.51403E-06
TOPT.'STMIL'.nom_mil.'SINAT ' = 2.24794E-05
TOPT.'STMIL'.nom_mil.'TEMPERATURE' = 21
TOPT.'STMIL'.nom_mil.'U234 ' = 7.8E-06
TOPT.'STMIL'.nom_mil.'U235 ' = 1.2E-03
TOPT.'STMIL'.nom_mil.'U236 ' = 3.49E-05
TOPT.'STMIL'.nom_mil.'U238 ' = 2.4E-02
TOPT.'STMIL'.nom_mil.'O16 ' = 5.0E-02
TOPT.'STMIL'.nom_mil.'AL27 ' = 4.17014E-06
TOPT.'STMIL'.nom_mil.'FENAT ' = 9.51403E-06
18/30
2. MR-like function: Promethee project
u4=7.1E-06
u5=1.1E-03
u6=3.18E-05
u8=2.2E-02
o=4.6E-02
temp=21
-> CPU #1
u4=7.8E-06
u5=1.2E-03
u6=3.49E-05
u8=2.4E-02
-> CPU #2
super-impose a "parameter" syntax on code input data syntax:1.
compile (search/replace) parametric dataset for given parameters values2.
send and execute these dataset, wait for end of calculation3.
TOPT.'STMIL'.nom_mil.'U234 ' = 7.1E-06
TOPT.'STMIL'.nom_mil.'U235 ' = 1.1E-03
TOPT.'STMIL'.nom_mil.'U236 ' = 3.18E-05
TOPT.'STMIL'.nom_mil.'U238 ' = 2.2E-02
TOPT.'STMIL'.nom_mil.'O16 ' = 4.6E-02
TOPT.'STMIL'.nom_mil.'AL27 ' = 4.17014E-06
TOPT.'STMIL'.nom_mil.'FENAT ' = 9.51403E-06
TOPT.'STMIL'.nom_mil.'SINAT ' = 2.24794E-05
TOPT.'STMIL'.nom_mil.'TEMPERATURE' = 21
TOPT.'STMIL'.nom_mil.'U234 ' = 7.8E-06
TOPT.'STMIL'.nom_mil.'U235 ' = 1.2E-03
TOPT.'STMIL'.nom_mil.'U236 ' = 3.49E-05
TOPT.'STMIL'.nom_mil.'U238 ' = 2.4E-02
TOPT.'STMIL'.nom_mil.'O16 ' = 5.0E-02
19/30
2. MR-like function: Promethee project
u4=7.1E-06
u5=1.1E-03
u6=3.18E-05
u8=2.2E-02
o=4.6E-02
temp=21
->
CPU
#1
...
->
local
u4=7.8E-06
u5=1.2E-03
u6=3.49E-05
->
CPU
#2
super-impose a "parameter" syntax on code input data syntax:1.
compile (search/replace) parametric dataset for given parameters values2.
send and execute these dataset, wait for end of calculation3.
get everything back and parse results4.
TOPT.'STMIL'.nom_mil.'U234 ' = 7.1E-06
TOPT.'STMIL'.nom_mil.'U235 ' = 1.1E-03
TOPT.'STMIL'.nom_mil.'U236 ' = 3.18E-05
TOPT.'STMIL'.nom_mil.'U238 ' = 2.2E-02
TOPT.'STMIL'.nom_mil.'O16 ' = 4.6E-02
TOPT.'STMIL'.nom_mil.'AL27 ' = 4.17014E-06
TOPT.'STMIL'.nom_mil.'FENAT ' = 9.51403E-06
TOPT.'STMIL'.nom_mil.'SINAT ' = 2.24794E-05
TOPT.'STMIL'.nom_mil.'TEMPERATURE' = 21
...
reactivity = 0.95469
...
TOPT.'STMIL'.nom_mil.'U234 ' = 7.8E-06
TOPT.'STMIL'.nom_mil.'U235 ' = 1.2E-03
...
reactivity = 0.9428620/30
2. MR-like function: Promethee projectsuper-impose a "parameter" syntax on code input data syntax:1.
compile (search/replace) parametric dataset for given parameters values2.
send and execute these dataset, wait for end of calculation3.
get everything back and parse results
Wrap this sequence into an R function (vectorized):
4.
Promethee.run( model="mycode_3.5-0",
input.files"input.dat",
input.design=list(u4=...,u5=...,u6=...), ...
)$reactivity
21/30
2. MR-like function: Promethee projectUnder the hood:
Compatibility:
Free to use (not yet GPL): http://promethee.irsn.org
local dispatcher engine: Java (>1.5)
remote daemons to launch calculations: Java (>1.5)
dedicated network protocol (TCP/UDP, uni/broad/multicast)
wrapping in R: rJava (>0.9)
·
·
·
·
client side / Promethee.run(): R (>2.11)
server side / mycode.exe: Java (>1.5) cluster, cloud, colleagues desktop
·
·
22/30
2. MR-like function: examples
Now, we can:
f <- function(u) {
Promethee.run( model="mycode_3.5-0", input.files"input.dat",
input.design=list(u4=u[,1],u5=u[,2],u6=u[,3]), ...
)$reactivity }
apply an arbitrary design:
f(lhs::maximinLHS(...))
perform a sensitivity analysis:
sensitivity::fast99(model=f,...)
build a metamodel/surrogate:
DiceKriging::km(design=X,response=f(X),...)
·
·
·
23/30
2. MR-like function: examples
Now, we can:
f <- function(u) {
Promethee.run( model="mycode_3.5-0", input.files"input.dat",
input.design=list(u4=u[,1],u5=u[,2],u6=u[,3]), ...
)$reactivity }
try to maximize the reactivity:
optim(fn=f, par=c(7.1E-06,1.1E-03,3.18E-05), ...)
cma_es(fn=f, par=c(7.1E-06,1.1E-03,3.18E-05),
vectorized=TRUE, ...)
search for the pareto front:
mco::nsga2(fn=f, idim=3, odim=2, ...)
·
·
24/30
2. MR-like function: examples
Now, we can:
f.u5 <- function(u5) {
Promethee.run( model="mycode_3.5-0", input.files"input.dat",
input.design=list(u5=u5[,1]), ...
)$reactivity }
try to find a matching input:
uniroot(f=f.u5, ...)
·
25/30
2. MR-like function: limitationsRemote launch overhead (1-2 seconds)
Efficient only if you call the function using vectorize capabilities
Needs some bridge if you use a queueing HPC service (SGE, PBS, LSF)
Needs to port your own existing parser/launcher in Promethee
·
·
·
·
26/30
2. MR-like function: limitations
This solution is acceptable,
but we still need some "standard" for (computer experiments) algorithms:
Remote launch overhead (1-2 seconds)
Efficient only if you call the function using vectorize capabilities
Needs some bridge if you use a queueing HPC service (SGE, PBS, LSF)
Needs to port your own existing parser/launcher in Promethee
·
·
·
·
Easier to integrate in any computer experiments workflow / CAD software
Easier to follow/understand/debug algorithm sequence
Using vectorized functions (if applicable)
·
·
·
27/30
3. Algorithm pattern "ask/tell"
(*) orig. proposed in sensitivity package
Main idea: instead of working on the function,
split the algorithm sequence in iterative tasks (*)
·
[ask] get batch of function evaluations (from the algorithm object)
[tell] update of the algorithm object with new evaluations
-
-
obj <- algorithm(...)
X <- ask(obj)tell(obj,Y) X <- ask(obj)
28/30
3. Algorithm pattern "ask/tell"Main idea: instead of working on the function,
split the algorithm sequence in iterative tasks (*)
Compliant with majority of algorithm sequences (except not-sync ones)
For "uninterrupted" usage, just loop on ask/tell:
·
[ask] get batch of function evaluations (from the algorithm object)
[tell] update of the algorithm object with new evaluations
-
-
·
·
while (!is.null(X)) {
Y = f(X)
tell(obj,Y)
X = ask(obj)
}29/30
In practice (@ IRSN)We achieve real-world computer experiments with R:
or:
The key point is algorithm vectorization
=> GSoC/R 2013 "Handle parallel (vectorized) objective functions ..."
vectorized algorithm & wrap the external code
=> give Promethee.run() as the objective function
·
vectorized algorithm & re-write/split it in ask/tell pattern
=> call the ask/tell from the code UI/CAD
·
30/30