public static LpSolve conv2MinCostLP( int[][] cs, int[][] cost, int[][] map, int s, int t, double objective) throws LpSolveException { int E = cntE(cs); int V = cntV(cs); LpSolve solver = conv2LPBase(cs, s, t, map, V, E); solver.strAddConstraint(objStr(cs, E, map, s), LpSolve.EQ, objective); solver.setMinim(); solver.setLpName("Converted min cost flow"); int[] constr = new int[E]; for (int from = 0; from < cs.length; from++) { for (int to = 0; to < cs.length; to++) { if (cs[from][to] != 0) { constr[map[from][to]] = cost[from][to]; } } } solver.strSetObjFn(constrStr(constr)); return solver; }
public static LpSolve conv2LPBase(int[][] cs, int s, int t, int[][] map, int V, int E) throws LpSolveException { LpSolve solver = LpSolve.makeLp(0, E); // GENERATE capacity constraints for (int from = 0; from < cs.length; from++) { for (int to = 0; to < cs.length; to++) { if (cs[from][to] != 0) { int[] constr = new int[E]; constr[map[from][to]] = 1; solver.strAddConstraint(constrStr(constr), LpSolve.LE, cs[from][to]); } } } // GENERATE in-out-equalities for (int u = 0; u < cs.length; u++) { if (u != s && u != t) { int[] constr = new int[E]; for (int v = 0; v < cs.length; v++) { if (cs[v][u] != 0) { constr[map[v][u]] = 1; } if (cs[u][v] != 0) { constr[map[u][v]] = -1; } } solver.strAddConstraint(constrStr(constr), LpSolve.EQ, 0); } } return solver; }
public static LpSolve conv2LP(int[][] cs, int s, int t, int[][] map) throws LpSolveException { int E = cntE(cs); int V = cntV(cs); LpSolve solver = conv2LPBase(cs, s, t, map, V, E); // GENERATE objective function solver.strSetObjFn(objStr(cs, E, map, s)); // other options solver.setMaxim(); solver.setLpName("Converted max flow"); // solver.setVerbose(3); return solver; }