Skip to main content

How to terminate a Linear program once i get a desired difference(say 1) between primal and dual solution?

Answered

Comments

8 comments

  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    Check this parameter MIPGap

    0
  • Ishit Bajpai
    Gurobi-versary
    First Comment
    First Question

    Thanks for your quick response.

    I am facing an issue while using the below code.

    m1.setParam('MIPGapAbs', 5)  
    m1.optimize()

    This code is not able to terminate the LP when |Primal(objective) - Dual(objective)| <= 5.

    Below are the log files,

    I think it should have terminated after 8th iteration but it keeps on going.

    Can you please point out the error?

     

    PS. This is a continuous model.

    Does gurobi have any methods like MIPGapAbs for continuous model ?

     

    0
  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    Hi Ishit,

    If you can stick with Barrier (Method = 2), you can try BarConvTol, or more generally using OptimalityTol.
    You can check out more Parameters.

    For more fine control you can use a callback. See Callback Codes for the available attributes.

    import sys
    import gurobipy as gp
    from gurobipy import GRB


    def mycallback(model, where):

        if where == GRB.Callback.BARRIER:
            # Barrier callback
            itcnt = model.cbGet(GRB.Callback.BARRIER_ITRCNT)
            primobj = model.cbGet(GRB.Callback.BARRIER_PRIMOBJ)
            dualobj = model.cbGet(GRB.Callback.BARRIER_DUALOBJ)
            priminf = model.cbGet(GRB.Callback.BARRIER_PRIMINF)
            dualinf = model.cbGet(GRB.Callback.BARRIER_DUALINF)
            cmpl = model.cbGet(GRB.Callback.BARRIER_COMPL)
          if abs(primobj - dualobj) <= 1:
                print("Custom gap reached")
                model.terminate()
            print("%d %g %g %g %g %g" % (itcnt, primobj, dualobj, priminf, dualinf, cmpl))


    if len(sys.argv) < 2:
        print("Usage: callback.py filename")
        sys.exit(0)


    # Read model from file
    model = gp.read(sys.argv[1])
    model.params.Method = 2 # fix to barrier
    model.optimize(mycallback)

    Cheers, 
    David

    0
  • Ishit Bajpai
    Gurobi-versary
    First Comment
    First Question

    Thanks for your answer.

    I set my difference as 1 and got the desired result.

    Custom gap reached
    12 2.94524 2.2359 0.00116249 1.27134e-14 1.57297e-06

    However,I want decision variable's value also.

    objective value :  inf

    Is there any way that i can choose my optimal value to be next greatest integer i.e 3 and get decision variable's value corresponding to it?

    0
  • Ishit Bajpai
    Gurobi-versary
    First Comment
    First Question

    is there any function like BarConvTool which instead of taking relative difference as parameter takes absolute difference as parameter ?


    0
  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    To retrieve the solution using this callback with a custom stopping criterion, set the Crossover parameter to 0.

    Cheers, 
    David

    0
  • Ishit Bajpai
    Gurobi-versary
    First Comment
    First Question

    Thanks for your reply.

    After adding this i am getting my objective value as 0.I am also unable to retrieve decision variable's value.

    Following is the code where mycallback is the function used above.

    m1.params.Crossover = 0   

    m1.optimize(mycallback)

     

     

    0
  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    Is it because it is terminating at the first iteration with \(\texttt{primobj}\) and \(\texttt{dualobj}\) equal to 0?
    I have checked a fair number of models (the ones in the Gurobi examples data directory) with this approach and it seems to work.
    If so, you can add an extra condition to your termination criteria, to say something like

            if primobj != 0 and dualobj != 0 and abs(primobj - dualobj) <= 1:
                print("Custom gap reached")
                model.terminate()

    Cheers, 
    David

    0

Please sign in to leave a comment.