Пример #1
0
def main():
    """Examples: how to use VBP, AFG, MPS, LP and VPSolver"""

    # Create instanceA:
    instanceA = VBP([5180], [1120, 1250, 520, 1066, 1000, 1150],
                            [9, 5, 91, 18, 11, 64], verbose=False)

    # Create instanceB from a .vbp file
    instanceB = VBP.from_file("instance.vbp", verbose=False)

    # Create an arc-flow graph for instanceA
    afg = AFG(instanceA, verbose=False)

    # Create .mps and .lp models for instanceA
    mps_model = MPS(afg, verbose=False)
    lp_model = LP(afg, verbose=False)

    # Draw the arc-flow graph for instanceA (requires pygraphviz)
    try:
        afg.graph().draw("tmp/graph.svg")
    except Exception as e:
        print repr(e)

    # Solve instanceA using bin/vpsolver (requires Gurobi)
    try:
        out, sol = VPSolver.vpsolver(instanceA, verbose=True)
    except Exception as e:
        print "Failed to call vpsolver"
        print repr(e)

    # Solve instanceA using any vpsolver script (i.e., any MIP solver):
    #   The scripts accept models with and without the underlying graphs.
    #   However, the graphs are required to extract the solution.
    out, sol = VPSolver.script("vpsolver_glpk.sh", lp_model, afg, verbose=True)
    try:
        out, sol = VPSolver.script(
            "vpsolver_gurobi.sh", mps_model, verbose=True
        )
    except Exception as e:
        print repr(e)

    # Solve an instance directly without creating AFG, MPS or LP objects:
    out, sol = VPSolver.script("vpsolver_glpk.sh", instanceB, verbose=True)

    # Print the solution:
    obj, patterns = sol
    print "Objective:", obj
    print "Solution:", patterns

    # Pretty-print the solution:
    vbpsolver.print_solution(obj, patterns)

    assert obj == 21  # check the solution objective value