Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Printing out lazy constraints in CPLEX Python API

    Posted 02/22/17 02:37 PM

    Originally posted by: SinaF


    Hi,

    I am using CPLEX Python API to solve a MIP optimization problem in which I am adding some lazy constraints through callback function. To make sure that these lazy constraints are correct, I want to print them out. Could you please tell me how can I do this within the callback function?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Printing out lazy constraints in CPLEX Python API

    Posted 02/22/17 07:10 PM

    I'm not sure if this is what you're asking, but if you attempt to write out the model after `solve()` it will not include the lazy constraints that you add through the callback function.  If you want to literally print them out for debugging, you'll have to do that manually.  For example, I modified the LazyConstraintCallback in the admipex5.py example that is included in the CPLEX installer:

    #  Add the following constraint to the noswot model via lazy
    #  constraint callback; the optimal solution will be cut off:
    #
    #  lazy_con : W11 + W12 + W13 + W14 + W15 <= 3
    #
    class MyLazy(LazyConstraintCallback):
    
        def __call__(self):
            indices = ["W11", "W12", "W13", "W14", "W15"]
            act = 0.0
            for i in indices:
                act += self.get_values(i)
            if act > 3.01:
                constraint = cplex.SparsePair(ind=indices, val=[1.0] * 5)
                sense = "L"
                rhs = 3.0
                print("LC:", constraint, sense, rhs)
                self.add(constraint=constraint,
                         sense=sense,
                         rhs=rhs)
    

     

    With this you see lines like the following in the output:

    LC: SparsePair(ind = ['W11', 'W12', 'W13', 'W14', 'W15'], val = [1.0, 1.0, 1.0, 1.0, 1.0]) L 3.0

     


    #CPLEXOptimizers
    #DecisionOptimization