Esempio n. 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
Esempio n. 2
0
def main():
    """Example: solve a vector packing instance using 'solvers.vbp'"""

    W = (5180, 9)
    w = [(1120,1), (1250,1), (520,1), (1066,1), (1000,1), (1150,1)]
    b = [9, 5, 91, 18, 11, 64]

    # Solve:
    obj, sol = vbpsolver.solve(
        W, w, b,
        svg_file="tmp/graph_vbp.svg",
        script="vpsolver_glpk.sh",
        verbose=True
    )
    print "obj:", obj
    print "sol:", sol
    vbpsolver.print_solution(obj, sol)

    assert obj == 33  # check the solution objective value
Esempio n. 3
0
def main():
    """Example: solve a vector packing instance using 'solvers.vbpsolver'"""
    from pyvpsolver.solvers import vbpsolver
    os.chdir(os.path.dirname(__file__) or os.curdir)

    W = (5180, 9)
    w = [(1120, 1), (1250, 1), (520, 1), (1066, 1), (1000, 1), (1150, 1)]
    b = [9, 5, 91, 18, 11, 64]

    # Solve:
    solution = vbpsolver.solve(
        W, w, b,
        svg_file="tmp/graph_vbp.svg",
        script="vpsolver_glpk.sh",
        verbose=True
    )
    vbpsolver.print_solution(solution)

    # check the solution objective value
    obj, patterns = solution
    assert obj == 33
Esempio n. 4
0
def main():
    """Example: solve a vector packing instance using 'solvers.vbpsolver'"""
    from pyvpsolver.solvers import vbpsolver
    os.chdir(os.path.dirname(__file__) or os.curdir)

    W = (5180, 9)
    w = [(1120, 1), (1250, 1), (520, 1), (1066, 1), (1000, 1), (1150, 1)]
    b = [9, 5, 91, 18, 11, 64]

    # Solve:
    solution = vbpsolver.solve(W,
                               w,
                               b,
                               svg_file="tmp/graph_vbp.svg",
                               script="vpsolver_glpk.sh",
                               verbose=True)
    vbpsolver.print_solution(solution)

    # check the solution objective value
    obj, patterns = solution
    assert obj == 33
Esempio n. 5
0
def test_vbpsolver():
    """Test vbpsolver."""
    from pyvpsolver import VPSolver
    from pyvpsolver.solvers import vbpsolver
    W, w, b = (1, ), [(1, )], [1]
    lp_file = VPSolver.new_tmp_file(".lp")
    mps_file = VPSolver.new_tmp_file(".mps")
    svg_file = VPSolver.new_tmp_file(".svg")

    solution = vbpsolver.solve(W, w, b, script="vpsolver_glpk.sh")
    vbpsolver.print_solution(solution)
    obj, patterns = solution
    assert obj == 1

    solution = vbpsolver.solve(W,
                               w,
                               b,
                               lp_file=lp_file,
                               mps_file=mps_file,
                               svg_file=svg_file,
                               script="vpsolver_glpk.sh")
    vbpsolver.print_solution(solution)
    obj, patterns = solution
    assert obj == 1
    vbpsolver.print_solution(obj, patterns)
Esempio n. 6
0
def test_vbpsolver():
    """Test vbpsolver."""
    from pyvpsolver import VPSolver
    from pyvpsolver.solvers import vbpsolver
    W, w, b = (1,), [(1,)], [1]
    lp_file = VPSolver.new_tmp_file(".lp")
    mps_file = VPSolver.new_tmp_file(".mps")
    svg_file = VPSolver.new_tmp_file(".svg")

    solution = vbpsolver.solve(W, w, b, script="vpsolver_glpk.sh")
    vbpsolver.print_solution(solution)
    obj, patterns = solution
    assert obj == 1

    solution = vbpsolver.solve(
        W, w, b, lp_file=lp_file, mps_file=mps_file, svg_file=svg_file,
        script="vpsolver_glpk.sh"
    )
    vbpsolver.print_solution(solution)
    obj, patterns = solution
    assert obj == 1
    vbpsolver.print_solution(obj, patterns)
Esempio n. 7
0
def main():
    """Examples: how to use VBP, MVP, AFG, MPS, LP and VPSolver"""
    from pyvpsolver import VPSolver, VBP, MVP, AFG, MPS, LP
    from pyvpsolver.solvers import vbpsolver, mvpsolver
    os.chdir(os.path.dirname(__file__) or os.curdir)

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

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

    # 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.draw("tmp/graph1.svg")
    except ImportError 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, solution = VPSolver.script(
        "vpsolver_glpk.sh", instanceB, verbose=True
    )

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

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

    # check the solution objective value
    obj, patterns = solution
    assert obj == 21

    # Create instanceC:
    W1 = (100, 100)
    W2 = (50, 120)
    W3 = (150, 25)
    ws1, b1 = [(50, 25), (25, 50), (0, 75)], 1
    ws2, b2 = [(40, 40), (60, 25), (25, 60)], 1
    ws3, b3 = [(30, 10), (20, 40), (10, 50)], 1
    Ws = [W1, W2, W3]     # capacities
    Cs = [3, 7, 2]        # costs
    Qs = [-1, -1, -1]     # number of bins available
    ws = [ws1, ws2, ws3]  # items
    b = [b1, b2, b3]      # demands
    instanceC = MVP(Ws, Cs, Qs, ws, b)

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

    # check the solution objective value
    obj, patterns = solution
    assert obj == 3

    # Create instanceD from a .mvp file
    instanceD = MVP.from_file("instance.mvp")

    # Draw the arc-flow graph for instanceD (requires pygraphviz)
    try:
        AFG(instanceD).draw("tmp/graph2.svg")
    except ImportError as e:
        print(repr(e))

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

    # check the solution objective value
    obj, patterns = solution
    assert obj == 8
Esempio n. 8
0
def main():
    """Examples: how to use VBP, MVP, AFG, MPS, LP and VPSolver"""
    from pyvpsolver import VPSolver, VBP, MVP, AFG, MPS, LP
    from pyvpsolver.solvers import vbpsolver, mvpsolver
    os.chdir(os.path.dirname(__file__) or os.curdir)

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

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

    # 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.draw("tmp/graph1.svg")
    except ImportError 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, solution = VPSolver.script("vpsolver_glpk.sh",
                                    instanceB,
                                    verbose=True)

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

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

    # check the solution objective value
    obj, patterns = solution
    assert obj == 21

    # Create instanceC:
    W1 = (100, 100)
    W2 = (50, 120)
    W3 = (150, 25)
    ws1, b1 = [(50, 25), (25, 50), (0, 75)], 1
    ws2, b2 = [(40, 40), (60, 25), (25, 60)], 1
    ws3, b3 = [(30, 10), (20, 40), (10, 50)], 1
    Ws = [W1, W2, W3]  # capacities
    Cs = [3, 7, 2]  # costs
    Qs = [-1, -1, -1]  # number of bins available
    ws = [ws1, ws2, ws3]  # items
    b = [b1, b2, b3]  # demands
    instanceC = MVP(Ws, Cs, Qs, ws, b)

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

    # check the solution objective value
    obj, patterns = solution
    assert obj == 3

    # Create instanceD from a .mvp file
    instanceD = MVP.from_file("instance.mvp")

    # Draw the arc-flow graph for instanceD (requires pygraphviz)
    try:
        AFG(instanceD).draw("tmp/graph2.svg")
    except ImportError as e:
        print(repr(e))

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

    # check the solution objective value
    obj, patterns = solution
    assert obj == 8