Reputation: 151
Please note that I am not very familiar with R and I am trying to solve the following optimization (minimization). Any inputs would be greatly appreciated. The issue seems to be with the initial values; am unsure how to pick valid initial values. Though it seems to satisfy the criteria given in the documentation.
thetaImp = 5*(10^-5);
eps1 = -0.23;
eps2 = 0.31;
minFunc <- function(x) {
x1 <- x[1];
x2 <- x[2];
-1*(max(thetaImp*x1+eps1,0)*(x1) + max(thetaImp*x2+eps2,0)*(x1+x2))
}
ui = rbind(c(1,1), c(1,0), c(0,1));
ci = c(10000,0,0);
initValues = c(5000,5000);
constrOptim(initValues, minFunc, NULL, ui, ci);
ui %*% initValues - ci
Please note that this is also posted on the statistics website with the full description of the problem. The above is only a sample.
Upvotes: 0
Views: 195
Reputation: 84699
For optimization with an equality constraint, there is the Rsolnp
package.
library(Rsolnp)
thetaImp = 5*(10^-5);
eps1 = -0.23;
eps2 = 0.31;
W = 10000
f <- function(x) { # function to minimize
x1 <- x[1];
x2 <- x[2];
max(thetaImp*x1+eps1,0)*x1 + max(thetaImp*x2+eps2,0)*W
}
eqfun <- function(x){ # function defining the equality
x[1] + x[2]
}
solnp(c(5000, 5000), # starting values
f,
eqfun = eqfun,
eqB = W, # the equality constraint
LB=c(0,0) # lower bounds
)
Output:
Iter: 1 fn: 5435.5000 Pars: 7300.06425 2699.93575
Iter: 2 fn: 5435.5000 Pars: 7300.06425 2699.93575
solnp--> Completed in 2 iterations
$pars
[1] 7300.064 2699.936
......
In this case K=2
, we can equivalently solve the problem with an unidimensional optimization, to check:
g <- function(t) { # function to minimize
x1 <- t
x2 <- W-t;
max(thetaImp*x1+eps1,0)*x1 + max(thetaImp*x2+eps2,0)*W
}
optim(5000, g, method="L-BFGS-B", lower=0, upper=W)
> optim(5000, g, lower=0, upper=W)
$par
[1] 7300
......
We get almost the same result.
Upvotes: 1