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

Incompatible dimensions in building constraints #25

Closed
jywang2016 opened this issue Mar 19, 2018 · 6 comments
Closed

Incompatible dimensions in building constraints #25

jywang2016 opened this issue Mar 19, 2018 · 6 comments

Comments

@jywang2016
Copy link

jywang2016 commented Mar 19, 2018

Hi! Thanks for your (and your team) amazing work in CVXR package. I use this package for some control purposes, and an Error appeared when I try to build constraints about state-space equations.

` library(CVXR)

A <- matrix(c(2,-1,1,0.2),byrow = T,nrow = 2)
B <- matrix(c(1,0),byrow = T,nrow = 2)

N <- 7
Q <- diag(2)
R <- 2
N <- 7

x0 <- matrix(c(3,1),byrow = T)
u <- Variable(rows = 1, cols = N)
x <- Variable(rows = 2, cols = N)
objective <- Minimize(sum(Q %% x) + sum(R %% u))

constraints <- list(u >= -1,
u <= 1 ,
x >= -5,
x <= 5,
x[,1] == x0,
#x[,2:N] == A %% x[,1:(N-1)] + B %% u[,1:(N-1)]
x[,2] == A %% x[,1] + B %% u[,1]) `

Error in mul_shapes(size(object@args[[1]]), size(object@args[[2]])) : Incompatible dimensions.
If I use the second last lines ( #x[,2:N] == A %*% x[,1:(N-1)] + B %*% u[,1:(N-1)] ), the console proceed with an Error like above. I have to use the for loop to complete modeling (add constraints one by one). And the dimensions in the second last lines seem compatible.

By the way, I have another try on the building constraints for the one-dimension state-space equation in the way like x[2:N] == A %*% x[1:(N-1)] + B %*% u[1:(N-1)], it works. Therefore, I am not sure the problem lies in syntax or the matrix multiplication bug.

Thanks for your help!

@bnaras
Copy link
Collaborator

bnaras commented Mar 19, 2018

Please provide a full reproducible example. The above is syntactically wrong, and incomplete.

@bnaras
Copy link
Collaborator

bnaras commented Mar 19, 2018

Here. for example, is what I get after fixing the syntax.

> library(CVXR)
> A <- matrix(c(2,-1,1,0.2),byrow = T,nrow = 2)
> B <- matrix(c(1,0),byrow = T,nrow = 2)
> N <- 7
> Q <- diag(2)
> R <- 2
> N <- 7
> x0 <- matrix(c(3,1),byrow = T)
> u <- Variable(rows = 1, cols = N)
> x <- Variable(rows = 2, cols = N)
> objective <- Minimize(sum(Q %*% x) + sum(R %*% u))
> constraints <- list(u >= -1,
+ u <= 1 ,
+ x >= -5,
+ x <= 5,
+ x[,1] == x0,
+ x[,2] == A %*% x[,1] + B %*% u[,1])
> p <- Problem(objective, constraints)
> res <- solve(p)
res$getValue(u)
res$getValue(x)
>  
    [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]   -1   -1   -1   -1   -1   -1   -1
>  
    [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    3  4.0   -5   -5   -5   -5   -5
[2,]    1  3.2   -5   -5   -5   -5   -5
> res$getValue(x)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    3  4.0   -5   -5   -5   -5   -5
[2,]    1  3.2   -5   -5   -5   -5   -5
> res$getValue(x)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    3  4.0   -5   -5   -5   -5   -5
[2,]    1  3.2   -5   -5   -5   -5   -5

@jywang2016
Copy link
Author

Thanks for your reply. I'm sorry for these mistakes.
In my case, here is the code:

library(CVXR)
A <- matrix(c(2,-1,1,0.2),byrow = T,nrow = 2)
B <- matrix(c(1,0),byrow = T,nrow = 2)
N <- 7
Q <- diag(2)
R <- 2

x0 <- matrix(c(3,1),byrow = T)
u <- Variable(rows = 1, cols = N)
x <- Variable(rows = 2, cols = N)
objective <- Minimize(sum(Q %*% x) + sum(R %*% u))
constraints <- list(u >= -1,
                    u <= 1 ,
                    x >= -5,
                    x <= 5,
                    x[,1] == x0,
                    x[,2] == A %*% x[,1] + B %*% u[,1])

for(i in 3:N)
{
  constraints <- c(constraints, x[,i] == A %*% x[,i-1] + B %*% u[,i-1])
}

p <- Problem(objective, constraints)
res <- solve(p)
res$getValue(u)
res$getValue(x)

The chunk above is reproducible and complete. The point concerned is why can't I just use x[,2:N] == A %*% x[,1:(N-1)] + B %*% u[,1:(N-1)] to build the constraints. If I modify the code as follows:

library(CVXR)
A <- matrix(c(2,-1,1,0.2),byrow = T,nrow = 2)
B <- matrix(c(1,0),byrow = T,nrow = 2)
N <- 7
Q <- diag(2)
R <- 2

x0 <- matrix(c(3,1),byrow = T)
u <- Variable(rows = 1, cols = N)
x <- Variable(rows = 2, cols = N)
objective <- Minimize(sum(Q %*% x) + sum(R %*% u))
constraints <- list(u >= -1,
                    u <= 1 ,
                    x >= -5,
                    x <= 5,
                    x[,1] == x0,
                    x[,2:N] == A %*% x[,1:(N-1)] + B %*% u[,1:(N-1)])

# why can't I just use x[,2:N] == A %*% x[,1:(N-1)] + B %*% u[,1:(N-1)]

p <- Problem(objective, constraints)
res <- solve(p)
res$getValue(u)
res$getValue(x)

The error Error in mul_shapes(size(object@args[[1]]), size(object@args[[2]])) : Incompatible dimensions would appear in the console. I think that dimensions are correct after checking twice. And I want to know where is the mistake.

Thanks for your help!

@bnaras
Copy link
Collaborator

bnaras commented Mar 21, 2018

This appears to be a bug, most likely a dimension being dropped in the indexing (drop=FALSE in R parlance). Here is a simplified example that will make its way into our future tests.

> B <- matrix(c(1,0),byrow = T,nrow = 2)
> N <- 7
> u <- Variable(rows = 1, cols = N)
> B %*% u[, 1:(N-1)]  ## Fails when result should be 2 x 6
Error in mul_shapes(size(object@args[[1]]), size(object@args[[2]])) : 
  Incompatible dimensions
> B <- matrix(c(1,0,0,1),byrow = T,nrow = 2)
> u <- Variable(rows = 2, cols = N)
> B %*% u[, 1:(N-1)]
MulExpression(Constant(CONSTANT, POSITIVE, (2,2)), c("Expression(AFFINE, UNKNOWN, 2)", "Expression(AFFINE, UNKNOWN, 6)"))> 

@jywang2016
Copy link
Author

Thanks for your help! Looking forward to the future version of CVXR.

bnaras added a commit that referenced this issue May 8, 2018
Fix up indexing issue #25
@bnaras
Copy link
Collaborator

bnaras commented May 14, 2018

Fixed in version 0.97.

@bnaras bnaras closed this as completed May 14, 2018
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