| Line 70... |
Line 70... |
| 70 |
|
70 |
|
| 71 |
Method \code{"L-BFGS-B"} is that of Byrd \emph{et. al.} (1995) which
|
71 |
Method \code{"L-BFGS-B"} is that of Byrd \emph{et. al.} (1995) which
|
| 72 |
allows \emph{box constraints}, that is each variable can be given a lower
|
72 |
allows \emph{box constraints}, that is each variable can be given a lower
|
| 73 |
and/or upper bound. The initial value must satisfy the constraints.
|
73 |
and/or upper bound. The initial value must satisfy the constraints.
|
| 74 |
This uses a limited-memory modification of the BFGS quasi-Newton
|
74 |
This uses a limited-memory modification of the BFGS quasi-Newton
|
| 75 |
method. If non-trivial bounds are supplied, this method will be
|
75 |
method. If non-trivial bounds are supplied, this method will be
|
| 76 |
selected, with a warning.
|
76 |
selected, with a warning.
|
| 77 |
|
77 |
|
| 78 |
Nocedal and Wright (1999) is a comprehensive reference for the
|
78 |
Nocedal and Wright (1999) is a comprehensive reference for the
|
| 79 |
previous three methods.
|
79 |
previous three methods.
|
| 80 |
|
80 |
|
| Line 305... |
Line 305... |
| 305 |
{ p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) }
|
305 |
{ p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) }
|
| 306 |
## 25-dimensional box constrained
|
306 |
## 25-dimensional box constrained
|
| 307 |
optim(rep(3, 25), flb, NULL, method = "L-BFGS-B",
|
307 |
optim(rep(3, 25), flb, NULL, method = "L-BFGS-B",
|
| 308 |
lower = rep(2, 25), upper = rep(4, 25)) # par[24] is *not* at boundary
|
308 |
lower = rep(2, 25), upper = rep(4, 25)) # par[24] is *not* at boundary
|
| 309 |
|
309 |
|
| - |
|
310 |
|
| 310 |
## "wild" function , global minimum at about -15.81515
|
311 |
## "wild" function , global minimum at about -15.81515
|
| 311 |
fw <- function (x)
|
312 |
fw <- function (x)
|
| 312 |
10*sin(0.3*x)*sin(1.3*x^2) + 0.00001*x^4 + 0.2*x+80
|
313 |
10*sin(0.3*x)*sin(1.3*x^2) + 0.00001*x^4 + 0.2*x+80
|
| 313 |
plot(fw, -50, 50, n = 1000, main = "optim() minimising 'wild function'")
|
314 |
plot(fw, -50, 50, n = 1000, main = "optim() minimising 'wild function'")
|
| 314 |
|
315 |
|
| 315 |
res <- optim(50, fw, method = "SANN",
|
316 |
res <- optim(50, fw, method = "SANN",
|
| 316 |
control = list(maxit = 20000, temp = 20, parscale = 20))
|
317 |
control = list(maxit = 20000, temp = 20, parscale = 20))
|
| 317 |
res
|
318 |
res
|
| 318 |
## Now improve locally {typically only by a small bit}:
|
319 |
## Now improve locally {typically only by a small bit}:
|
| 319 |
(r2 <- optim(res$par, fw, method = "BFGS"))
|
320 |
(r2 <- optim(res$par, fw, method = "BFGS"))
|
| 320 |
points(r2$par, r2$value, pch = 8, col = "red", cex = 2)
|
321 |
points(r2$par, r2$value, pch = 8, col = "red", cex = 2)
|
| 321 |
|
322 |
|
| 322 |
## Combinatorial optimization: Traveling salesman problem
|
323 |
## Combinatorial optimization: Traveling salesman problem
|
| 323 |
library(stats) # normally loaded
|
324 |
library(stats) # normally loaded
|
| 324 |
|
325 |
|
| 325 |
eurodistmat <- as.matrix(eurodist)
|
326 |
eurodistmat <- as.matrix(eurodist)
|