I am trying to translate cvx code to cvxpy. The major problem I am having is finding something similar to expressions. I used expressions to set values for an entire list of len(n). From my understanding the attributes in a variable in cvx cannot be modified while an expression can. In cvx I would do this by:
n = 100;
init = 10;
cvx begin
variables A(n), B(n), C(n)
expression X(n)
X(1) = init;
for t=2:n
X(t) = X(t - 1) + A(t - 1) + B(t - 1) + C(t - 1)
end
minimize(sum(A) + max(B))
subject to
for t = 1:n
A(t) >= 1;
B(t) <= 1;
C(t) >= 1;
end
cvx end
According to a previous post(How to set cvxpy n-dim variable first value?)there seems to be no equivalent to expressions in cvxpy afaik, so I would need to create that variable to a constraint like so.
import cvxpy as cp
n = 100
init = 10
A = cp.variable(n)
B = cp.variable(n)
C = cp.variable(n)
X = cp.variable(n)
obj = cp.Minimize(sum(A) + max(B))
# TODO automate introduction of variables.
cons = [
X[0] == init,
A[0] >= 1,
B[0] <= 1,
C[0] >= 1
]
cons2 = [
X[t] == X[t - 1] + A[t - 1] + B[t - 1] + C[t - 1],
A[t] >= 1,
B[t] <= 1,
C[t] >= 1
for t in range(1,n)
]
cons.append(cons2)
prob = cp.Problem(obj, cons)
I get this error message: "NotImplementedError: Strict inequalities are not allowed." Apparently cvxpy does not like == in the constraints, but I am not sure how to populate X otherwise. Also, I think I might be off with my list creation for the constraints as well. Thank you for the help.
Alternate cvxpy code:
import cvxpy as cp
n = 100
init = 10
A = cp.variable(n)
B = cp.variable(n)
C = cp.variable(n)
X = cp.variable(n)
obj = cp.Minimize(sum(A) + max(B))
# TODO automate introduction of variables.
cons = [
X[0] == init,
A[0] >= 1,
B[0] <= 1,
C[0] >= 1
]
for t in range(1,n)
cons2 = [
X[t] == X[t - 1] + A[t - 1] + B[t - 1] + C[t - 1],
A[t] >= 1,
B[t] <= 1,
C[t] >= 1
]
cons.append(cons2)
prob = cp.Problem(obj, cons)