Hi All, I am having some real difficulty in trying to carry out constrained optimization. I have had no problems with the Optim() function but when I try to constrain the problem I am getting all sorts of error messages. The model is a simple 2 area, 2 fleet, 1 stock model of a fishery. The code is below. This code runs well but when I try the constrOptim() command with an upper limit of f=6 for each fleet (total across 2 areas), the solver does not run. Does anybody have any suggestions? Thanks. nfleets<-2 nareas<-2 M<-1 M<-array(M,dim=c(nfleets,nareas)) N<-1000 cost<-c(40,40) cost<-array(cost,dim=c(nfleets,nareas)) Price<-2 Price<-array(Price,dim=c(nfleets,nareas)) q<-array(0.1,dim=c(nfleets,nareas)) f<-1 f<-array(f,dim=c(nfleets,nareas)) init.eff<-array(3,dim=c(nfleets,nareas)) OF<-array(c(q*f), dim=c(nfleets, nareas)) Catch<-array(0,dim=c(nfleets, nareas)) obj<-function(f){ f <- array(f, dim=c(nfleets, nareas)) F <- q*f Z <- M+sum(F) S <- exp(-Z) Catch<- N*F/Z*(1-S) Tot.Catch <- sum(Catch) NR<-array(0,dim=c(nfleets,nareas)) NR<-Price*Catch - f*cost d.NR<-array(0,dim=c(nfleets,nareas)) f <- apply(f, 1, sum) d.NR<- N*q/Z*(1-S-F/Z+F/Z*S+F*S)*Price - cost return(sum(d.NR*d.NR)) } #zero.bnd <- rep.int(0, length(f)) #opt.eff <- optim( init.eff, obj, method="L-BFGS-B" ) upper<-c(6,6) lower<-c(0,0) constropt.eff<-constrOptim(init.eff,obj,ui=upper, ci=lower, method="Nelder-Mead") -- View this message in context: http://r.789695.n4.nabble.com/constrained-optimization-help-please-tp4648176.html Sent from the R help mailing list archive at Nabble.com.
On 01-11-2012, at 21:59, hayduke wrote:> Hi All, > I am having some real difficulty in trying to carry out constrained > optimization. I have had no problems with the Optim() function but when IBeing pedantic: I assume you mean optim(...)?> try to constrain the problem I am getting all sorts of error messages. > The model is a simple 2 area, 2 fleet, 1 stock model of a fishery. The code > is below. This code runs well but when I try the constrOptim() command with > an upper limit of f=6 for each fleet (total across 2 areas), the solver does > not run. > Does anybody have any suggestions? Thanks. > nfleets<-2 > nareas<-2 > M<-1 > M<-array(M,dim=c(nfleets,nareas)) > N<-1000 > cost<-c(40,40) > cost<-array(cost,dim=c(nfleets,nareas)) > Price<-2 > Price<-array(Price,dim=c(nfleets,nareas)) > q<-array(0.1,dim=c(nfleets,nareas))Why make it so complicated? q <- 01. will do what you want> f<-1 > f<-array(f,dim=c(nfleets,nareas)) > init.eff<-array(3,dim=c(nfleets,nareas)) > OF<-array(c(q*f), dim=c(nfleets, nareas)) > Catch<-array(0,dim=c(nfleets, nareas)) > > obj<-function(f){ > f <- array(f, dim=c(nfleets, nareas)) > F <- q*for simply F <- .1 * F> Z <- M+sum(F) > S <- exp(-Z) > Catch<- N*F/Z*(1-S) > Tot.Catch <- sum(Catch) > NR<-array(0,dim=c(nfleets,nareas)) > NR<-Price*Catch - f*cost > d.NR<-array(0,dim=c(nfleets,nareas)) > f <- apply(f, 1, sum) > d.NR<- N*q/Z*(1-S-F/Z+F/Z*S+F*S)*Price - cost > return(sum(d.NR*d.NR)) > } > > #zero.bnd <- rep.int(0, length(f)) > #opt.eff <- optim( init.eff, obj, method="L-BFGS-B" ) > > upper<-c(6,6) > lower<-c(0,0) > > constropt.eff<-constrOptim(init.eff,obj,ui=upper, ci=lower, > method="Nelder-Mead") >Your init.eff is an array. It should be a vector as described in the documentation of constrOptim. The argument ui is not the upperlimit but should be a matrix. The documentation of constrOptim clearly states the constraints are of the form ui %*% theta - ci >= 0. This will work: init.eff <- init.eff - 1 B <- rbind(c(1,0,1,0),c(0,1,0,1)) constropt.eff<-constrOptim(as.vector(init.eff),obj,ui=-B, ci=-c(6,6), method="Nelder-Mead") constropt.eff - you need to do init.eff <- init.eff - 1 to get the initial parameter in the interior of the feasible region - as.vector(init.eff) stacks the columns so the order is f[1,1],f[2,1],f[1,2],f[2,2] - If I understand correctly what you want the constraints are f[1,1]+f[1,2] < 6 and f[2,1]+f[2,2] < 6 That is B %*% as.vector(init.eff) -c(6,6) <= 0 To get the constraints in the correct format for constrOptim() you need to multiply by -1. So ui=-B and ci=-c(6,6) Whether the result is correct is for you to decide. In addition: your problem appears to be quadratic with linear constraints. Have a look at function lsei() in package limSolve. Berend
Thank-you Berend. This approach does work. Now I need to constrain the problem so that sum(d.NR) is positive for each fleet. I tried this but with no luck: *nfleets<-2 nareas<-2 M<-1 M<-array(M,dim=c(nfleets,nareas)) N<-1000 cost<-c(40,40) cost<-array(cost,dim=c(nfleets,nareas)) Price<-2 Price<-array(Price,dim=c(nfleets,nareas)) q<-array(0.1,dim=c(nfleets,nareas)) f<-1 f<-array(f,dim=c(nfleets,nareas)) init.eff<-rbind(c(3,3),c(3,3),c(2,2)) #init.eff<-array(3,3,3,3,2,2,dim=c(nfleets,nareas)) OF<-array(c(q*f), dim=c(nfleets, nareas)) Catch<-array(0,dim=c(nfleets, nareas)) f <- array(f, dim=c(nfleets, nareas)) F <- q*f Z <- M+sum(F) S <- exp(-Z) Catch<- N*F/Z*(1-S) Tot.Catch <- sum(Catch) obj<-function(f){ NR<-array(0,dim=c(nfleets,nareas)) NR<-Price*Catch - f*cost d.NR<-array(0,dim=c(nfleets,nareas)) f <- apply(f, 1, sum) d.NR<- N*q/Z*(1-S-F/Z+F/Z*S+F*S)*Price - cost return(sum(d.NR*d.NR)) } init.eff <- init.eff - 1 B <- rbind(c(1,0,1,0,0,0),c(0,1,0,1,0,0),c(0,0,0,0,-sum(N*q/Z*(1-S-F/Z+F/Z*S+F*S)*Price - cost),0),c(0,0,0,0,0,-sum(N*q/Z*(1-S-F/Z+F/Z*S+F*S)*Price - cost))) constropt.eff<-constrOptim(as.vector(init.eff),obj, NULL ,ui=-B, ci=-c(14,14,0,0), method="Nelder-Mead") constropt.eff* Is it possible to constrain my problem in this way? -- View this message in context: http://r.789695.n4.nabble.com/constrained-optimization-help-please-tp4648176p4648526.html Sent from the R help mailing list archive at Nabble.com.