4
# .onLoad <- function(libname, pkgname){
5
# verbose <- .Options$Hverbose
6
# if(!length(verbose) || verbose){
7
# cat("Rdonlp2 - a wrapper library for \"DONLP2 (C) Peter Spellucci\"\n\n")
9
# library.dynam("Rdonlp2", pkgname, libname)
14
# ------------------------------------------------------------------------------
20
iterma = 4000, nstep = 20,fnscale = 1,
21
report = FALSE, rep.freq = 1,
22
# perfomance and tunings
23
tau0 = 1.0, tau = 0.1, del0 = 1.0,
24
# termination criteria
25
epsx = 1e-5, delmin = 0.1*del0,
26
epsdif = 1e-8, nreset.multiplier = 1,
27
# numerical differentiation
28
difftype = 3, epsfcn = 1e-16, taubnd = 1.0,
31
te0 = TRUE, te1 = FALSE, te2 = FALSE, te3 = FALSE,
32
silent = FALSE, intakt = TRUE )
38
iterma = as.integer(iterma),
39
nstep = as.integer(nstep),
42
rep.freq = as.integer(ifelse(rep.freq<1, 1, rep.freq)),
49
nreset.multiplier = nreset.multiplier,
50
difftype = as.integer(ifelse(!difftype%in%c(1,2,3), 3, difftype)),
63
# ------------------------------------------------------------------------------
69
par.upper = rep(+Inf, length(par)),
70
par.lower = rep(-Inf, length(par)),
73
lin.upper = rep(+Inf, length(par)),
74
lin.lower = rep(-Inf, length(par)),
77
nlin.upper = rep(+Inf, length(nlin)),
78
nlin.lower = rep(-Inf, length(nlin)),
80
control = rdonlp2Control(),
81
control.fun = function(lst){return(TRUE)},
82
env = .GlobalEnv, name = NULL)
87
# use analytical gradients?
88
if (is.function(attr(fn, "gr")) &
89
all(lapply(nlin, function(e)is.function(attr(e,"gr"))))){
90
control["analyt"] = TRUE
92
control["analyt"] = FALSE
95
# check parameter and its box constraints
96
if (length(par) != length(par.upper) | length(par) != length(par.lower) ){
97
stop("# of elements for box constraints != # of parameters")
100
# check linear constraints matrix A
104
lin.upper <- lin.lower <- c()
107
if (ncol(A) != length(par))
108
stop("# of ncol(A) should be equal to # of par")
109
if (length(lin.upper) != num.lin | length(lin.lower) != num.lin)
110
stop("# of bounds for linear constraints should be equal to nrow(A)")
114
# nonlinear constraints
115
num.nlin <- length(nlin)
116
if (length(nlin.upper)!=num.nlin | length(nlin.lower)!=num.nlin)
117
stop("# of bounds for nonlinear constraints should be equal to length(nlin)")
118
# concatenate bounds for internal use
119
lbd <- c(par.lower, lin.lower, nlin.lower)
120
ubd <- c(par.upper, lin.upper, nlin.upper)
123
# the wrapper for objective and constraint function
124
# (called from eval_extern())
125
# mode == 0: EVAL_FN(evaluate objective and constraint function)
126
# mode == 1: EVAL_GR(evaluate gr of objective and constraint function)
128
# fun.id == 0: evaluate objective function 'fn'
129
# fun.id >= 1: evaluate constraint function 'nlin[[fun.id]]'
132
confun <- function(arg) {
136
if (mode == 0){ # evaluate function values
138
return(as.double(eval(fn(p), env)))
140
return(as.double(eval(nlin[[fun.id]](p), env)))
141
} else if (mode == 1) { # evaluate gradient values
143
return(as.double(eval(fn@gr(p), env)))
145
return(as.double(eval((nlin[[fun.id]]@gr)(p), env)))
147
stop("unknown evaluation mode: %d", mode)
152
accfun <- function(lst){
153
return(as.logical(control.fun(lst)))
165
ans <- .Call("call_donlp2",
168
as.integer(num.nlin),
172
as.double(lbd), as.double(ubd),
176
confun, environment(confun),
177
PACKAGE = "Rdonlp2"),
178
# ensure to free memory and close .mes .pro files if opened
179
finally=.Call("teardown", 0,
180
PACKAGE = "Rdonlp2"))
181
ans$nr.update <- matrix(ans$nr.update, nr = length(par))
182
if (control$hessian) {
183
ans$hessian = matrix(ans$hessian, nr = length(par))
193
################################################################################