dev:algorithm:basic [Promethee]

Basic [R] algorithm wrapping

The basic [R] designer wrapper is composed of a unique ”.R” [R] script files providing following basic functions:
  • init ← function() {…} used to initialize library dependencies, casting of input parameters, …
  • initDesign ← function(d) {…} returning first design of experiments of d-dimension (normalized in [0,1])
  • nextDesign ← function(X,Y) {…} optionally returning next design of experiments of d-dimension
  • analyseDesign ← function(X,Y) {…} returning HTML string of conclusion of this design
and having a simple header as follows:
  • #help=something where “something” describes quickly the algorithm
  • #type=something where “something” is a generic descriptor of algorithm (such as “optimization”, “uncertainties propagation”, …)
  • #output=outputname where “outputname” gives name to the main output of the algorithm (such as “minimum value” for a minimizer algorithm)
  • #parameters=commalist where “commalist” lists (with separator ”,”) input parameters of the algorithm like “n=10,N=3”
  • #requires=libraries where “libraries” lists (with separator ”,”) library dependencies needed.

Example

A typical .R file should be:
#help=This algorithm is dedicated to test<br/>uhh!
#type=simple doe
#output=a value
#parameters=n=10,N=3
 
# iteration index
i = 0
 
## constructor and initializer of R session
init <- function() {
# load all needed libraries
    library(lhs)
# all parameters are initialy strings, so you have to put as global non-string values
    n <<- as.integer(n)
    N <<- as.double(N)
}
 
## first design building. All variables are set in [0,1]. d is the dimension, or number of variables
## @param d number of variables
initDesign <- function(d) {
    lhs <- maximinLHS(n=n,k=d)
    return(as.matrix(lhs))
}
 
## iterated design building.
## @param X data frame of current doe variables (in [0,1])
## @param Y data frame of current results
## @return data frame or matrix of next doe step
nextDesign <- function(X,Y) {
    if (i>N) return();
    lhs <- maximinLHS(n=n,k=dim(X)[2])
    i <<- i+1
    return(as.matrix(lhs))
}
 
## final analysis. All variables are set in [0,1]. Return HTML string
## @param X data frame of doe variables (in [0,1])
## @param Y data frame of  results
## @return HTML string of analysis
analyseDesign <- function(X,Y) {
    return(paste(sep="<hr/>",paste(collapse="<br/>",capture.output(X)),paste(collapse="<br/>",capture.output(Y))))
}
To write your own .R file, you can just use your favorite R script editor to modify previous sample.

Deployment

In order to test this R script, you can load it in your R environment, and use a test script like the following:
f <- function(x,y) {
    return(x^2*y)
}
 
test <- function(Rscript) {
    source(Rscript)
    nmax = 100
    delta = 0.1
    epsilon = 0.01
 
    init()
 
    d = length(names(formals(f)))
 
    X0 = data.frame(initDesign(d))
    Y0 = as.data.frame(f(X0[,1],X0[,2]))
 
    Xp<-X0
    Yp<-Y0
 
    finished = FALSE
    while (!finished) {
        xnext = nextDesign(Xp,Yp)
        if (length(xnext) == 0) {
            finished = TRUE
            break
        }
 
        Xn = rbind(Xp,as.data.frame(xnext))
        Yn = as.data.frame(f(as.matrix(Xn[,1]),as.matrix(Xn[,2])))
 
        Xp <- Xn
        Yp <- Yn
    }
 
    print(analyseDesign(Xp,Yp))
}
Once tested, the .R file 1) as soon as it is placed in the Promethee install directory, within plugins/doe path.All these features are designed to be as flexible as possible, but in many cases, it does not match need for advanced algorithms for instance. In such cases, it is possible to switch to extended wrapping to provide much more flexible support for user interactions.
1) the prefix (i.e. “mydoe” in “mydoe.R”) will be finally used as an algorithm name in Promethee GUI
© IRSN - All right reserved - Legal information