Commit 200e54b7 authored by Florian Gerber's avatar Florian Gerber
Browse files

v 0.8

parent f46556ce
Package: optimParallel
Type: Package
Title: Parallel Versions of the Gradient-Based optim() Methods
Version: 0.7-2
Date: 2018-05-01
Title: Parallel Version of the L-BFGS-B Optimization Method
Version: 0.8
Date: 2019-02-25
Author: Florian Gerber
Maintainer: Florian Gerber <florian.gerber@math.uzh.ch>
Description: Provides parallel versions of the gradient-based optim() methods. The main function of the package is optimParallel(), which has the same usage and output as optim(). Using optimParallel() can significantly reduce the optimization time.
Description: Provides a parallel version of the L-BFGS-B method of optim(). The main function of the package is optimParallel(), which has the same usage and output as optim(). Using optimParallel() can significantly reduce the optimization time.
License: GPL (>= 2)
URL: https://git.math.uzh.ch/florian.gerber/optimParallel
BugReports: https://git.math.uzh.ch/florian.gerber/optimParallel/issues
Depends: R (>= 3.1), stats, parallel
Suggests: R.rsp, roxygen2, spam, microbenchmark, testthat, ggplot2, numDeriv
Depends: R (>= 3.5), stats, parallel
Suggests: R.rsp, roxygen2, spam, microbenchmark, testthat, ggplot2, numDeriv, lbfgsb3, lbfgsb3c
VignetteBuilder: R.rsp
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
# Generated by roxygen2: do not edit by hand
export(optimParallel)
importFrom(parallel,clusterEvalQ)
importFrom(parallel,clusterExport)
importFrom(parallel,getDefaultCluster)
importFrom(parallel,parLapply)
importFrom(stats,optim)
- version 0.7-1: CRAN release
commit dbc3d37114f3d1acfeea52f58e1693cb31d01eff
- version 0.8:
commit f0a8ed6e658cb481d549fd908d5640bf03abf394
Author: Florian Gerber <florian.gerber@math.uzh.ch>
Date: Mon Apr 30 14:15:58 2018 +0200
Date: Mon Feb 25 18:38:17 2019 +0100
(1) the mechanism to call 'fn' and 'gr' in parallel
was improved to avoid unecessary copies of objects
between processes in the cluster.
(2) the 'BFGS' and 'GC' methods are nolonger supported.
- version 0.7-4: bug fix
commit 813a06269b6e777157fbe529bc62e38fc4d59de4
Author: Florian Gerber <florian.gerber@math.uzh.ch>
Date: Mon Oct 15 22:40:37 2018 -0600
(1) 'fn'. 'gr' can now be functions from other packages
calling compiled code, e.g.,
optimParallel(par=1, fn=dnorm, mean=3)
- version 0.7-3: bug fix
commit 01ebb9016000a7b087c01b190db0b7ab9d4778d6
Author: Florian Gerber <florian.gerber@math.uzh.ch>
Date: Fri Jul 27 22:40:37 2018 -0600
(1) indexing 'par' by names inside 'fn' works now.
e.g. optimParallel(par=c(a=1), fn <- function(x) {x["a"]^2})
(2) using functions and method from loaded packages works now.
'fn' and 'gr' have now .GlobalEnv as parent environment.
(3) the default value for the argument 'method' is now "L-BFGS-B".
(4) the help page ?optimParallel contains a new section 'Notes'
summarizing considerations for the practical use.
- version 0.7-2: CRAN release
commit f46556cee870a5c13145ddfdf5f2e3998ea0b317
Author: Florian Gerber <florian.gerber@math.uzh.ch>
Date: Tue May 1 12:43:35 2018 +0200
\ No newline at end of file
integratArgs <- function(f, args){
form <- formals(f)
if(!is.null(form))
for(i in seq_along(form))
assign(names(form)[i], form[[i]])
if(!is.null(args))
for(i in seq_along(args))
assign(names(args)[i], args[[i]])
ff <- function(){}
body(ff) <- body(f)
if(any(names(form) == "..."))
formals(ff) <- form[names(form) == "..."]
ff
}
getFunctions <- function(f, args, firstArg){
if(is.vector(firstArg))
firstArg <- matrix(data=firstArg)
lapply(1:ncol(firstArg), function(x){
args[[names(formals(f))[1]]] <- firstArg[,x]
integratArgs(f=f, args=args)
})
}
#' @importFrom parallel parLapply
evalParallel <- function(cl, f, args, firstArg){
funlist <- getFunctions(f=f, args=args, firstArg=firstArg)
parallel::parLapply(cl=cl, X=funlist, fun=function(x) x())
}
This diff is collapsed.
.onLoad <- function(libname, pkgname)
.onAttach <- function(libname, pkgname)
{
options(optimParallel.forward=getOption("optimParallel.forward", FALSE))
options(optimParallel.loginfo=getOption("optimParallel.loginfo", FALSE))
......
The R package optimParallel
===========================
The package provides parallel versions of the gradient-based optim methods
"L-BFGS-B", "BFGS", and "CG". If the evaluation of the function fn takes more than 0.05 seconds,
optimParallel can significantly reduce the optimization time. For a p-parameter optimization based
on "L-BFGS-B", the speed increase is about factor 1+2p when no analytic gradient is specified and
The package provides a parallel versions of the L-BFGS-B optim method.
If the evaluation of the function fn takes more than 0.1 seconds,
optimParallel can significantly reduce the optimization time. For a p-parameter optimization,
the speed increase is about factor 1+2p when no analytic gradient is specified and
1+2p processor cores are available.
See the ArXiv e-prints URL http://arxiv.org/abs/1804.11058
also available as package vignette.
also available as package vignette for more information.
R> vignette("optimParallel")
\ No newline at end of file
......@@ -10,12 +10,10 @@
\alias{OptimParallel-Package}
\alias{optimparallel-package}
\alias{optimparallel-Package}
\alias{optimParallel-package}
\title{parallel version of \code{\link[stats]{optim}}}
\title{parallel version of the L-BFGS-B method of \code{\link[stats]{optim}}}
\usage{
optimParallel(par, fn, gr = NULL, ..., method = c("BFGS", "L-BFGS-B", "CG"),
lower = -Inf, upper = Inf, control = list(), hessian = FALSE,
parallel = list())
optimParallel(par, fn, gr = NULL, ..., lower = -Inf, upper = Inf,
control = list(), hessian = FALSE, parallel = list())
}
\arguments{
\item{par}{see the documentation of \code{\link[stats]{optim}}.}
......@@ -25,12 +23,7 @@ optimParallel(par, fn, gr = NULL, ..., method = c("BFGS", "L-BFGS-B", "CG"),
\item{gr}{see the documentation of \code{\link[stats]{optim}}.}
\item{...}{see the documentation of \code{\link[stats]{optim}}.
Note that depending on the chosen cluster type, \code{fn} and \code{gr} have no access to \code{.GlobalEnv}.
Hence, all object required by \code{fn} and \code{gr} have to be added here.}
\item{method}{parallel versions of the gradient-based methods \code{"L-BFGS-B"}, \code{"BFGS"}, and \code{"CG"} of \code{\link[stats]{optim}} are available.
See the documentation of \code{\link[stats]{optim}}.
If another method is specified, the arguments are directly passed to \code{\link[stats]{optim}}.}
All R object required by \code{fn} and \code{gr} have to be added here.}
\item{lower}{see the documentation of \code{\link[stats]{optim}}.}
......@@ -47,23 +40,22 @@ See \code{\link[parallel]{makeCluster}} for more information.
If the argument is not specified or \code{NULL}, the default cluster is used.
See \code{\link[parallel]{setDefaultCluster}} for information on how to set up a default cluster.}
\item{\code{forward}}{ logical vector of length 1. If \code{FALSE} (default when loading the package), a numeric central difference approximation of the gradient defined as
\eqn{(fn(x+\epsilon)-fn(x-\epsilon))/(2\epsilon)} is used, which corresponds to the approximation used in \code{\link[stats]{optim}}.
If \code{TRUE}, a nummeric forward difference approximation of the gradient essentially defined as
\eqn{(fn(x+\epsilon)-fn(x))/\epsilon} is used. This reduces the number of function calls from \eqn{1+2p} to \eqn{1+p} and can be useful, if the number of available cores is smaller than \eqn{1+2p}.}
\eqn{(fn(x+\epsilon)-fn(x-\epsilon))/(2\epsilon)} is used, which corresponds to the gradient approximation used in \code{\link[stats]{optim}}.
If \code{TRUE}, a numeric forward difference approximation of the gradient essentially defined as
\eqn{(fn(x+\epsilon)-fn(x))/\epsilon} is used. This reduces the number of function calls from \eqn{1+2p} to \eqn{1+p} and can be useful if the number of available cores is smaller than \eqn{1+2p} or if the memory limit is reached. Note that the numeric central difference approximation is more accurate than the numeric forward difference approximation.}
\item{\code{loginfo}}{ logical vector of length 1 with default value \code{FALSE} when loading the package. If \code{TRUE},
additional log information containing the evaluated parameters as well as return the values of \code{fn} and \code{gr} is returned.}
additional log information containing the evaluated parameters as well as return values of \code{fn} and \code{gr} is returned.}
}}
}
\value{
Same as \code{\link[stats]{optim}}. See the documentation thereof.\cr
If a gradient-based method is specified and \code{parallel=list(loginfo=TRUE)}, additional log information containing the evaluated parameters as well as
Same as the return value of \code{\link[stats]{optim}}. See the documentation thereof for more information.\cr
If \code{parallel=list(loginfo=TRUE)}, additional log information containing the evaluated parameters as well as
the return values of \code{fn} and \code{gr} is returned.
}
\description{
The function provides parallel versions of the gradient-based \code{\link[stats]{optim}} methods
\code{"L-BFGS-B"}, \code{"BFGS"}, and \code{"CG"}.
If the evaluation of the function \code{fn} takes more than 0.05 seconds, \code{optimParallel} can significantly reduce the optimization time.
For a \eqn{p}-parameter optimization based on \code{"L-BFGS-B"}, the speed increase is about factor \eqn{1+2p} when no analytic gradient is specified and \eqn{1+2p} processor cores are available.
The function provides a parallel version of the L-BFGS-B method of \code{\link[stats]{optim}}.
If the evaluation time of the objective function \code{fn} is more than 0.1 sceconds, \code{optimParallel} can significantly reduce the optimization time.
For a \eqn{p}-parameter optimization the speed increase is about factor \eqn{1+2p} when no analytic gradient is specified and \eqn{1+2p} processor cores are available.
}
\details{
\code{optimParallel} is a wrapper to \code{\link[stats]{optim}} and relies on the lexical scoping mechanism of R
......@@ -71,6 +63,13 @@ and the R package \pkg{parallel} to evaluate \code{fn}
and its (approximate) gradient in parallel.\cr\cr
Some default values of the argument \code{parallel} can be set via\cr\code{options("optimParallel.forward", "optimParallel.loginfo")}.
}
\section{Issues and bug report}{
A list of known issues of \code{optimParallel} can be found at \url{https://git.math.uzh.ch/florian.gerber/optimParallel/issues}.
Please report issues not listed there to\eqn{\,} \email{florian.gerber@math.uzh.ch}. Do not forget to include
an R script reproducing the issue and the output of \code{sessionInfo()}.
}
\examples{
negll <- function(par, x, sleep=0, verbose=TRUE){
if(verbose)
......@@ -83,12 +82,10 @@ set.seed(13); x <- rnorm(1000, 5, 2)
cl <- makeCluster(2) # set the number of processor cores
setDefaultCluster(cl=cl) # set 'cl' as default cluster
optimParallel(par=c(1,1), fn=negll, x=x,
method = "L-BFGS-B", lower=c(-Inf, .0001))
optimParallel(par=c(1,1), fn=negll, x=x, lower=c(-Inf, .0001))
optimParallel(par=c(1,1), fn=negll, x=x,
method = "L-BFGS-B", lower=c(-Inf, .0001),
parallel=list(loginfo=TRUE))
optimParallel(par=c(1,1), fn=negll, x=x, sleep=0, verbose=TRUE,
lower=c(-Inf, .0001), parallel=list(loginfo=TRUE))
setDefaultCluster(cl=NULL); stopCluster(cl)
......@@ -111,14 +108,12 @@ options(optimParallel.loginfo=TRUE)
## stop if change of f(x) is smaller than 0.01
control <- list(factr=.01/.Machine$double.eps)
optimParallel(par=c(1,1), fn=negll, x=x, sleep=.5,
verbose=TRUE, method="L-BFGS-B",
lower=c(-Inf, .0001), control=control)
optimParallel(par=c(1,1), fn=negll, x=x, sleep=.5, verbose=TRUE,
verbose=TRUE, lower=c(-Inf, .0001), control=control)
## each step invokes 5 parallel calls to negll()
optimParallel(par=c(1,1), fn=negll, x=x, sleep=.5,
method ="L-BFGS-B", lower=c(-Inf, .0001),
control=control,
optimParallel(par=c(1,1), fn=negll, x=x, sleep=.5, verbose=TRUE,
lower=c(-Inf, .0001), control=control,
parallel=list(forward=TRUE))
## each step invokes 3 parallel calls to negll()
......
## rm(list=ls())
## library("testthat")
## library("optimParallel", lib.loc = "../../../lib/")
context("test-evalParallel")
source("testsetup.R")
f1 <- function(x){
x
}
f2 <- function(x,y){
sum(10*x, y)
}
f3 <- function(x,y=1){
x+y
}
test_that("evalParallel: f1", {
expect_equal(optimParallel:::evalParallel(cl=cl, f=f1, args=NULL,
firstArg=c(1)),
list(1))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f1, args=NULL,
firstArg=c(1,2)),
list(c(1,2)))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f1, args=NULL,
firstArg=matrix(c(1,2), ncol=2)),
list(1,2))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f1, args=NULL,
firstArg=c(1,2,3,4,5)),
list(c(1,2,3,4,5)))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f1, args=list(a=1),
firstArg=c(1,2,3,4,5)),
list(c(1,2,3,4,5)))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f1, args=list(x=100),
firstArg=c(1,2,3,4,5)),
list(c(1,2,3,4,5)))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f1, args=NULL,
firstArg=matrix(c(1,2,3,4,5), ncol=5)),
list(1,2,3,4,5))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f1, args=list(a=1),
firstArg=matrix(c(1,2,3,4,5), ncol=5)),
list(1,2,3,4,5))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f1, args=list(x=100),
firstArg=matrix(c(1,2,3,4,5), ncol=5)),
list(1,2,3,4,5))
})
test_that("evalParallel: f2", {
expect_equal(optimParallel:::evalParallel(cl=cl, f=f2, args=list(x=1,y=1),
firstArg=matrix(c(1:4), ncol=4)),
list(f2(x=1, y=1),
f2(x=2, y=1),
f2(x=3, y=1),
f2(x=4, y=1)))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f2, args=list(x=1,y=1),
firstArg=t(array(1:4, c(4,3)))),
list(f2(x=c(1,1,1), y=1),
f2(x=c(2,2,2), y=1),
f2(x=c(3,3,3), y=1),
f2(x=c(4,4,4), y=1)))
expect_equal(optimParallel:::evalParallel(cl=cl, f=f2, args=list(y=1,x=1),
firstArg=t(array(1:4, c(4,3)))),
list(f2(x=c(1,1,1), y=1),
f2(x=c(2,2,2), y=1),
f2(x=c(3,3,3), y=1),
f2(x=c(4,4,4), y=1)))
expect_error(optimParallel:::evalParallel(cl=cl, f=f2, args=list(x=1),
firstArg=t(array(1:4, c(4,3)))))
})
test_that("evalParallel: f3 defaults", {
expect_equal(optimParallel:::evalParallel(cl=cl, f=f3, args=list(x=1),
firstArg=1),
list(f3(x=1)))
})
......@@ -5,6 +5,9 @@ source("testsetup.R")
context("test-issues")
control <- structure(list(maxit = 10,
factr = 2.22044604925031e-16),
.Names = c("maxit","factr"))
FN1 <- function(par, sleep){
Sys.sleep(sleep)
......@@ -18,11 +21,51 @@ GR1 <- function(par, sleep){
test_that("optimParallel",{
compareOptim(list(par=c(1,2,3), fn=FN1, gr=GR1, sleep=0,
method = "L-BFGS-B",
control=structure(list(maxit = 10,
factr = 2.22044604925031e-16,
ndeps = 0.001),
.Names = c("maxit","factr", "ndeps"))),
control=control),
verbose=verbose)
})
FN2 <- function(par, sleep){
Sys.sleep(sleep)
par["a"]^2+par["b"]^2
}
GR2 <- function(par, sleep){
Sys.sleep(sleep)
2*c(par["a"],par["b"])
}
FN3 <- function(par, sleep){
Sys.sleep(sleep)
par["a"]^2
}
GR3 <- function(par, sleep){
Sys.sleep(sleep)
2*c(par["a"])
}
test_that("optimParallel - named arguments",{
compareOptim(list(par=c(a=1,b=2), fn=FN2, sleep=0,
control=control),
verbose=verbose)
compareOptim(list(par=c(a=1,b=2), fn=FN2, gr= GR2, sleep=0,
control=control),
verbose=verbose)
compareOptim(list(par=c(a=1), fn=FN3, sleep=0,
control=control),
verbose=verbose)
compareOptim(list(par=c(a=1), fn=FN3, gr= GR3, sleep=0,
control=control),
verbose=verbose)
})
test_that("optimParallel - use compiled code from other packages",{
compareOptim(list(par=c(a=1), fn=dnorm,
control=control),
verbose=verbose)
compareOptim(list(par=c(a=1), fn=dnorm, mean=1,
control=control),
verbose=verbose)
})
......@@ -7,16 +7,14 @@ source("testsetup.R")
verbose <- FALSE
f1 <- function(x){
if(verbose) cat(x, "\n")
# if(verbose) cat(x, "\n")
sum(x)
}
f2 <- function(x){
if(verbose) cat(x, "\n")
if(any(x<0)) stop()
sum(x)
}
f3 <- function(x){
if(verbose) cat(x, "\n")
if(any(x>0)) stop()
sum(x)
}
......@@ -27,27 +25,67 @@ f5 <- function(x){
x[1]^2 + (1-x[2])^2+log(x[3])
}
test_that("basic",{
o1 <- optimParallel:::parallel_fg_generator(f1, cl=cl, args_list=list(verbose=verbose))
o1 <- optimParallel:::FGgenerator(par=c(1), f1, parallel=list(cl=cl))
expect_equal(o1$f(1), 1)
expect_equal(o1$g(1), 1)
o1 <- optimParallel:::FGgenerator(par=c(4,3), f1, parallel=list(cl=cl))
expect_equal(o1$f(c(1,1)), 2)
expect_equal(o1$g(c(1,1)), c(1,1))
o1 <- optimParallel:::FGgenerator(par=1:3, f1, parallel=list(cl=cl))
expect_equal(o1$f(c(1,1,2)), 4)
expect_equal(o1$g(c(1,1,2)), c(1,1,1))
o1 <- optimParallel:::FGgenerator(par=c(1), f1, gr=f1, parallel=list(cl=cl))
expect_equal(o1$f(1), 1)
expect_equal(o1$g(1), 1)
o1 <- optimParallel:::parallel_fg_generator(f1, args_list=list(verbose=verbose))
o1 <- optimParallel:::FGgenerator(par=c(4,3), f1, parallel=list(cl=cl))
expect_equal(o1$f(c(1,1)), 2)
expect_equal(o1$g(c(1,1)), c(1,1))
o1 <- optimParallel:::parallel_fg_generator(f1, args_list=list(verbose=verbose))
o1 <- optimParallel:::FGgenerator(par=1:3, f1, parallel=list(cl=cl))
expect_equal(o1$f(c(1,1,2)), 4)
expect_equal(o1$g(c(1,1,2)), c(1,1,1))
})
test_that("default args",{
ff1 <- function(x, a=1){
sum(x)+a
}
o1 <- optimParallel:::FGgenerator(par=c(1), ff1, parallel=list(cl=cl))
expect_equal(o1$f(1), 2)
expect_equal(o1$g(1), 1)
o1 <- optimParallel:::FGgenerator(par=c(1), ff1, a=10, parallel=list(cl=cl))
expect_equal(o1$f(1), 11)
expect_equal(o1$g(1), 1)
ff2 <- function(x, ...){
sum(x)+list(...)[["a"]]
}
o1 <- optimParallel:::FGgenerator(par=c(1), ff2, a=1, parallel=list(cl=cl))
expect_equal(o1$f(1), 2)
expect_equal(o1$g(1), 1)
o1 <- optimParallel:::FGgenerator(par=c(1), ff2, a=10, parallel=list(cl=cl))
expect_equal(o1$f(1), 11)
expect_equal(o1$g(1), 1)
o1 <- optimParallel:::FGgenerator(par=c(1), dnorm, a=1, parallel=list(cl=cl))
expect_equal(o1$f(2), dnorm(2))
expect_equal(o1$g(2), (dnorm(2+0.001)-dnorm(2-0.001))/0.002)
o1 <- optimParallel:::FGgenerator(par=c(1), dnorm, log=TRUE, parallel=list(cl=cl))
expect_equal(o1$f(2), dnorm(2, log=TRUE))
expect_equal(o1$g(2), (dnorm(2+0.001, log=TRUE)-dnorm(2-0.001, log=TRUE))/0.002)
})
test_that("bounds",{
o2 <- optimParallel:::parallel_fg_generator(f2, args_list=list(verbose=verbose), lower=0)
o2 <- optimParallel:::FGgenerator(1, f2, lower=0, parallel=list(cl=cl))
expect_equal(o2$f(1), 1)
expect_equal(o2$g(1), 1)
expect_equal(o2$f(0), 0)
expect_equal(o2$g(0), 1)
expect_error(o2$g(-1))
o3 <- optimParallel:::parallel_fg_generator(f3, upper=0, args_list=list(verbose=verbose))
o3 <- optimParallel:::FGgenerator(1, f3, upper=0, parallel=list(cl=cl))
expect_equal(o3$f(-1), -1)
expect_equal(o3$g(-1), 1)
expect_equal(o3$f(0), 0)
......@@ -58,12 +96,12 @@ test_that("bounds",{
test_that("derivative",{
o4 <- optimParallel:::parallel_fg_generator(f4)
o4 <- optimParallel:::FGgenerator(1:3, f4, parallel=list(cl=cl))
expect_equal(o4$g(c(1,2,3)), numDeriv::grad(f4, c(1,2,3)),
tolerance=1e-3)
expect_equal(o4$g(c(-1,2,-3.3)), numDeriv::grad(f4, c(-1,2,-3.3)),
tolerance=1e-3)
o4_2 <- optimParallel:::parallel_fg_generator(f4, forward=TRUE)
o4_2 <- optimParallel:::FGgenerator(1:3, f4, forward=TRUE, parallel=list(cl=cl))
expect_equal(o4_2$g(c(1,2,3)), numDeriv::grad(f4, c(1,2,3)),
tolerance=1e-3)
expect_equal(o4_2$g(c(-1,2,-3.3)), numDeriv::grad(f4, c(-1,2,-3.3)),
......@@ -71,15 +109,15 @@ test_that("derivative",{
})
test_that("eps",{
o5 <- optimParallel:::parallel_fg_generator(f5, ndeps=1e-3)
o5 <- optimParallel:::FGgenerator(1:3, f5, ndeps=1e-3, parallel=list(cl=cl))
expect_equal(o5$g(c(5,6,7)), numDeriv::grad(f5, c(5,6,7)),
tolerance=1e-3)
o5_2 <- optimParallel:::parallel_fg_generator(f5, ndeps=c(.01,.05,.001))
o5_2 <- optimParallel:::FGgenerator(1:3, f5, ndeps=c(.01,.05,.001), parallel=list(cl=cl))
expect_equal(o5_2$g(c(5,6,71)),
numDeriv::grad(f5, c(5,6,71),
method="simple"),
tolerance=1e-3)
o5_3 <- optimParallel:::parallel_fg_generator(f5, ndeps=c(.01,.05))
o5_3 <- optimParallel:::FGgenerator(1:3, f5, ndeps=c(.01,.05), parallel=list(cl=cl))
expect_equal(o5_3$g(c(5,6,71)),
numDeriv::grad(f5, c(5,6,71),
method="simple"),
......
......@@ -10,6 +10,7 @@ f1 <- function(par, x){
-sum(dnorm(x, par[1], par[2], log=TRUE))
}
f2 <- function(par, y){
# print(y)
-sum(dnorm(y, par[1], par[2], log=TRUE))
}
f3 <- function(par){
......@@ -42,62 +43,73 @@ f6 <- function(par){
par[1]^2+(1-par[2])^2
}
f7 <- function(x, ...){
dots <- list(...)
if(!identical(dots, list()))
return(sum((x-dots[[1]])^2))
sum(x^2)
}
f8 <- function(zz, x){
# print(par)
-sum(dnorm(x, zz[1], zz[2], log=TRUE))
}
test_that("optimParallel",{
compareOptim(list(par=c(2,1), fn=f1, x=x, method = "L-BFGS-B",
lower=c(-Inf,0.001),
compareOptim(list(par=c(2,1), fn=f1, x=x, lower=c(-Inf,0.001),
control=list(factr=factr)), verbose=verbose)
compareOptim(list(par=c(2,1), fn=f1, x=x, method = "L-BFGS-B",
compareOptim(list(par=c(2,1), fn=f1, x=x,
lower=c(-Inf,0.001),
control=list(factr=factr)),
parallel=list(forward=TRUE),
verbose=verbose, tolerance=1e-2)
compareOptim(list(par=c(12,100), fn=f1, x=x, method = "L-BFGS-B",
compareOptim(list(par=c(12,100), fn=f1, x=x,
lower=c(-Inf,0.001),
control=list(factr=factr)), verbose=verbose)
compareOptim(list(par=c(12,100), fn=f1, x=x, method = "L-BFGS-B",
compareOptim(list(par=c(12,100), fn=f1, x=x,
lower=c(-Inf,0.001),
control=list(factr=factr)),
parallel=list(forward=TRUE), tolerance=1e-2,
verbose=verbose)
compareOptim(list(par=c(12,100), fn=f2, y=x,
method = "L-BFGS-B", lower=c(-Inf,0.001),
lower=c(-Inf,0.001),
control=list(factr=factr)), verbose=verbose)
expect_error(optimParallel(par=c(12,100), fn=f2,
method = "L-BFGS-B",
lower=c(-Inf,0.001),
control=list(factr=factr)))
control=list(factr=factr)),
"argument \"y\" is missing, with no default")
})
test_that("bounds",{
compareOptim(list(par=c(2), fn=f3, method = "L-BFGS-B",
compareOptim(list(par=c(2), fn=f3,