if (FALSE) {
## List all options
snomadr(information=list("help"="-h"))
## Print given option, for example, MESH_SIZE
snomadr(information=list("help"="-h MESH_SIZE"))
## Print the version of NOMAD
snomadr(information=list("version"="-v"))
## Print usage and copyright
snomadr(information=list("info"="-i"))
## This is the example found in
## NOMAD/examples/basic/library/single_obj/basic_lib.cpp
eval.f <- function ( x ) {
f <- c(Inf, Inf, Inf);
n <- length (x);
if ( n == 5 && ( is.double(x) || is.integer(x) ) ) {
f[1] <- x[5];
f[2] <- sum ( (x-1)^2 ) - 25;
f[3] <- 25 - sum ( (x+1)^2 );
}
return ( as.double(f) );
}
## Initial values
x0 <- rep(0.0, 5 )
bbin <-c(1, 1, 1, 1, 1)
## Bounds
lb <- rep(-6.0,5 )
ub <- c(5.0, 6.0, 7.0, 1000000, 100000)
bbout <-c(0, 2, 1)
## Options
opts <-list("MAX_BB_EVAL"=500,
"MIN_MESH_SIZE"=0.001,
"INITIAL_MESH_SIZE"=0.1,
"MIN_POLL_SIZE"=1)
snomadr(eval.f=eval.f,n=5, x0=x0, bbin=bbin, bbout=bbout, lb=lb, ub=ub, opts=opts)
## How to transfer other parameters into eval.f
##
## First example: supply additional arguments in user-defined functions
##
## objective function and gradient in terms of parameters
eval.f.ex1 <- function(x, params) {
return( params[1]*x^2 + params[2]*x + params[3] )
}
## Define parameters that we want to use
params <- c(1,2,3)
## Define initial value of the optimization problem
x0 <- 0
## solve using snomadr
snomadr( n =1,
x0 = x0,
eval.f = eval.f.ex1,
params = params )
##
## Second example: define an environment that contains extra parameters
##
## Objective function and gradient in terms of parameters
## without supplying params as an argument
eval.f.ex2 <- function(x) {
return( params[1]*x^2 + params[2]*x + params[3] )
}
## Define initial value of the optimization problem
x0 <- 0
## Define a new environment that contains params
auxdata <- new.env()
auxdata$params <- c(1,2,3)
## pass The environment that should be used to evaluate functions to snomadr
snomadr(n =1,
x0 = x0,
eval.f = eval.f.ex2,
snomadr.environment = auxdata )
## Solve using algebra
cat( paste( "Minimizing f(x) = ax^2 + bx + c\n" ) )
cat( paste( "Optimal value of control is -b/(2a) = ", -params[2]/(2*params[1]), "\n" ) )
cat( paste( "With value of the objective function f(-b/(2a)) = ",
eval.f.ex1( -params[2]/(2*params[1]), params ), "\n" ) )
## The following example is NOMAD/examples/advanced/multi_start/multi.cpp
## This will call smultinomadRSolve to resolve the problem.
eval.f.ex1 <- function(x, params) {
M<-as.numeric(params$M)
NBC<-as.numeric(params$NBC)
f<-rep(0, M+1)
x<-as.numeric(x)
x1 <- rep(0.0, NBC)
y1 <- rep(0.0, NBC)
x1[1]<-x[1]
x1[2]<-x[2]
y1[3]<-x[3]
x1[4]<-x[4]
y1[4]<-x[5]
epi <- 6
for(i in 5:NBC){
x1[i]<-x[epi]
epi <- epi+1
y1[i]<-x[epi]
epi<-epi+1
}
constraint <- 0.0
ic <- 1
f[ic]<-constraint
ic <- ic+1
constraint <- as.numeric(1.0)
distmax <- as.numeric(0.0)
avg_dist <- as.numeric(0.0)
dist1<-as.numeric(0.0)
for(i in 1:(NBC-1)){
for (j in (i+1):NBC){
dist1 <- as.numeric((x1[i]-x1[j])*(x1[i]-x1[j])+(y1[i]-y1[j])*(y1[i]-y1[j]))
if((dist1 > distmax)) {distmax <- as.numeric(dist1)}
if((dist1[1]) < 1) {constraint <- constraint*sqrt(dist1)}
else if((dist1) > 14) {avg_dist <- avg_dist+sqrt(dist1)}
}
}
if(constraint < 0.9999) constraint <- 1001.0-constraint
else constraint = sqrt(distmax)+avg_dist/(10.0*NBC)
f[2] <- 0.0
f[M+1] <- constraint
return(as.numeric(f) )
}
## Define parameters that we want to use
params<-list()
NBC <- 5
M <- 2
n<-2*NBC-3
params$NBC<-NBC
params$M<-M
x0<-rep(0.1, n)
lb<-rep(0, n)
ub<-rep(4.5, n)
eval.f.ex1(x0, params)
bbout<-c(2, 2, 0)
nmulti=5
bbin<-rep(0, n)
## Define initial value of the optimization problem
## Solve using snomadRSolve
snomadr(n = as.integer(n),
x0 = x0,
eval.f = eval.f.ex1,
bbin = bbin,
bbout = bbout,
lb = lb,
ub = ub,
params = params )
## Solve using smultinomadRSolve, if x0 is provided, x0 will
## be the first initial point, otherwise, the program will
## check best_x.txt, if it exists, it will be read in as
## the first initial point. Other initial points will be
## generated by uniform distribution.
## nmulti represents the number of mads will run.
##
snomadr(n = as.integer(n),
eval.f = eval.f.ex1,
bbin = bbin,
bbout = bbout,
lb = lb,
ub = ub,
nmulti = as.integer(nmulti),
print.output = TRUE,
params = params )
}
Run the code above in your browser using DataLab