1+ # help: Random uniform sampling
2+ # tags: uncertainties
3+ # options: sample_size='100'; seed='1'
4+ # input: x=list(min=0,max=1)
5+ # output: y=0.99
6+
7+ # ' constructor and initializer of R session
8+ UniformSampling <- function (opts ) {
9+ uniformsampling = new.env()
10+
11+ uniformsampling $ sample_size <- as.integer(opts $ sample_size )
12+ uniformsampling $ seed <- as.integer(opts $ seed )
13+
14+ return (uniformsampling )
15+ }
16+
17+ # ' first design building. All variables are set in [min,max]
18+ # ' @param input variables description (min/max, properties, ...)
19+ # ' @param output values of interest description
20+ getInitialDesign <- function (algorithm ,input ,output ) {
21+ algorithm $ input = input
22+ algorithm $ output = output
23+ set.seed(algorithm $ seed )
24+ X = matrix (runif(algorithm $ sample_size * length(input )),ncol = length(input ))
25+ names(X ) <- names(input )
26+ return (from01(X ,algorithm $ input ))
27+ }
28+
29+ # ' iterated design building.
30+ # ' @param X data frame of current doe variables
31+ # ' @param Y data frame of current results
32+ # ' @return data frame or matrix of next doe step
33+ getNextDesign <- function (algorithm ,X ,Y ) {
34+ return (NULL )
35+ }
36+
37+ # ' final analysis. Return HTML string
38+ # ' @param X data frame of doe variables
39+ # ' @param Y data frame of results
40+ # ' @return HTML string of analysis
41+ displayResults <- function (algorithm ,X ,Y ) {
42+ Y = Y [,1 ]
43+
44+ algorithm $ files <- paste0(" hist.png" ,sep = " " )
45+ png(file = algorithm $ files ,bg = " transparent" ,height = 600 ,width = 600 )
46+ hist(na.omit(Y ),xlab = algorithm $ output , main = paste(" Histogram of" , algorithm $ output ))
47+ dev.off()
48+
49+ html = paste0(" <HTML name='summary'>mean=" ,mean(Y ,na.rm = TRUE )," <br/>" ,
50+ " standard deviation=" ,sd(Y ,na.rm = TRUE )," <br/>" ,
51+ " median=" ,median(Y ,na.rm = TRUE )," <br/>" ,
52+ " quantile 0.05=" ,quantile(Y ,0.05 ,na.rm = TRUE )," <br/>" ,
53+ " quantile 0.95=" ,quantile(Y ,0.95 ,na.rm = TRUE )," <br/>" ,
54+ " <img src='" , algorithm $ files , " ' width='600' height='600'/></HTML>" )
55+
56+ m = paste(" <mean>" ,mean(Y ,na.rm = TRUE )," </mean>" )
57+ sd = paste(" <sd>" ,sd(Y ,na.rm = TRUE )," </sd>" )
58+ sd = paste(" <median>" ,median(Y ,na.rm = TRUE )," </median>" )
59+ q05 = paste(" <q05>" ,quantile(Y ,0.05 ,na.rm = TRUE )," </q05>" )
60+ q95 = paste(" <q95>" ,quantile(Y ,0.95 ,na.rm = TRUE )," </q95>" )
61+
62+ return (paste(html ,m ,sd ,q05 ,q95 ,collapse = ' ;' ))
63+ }
64+
65+ # ' temporary analysis. Return HTML string
66+ # ' @param X data frame of doe variables
67+ # ' @param Y data frame of results
68+ # ' @returnType String
69+ # ' @return HTML string of analysis
70+ displayResultsTmp <- function (algorithm ,X ,Y ) {
71+ Y = Y [,1 ]
72+
73+ algorithm $ files <- paste0(" hist_" ,nrow(Y )," .png" ,sep = " " )
74+ png(file = algorithm $ files ,bg = " transparent" ,height = 600 ,width = 600 )
75+ hist(na.omit(Y ),xlab = algorithm $ output , main = paste(" Histogram of" , algorithm $ output ))
76+ dev.off()
77+
78+ html = paste0(" <HTML name='hist'><img src='" , algorithm $ files , " ' width='600' height='600'/></HTML>" )
79+
80+ return (html )
81+ }
82+
83+ from01 = function (X , inp ) {
84+ nX = names(X )
85+ for (i in 1 : ncol(X )) {
86+ namei = nX [i ]
87+ X [,i ] = X [,i ] * (inp [[ namei ]]$ max - inp [[ namei ]]$ min ) + inp [[ namei ]]$ min
88+ }
89+ return (X )
90+ }
91+
92+ to01 = function (X , inp ) {
93+ nX = names(X )
94+ for (i in 1 : ncol(X )) {
95+ namei = nX [i ]
96+ X [,i ] = (X [,i ] - inp [[ namei ]]$ min ) / (inp [[ namei ]]$ max - inp [[ namei ]]$ min )
97+ }
98+ return (X )
99+ }
100+
101+ # #############################################################################################
102+ # @test
103+ # f <- function(X) matrix(apply(X,1,function (x) {
104+ # x1 <- x[1] * 15 - 5
105+ # x2 <- x[2] * 15
106+ # (x2 - 5/(4 * pi^2) * (x1^2) + 5/pi * x1 - 6)^2 + 10 * (1 - 1/(8 * pi)) * cos(x1) + 10
107+ # }),ncol=1)
108+ # # f1 = function(x) f(cbind(.5,x))
109+ #
110+ # options = list(sample_size = 100, seed = 1)
111+ # a = UniformSampling(options)
112+ #
113+ # X0 = getInitialDesign(a, input=list(x1=list(min=0,max=1),x2=list(min=0,max=1)), "y")
114+ # Y0 = f(X0)
115+ # # X0 = getInitialDesign(a, input=list(x2=list(min=0,max=1)), NULL)
116+ # # Y0 = f1(X0)
117+ # Xi = X0
118+ # Yi = Y0
119+ #
120+ # finished = FALSE
121+ # while (!finished) {
122+ # Xj = getNextDesign(a,Xi,Yi)
123+ # if (is.null(Xj) | length(Xj) == 0) {
124+ # finished = TRUE
125+ # } else {
126+ # Yj = f1(Xj)
127+ # Xi = rbind(Xi,Xj)
128+ # Yi = rbind(Yi,Yj)
129+ # }
130+ # }
131+ #
132+ # print(displayResults(a,Xi,Yi))
0 commit comments