# Algorithm Template

This simple algorithm implementation template aims at simplifying cross dissemination of developments.
Not a constraint, it may be taken as a proposal which helps spreading of ideas and ease exchange of GDR members practice.
It is intended to ship good practices for implementation of Computer Experiments algorithms, such as simulation decoupling and testability.

Basics:

• The template is provided for many languages, so I/O objects have to remain in common types (numeric arrays/matrices, strings).
• Four methods/functions are required:
• MyAlgorithm, which allows to initialize the environment, parse options, load dependencies
• getInitialDesign, which returns the first design of experiments
• getNextDesign, which returns the new experiments to perform, or null/empty object if algorithm ends.
• displayResults, which returns a textual/html analysis, possibly including images
• Alongside with these requirements, many functions/methods may be appended
• Extended information may be provided in header commented lines (information, algorithm options, dependencies, …)

## Empty template files

• R
• Matlab
• Python
• Java
• C
• Fortran
• Others: GitHub repository

## Using template files

The template is primarily designed to be human readable.
Using any template file to perform a true design on an [external] simulation code is simple as running in your environment (R, matlab, python, …) the following sequence:

1. setup the algorithm options according to `option:` header line
2. `algorithm = MyAlgorithm(options)` to return the algorithm with its options intiialized
3. `X0 = getInitialDesign(algorithm,d)` to return a first list of experiments, as a matrix of `d` columns (say `X0`)
4. perform simulations to get each response (say `Y0`) for each matrix `X0` line
5. `X1 = getNextDesign(algorithm,X0,Y0)` to return a new list of experiments, as a new matrix of `d` columns (say `X1`)
6. perform simulations to get each response (say `Y1`) for each matrix `X1` line
7. [optional] `displayResults(algorithm,X1,Y1)` to return a temporary analysis of results, as an HTML string
8. `Xj = getNextDesign(algorithm,Xi,Yi)` to return a new list of experiments, as a new matrix of `d` columns (say `Xj`)
9. perform simulations to get each response (say `Yj`) for each matrix `Xj` line
10. `displayResults(algorithm,Xn,Yn)` to return a final analysis of results, as an HTML string

If the simulation code is not an external software, but a function available in the environment, the previous sequence may be run using a simple script:

```MyCode = function(X) { ... return(Y) }
source("MyAlgorithm.R")

options = list(nmax = 100, delta = 0.1, epsilon = 0.01, ...)
algorithm = MyAlgorithm(options)

X0 = getInitialDesign(algorithm, ...)
Y0 = MyCode(X0)
Xi = X0
Yi = Y0

finished = FALSE
while (!finished) {
Xj = getNextDesign(algorithm,Xi,Yi)
if (is.null(Xj) | length(Xj) == 0) {
finished = TRUE
} else {
Yj = MyCode(Xj)
Xi = rbind(Xi,Xj)
Yi = rbind(Yi,Yj)
}
}

print(displayResults(algorithm,Xi,Yi))```

## Some algorithms template files

(See GitHub repository)

• Brent: basic 1D inversion Brent.R
• Morris: screening OAT (based on sensitivity package) Morris.R
• FAST: lightweight Sobol (1st and total order) indices (based on sensitivity package) FAST.R
• EGO: Kriging based optimization (based on Dice* packages) EGO.R
• EGO: Kriging based optimization (based on STK package) @EGO