Git Product home page Git Product logo

Comments (5)

anqif avatar anqif commented on September 3, 2024

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.

from cvxr.

benubah avatar benubah commented on September 3, 2024

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

from cvxr.

anqif avatar anqif commented on September 3, 2024

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.

from cvxr.

benubah avatar benubah commented on September 3, 2024

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.

from cvxr.

anqif avatar anqif commented on September 3, 2024

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/.

from cvxr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.