Comments (6)
Please provide a full reproducible example. The above is syntactically wrong, and incomplete.
from cvxr.
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
from cvxr.
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!
from cvxr.
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)"))>
from cvxr.
Thanks for your help! Looking forward to the future version of CVXR.
from cvxr.
Fixed in version 0.97.
from cvxr.
Related Issues (20)
- Solving SDP problem with cvx - difference between MATLAB and R solution HOT 1
- Need better handling of GUROBI solver error
- Problem does not follow GDP rules
- Problem does not follow DCP rules when using large number
- Minimize() and Maximize() prompt "illegal operation" HOT 2
- GLPK_MI, feasible solution, error: "the condition has length > 1" HOT 1
- wrong link in homepage HOT 1
- xpress support
- Wrong table about SCS and QP in homepage HOT 7
- Solver specific variables in CVXR::solve
- Please revisit the License file HOT 1
- Applying reduction GLPK_MI appears to be in an infinite loop HOT 3
- Reproducing glmnet results runs into issues with indicator variables and ultrasparse lambdas HOT 3
- Problem with nested optimization functions HOT 2
- Issue installing CVXR
- Inaccurate timing results HOT 1
- How to use CVXR variable in Rcpp? The S4 object cannot multiply an int cause an error in Rcpp. HOT 2
- mac mini m1 silicon --- try the catenary example HOT 1
- Code that previously worked now does not follow DCP rules HOT 1
- Gurobi and TimeLimit HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cvxr.