mercredi 19 mai 2021

Generate random numbers using hitandrun package R

I'm trying to generate uniform random numbers using the hitandrun package of R. I use inequalities and equalities contraints. That is my code:

library(hitandrun)

ineq.constr<-mergeConstraints(lowerBoundConstraint(16,1,0),upperBoundConstraint(16,1,1))
for(i in 2:16){
ineq.constr<-mergeConstraints(ineq.constr,
lowerBoundConstraint(16,i,0),upperBoundConstraint(16,i,1))
}
add.constr<- mergeConstraints(
list(constr= c(1,1,1,-1,-1,-1,-1,1,-1,-1,-1,1,1,1,1,-1), dir='<=', rhs=4),
list(constr= c(-1,-1,-1,1,1,1,1,-1,1,1,1,-1,-1,-1,-1,1), dir='<=', rhs=-2))
ineq.constr<-mergeConstraints(ineq.constr,add.constr)

eq.constr<- mergeConstraints(
list(constr= c(1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0), dir='=', rhs=1),
list(constr= c(0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0), dir='=', rhs=1),
list(constr= c(0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0), dir='=', rhs=1),
list(constr= c(0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1), dir='=', rhs=1),
list(constr= c(1,-1,0,0,1,-1,0,0,0,0,0,0,0,0,0,0), dir='=', rhsh=0),
list(constr= c(0,0,1,-1,0,0,1,-1,0,0,0,0,0,0,0,0), dir='=', rhsh=0),
list(constr= c(0,0,0,0,0,0,0,0,1,-1,0,0,1,-1,0,0), dir='=', rhsh=0),
list(constr= c(0,0,0,0,0,0,0,0,0,0,1,-1,0,0,1,-1), dir='=', rhsh=0),
list(constr= c(1,0,-1,0,0,0,0,0,1,0,-1,0,0,0,0,0), dir='=', rhsh=0),
list(constr= c(0,1,0,-1,0,0,0,0,0,1,0,-1,0,0,0,0), dir='=', rhsh=0),
list(constr= c(0,0,0,0,1,0,-1,0,0,0,0,0,1,0,-1,0), dir='=', rhsh=0),
list(constr= c(0,0,0,0,0,1,0,-1,0,0,0,0,0,1,0,-1), dir='=', rhsh=0))
constr<-mergeConstraints(ineq.constr, eq.constr)
samples <- hitandrun(constr, n.samples=2000000)
stopifnot(dim(samples) == c(2000000, 16))
df<-matrix(unlist(samples), nrow=2000000, ncol=16)
df<-data.frame(df)
write.table(df, file="har_samples.txt", row.names=FALSE, col.names=FALSE)

All the constraints seems to be working except for this one (if i remove the following lines nothing changes):

add.constr<- mergeConstraints(
list(constr= c(1,1,1,-1,-1,-1,-1,1,-1,-1,-1,1,1,1,1,-1), dir='<=', rhs=4),
list(constr= c(-1,-1,-1,1,1,1,1,-1,1,1,1,-1,-1,-1,-1,1), dir='<=', rhs=-2))
ineq.constr<-mergeConstraints(ineq.constr,add.constr)

I am sure that the constraints are compatible can someone help?




Aucun commentaire:

Enregistrer un commentaire