Search code examples
pythonmathematical-optimizationor-tools

How to obtain the dual problem with OR-Tools, having used solver.Add() to add constraints


I have followed this tuto to solve an MIP with or-tools: https://developers.google.com/optimization/mip/integer_opt

Here is the code:

from ortools.linear_solver import pywraplp


def main():
    # Create the mip solver with the SCIP backend.
    solver = pywraplp.Solver.CreateSolver('SCIP')

    infinity = solver.infinity()
    # x and y are integer non-negative variables.
    x = solver.IntVar(0.0, infinity, 'x')
    y = solver.IntVar(0.0, infinity, 'y')

    print('Number of variables =', solver.NumVariables())

    # x + 7 * y <= 17.5.
    solver.Add(x + 7 * y <= 17.5)

    # x <= 3.5.
    solver.Add(x <= 3.5)

    print('Number of constraints =', solver.NumConstraints())

    # Maximize x + 10 * y.
    solver.Maximize(x + 10 * y)

    status = solver.Solve()

    if status == pywraplp.Solver.OPTIMAL:
        print('Solution:')
        print('Objective value =', solver.Objective().Value())
        print('x =', x.solution_value())
        print('y =', y.solution_value())
    else:
        print('The problem does not have an optimal solution.')

    print('\nAdvanced usage:')
    print('Problem solved in %f milliseconds' % solver.wall_time())
    print('Problem solved in %d iterations' % solver.iterations())
    print('Problem solved in %d branch-and-bound nodes' % solver.nodes())


if __name__ == '__main__':
    main()

Now my problem is to obtain the dual problem, or at least the values of the dual variables. I have found this piece of code https://github.com/google/or-tools/issues/419 but they don't implement constraints in the same way, and I'd rather not have to rewrite the whole code (which is now pretty long).


Solution

  • The dual variables are not exposed. I am not even sure they are created. Furthermore, dual values are only available for pure LP, not for MIPs.