Michael
Michael

Reputation: 1924

finding which constraints are not true when pulp optimization does not solve in time limit

I have a really big optimization that is not solving for large datasets. I would like to look at the constraints to see which ones are holding and which ones are not. Can this be done?

Upvotes: 1

Views: 803

Answers (2)

kabdulla
kabdulla

Reputation: 5419

I am not aware of a method of the LpConstraint class which allows you to easily check if it has been satisfied by the incumbent "solution" (not really a solution as does not satisfy all constraints). link to LpConstraint docs

A similar question was asked a while back, it has no answer, so I suspect this functionality doesn't exist. It might be worth submitting a feature request on the pulp GitHub page.

In the meantime you can probably hack together something that might work, along the lines of the following (where prob is your Pulp problem object):

soln_dict = {i.name: i.varValue for i in prob.variables()}

for c in prob.constraints.values():
    c_dict = c.toDict()
    # print(c_dict)
    satisfied = False
    
    LHS = sum([soln_dict[i['name']]*i['value'] for i in c_dict['coefficients']])
    LHS = LHS + c_dict['constant']
    
    if c_dict['sense'] == 0:
        satisfied = (LHS == 0)
   
    if c_dict['sense'] == -1:
        satisfied = (LHS <= 0)
    
    if c_dict['sense'] == 1:
        satisfied = (LHS >= 0)
   
    print(c)
    
    if satisfied:
        #print('LHS: ', LHS)
        print('is satisfied')
    else:
        #print('LHS: ', LHS)
        print('not satisfied')

Upvotes: 4

Michael
Michael

Reputation: 1924

looking at the code looks like constraints have a valid function that is not in the docs

def valid(self, eps=0):
        val = self.value()
        if self.sense == const.LpConstraintEQ:
            return abs(val) <= eps
        else:
            return val * self.sense >= -eps

Upvotes: 1

Related Questions