def _generate_graph(self, Ws, ws, labels, bounds, binary, S, Ts, LOSS): """Generate an arc-flow graph.""" from pyvpsolver import MVP, AFG m = len(ws) ndims = len(Ws[0]) if bounds is not None: b = bounds else: maxW = [max(Wi[d] for Wi in Ws) for d in range(ndims)] b = [None] * m for i in range(m): minw = [min(wi[d] for wi in ws[i]) for d in range(ndims)] b[i] = min(maxW[d] // minw[d] for d in range(ndims) if minw[d] != 0) Cs = [1] * len(Ws) Qs = [-1] * len(Ws) instance = MVP(Ws, Cs, Qs, ws, b, binary=binary, verbose=False) graph = AFG(instance, verbose=Tools.VERBOSE).graph() vlbl = {} assert Ts is None or len(Ts) == len(graph.Ts) for u in graph.V: if u == graph.S: vlbl[u] = S elif u in graph.Ts: vlbl[u] = Ts[graph.Ts.index(u)] else: vlbl[u] = str(u) graph.relabel(lambda u: vlbl.get(u), lambda lbl: labels[lbl] if lbl != graph.LOSS else LOSS) return graph
def _generate_graph(self, W, w, labels, bounds, binary, S, T, LOSS): """Generate an arc-flow graph.""" from pyvpsolver import VBP, AFG m = len(w) ndims = len(W) if bounds is not None: b = bounds else: b = [ min(W[d] // w[i][d]) for d in range(ndims) if w[i][d] != 0 for i in range(m) ] instance = VBP(W, w, b, binary=binary, verbose=False) graph = AFG(instance, verbose=Tools.VERBOSE).graph() graph.relabel( lambda u: S if u == graph.S else T if u == graph.Ts[0] else str(u), lambda lbl: labels[lbl] if lbl != graph.LOSS else LOSS) return graph
def _generate_graph(self, W, w, labels, bounds, binary, S, T, LOSS): """Generate an arc-flow graph.""" from pyvpsolver import VBP, AFG m = len(w) ndims = len(W) if bounds is not None: b = bounds else: b = [ min(W[d]//w[i][d]) for d in range(ndims) if w[i][d] != 0 for i in range(m) ] instance = VBP(W, w, b, binary=binary, verbose=False) graph = AFG(instance, verbose=Tools.VERBOSE).graph() graph.relabel( lambda u: S if u == graph.S else T if u == graph.Ts[0] else str(u), lambda lbl: labels[lbl] if lbl != graph.LOSS else LOSS ) return graph
def _generate_graph(self, Ws, ws, labels, bounds, binary, S, Ts, LOSS): """Generate an arc-flow graph.""" from pyvpsolver import MVP, AFG m = len(ws) ndims = len(Ws[0]) if bounds is not None: b = bounds else: maxW = [max(Wi[d] for Wi in Ws) for d in range(ndims)] b = [None]*m for i in range(m): minw = [min(wi[d] for wi in ws[i]) for d in range(ndims)] b[i] = min( maxW[d]//minw[d] for d in range(ndims) if minw[d] != 0 ) Cs = [1]*len(Ws) Qs = [-1]*len(Ws) instance = MVP(Ws, Cs, Qs, ws, b, binary=binary, verbose=False) graph = AFG(instance, verbose=Tools.VERBOSE).graph() vlbl = {} assert Ts is None or len(Ts) == len(graph.Ts) for u in graph.V: if u == graph.S: vlbl[u] = S elif u in graph.Ts: vlbl[u] = Ts[graph.Ts.index(u)] else: vlbl[u] = str(u) graph.relabel( lambda u: vlbl.get(u), lambda lbl: labels[lbl] if lbl != graph.LOSS else LOSS ) return graph