File: ox.R

package info (click to toggle)
r-cran-optimx 2022-4.30%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,524 kB
  • sloc: sh: 21; makefile: 5
file content (355 lines) | stat: -rw-r--r-- 9,503 bytes parent folder | download | duplicates (3)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
## ox - interactive example
if(!require("optimx"))stop("this test requires package optimx.")

require(graphics)

fr <- function(x) {   ## Rosenbrock Banana function
    x1 <- x[1]
    x2 <- x[2]
    100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
grr <- function(x) { ## Gradient of 'fr'
    x1 <- x[1]
    x2 <- x[2]
    c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
       200 *      (x2 - x1 * x1))
}
ans1<-optimx(c(-1.2,1), fr)
ans1
tmpin<-readline("cont?")
str(ans1)
tmpin<-readline("cont?")
print(attr(ans1,"details"))
tmpin<-readline("cont?")
coef(ans1)
print(dim(coef(ans1)))
tmpin<-readline("cont?")
summary(ans1)
tmpin<-readline("cont?")
best1<-summary(ans1, sort.order=value)[1, ]
best1
dim(best1)

tmpin<-readline("cont?")

####
##JN Since this is a single method, the details could have wrong structure
ans2<-optimx(c(-1.2,1), fr, grr, method = "BFGS")
ans2
tmpin<-readline("cont?")
str(ans2)
tmpin<-readline("cont?")
det2<-attr(ans2,"details")
##JN cat("dim(det2)\n")
print(dim(det2))
tmpin<-readline("cont?")
coef(ans2)
print(dim(coef(ans2)))
tmpin<-readline("cont?")
ans2d<-as.data.frame(ans2)
print(ans2d)
attributes(ans2d)
tmpin<-readline("cont?")
ans2parval<-ans2[,1:(attr(ans2,"npar")+1)]
print(ans2parval)
class(ans2parval)

tmpin<-readline("cont?")
summary(ans2)
dim(summary(ans2))
tmpin<-readline("cont?")



## The next line will fail if executed because 'hessian = TRUE' no longer allowed
# ans3<-optimx(c(-1.2,1), fr, NULL, method = "BFGS", hessian = TRUE)
ans3<-optimx(c(-1.2,1), fr, NULL, method = "BFGS", hessian = TRUE)
ans3
tmpin<-readline("cont?")
str(ans3)
tmpin<-readline("cont?")
####q

ans4<-optimx(c(-1.2,1), fr, grr, method = "CG",control=list(trace=1))
ans4
tmpin<-readline("cont?")
str(ans4)
tmpin<-readline("cont?")
####

ans5<-optimx(c(-1.2,1), fr, grr, method = "CG", control=list(type=2))
ans5
tmpin<-readline("cont?")
str(ans5)
tmpin<-readline("cont?")
####

ans6<-optimx(c(-1.2,1), fr, grr, method = "L-BFGS-B")
ans6
tmpin<-readline("cont?")
str(ans6)
tmpin<-readline("cont?")
####

flb <- function(x)
    { p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) }
## 25-dimensional box constrained
ans7<- optimx(rep(3, 25), flb, NULL, method = "L-BFGS-B",
      lower=rep(2, 25), upper=rep(4, 25)) # par[24] is *not* at boundary
ans7
tmpin<-readline("cont?")
str(ans7)
tmpin<-readline("cont?")

## "wild" function , global minimum at about -15.81515
## fw <- function (x)
##    10*sin(0.3*x)*sin(1.3*x^2) + 0.00001*x^4 + 0.2*x+80
## plot(fw, -50, 50, n=1000, main = "optim() minimising 'wild function'")

## Suppressed for optimx() ans7 <- optimx(50, fw, method="SANN",
##             control=list(maxit=20000, temp=20, parscale=20))
## ans7a
## Now improve locally {typically only by a small bit}:
## newpar<-unlist(ans7$par) # NOTE: you need to unlist the parameters as optimx() has multiple outputs
##(r2 <- optimx(newpar, fw, method="BFGS"))
##points(r2$par, r2$value, pch = 8, col = "red", cex = 2)

## Show multiple outputs of optimx using all.methods
# genrose function code
genrose.f<- function(x, gs=NULL){ # objective function
## One generalization of the Rosenbrock banana valley function (n parameters)
	n <- length(x)
        if(is.null(gs)) { gs=100.0 }
	fval<-1.0 + sum (gs*(x[1:(n-1)]^2 - x[2:n])^2 + (x[2:n] - 1)^2)
        return(fval)
}

genrose.g <- function(x, gs=NULL){
# vectorized gradient for genrose.f
# Ravi Varadhan 2009-04-03
	n <- length(x)
        if(is.null(gs)) { gs=100.0 }
	gg <- as.vector(rep(0, n))
	tn <- 2:n
	tn1 <- tn - 1
	z1 <- x[tn] - x[tn1]^2
	z2 <- 1 - x[tn]
	gg[tn] <- 2 * (gs * z1 - z2)
	gg[tn1] <- gg[tn1] - 4 * gs * x[tn1] * z1
	return(gg)
}

genrose.h <- function(x, gs=NULL) { ## compute Hessian
   if(is.null(gs)) { gs=100.0 }
	n <- length(x)
	hh<-matrix(rep(0, n*n),n,n)
	for (i in 2:n) {
		z1<-x[i]-x[i-1]*x[i-1]
		z2<-1.0-x[i]
                hh[i,i]<-hh[i,i]+2.0*(gs+1.0)
                hh[i-1,i-1]<-hh[i-1,i-1]-4.0*gs*z1-4.0*gs*x[i-1]*(-2.0*x[i-1])
                hh[i,i-1]<-hh[i,i-1]-4.0*gs*x[i-1]
                hh[i-1,i]<-hh[i-1,i]-4.0*gs*x[i-1]
	}
        return(hh)
}

startx<-4*seq(1:10)/3.
ans8<-optimx(startx,fn=genrose.f,gr=genrose.g, hess=genrose.h, control=list(all.methods=TRUE, save.failures=TRUE, trace=0), gs=10)
ans8
tmpin<-readline("cont?")
str(ans8)
tmpin<-readline("cont?")
ans8[, "gevals"]
tmpin<-readline("cont?")
ans8["spg", ]
tmpin<-readline("cont?")
summary(ans8, par.select = 1:3)
tmpin<-readline("cont?")
summary(ans8, sort.order = value)[1, ] # show best value
tmpin<-readline("cont?")
head(summary(ans8, sort.order = value)) # best few
tmpin<-readline("cont?")
ans8.best<-summary(ans8, sort.order = value)[1,]
print(ans8.best)
attr(ans8.best,"details")
tmpin<-readline("cont?")

## order by value.  Within those values the same to 3 decimals order by fevals.
summary(ans8, sort.order = list(round(value, 3), fevals), par.select = FALSE)

summary(ans8, sort.order = rownames, par.select = FALSE) # order by method name
tmpin<-readline("cont?")

summary(ans8, par.select = FALSE) # same
tmpin<-readline("cont?")
## Test missing methods
ans8missmeth<-optimx(startx,fn=genrose.f,gr=genrose.g, method=c("spg", "Rcgmin"),
     control=list(save.failures=TRUE, trace=0), gs=10)
# There are just 2 methods here
ans8missmeth
tmpin<-readline("cont?")
# ans8missmeth["Rvmmin", ]
# When we try for a method that is not there in the structure
# the rowname (that is, the method) comes back "NA"
ans8missmeth["Rvmmin", ]
cat("Is method Rvmmin missing? ", (row.names(ans8missmeth["Rvmmin",])[[1]] == "NA"),"\n")

tmp<-readline("Getting items in attributes")
abest<-summary(ans8, order=value)[1,]
print(abest)
attributes(abest)
abestdetails<-attr(abest, "details")
abestdetails[2]
abestdetails[3]
abestdetails[,"ngatend"]
abestdetails[,"hev"]



tmpin<-readline("Polyalgorithm")

startx<-4*seq(1:10)/3.

####

## "Polyalgorithm with 200 steps NM followed by up to 75 of ucminf
ans9<-optimx(startx,fn=genrose.f,gr=genrose.g, hess=genrose.h, method=c("Nelder-Mead","ucminf"),
             itnmax=c(200,75), control=list(follow.on=TRUE, save.failures=TRUE,trace=0), gs=10)
ans9
tmpin<-readline("cont? Again, but with trace on")
ans9<-optimx(startx,fn=genrose.f,gr=genrose.g, hess=genrose.h, method=c("Nelder-Mead","ucminf"),
             itnmax=c(200,75), control=list(follow.on=TRUE, save.failures=TRUE,trace=1), gs=10)

####

startx<-4*seq(1:10)/3.
## 200 steps NM is not enough to terminate
ans10<-optimx(startx,fn=genrose.f,gr=genrose.g, method=c("Nelder-Mead"),
             itnmax=c(200), control=list(trace=0, save.failures=FALSE), gs=10)
## The answer should be NULL
ans10
tmpin<-readline("cont?")
str(ans10)
tmpin<-readline("cont?")

####

startx<-4*seq(1:10)/3.
## Try getting hessian but not kkt tests
ans11<-optimx(startx,fn=genrose.f,gr=genrose.g, hessian=TRUE,  
	control=list(all.methods=TRUE, trace=0, save.failures=FALSE, kkt=FALSE), gs=10)
ans11
tmpin<-readline("cont?")
str(ans11)
tmpin<-readline("cont?")

## Method 7 should be Rvmmin
attr(ans11,"details")[7]

####

startx<-4*seq(1:10)/3.
## Use analytic hessian and no KKT tests
ans12<-optimx(startx,fn=genrose.f,gr=genrose.g, hess=genrose.h, 
      control=list(trace=0, save.failures=FALSE, kkt=FALSE), gs=10)
ans12
attr(ans12,"details")
tmpin<-readline("cont?")
str(ans12)
tmpin<-readline("cont?")

#### 

## Maximization test

maxfn<-function(x) {
      	n<-length(x)
	ss<-seq(1,n)
	f<-10-(crossprod(x-ss))^2
	f<-as.numeric(f)
	return(f)
}

x0<-rep(pi,4)
ans.mx<-optimx(x0,maxfn,control=list(maximize=TRUE,all.methods=TRUE,save.failures=TRUE,trace=TRUE))
ans.mx
tmpin<-readline("cont?")
str(ans.mx)
tmpin<-readline("cont?")
best.mx<-summary(ans.mx, sort.order = value)[1, ]
best.mx
## Check that hessian eigenvalues are negative
attr(best.mx,"details")["hev"]
tmpin<-readline("cont?")
attr(best.mx,"details")$nhatend
tmpin<-readline("cont?")

cat("test names on parameters\n")

## Bates model: y = Asym/(1+exp((xmid-t)/scal))
##      = b1 / (1 + exp(xmid/scal)*exp(-t/scal))
##      = b1 / (1 + b2 * exp(-b3*t))
##  where b2 = exp(xmid/scal)
##        b3 = 1/scal        b1 = Asym
##    So Asym = b1,  scal = 1/b3, xmid = log(b2) * scal = log(b2)/b3



hbates.res<-function(x) { 
    if(length(x) != 3) stop("hobbs.res -- parameter vector n!=3")
    y<-c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443, 
         38.558, 50.156, 62.948, 75.995, 91.972)
    t<-1:12
#    if(abs(12*x[3])>50) {
#       res<-rep(Inf,12)
#    } else {
       res<-x[1]/(1+exp((x[2]-t)/x[3])) - y
#    }
}

hbates.f<- function(x){ # # Hobbs weeds problem -- function
#    if (abs(12*x[3]) > 500) { # check computability
#       fbad<-.Machine$double.xmax
#       return(fbad)
#    }
    res<-hbates.res(x)
    f<-sum(res*res)
}

hbates.jac<-function(x){ # Jacobian of Hobbs weeds problem
   jj<-matrix(0.0, 12, 3)
   t<-1:12
    yy<-exp((x[2]-t)/x[3])
    zz<-1.0/(1+yy)
     jj[ ,1] <- zz
     jj[ ,2] <- -x[1]*zz*zz*yy/x[3]
     jj[ ,3] <- x[1]*zz*zz*yy*(x[2]-t)/(x[3]*x[3])
   return(jj)
}

hbates.g<-function(x){ # gradient of Hobbs weeds problem
    # NOT EFFICIENT TO CALL AGAIN
    jj<-hbates.jac(x)
    res<-hbates.res(x)
    gg<-as.vector(2.*t(jj) %*% res)
    return(gg)
}

start<-c(200,10,10)
names(start)<-NULL # just to be safe
# no names
anoname<-optimx(start, hbates.f, hbates.g, control=list(all.methods=TRUE))
print(summary(anoname))

names(start)<-c("Asym", "xmid", "scal")

anames<-optimx(start, hbates.f, hbates.g, control=list(all.methods=TRUE))
print(summary(anames))

tmpin<-readline("cont?")

cat("DONE!")