Search code examples
pythonnumpycvxpy

Non-strict inequalities as contstraints in Python's library cvxpy


I am playing with the cvxpy library in order to solve some particular optimisation problem

import cvxpy as cp
import numpy as np

(...)

prob = cp.Problem(
    cp.Minimize(max(M*theta-b)) <= 45,
    [-48 <= theta, theta <= 48])

(Here M and b are certain numpy matrices.)

Interestingly, it screams:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-62-0296c965b1ff> in <module>
      1 prob = cp.Problem(
----> 2     cp.Minimize(max(M*theta-b)) <= 45,
      3     [-10 <= theta, theta <= 10])

~\Anaconda3\lib\site-packages\cvxpy\expressions\expression.py in __gt__(self, other)
    595         """Unsupported.
    596         """
--> 597         raise NotImplementedError("Strict inequalities are not allowed.")

NotImplementedError: Strict inequalities are not allowed.

however, to me, they do not look strict at all...


Solution

  • Same reason as in your earlier question (although things like that are hard to analyze).

    You need to ask cvxpy for it's max function explicitly. This is always required / recommended.

    cp.Minimize(max(M*theta-b))
    

    should be

    cp.Minimize(cp.max(M*theta-b))
    

    You basically have to use only functions from cvxpy, except for the following:

    The CVXPY function sum sums all the entries in a single expression. The built-in Python sum should be used to add together a list of expressions.