def test_mvpsolvers(): """Test mvpsolvers.""" from pyvpsolver import VPSolver from pyvpsolver.solvers import mvpsolver2013, mvpsolver2016 Ws = [(100, 75), (75, 50), (75, 50), (100, 100)] Cs = [3, 2, 3, 100] Qs = [inf, -1, -1, -1] ws = [[(75, 50)], [(40, 75), (25, 25)]] b = [2, 1] for mvpsolver in [mvpsolver2013, mvpsolver2016]: solution = mvpsolver.solve( Ws, Cs, Qs, ws, b, script="vpsolver_glpk.sh" ) mvpsolver.print_solution(solution) obj, patterns = solution assert obj == 5 lp_file = VPSolver.new_tmp_file(".lp") mps_file = VPSolver.new_tmp_file(".mps") svg_file = VPSolver.new_tmp_file(".svg") solution = mvpsolver.solve( Ws, Cs, Qs, ws, b, lp_file=lp_file, mps_file=mps_file, svg_file=svg_file, script="vpsolver_glpk.sh", verbose=True ) mvpsolver.print_solution(solution) obj, patterns = solution assert obj == 5 mvpsolver.print_solution(obj, patterns)
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)
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
def solve_worker(app_name, method, form, args, output=sys.stdout): """Worker for solving the problem in a separate process.""" VPSolver.PLIST = [] def signal_handler(sig, frame): """Signal handler for cleaner exit.""" for p in VPSolver.PLIST: try: os.killpg(p.pid, signal.SIGTERM) except Exception as e: pass sys.exit(0) signal.signal(signal.SIGTERM, signal_handler) sys.stdout = output sys.stderr = output input_ = form["input"].strip("\n") if DEBUG: print("Input:\n{0}\n\nOutput:".format(input_)) output.flush() if app_name == "vbp": tmpfile = VPSolver.new_tmp_file(ext=".vbp") with open(tmpfile, "w") as f: f.write(input_) instance = VBP.from_file(tmpfile, verbose=False) afg = AFG(instance, verbose=True) lp_model = LP(afg, verbose=False) out, sol = VPSolver.script(form["script"], lp_model, afg, pyout=False, verbose=True) elif app_name == "mvp": tmpfile = VPSolver.new_tmp_file(ext=".mvp") with open(tmpfile, "w") as f: f.write(input_) instance = MVP.from_file(tmpfile, verbose=False) afg = AFG(instance, verbose=True) lp_model = LP(afg, verbose=False) out, sol = VPSolver.script(form["script"], lp_model, afg, pyout=False, verbose=True) print("EOF\n") output.flush()
def main(): """ Computes minimal equivalent knapsack inequalities using 'equivknapsack.mod' """ kp_cons = [([3, 5], 17, None)] cons = set() for k in xrange(len(kp_cons)): a, a0, bounds = kp_cons[k] if bounds is None: bounds = [a0] * len(a) for i in xrange(len(a)): bounds[i] = a0 / a[i] if a[i] != 0 else 0 sum_a = sum(x * y for x, y in zip(a, bounds)) aS = abs(2 * a0 + 1 - sum_a) if a0 < (sum_a - 1) / 2: a0 += aS fix_as = 1 else: if aS > a0: continue fix_as = 0 a = a + [aS] bounds = bounds + [1] mod_in = "equivknapsack.mod" mod_out = "tmp/equivknapsack.out.mod" parser = PyMPL(locals_=locals()) parser.parse(mod_in, mod_out) lp_out = "tmp/equivknapsack.lp" glpkutils.mod2lp(mod_out, lp_out) # exit_code = os.system("glpsol --math {0}".format(mod_out)) # assert exit_code == 0 out, varvalues = VPSolver.script_wsol("vpsolver_glpk.sh", lp_out, verbose=True) b = [varvalues.get("pi({0})".format(i + 1), 0) for i in xrange(len(a))] b0 = varvalues.get("pi(0)", 0) # print a, a0 # print b, b0 if fix_as == 1: b0 -= b[-1] b = b[:-1] else: b = b[:-1] if sum(b) != 0: cons.add((tuple(b), b0, tuple(bounds))) print "Original knapsack inequalities:" for a, a0, bounds in sorted(kp_cons, key=lambda x: (x[1], x[0])): # print a, a0 print " + ".join("{0:2g} x{1:d}".format(a[i], i + 1) for i in xrange(len(a))), "<=", a0 print "Minimal equivalent knapsack inequalities:" for b, b0, bounds in sorted(cons, key=lambda x: (x[1], x[0])): # print b, b0 print " + ".join("{0:2g} x{1:d}".format(b[i], i + 1) for i in xrange(len(b))), "<=", b0, bounds[:-1]
def test_vbpsol(): """Test vbpsol.""" from pyvpsolver import VPSolver, VBP, MVP, AFG, LP, MPS vbp = VBP(W=(1, ), w=[(1, )], b=[1], verbose=True) afg = AFG(vbp, verbose=True) lp = LP(afg, verbose=True) sol_file = VPSolver.new_tmp_file(".sol") output, solution = VPSolver.script_wsol("vpsolver_glpk.sh", lp) assert isinstance(solution, dict) with open(sol_file, "w") as f: lst = [] for var, value in solution.items(): lst.append(str(var)) lst.append(str(value)) print(" ".join(lst), file=f) obj, patterns = VPSolver.vbpsol(afg, sol_file) assert obj == 1
def solve_worker(app_name, method, form, args, output=sys.stdout): """Worker for solving the problem in a separate process.""" VPSolver.PLIST = [] def signal_handler(sig, frame): """Signal handler for cleaner exit.""" for p in VPSolver.PLIST: try: os.killpg(p.pid, signal.SIGTERM) except Exception as e: pass sys.exit(0) signal.signal(signal.SIGTERM, signal_handler) sys.stdout = output sys.stderr = output input_ = form["input"].strip("\n") if DEBUG: print("Input:\n{0}\n\nOutput:".format(input_)) output.flush() if app_name == "vbp": tmpfile = VPSolver.new_tmp_file(ext=".vbp") with open(tmpfile, "w") as f: f.write(input_) instance = VBP.from_file(tmpfile, verbose=False) afg = AFG(instance, verbose=True) lp_model = LP(afg, verbose=False) out, sol = VPSolver.script( form["script"], lp_model, afg, verbose=True ) elif app_name == "pympl": tmpfile = VPSolver.new_tmp_file(ext=".mod") parser = PyMPL() parser.input = input_ parser.parse() parser.write(tmpfile) VPSolver.run( "glpsol --math {0}".format(tmpfile), grepv="Generating", verbose=True ) print("EOF\n") output.flush()
def test_vbpsol(): """Test vbpsol.""" from pyvpsolver import VPSolver, VBP, MVP, AFG, LP, MPS vbp = VBP(W=(1,), w=[(1,)], b=[1], verbose=True) afg = AFG(vbp, verbose=True) lp = LP(afg, verbose=True) sol_file = VPSolver.new_tmp_file(".sol") output, solution = VPSolver.script_wsol("vpsolver_glpk.sh", lp) assert isinstance(solution, dict) with open(sol_file, "w") as f: lst = [] for var, value in solution.items(): lst.append(str(var)) lst.append(str(value)) print(" ".join(lst), file=f) obj, patterns = VPSolver.vbpsol(afg, sol_file) assert obj == 1
def test_draw(): """Test scripts.""" from pyvpsolver import VPSolver, VBP, MVP, AFG vbp = VBP(W=(1, ), w=[(1, )], b=[1]) mvp = MVP(Ws=[(1, )], Cs=[1], Qs=[inf], ws=[[(1, )]], b=[1]) svg_file = VPSolver.new_tmp_file(".svg") for instance in [vbp, mvp]: afg = AFG(instance) try: afg.draw(svg_file, lpaths=True, graph_attrs={"size": "8,8"}) except Exception as e: print(repr(e)) try: VPSolver.afg2svg(afg, svg_file) except Exception as e: print(repr(e)) try: VPSolver.afg2svg(afg.filename, svg_file) except Exception as e: print(repr(e))
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)
def test_draw(): """Test scripts.""" from pyvpsolver import VPSolver, VBP, MVP, AFG vbp = VBP(W=(1,), w=[(1,)], b=[1]) mvp = MVP(Ws=[(1,)], Cs=[1], Qs=[inf], ws=[[(1,)]], b=[1]) svg_file = VPSolver.new_tmp_file(".svg") for instance in [vbp, mvp]: afg = AFG(instance) try: afg.draw( svg_file, lpaths=True, graph_attrs={"size": "8,8"} ) except Exception as e: print(repr(e)) try: VPSolver.afg2svg(afg, svg_file) except Exception as e: print(repr(e)) try: VPSolver.afg2svg(afg.filename, svg_file) except Exception as e: print(repr(e))
def test_mvpsolvers(): """Test mvpsolvers.""" from pyvpsolver import VPSolver from pyvpsolver.solvers import mvpsolver2013, mvpsolver2016 Ws = [(100, 75), (75, 50), (75, 50), (100, 100)] Cs = [3, 2, 3, 100] Qs = [inf, -1, -1, -1] ws = [[(75, 50)], [(40, 75), (25, 25)]] b = [2, 1] for mvpsolver in [mvpsolver2013, mvpsolver2016]: solution = mvpsolver.solve(Ws, Cs, Qs, ws, b, script="vpsolver_glpk.sh") mvpsolver.print_solution(solution) obj, patterns = solution assert obj == 5 lp_file = VPSolver.new_tmp_file(".lp") mps_file = VPSolver.new_tmp_file(".mps") svg_file = VPSolver.new_tmp_file(".svg") solution = mvpsolver.solve(Ws, Cs, Qs, ws, b, lp_file=lp_file, mps_file=mps_file, svg_file=svg_file, script="vpsolver_glpk.sh", verbose=True) mvpsolver.print_solution(solution) obj, patterns = solution assert obj == 5 mvpsolver.print_solution(obj, patterns)
def main(): """Parses 'instance.mod'""" mod_in = "instance.mod" mod_out = "tmp/instance.out.mod" parser = PyMPL() parser.parse(mod_in, mod_out) lp_out = "tmp/instance.lp" glpkutils.mod2lp(mod_out, lp_out, True) out, varvalues = VPSolver.script_wsol( "vpsolver_glpk.sh", lp_out, verbose=True ) sol, varvalues = parser["FLOW"].extract(varvalues, verbose=True) print print "sol:", sol print "varvalues:", [(k, v) for k, v in sorted(varvalues.items())] print assert varvalues['Z'] == 21 # check the solution objective value exit_code = os.system("glpsol --math {0}".format(mod_out)) assert exit_code == 0
def main(): """Parses 'graph.mod'""" mod_in = "graph.mod" mod_out = "tmp/graph.out.mod" parser = PyMPL() parser.parse(mod_in, mod_out) lp_out = "tmp/graph.lp" glpkutils.mod2lp(mod_out, lp_out) try: out, varvalues = VPSolver.script_wsol( "vpsolver_glpk.sh", lp_out, verbose=True ) print print "varvalues:", [(k, v) for k, v in sorted(varvalues.items())] print assert varvalues['Z0'] == 9 # check the solution objective value except Exception as e: print repr(e) raise exit_code = os.system("glpsol --math {0}".format(mod_out)) assert exit_code == 0
def test_scripts(): """Test scripts.""" from pyvpsolver import VPSolver, VBP, MVP, AFG, LP, MPS VPSolver.clear() vbp = VBP(W=(1, ), w=[(1, )], b=[1], verbose=True) mvp = MVP(Ws=[(1, )], Cs=[1], Qs=[inf], ws=[[(1, )]], b=[1], verbose=True) for instance in [vbp, mvp]: afg = AFG(instance, verbose=True) lp = LP(afg, verbose=True) mps = MPS(afg, verbose=True) VPSolver.set_verbose(False) output, solution = VPSolver.script("vpsolver_glpk.sh", instance, options="--seed 1234") assert solution[0] == 1 if isinstance(instance, (VBP, MVP)): instance_file = instance.filename output, solution = VPSolver.script("vpsolver_glpk.sh", instance_file) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", afg) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", afg, lp) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", afg, mps) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", lp) assert solution is None output, solution = VPSolver.script("vpsolver_glpk.sh", mps) assert solution is None output, solution = VPSolver.script("vpsolver_glpk.sh", afg.filename) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", lp.filename) assert solution is None output, solution = VPSolver.script("vpsolver_glpk.sh", mps.filename) assert solution is None
def test_lowlevel(): """Test low-level API.""" from pyvpsolver import VPSolver, VBP, MVP, AFG vbp = VBP(W=(1,), w=[(1,)], b=[1]) mvp = MVP(Ws=[(1,)], Cs=[1], Qs=[inf], ws=[[(1,)]], b=[1]) afg_file = VPSolver.new_tmp_file(".afg") lp_file = VPSolver.new_tmp_file(".lp") mps_file = VPSolver.new_tmp_file(".mps") svg_file = VPSolver.new_tmp_file(".svg") VPSolver.vbp2afg(vbp, afg_file) VPSolver.vbp2afg(mvp, afg_file) VPSolver.vbp2afg(vbp.filename, afg_file) VPSolver.vbp2afg(mvp.filename, afg_file) VPSolver.afg2lp(afg_file, lp_file) VPSolver.afg2mps(afg_file, mps_file) VPSolver.afg2lp(AFG(vbp), lp_file) VPSolver.afg2mps(AFG(mvp), mps_file)
def test_scripts(): """Test scripts.""" from pyvpsolver import VPSolver, VBP, MVP, AFG, LP, MPS VPSolver.clear() vbp = VBP(W=(1,), w=[(1,)], b=[1], verbose=True) mvp = MVP(Ws=[(1,)], Cs=[1], Qs=[inf], ws=[[(1,)]], b=[1], verbose=True) for instance in [vbp, mvp]: afg = AFG(instance, verbose=True) lp = LP(afg, verbose=True) mps = MPS(afg, verbose=True) VPSolver.set_verbose(False) output, solution = VPSolver.script( "vpsolver_glpk.sh", instance, options="--seed 1234" ) assert solution[0] == 1 if isinstance(instance, (VBP, MVP)): instance_file = instance.filename output, solution = VPSolver.script("vpsolver_glpk.sh", instance_file) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", afg) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", afg, lp) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", afg, mps) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", lp) assert solution is None output, solution = VPSolver.script("vpsolver_glpk.sh", mps) assert solution is None output, solution = VPSolver.script("vpsolver_glpk.sh", afg.filename) assert solution[0] == 1 output, solution = VPSolver.script("vpsolver_glpk.sh", lp.filename) assert solution is None output, solution = VPSolver.script("vpsolver_glpk.sh", mps.filename) assert solution is None
def test_lowlevel(): """Test low-level API.""" from pyvpsolver import VPSolver, VBP, MVP, AFG vbp = VBP(W=(1, ), w=[(1, )], b=[1]) mvp = MVP(Ws=[(1, )], Cs=[1], Qs=[inf], ws=[[(1, )]], b=[1]) afg_file = VPSolver.new_tmp_file(".afg") lp_file = VPSolver.new_tmp_file(".lp") mps_file = VPSolver.new_tmp_file(".mps") svg_file = VPSolver.new_tmp_file(".svg") VPSolver.vbp2afg(vbp, afg_file) VPSolver.vbp2afg(mvp, afg_file) VPSolver.vbp2afg(vbp.filename, afg_file) VPSolver.vbp2afg(mvp.filename, afg_file) VPSolver.afg2lp(afg_file, lp_file) VPSolver.afg2mps(afg_file, mps_file) VPSolver.afg2lp(AFG(vbp), lp_file) VPSolver.afg2mps(AFG(mvp), mps_file)
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
def main(): """ Computes minimal equivalent knapsack inequalities using 'equivknapsack01.mod' """ kp_cons = [ ([8, 12, 13, 64, 22, 41], 80), ([8, 12, 13, 75, 22, 41], 96), ([3, 6, 4, 18, 6, 4], 20), ([5, 10, 8, 32, 6, 12], 36), ([5, 13, 8, 42, 6, 20], 44), ([5, 13, 8, 48, 6, 20], 48), ([0, 0, 0, 0, 8, 0], 10), ([3, 0, 4, 0, 8, 0], 18), ([3, 2, 4, 0, 8, 4], 22), ([3, 2, 4, 8, 8, 4], 24), # ([3, 3, 3, 3, 3, 5, 5, 5], 17), ] cons = set() for k in xrange(len(kp_cons)): a, a0 = kp_cons[k] aS = abs(2*a0+1-sum(a)) if a0 < (sum(a)-1)/2: a0 += aS fix_as = 1 else: fix_as = 0 if aS > a0: continue a = a+[aS] mod_in = "equivknapsack01.mod" mod_out = "tmp/equivknapsack01.out.mod" parser = PyMPL(locals_=locals(), globals_=globals()) parser.parse(mod_in, mod_out) lp_out = "tmp/equivknapsack01.lp" glpkutils.mod2lp(mod_out, lp_out) # exit_code = os.system("glpsol --math {0}".format(mod_out)) # assert exit_code == 0 out, varvalues = VPSolver.script_wsol( "vpsolver_glpk.sh", lp_out, verbose=False ) b = [varvalues.get("pi({0})".format(i+1), 0) for i in xrange(len(a))] b0 = varvalues.get("pi(0)", 0) # print a, a0 # print b, b0 if fix_as == 1: b0 -= b[-1] b = b[:-1] else: b = b[:-1] if sum(b) != 0: cons.add((tuple(b), b0)) print "Original knapsack inequalities:" for a, a0 in sorted(kp_cons, key=lambda x: (x[1], x[0])): # print a, a0 print " + ".join( "{0:2g} x{1:d}".format(a[i], i+1) for i in xrange(len(a)) ), "<=", a0 print "Minimal equivalent knapsack inequalities:" for b, b0 in sorted(cons, key=lambda x: (x[1], x[0])): # print b, b0 print " + ".join( "{0:2g} x{1:d}".format(b[i], i+1) for i in xrange(len(b)) ), "<=", b0
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
from pyvpsolver import VPSolver, MVP, AFG, MPS from pyvpsolver.solvers import mvpsolver Ws = [(100, 75), (75, 50), (40, 60)] Cs = [3, 2, 1] Qs = [-1, -1, -1] ws = [[(75, 50)], [(40, 15), (25, 25)], [(30, 19), (24, 34)], [(59, 45)]] b = [1, 1, 1, 1] instance = MVP(Ws, Cs, Qs, ws, b) output, solution = VPSolver.script("vpsolver_glpk.sh", instance) obj, patterns = solution print("Obj: {}".format(obj)) print("Solution: {}".format(patterns)) mvpsolver.print_solution(solution)