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

Constraint Problem #18

Closed
benubah opened this issue Jan 10, 2018 · 5 comments
Closed

Constraint Problem #18

benubah opened this issue Jan 10, 2018 · 5 comments

Comments

@benubah
Copy link

benubah commented Jan 10, 2018

Hi,
I am porting an example of CVXPY using CVXR in R found here: https://groups.google.com/forum/#!topic/cvxpy/5hBSB9KVbuI

The Python Code:

import numpy as np
np.random.seed(1)
n = 8
m = 2
T = 50
alpha = 0.2
beta = 5
A = np.eye(n) + alpha*np.random.randn(n,n)
B = np.random.randn(n,m)
x_0 = beta*np.random.randn(n,1)

from cvxpy import *
x = Variable(n, T+1)
u = Variable(m, T)

states = []
for t in range(T):
    cost = sum_squares(x[:,t+1]) + sum_squares(u[:,t])
    constr = [x[:,t+1] == A*x[:,t] + B*u[:,t],
              norm(u[:,t], 'inf') <= 1]
    states.append( Problem(Minimize(cost), constr) )
# sums problem objectives and concatenates constraints.
prob = sum(states)
prob.constraints += [x[:,T] == 0, x[:,0] == x_0]
prob.solve()

This the R code:

n = 8
m = 2
T1 = 50
alpha = 0.2
beta = 5
A = pracma::eye(n) + alpha*pracma::randn(n,n)
B = pracma::randn(n,m)
x_0 = beta*pracma::randn(n,1)
x = Variable(n, T1+1)
u = Variable(m, T1)

states = c()
for (t in 1:T1) {
  cost = sum_squares(x[,t+1]) + sum_squares(u[,t])
constr = list(x[, t+1] == A*x[, t] + B*u[, t], pracma::Norm(u[,t], Inf) <= 1)
states = append(states, Problem(Minimize(cost), constr) )
}
prob = sum(states)
prob.constraints += [x[ ,T1] == 0, x[ ,0] == x_0]
prob.solve()

I am getting the following error:

Error in sum_shapes(lapply(object@args, function(arg) { :
Incompatible dimensions

I am suspecting it is from the following statement in the for loop because taking that line out shows a different case:

constr = list(x[, t+1] == Ax[, t] + Bu[, t], pracma::Norm(u[,t], Inf) <= 1)

Please could you look into this and suggest what could be possibly the cause for this error.

Thank you.

@anqif
Copy link
Collaborator

anqif commented Jan 10, 2018

You need to use R's semantics for matrix multiplication, i.e. A %% x[,t] + B %% u[,t]. The operator * refers to elementwise multiplication, which requires the operands to have the same dimensions, hence the error.

@benubah
Copy link
Author

benubah commented Jan 10, 2018

Thank you for your quick response.
I had tried that out before now and it usually shows another error:
Error: is.numeric(x) || is.complex(x) is not TRUE

To be sure where the problem lies, I had to comment out the remaining part of the program after the for loop.

Thanks

@anqif
Copy link
Collaborator

anqif commented Jan 10, 2018

CVXR does not support the combination of pracma library functions with CVXR objects such as your variables, x and u. You should use R's built-in eye, rnorm in place of randn, and CVXR's norm_inf.

@benubah
Copy link
Author

benubah commented Jan 10, 2018

Hi,

thank you!
Just using norm_inf() solves the error.
However, another error (Error in min(constant) : invalid 'type' (list) of argument) is shown by the following line:
prob = sum_entries(states)

The updated R code is:

#np.random.seed(1)
set.seed(1)
n = 8
m = 2
T1 = 50
alpha = 0.2
beta = 5
A = diag(n) + alpha*replicate(n, rnorm(n))
B = replicate(m, rnorm(n))
x_0 = beta*replicate(1, rnorm(n))
x = Variable(n, T1+1)
u = Variable(m, T1)
states = c()
for (t in 1:T1) {
  cost = sum_squares(x[,t+1]) + sum_squares(u[,t])
constr = list(x[, t+1] == A%*%x[, t] + B%*%u[, t],
         norm_inf(u[,t]) <= 1)
states = c(states, Problem(Minimize(cost), constr) )
}
# sums problem objectives and concatenates constraints.
prob = CVXR::sum_entries(states)
prob.constraints += list(x[ ,T1] == 0, x[ ,0] == x_0)
prob.solve()

How could the second-to-last line (concatenating constraints) be represented using CVXR:
prob.constraints += list(x[ ,T1] == 0, x[ ,0] == x_0)

Thank you very much for your kind assistance.

@anqif
Copy link
Collaborator

anqif commented Jan 10, 2018

You cannot just copy the Python code, you need to understand what it is doing and rewrite it using R's syntax. For instance, you want to sum the problems, which are contained in the list states, so you must use R's Reduce function, i.e. Reduce("+", states). The next line concatenates a new constraint to prob's list of constraints, which is done using c().

I'm closing this thread since none of these are bugs with CVXR. You should work through the examples at http://cvxr.rbind.io/.

@anqif anqif closed this as completed Jan 10, 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