Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

python - Remainder operation in cvxpy

Is there any built in function to compute the remainder of a variable in cvxpy? For instance, the following example code:

m = 3
n = 2
k = 5
A = cp.Variable((m,n),boolean=True)
B = np.ones((1,m))
C = np.ones(n)
constraints = []
objective = cp.Maximize((B@A%2)@C)
prob = cp.Problem(objective, constraints)
optimal_value = prob.solve()

gives error:

Exception has occurred: TypeError
unsupported operand type(s) for %: 'MulExpression' and 'int'

Because of the % operation

question from:https://stackoverflow.com/questions/65874543/remainder-operation-in-cvxpy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The remainder r = a mod n, with a ≥ 0, can be found as:

  a = q*n + r      (assume n is constant) 
  q ∈ {0,1,...}    (integer variable, non-negative}
  r ∈ {0,..,n-1}   (integer variable between 0 and n-1)

This is just a linear constraint. See also: https://en.wikipedia.org/wiki/Modulo_operation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...