Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in t.default(const.mat) : argument is not a matrix #3

Open
chuongask opened this issue May 12, 2020 · 16 comments
Open

Error in t.default(const.mat) : argument is not a matrix #3

chuongask opened this issue May 12, 2020 · 16 comments

Comments

@chuongask
Copy link

chuongask commented May 12, 2020

Hi All,
I am using lpSolve in R to solve a linear program. Since our problem has a big constraint matrix and so I used a sparse matrix for the constraint of the problem as follows:

K<-30000
n<-7
A<-matrix(round(runif(nK, min=0, max=1),4), K)
f.obj<-c(rep(1,2
K),rep(0,n),0)
f.con <- matrix (0,K+1, 2*K+n+1)

for (j in (2K+1):(2K+n)) {
f.con[K+1, j]<- 1
}

for (i in 1:K) {
f.con[i, 2*(i-1)+1]<- -1
f.con[i, 2*(i-1)+2]<- 1
f.con[i,2K+n+1]<- -1
for (j in 1: n) {
f.con[i, 2
K+j]<-A[i,j]
}
}
f.con<-Matrix(f.con, sparse=TRUE)

f.dir<-c(rep("=",K+1))

rhs<-c(rep(0,K),1)

prod.trans<-lp("min", f.obj, f.con, f.dir, rhs, compute.sens = TRUE)
#LP solution
prod.trans$objval
prod.trans$solution

However, it showed an error "Error in t.default(const.mat) : argument is not a matrix". Could you please show me how I can fix it?
Thank you for your kind help.

@buttrey
Copy link

buttrey commented May 13, 2020

Hi. Unfortunately lp() does not recognize the sparse matrix type Matrix. You can create your own three-column array of constraints, where a value of (i, j, k) says that in the ith constraint, the value of j is k. Then you can pass that into lp() using the denst.cont argument. I hope this helps.

@chuongask
Copy link
Author

chuongask commented May 14, 2020

Hi Buttrey,
Thank you for your comments. Do you know how to transfer a (big) normal matrix (called A) to a three column array as you suggested, or do you have an example to create a such three column array from the normal matrix A? Here, my problem involves the normal matrix A first and then transforms it to a sparse matrix.
Best regards,

@buttrey
Copy link

buttrey commented May 14, 2020

Hi. Sure. The easy way is to make use of the which() command with the arr.ind = T argument. This returns a two-column matrix giving the rows and columns in which particular entries (in this case, non-zero ones) are found. All we need to do to construct our three-column matrix is to add the non-zero entries to that two-column matrix returned from which().

Here’s an example. I’m going to make up a small matrix that’s mostly zeros. This is not a real problem; it’s just an example.

A <- matrix (0, 5, 4)
A[1,1] <- 1; A [1,2] <- 3; A[1,3] <- -2; A[3,1] <- 5; A[4,4] <- 0.1
A
[,1] [,2] [,3] [,4]
[1,] 1 3 -2 0.0
[2,] 0 0 0 0.0
[3,] 5 0 0 0.0
[4,] 0 0 0 0.1
[5,] 0 0 0 0.0

Now let’s create the matrix where each row gives the row number, column number, and value of a non-zero entry. In this case we expect a 5x3 matrix since A has 5 non-zero entries.

cbind (which (A != 0, T), A[A != 0])
row col
[1,] 1 1 1.0
[2,] 3 1 5.0
[3,] 1 2 3.0
[4,] 1 3 -2.0
[5,] 4 4 0.1

Does that help?

@chuongask
Copy link
Author

chuongask commented May 14, 2020

Hi Hi Buttrey,
Thank you for your hint. I followed your approach and put the command like this:
prod.trans<-lp("min", f.objthu, , constr.dirthu, rhs, dense.const = A)
It showed an error: "Error in lp("min", f.objthu, , constr.dirthu, rhs, dense.const = A) :
Dense constraints must be in three columns."
Do you have any commments?
Thank you.
Best

@buttrey
Copy link

buttrey commented May 14, 2020

Hi. I need more information. What is dim(A)? What are the commands you used, exactly? Also, you don't want to pass in both types of constraints. Is constr.dirthu the original set of constraints? Or are those the directions?

@chuongask
Copy link
Author

chuongask commented May 14, 2020

Hi, My command is as follows:
K<-30000
n<-7
A<-matrix(round(runif(nK, min=0, max=1),4), K)
f.obj<-c(rep(1,2K),rep(0,n),0)
f.con <- matrix (0,K+1, 2*K+n+1)

for (j in (2K+1):(2K+n)) {
f.con[K+1, j]<- 1
}

for (i in 1:K) {
f.con[i, 2*(i-1)+1]<- -1
f.con[i, 2*(i-1)+2]<- 1
f.con[i,2K+n+1]<- -1
for (j in 1: n) {
f.con[i, 2K+j]<-A[i,j]
}
}

f.con<-cbind(which(f.con!=0,T), f.con[f.con!=0])

f.dir<-c(rep("=",K+1))

rhs<-c(rep(0,K),1)

prod.trans<-lp("min", f.obj, , f.con, rhs, dense.const =f.con)

@chuongask
Copy link
Author

Hi buttrey,
I added "transpose.constraints = TRUE" into the command pod.trans as prod.trans<-lp("min", f.obj, , f.con, rhs, transpose.constraints = TRUE,dense.const =f.con) and now it works well.
Thank you very much for your kind helps with hints.
Best regards,

@chuongask chuongask reopened this May 14, 2020
@chuongask
Copy link
Author

chuongask commented May 15, 2020

Hi Buttrey,
Can I create a three-column matrix (f.con) as you said above from beginning for the following matrix?
K<-30000
n<-7
A<-matrix(round(runif(nK, min=0, max=1),4), K)
f.obj<-c(rep(1,2K),rep(0,n),0)
f.con <- matrix (0,K+1, 2*K+n+1)

for (j in (2K+1):(2K+n)) {
f.con[K+1, j]<- 1
}

for (i in 1:K) {
f.con[i, 2*(i-1)+1]<- -1
f.con[i, 2*(i-1)+2]<- 1
f.con[i,2K+n+1]<- -1
for (j in 1: n) {
f.con[i, 2K+j]<-A[i,j]
}
}
Thanks.

@buttrey
Copy link

buttrey commented May 15, 2020 via email

@chuongask
Copy link
Author

Hi buttrey,
Thank you very much. As I said yesterday, your formulation works well. But my problem is still unsolved. When I run bigger constraint matrix, it shows an error "cannot load a matrix of 13.Gb". Thus, I would like to know if I can insert a three-column matrix constraint f.con without using newBig as follows:
K<-30000
n<-7
A<-matrix(round(runif(n * K, min=0, max=1),4), K)
f.obj<-c(rep(1,2 * K),rep(0,n),0)
f.con <- matrix (0,K+1, 2*K+n+1) # how to insert a three-column matrix here

for (j in (2 * K+1):(2* K+n)) {
f.con[K+1, j]<- 1
}

for (i in 1:K) {
f.con[i, 2*(i-1)+1]<- -1
f.con[i, 2*(i-1)+2]<- 1
f.con[i,2 * K+n+1]<- -1
for (j in 1: n) {
f.con[i, 2* K+j]<-A[i,j]
}
}
Thanks.

@buttrey
Copy link

buttrey commented May 15, 2020

Hi. Sorry, I don't understand the question. Once you have your f.con (that's the "sparse" matrix") set up, that's when you create your new "dense" matrix using the strategy we've been talking about. You should end up with a three-column matrix that has perhaps 300,000 rows.

Does that work? Or not?

@chuongask
Copy link
Author

Hi Buttrey,
My question is that I could not set up successfully the matrix f.con=matrix (0,K+1, 2K+n+1) before reaching to set up the sparse matrix newBig when increasing bigger K and n. I am wondering if I can set up a sparse three-column matrix (f.con) by insert directly entries: for (j in (2 * K+1):(2 K+n)) {
f.con[K+1, j]<- 1 and so on.
Thanks.

@buttrey
Copy link

buttrey commented May 15, 2020 via email

@chuongask
Copy link
Author

Thanks Buttrey,
I will think about this. It would be highly appreciated if you would know a simple example to insert a dense constraint matrix directly, and please let me know. I have searched online but found nothing.
Best regards,

@buttrey
Copy link

buttrey commented May 15, 2020

Hi. I ran your code above, and produced an f.con matrix that is 30,0001 x 60,008.

Then I ran this line:

newBig <- cbind (which (f.con != 0, T), f.con[f.con != 0])

That created a matrix that is 299,998 x 3.

Now run lp(), but instead of passing in f.con as the "const.mat" argument, pass newBig as the "dense.const" argument. Does that work?

@chuongask
Copy link
Author

Hi buttrey,
Thank you very much.
Yes, your formulation works well for this example as I said earlier but I encountered an issue of loading for a bigger matrix. Now, I know how to overcome this issue by first creating a sparse matrix as follows:
f.con <-as(Matrix(0,K+1, 2*K+n+1),"dgTMatrix")

for (j in (2 * K+1):(2* K+n)) {
f.con[K+1, j]<- 1
}

for (i in 1:K) {
f.con[i, 2*(i-1)+1]<- -1
f.con[i, 2*(i-1)+2]<- 1
f.con[i,2 * K+n+1]<- -1
for (j in 1: n) {
f.con[i, 2* K+j]<-A[i,j]
}
}
And then transforming this sparse matrix into a three-column matrix by
f.con<-summary(Matrix(f.con, sparse = T))
f.con<-as.matrix(f.con)# this is a three-column matrix
So it can be run for bigger K and n.
Best regards

gaborcsardi added a commit that referenced this issue Sep 27, 2022
Following lpSolveAPI.

Original error:
```
> ### Name: lp
> ### Title: Linear and Integer Programming
> ### Aliases: lp
> ### Keywords: optimize
>
> ### ** Examples
>
> #
> # Set up problem: maximize
> #   x1 + 9 x2 +   x3 subject to
> #   x1 + 2 x2 + 3 x3  <= 9
> # 3 x1 + 2 x2 + 2 x3 <= 15
> #
> f.obj <- c(1, 9, 1)
> f.con <- matrix (c(1, 2, 3, 3, 2, 2), nrow=2, byrow=TRUE)
> f.dir <- c("<=", "<=")
> f.rhs <- c(9, 15)
> #
> # Now run.
> #
> lp ("max", f.obj, f.con, f.dir, f.rhs)
lp_presolve.c:193:34: runtime error: applying non-zero offset 16 to null pointer
    #0 0x7f9ffc9213db in presolve_rebuildUndo /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lp_presolve.c:193:34
    #1 0x7f9ffc988821 in postsolve /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lp_presolve.c:5620:5
    #2 0x7f9ffc9fcbff in spx_solve /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lp_simplex.c:2062:9
    #3 0x7f9ffc9fe260 in lin_solve /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lp_simplex.c:2154:12
    #4 0x7f9ffca13511 in lpslink /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lpslink56.c:377:25
    #5 0x74ef88 in do_dotCode /data/gannet/ripley/R/svn/R-devel/src/main/dotcode.c
    #6 0x83f0a5 in bcEval /data/gannet/ripley/R/svn/R-devel/src/main/eval.c:7126:14
    #7 0x829ade in Rf_eval /data/gannet/ripley/R/svn/R-devel/src/main/eval.c:748:8
    #8 0x891fd3 in R_execClosure /data/gannet/ripley/R/svn/R-devel/src/main/eval.c
    #9 0x88dc7f in Rf_applyClosure /data/gannet/ripley/R/svn/R-devel/src/main/eval.c:1844:16
    #10 0x82a518 in Rf_eval /data/gannet/ripley/R/svn/R-devel/src/main/eval.c:871:12
    #11 0x95ade6 in Rf_ReplIteration /data/gannet/ripley/R/svn/R-devel/src/main/main.c:264:2
    #12 0x95e340 in R_ReplConsole /data/gannet/ripley/R/svn/R-devel/src/main/main.c:316:11
    #13 0x95e149 in run_Rmainloop /data/gannet/ripley/R/svn/R-devel/src/main/main.c:1194:5
    #14 0x95e482 in Rf_mainloop /data/gannet/ripley/R/svn/R-devel/src/main/main.c:1201:5
    #15 0x4f30ba in main /data/gannet/ripley/R/svn/R-devel/src/main/Rmain.c:29:5
    #16 0x7fa00c1cab74 in __libc_start_main (/lib64/libc.so.6+0x27b74) (BuildId: 08df60634339b221bb854d4e10b7278cafde70c4)
    #17 0x43231d in _start (/data/gannet/ripley/R/R-clang-SAN/bin/exec/R+0x43231d)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior lp_presolve.c:193:34 in
Success: the objective function is 40.5
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants