Dear,
I am using the solnp command (package Rsolnp) for a problem with
equality and inequality constraints.
I am not getting convergence for my problem but apart that I get 1
warning per iteration saying: ?In cbind(temp, funv) : number of rows
of result is not a multiple of vector length (arg 1)?.
I checked for equality and inequality functions and they seem fine to
me. Where this message comes from?
Interestingly also is that using different seed values the direction
of optimization of my (negative-value) function is always for
maximization. How can this occur?
Below you may find the code for ease of consultation.
Thanks in advance,
My best regards,
Diogo André Alagador
Univ. Évora, Portugal
## the code:
###################################################################
###################################################################
## DATA
N=7
BR=12
br=c(1,1,2,3,2,1,1,2,1,1,1,1)
A=c(1,0,0,0,1,0,0,0,0)
B=c(0,0,0,0,1,0,0,0,0)
C=c(1,1,0,1,0,0,1,0,0)
D=c(0,1,0,0,1,0,0,0,0)
E=c(0,0,1,0,0,0,0,0,0)
F=c(0,0,1,0,1,0,1,0,0)
G=c(0,1,1,0,0,0,0,0,0)
H=c(2,1,0,1,2,0,1,0,0)
I=c(0,2,3,0,2,0,1,0,0)
J=c(0,1,3,0,1,0,1,0,0)
K=c(0,1,2,0,1,0,1,0,0)
L=c(1,0,0,0,2,0,0,0,0)
mat=rbind(A,B,C,D,E,F,G,H,I,J,K,L)
CELL=dim(mat)[2]
###################################################################
###################################################################
## MAX FUNCTION
budget=2
library(Rsolnp)
# the objective function to maximize
#NOTE: by default the solver minimizes the objective. therefore the
original minus sign is not used
obj=function(x){
all1=sum(mat%*%x)
Hp=0
for (j in 1:BR){
p_b=mat[j,]%*%x/all1
Hp=Hp+p_b*log(p_b)*br[j]
}
return(Hp)
}
# the equality constraint function:
equal1=function(x){
all2=sum(mat%*%x)
sum_pterm=sum(mat[1:N,]%*%x/all2)
sum_x=sum(x)
return(c(sum_pterm,sum_x))
}
# the right hand side for the constraint
eqB=c(1,budget)
# the inequality function
inequal1=function(x){
all3=sum(mat%*%x)
p_b=mat[1:BR,]%*%x/all3
return(as.vector(p_b))
}
#the lower limit for inequalities
ineqLB=rep(0.00001,BR)
#the upper limit for the inequalities
ineqUB=rep(1.00001,BR)
# the lower and upper bounds for the variables
LB=rep(0,CELL)
UB=rep(1,CELL)
#the seed for variables
cells0=rep(.3,9)
#solving the problem
sol=solnp(cells0,fun=obj,eqfun=equal1,eqB=eqB,ineqfun
=inequal1,ineqLB=ineqLB,ineqUB=ineqUB,LB=LB,UB=UB,control=list(outer.iter=400))
cells=sol$pars
cells
equal1(cells)
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.