File: test-optimParallel.R

package info (click to toggle)
r-cran-optimparallel 1.0-2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: sh: 13; makefile: 2
file content (194 lines) | stat: -rw-r--r-- 6,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
## rm(list=ls())
## library("testthat")
## library("optimParallel", lib.loc = "../../../lib/")
context("test-optimParallel")
source("testsetup.R")

x <- rnorm(1000, 2, .4)
f1 <- function(par, x){
#    print(par)
    -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){
    -sum(par)
}
f4 <- function(par){
   # cat("f:", par, "\n")
#    Sys.sleep(1)
    sum(par^2)
}
g4 <- function(par){
# cat("g:", par,"\n")
#    Sys.sleep(1)
    2*par
}

f5 <- function(par, a){
   # cat("f:", par, "\n")
#    Sys.sleep(1)
    sum(par^2)+a
}
g5 <- function(par, a){
   # cat("g:", par,"\n")
#    print(a)
                                        #    Sys.sleep(1)
    2*par
}

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, lower=c(-Inf,0.001),
                      control=list(factr=factr)), verbose=verbose)
    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,
                      lower=c(-Inf,0.001),
                      control=list(factr=factr)), verbose=verbose)
    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,
                      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)),
                 "argument \"y\" is missing, with no default")    
})


test_that("bounds",{
    compareOptim(list(par=c(2), fn=f3, 
                      upper = c(10),
                      control=list(factr=factr)),
                 verbose=verbose)
    compareOptim(list(par=c(2), fn=f3, 
                      upper = c(10),
                      control=list(factr=factr)),
                 parallel=list(forward=TRUE),
                 verbose=verbose)

    compareOptim(list(par=c(2,1), fn=f3, 
                      upper = c(10,15),
                      control=list(factr=factr)),
                 verbose=verbose)
    compareOptim(list(par=c(2,1), fn=f3, 
                      upper = c(10,15),
                      control=list(factr=factr)),
                 parallel=list(forward=TRUE),
                 verbose=verbose)

    compareOptim(list(par=c(12,100), fn=function(x) sum(x),
                      lower=c(14,-21),
                      control=list(factr=factr)), verbose=verbose)
    compareOptim(list(par=c(12,100), fn=function(x) sum(x),
                      lower=c(14,-21),
                      control=list(factr=factr)),
                 parallel=list(forward=TRUE), verbose=verbose)
})


test_that("ndeps",{
    compareOptim(list(par=c(2,1), fn=f1, x=x,
                      lower=c(-Inf,0.001),
                      control=list(factr=factr, ndeps=c(.1,.1))),
                 verbose=verbose)

    ## compareOptim(list(par=c(2,1), fn=f1,
    ##                   lower=c(-Inf,0.001),
    ##                   control=list(factr=factr, ndeps=c(.1,.1))),
    ##              parallel=list(forward=TRUE), tolerance=1e-3,
    ##              verbose=verbose)

    compareOptim(list(par=c(12,100), fn=f1, x=x,
                      lower=c(-Inf,0.001),
                      control=list(factr=factr, ndeps=c(.1,.1))),
                 verbose=verbose)
    ## compareOptim(list(par=c(12,100), fn=f1,
    ##                   lower=c(-Inf,0.001),
    ##                   control=list(factr=factr, ndeps=c(.1,.1))),
    ##              parallel=list(forward=TRUE), tolerance=1e-3,
    ##              verbose=verbose)
})


test_that("fnscale",{
    compareOptim(list(par=c(2,1), fn=f1, x=x,
                      lower=c(-Inf,0.001),
                      control=list(factr=factr, fnscale=1000)), verbose=verbose)
    compareOptim(list(par=c(2,1), fn=f1, x=x,
                      lower=c(-Inf,0.001),
                      control=list(factr=factr, fnscale=1000)),
                 parallel=list(forward=TRUE), tolerance=1e-3,
                 verbose=verbose)
})


test_that("parscale",{
    compareOptim(list(par=c(2,1), fn=f1, x=x,
                      lower=c(-Inf,0.001),
                      control=list(factr=factr,
                                   parscale=c(2,4))),
                 verbose=verbose)
    compareOptim(list(par=c(2,1), fn=f1, x=x,
                      lower=c(-Inf,0.001),
                      control=list(parscale=c(2,4), maxit=1)),
                 parallel=list(forward=TRUE), tolerance=1e-2,
                 verbose=verbose)
    
    compareOptim(list(par=c(2,1), fn=f1, x=x, lower=c(-Inf,0.001),
                      control=list(factr=factr, parscale=c(.2,4))), verbose=verbose)
    ## compareOptim(list(par=c(2,1), fn=f1, lower=c(-Inf,0.001),
    ##                   control=list(factr=factr, parscale=c(.2,4))),
    ##              parallel=list(forward=TRUE), tolerance=1e-3, verbose=verbose)
})

test_that("gradient",{
    compareOptim(list(par=c(2), fn=f4, gr=g4,
                      control=list(factr=factr)),
                 verbose=verbose)
    compareOptim(list(par=c(2,1), fn=f4, gr=g4,
                      control=list(factr=factr)),
                 verbose=verbose)
    compareOptim(list(par=c(3,2,1), fn=f4, gr=g4,
                      control=list(factr=factr)),
                 verbose=verbose)
})


test_that("... args",{
    compareOptim(list(par=c(2), fn=f5, gr=g5,
                      control=list(factr=factr), a=1),
                 verbose=verbose)
    compareOptim(list(par=c(2), fn=f5,
                      control=list(factr=factr), a=1),
                 verbose=verbose)
})