Search code examples
constraintscvxpy

in CVXPY: Give a name or ID to constraints


In CVXPY, I have found that I am able to give a name or ID to the variables I define.

Now I would also like to give a string name the constraints I define, so that I can find them easily later. The reason is that I have many agents, which are defined in similar ways, but I would like to know which of the constraints belong to which agent. (so give the constraint the number of the agent for example).

I have looked in the documentation, where the Constraint class is defined as:

cvxpy.constraints.constraint.Constraint(args, constr_id=None)

Where the parameters are defined as

   args (list) – A list of expression trees.

   constr_id (int) – A unique id for the constraint.

So it seems that I can give an integer ID, but not a name.

Does anyone know whether it is possible to give a string name to a constraint?


Solution

  • I don't know if that's possible, but i would not try to dive that deep into it's internal-code.

    Why not just wrap it away? This should be more safe and customizable.

    Prototype (which does not check for duplicates for example):

    import cvxpy as cp
    
    class ConstraintBuilder:
        def __init__(self):
            self.constraintList = []
            self.str2constr = {}
    
        def addConstr(self, expr, str_):
            self.constraintList.append(expr)
            self.str2constr[str_] = len(self.constraintList)-1
    
        def get(self):
            return self.constraintList
    
        def getConstr(self, str_):
            return self.constraintList[self.str2constr[str_]]
    
    ####
    
    cb = ConstraintBuilder()
    
    x = cp.Variable(5)
    
    cb.addConstr(x >= 0, 'nonnegative')
    cb.addConstr(x[0] + x[1] >= 0.3, 'pair0')
    cb.addConstr(x[0] + x[1] >= 0.6, 'pair1')
    
    objective = cp.Minimize(cp.sum(x))
    problem = cp.Problem(objective, cb.get())
    
    problem.solve(verbose=True, solver=cp.ECOS)
    print(problem.status)
    
    print(cb.getConstr('pair1'))
    

    which outputs:

    ECOS 2.0.7 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS
    
    It     pcost       dcost      gap   pres   dres    k/t    mu     step   sigma     IR    |   BT
     0  +3.600e-01  +3.690e-01  +5e+00  7e-01  1e-02  1e+00  8e-01    ---    ---    1  1  - |  -  - 
     1  +4.151e-01  +4.114e-01  +8e-01  2e-01  2e-03  1e-01  1e-01  0.8590  3e-03   1  1  1 |  0  0
     2  +5.863e-01  +5.831e-01  +9e-02  3e-02  2e-04  1e-02  1e-02  0.9795  1e-01   1  1  1 |  0  0
     3  +5.998e-01  +5.998e-01  +1e-03  3e-04  2e-06  1e-04  2e-04  0.9887  1e-04   1  1  1 |  0  0
     4  +6.000e-01  +6.000e-01  +1e-05  3e-06  2e-08  2e-06  2e-06  0.9890  1e-04   1  0  0 |  0  0
     5  +6.000e-01  +6.000e-01  +1e-07  4e-08  2e-10  2e-08  2e-08  0.9890  1e-04   1  0  0 |  0  0
     6  +6.000e-01  +6.000e-01  +1e-09  4e-10  3e-12  2e-10  2e-10  0.9890  1e-04   1  0  0 |  0  0
    
    OPTIMAL (within feastol=4.2e-10, reltol=2.5e-09, abstol=1.5e-09).
    Runtime: 0.000236 seconds.
    
    optimal
    0.6 <= var0[0] + var0[1]