x0 <- c(1, 1)
fn <- function(x) (x[1] - 2) ^ 2 + (x[2] - 1) ^ 2
hin <- function(x) 0.25 * x[1]^2 + x[2] ^ 2 - 1 # hin <= 0
heq <- function(x) x[1] - 2 * x[2] + 1 # heq = 0
gr <- function(x) nl.grad(x, fn)
hinjac <- function(x) nl.jacobian(x, hin)
heqjac <- function(x) nl.jacobian(x, heq)
# with COBYLA
auglag(x0, fn, gr = NULL, hin = hin, heq = heq, deprecatedBehavior = FALSE)
# $par: 0.8228761 0.9114382
# $value: 1.393464
# $iter: 1001
auglag(x0, fn, gr = NULL, hin = hin, heq = heq, localsolver = "SLSQP",
deprecatedBehavior = FALSE)
# $par: 0.8228757 0.9114378
# $value: 1.393465
# $iter 184
## Example from the alabama::auglag help page
## Parameters should be roughly (0, 0, 1) with an objective value of 1.
fn <- function(x) (x[1] + 3 * x[2] + x[3]) ^ 2 + 4 * (x[1] - x[2]) ^ 2
heq <- function(x) x[1] + x[2] + x[3] - 1
# hin restated from alabama example to be <= 0.
hin <- function(x) c(-6 * x[2] - 4 * x[3] + x[1] ^ 3 + 3, -x[1], -x[2], -x[3])
set.seed(12)
auglag(runif(3), fn, hin = hin, heq = heq, localsolver= "lbfgs",
deprecatedBehavior = FALSE)
# $par: 4.861756e-08 4.732373e-08 9.999999e-01
# $value: 1
# $iter: 145
## Powell problem from the Rsolnp::solnp help page
## Parameters should be roughly (-1.7171, 1.5957, 1.8272, -0.7636, -0.7636)
## with an objective value of 0.0539498478.
x0 <- c(-2, 2, 2, -1, -1)
fn1 <- function(x) exp(x[1] * x[2] * x[3] * x[4] * x[5])
eqn1 <-function(x)
c(x[1] * x[1] + x[2] * x[2] + x[3] * x[3] + x[4] * x[4] + x[5] * x[5] - 10,
x[2] * x[3] - 5 * x[4] * x[5],
x[1] * x[1] * x[1] + x[2] * x[2] * x[2] + 1)
auglag(x0, fn1, heq = eqn1, localsolver = "mma", deprecatedBehavior = FALSE)
# $par: -1.7173645 1.5959655 1.8268352 -0.7636185 -0.7636185
# $value: 0.05394987
# $iter: 916
Run the code above in your browser using DataLab