def main(): t = 'k out of n' n = 12 seed(100) bound = [randint(5, 15) for _ in range(n)] tableutils.printmat([['Max sum of'] + bound]) for k in range(1, n + 1): s = pywraplp.Solver(t, pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) x = [s.NumVar(0, bound[i], '') for i in range(n)] y = [s.NumVar(0, bound[i], '') for i in range(n)] Costx = sum(x[i] for i in range(n)) Costy = sum(y[i] for i in range(n)) s.Maximize(Costx + Costy) k_out_of_n(s, k, x, '==') ldg = sosn(s, k, y) rc = s.Solve() if rc != 0: print 'Error', rc sy = SolVal(y) sx = SolVal(x) yy = [[' ', 'x'][e > 0] for e in sy] xx = [[' ', 'x'][e > 0] for e in sx] tableutils.printmat( tableutils.wrapmat( [xx, yy], ['{0}/{1}'.format(k, n), 'Adjacent {0}/{1}'.format(k, n)], None), 0, False) return rc
def main(): bounds = [] s = pywraplp.Solver('',pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) a = [[2],[-2]] b = [3,-12] x = s.NumVar(2,5,'x') z,l = maximax(s,a,[x],b) rc = s.Solve() print 'x = ',SolVal(x) print 'z = ',SolVal(z) print '\delta = ', SolVal(l)
def solve_model(D): s = newSolver('Project management') n = len(D) max = sum(D[i][1] for i in range(n)) t = [s.NumVar(0,max,'t[%i]' % i) for i in range(n)] Total = s.NumVar(0,max,'Total') for i in range(n): s.Add(t[i]+D[i][1] <= Total) for j in D[i][2]: s.Add(t[j]+D[j][1] <= t[i]) s.Minimize(Total) rc = s.Solve() return rc, SolVal(Total),SolVal(t)
def solve_model_clp(D): t = 'Project management' s = pywraplp.Solver(t,pywraplp.Solver.CLP_LINEAR_PROGRAMMING) n = len(D) max = sum(D[i][1] for i in range(n)) t = [s.NumVar(0,max,'t[%i]' % i) for i in range(n)] Total = s.NumVar(0,max,'Total') for i in range(n): s.Add(t[i]+D[i][1] <= Total) for j in D[i][2]: s.Add(t[j]+D[j][1] <= t[i]) s.Minimize(Total) rc = s.Solve() return rc, SolVal(Total),SolVal(t)
def minimize_piecewise_linear(Points,B,convex=True): t = 'Piecewise' s = pywraplp.Solver(t,pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) n = len(Points) x = s.NumVar(Points[0][0],Points[n-1][0],'x') l = [s.NumVar(0,1,'l[%i]' % (i,)) for i in range(n)] s.Add(1 == sum(l[i] for i in range(n))) d = sosn(s, 2, l) s.Add(x == sum(l[i]*Points[i][0] for i in range(n))) s.Add(x >= B) Cost = s.Sum(l[i]*Points[i][1] for i in range(n)) s.Minimize(Cost) rc = s.Solve() return rc,SolVal(l),SolVal(d[1])
def solve_model(D, F): t = 'Facility location problem' s = pywraplp.Solver(t, pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) m, n = len(D) - 1, len(D[0]) - 1 x = [[s.NumVar(0, D[i][-1], '') for j in range(n)] for i in range(m)] y = [s.IntVar(0, 1, '') for i in range(m)] for i in range(m): s.Add(D[i][-1] * y[i] >= sum(x[i][j] for j in range(n))) for j in range(n): s.Add(D[-1][j] == sum(x[i][j] for i in range(m))) Fcost = s.Sum(y[i] * F[i] for i in range(m)) Dcost = s.Sum(x[i][j] * D[i][j] for i in range(m) for j in range(n)) s.Minimize(Dcost + Fcost) rc = s.Solve() return rc, ObjVal(s), SolVal(x), SolVal(y)
def solve_model(S, I, R, P): s = newSolver('Staff Scheduling', True) nbS, nbI, nbSets, nbPairs, nbC = len(S), len(I), len(R), len( P), S[-1][1] + 1 print nbS, nbI, nbSets, nbPairs, nbC nbT = 1 + max(e[2] for e in S) x = [[s.IntVar(0, 1, '') for _ in range(nbS)] for _ in range(nbI)] z = [[[s.IntVar(0,1,'') for _ in range(len(P[p][1]))] for p in range(nbPairs)] \ for _ in range(nbI)] for j in range(nbS): k_out_of_n(s, 1, [x[i][j] for i in range(nbI)], '<=') for i in range(nbI): s.Add(sum(x[i][j] for j in range(nbS)) >= I[i][1][0]) s.Add(sum(x[i][j] for j in range(nbS)) <= I[i][1][1]) for t in range(nbT): k_out_of_n(s, 1, [x[i][j] for j in range(nbS) if S[j][2] == t], '<=') WC = sum(x[i][j] * I[i][2][c] for i in range(nbI) \ for j in range(nbS) for c in range(nbC) if S[j][1] == c) WR = sum(I[i][3][r] * sum(x[i][j] for j in R[r][1]) \ for r in range(nbSets) for i in range(nbI)) for i in range(nbI): for p in range(nbPairs): if I[i][4][p] != 0: for k in range(len(P[p][1])): reify(s, [1, 1], [x[i][P[p][1][k][0]], x[i][P[p][1][k][1]]], 2, z[i][p][k], '>=') WP = sum(z[i][p][k]*I[i][4][p] for i in range(nbI) \ for p in range(nbPairs) for k in range(len(P[p][1])) if I[i][4][p] != 0) s.Maximize(WC + WR + WP) rc, xs = s.Solve(), [] for i in range(nbI): xs.append([ i, [[ j, (I[i][2][S[j][1]], sum(I[i][3][r] for r in range(nbSets) if j in R[r][1]), sum( SolVal(z[i][p][k]) * I[i][4][p] / 2 for p in range(nbPairs) for k in range(len(P[p][1])) if j in P[p][1][k])) ] for j in range(nbS) if SolVal(x[i][j]) > 0] ]) return rc, SolVal(x), xs, ObjVal(s)
def solve_model(Part,Target,Cost,Inventory,D,SC,SL): s = newSolver('Multi-period soap blending problem') Oils, Periods, Acids = range(len(Part)), range(len(Cost[0])), range(len(Part[0])) Buy = [[s.NumVar(0,D,'') for _ in Periods] for _ in Oils] Blnd = [[s.NumVar(0,D,'') for _ in Periods] for _ in Oils] Hold = [[s.NumVar(0,D,'') for _ in Periods] for _ in Oils] Prod = [s.NumVar(0,D,'') for _ in Periods] CostP= [s.NumVar(0,D*1000,'') for _ in Periods] CostS= [s.NumVar(0,D*1000,'') for _ in Periods] Acid = [[s.NumVar(0,D*D,'') for _ in Periods] for _ in Acids] for i in Oils: s.Add(Hold[i][0] == Inventory[i][0]) for j in Periods: s.Add(Prod[j] == sum(Blnd[i][j] for i in Oils)) s.Add(Prod[j] >= D) if j < Periods[-1]: for i in Oils: s.Add(Hold[i][j] + Buy[i][j] - Blnd[i][j] == Hold[i][j+1]) s.Add(sum(Hold[i][j] for i in Oils) >= SL[0]) s.Add(sum(Hold[i][j] for i in Oils) <= SL[1]) for k in Acids: s.Add(Acid[k][j] == sum(Blnd[i][j]*Part[i][k] for i in Oils)) s.Add(Acid[k][j] >= Target[0][k] * Prod[j]) s.Add(Acid[k][j] <= Target[1][k] * Prod[j]) s.Add(CostP[j] == sum(Buy[i][j] * Cost[i][j] for i in Oils)) s.Add(CostS[j] == sum(Hold[i][j] * SC for i in Oils)) Cost_product = s.Sum(CostP[j] for j in Periods) Cost_storage = s.Sum(CostS[j] for j in Periods) s.Minimize(Cost_product+Cost_storage) rc = s.Solve() B,L,H,A,P = SolVal(Buy),SolVal(Blnd),SolVal(Hold),SolVal(Acid),SolVal(Prod) CP,CS = SolVal(CostP),SolVal(CostS) return rc,ObjVal(s),B,L,H,P,A,CP,CS
def solve_model(D): t = 'Power distribution problem' s = pywraplp.Solver(t, pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) m, n = len(D) - 1, len(D[0]) - 1 B = sum([D[-1][j] for j in range(n)]) G = [[s.NumVar(0,B if D[i][j] else 0,'') for j in range(n)] \ for i in range(m)] for i in range(m): s.Add(D[i][-1] >= sum(G[i][j] for j in range(n))) for j in range(n): s.Add(D[-1][j] == sum(G[i][j] for i in range(m))) Cost = s.Sum(G[i][j] * D[i][j] for i in range(m) for j in range(n)) s.Minimize(Cost) rc = s.Solve() return rc, ObjVal(s), SolVal(G)
def solve_model_eliminate(D, Subtours=[]): t = 'TSP' s = pywraplp.Solver(t, pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) n = len(D) x = [[s.IntVar(0, 1, '') for j in range(n)] for i in range(n)] for i in range(n): s.Add(1 == sum(x[i][j] for j in range(n))) s.Add(1 == sum(x[j][i] for j in range(n))) s.Add(0 == x[i][i]) for sub in Subtours: K = [x[sub[i]][sub[j]]+x[sub[j]][sub[i]]\ for i in range(len(sub)-1) for j in range(i+1,len(sub))] s.Add(len(sub) - 1 >= sum(K)) s.Minimize(s.Sum(x[i][j] * D[i][j] for i in range(n) for j in range(n))) rc = s.Solve() tours = extract_tours(SolVal(x), n) return rc, ObjVal(s), tours
def solve_model(M, nf, Q=None, P=None, no_part=False): t = 'Staffing' s = pywraplp.Solver(t, pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) nbt, n, B = len(M) - 1, len(M[0]) - 1, sum(M[t][-1] for t in range(len(M) - 1)) x = [s.IntVar(0, B, 'x[%i]' % i) for i in range(n)] for t in range(nbt): s.Add(sum([M[t][i] * x[i] for i in range(n)]) >= M[t][-1]) if Q: for i in range(n): s.Add(x[i] >= Q[i]) if P: s.Add(sum(x[i] for i in range(nf)) >= P) if no_part: for t in range(nbt): s.Add(B*sum([M[t][i] * x[i] for i in range(nf)]) \ >= sum([M[t][i] * x[i] for i in range(nf,n)])) s.Minimize(sum(x[i] * M[-1][i] for i in range(n))) rc = s.Solve() return rc, ObjVal(s), SolVal(x)
def solve_gas(C, D): s = newSolver('Gas blending problem') nR, nF = len(C), len(D) Roc, Rmax, Rcost = 0, 1, 2 Foc, Fmin, Fmax, Fprice = 0, 1, 2, 3 G = [[s.NumVar(0.0, 10000, 'G[%i][%i]' % (i, j)) for j in range(nF)] for i in range(nR)] R = [s.NumVar(0, C[i][Rmax], 'R[%i]' % i) for i in range(nR)] F = [s.NumVar(D[j][Fmin], D[j][Fmax], 'F[%i]' % j) for j in range(nF)] for i in range(nR): s.Add(R[i] == sum(G[i][j] for j in range(nF))) for j in range(nF): s.Add(F[j] == sum(G[i][j] for i in range(nR))) for j in range(nF): s.Add(F[j] * D[j][Foc] == s.Sum([G[i][j] * C[i][Roc] for i in range(nR)])) Cost = s.Sum(R[i] * C[i][Rcost] for i in range(nR)) Price = s.Sum(F[j] * D[j][Fprice] for j in range(nF)) s.Maximize(Price - Cost) rc = s.Solve() return rc, ObjVal(s), SolVal(G)
def solve_model(C, D=None, Z=False): t = 'Multi-commodity mincost flow problem' s = pywraplp.Solver(t, pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) K, n = len(C), len(C[0]) - 1, B = [sum(C[k][-1][j] for j in range(n)) for k in range(K)] x = [[[s.IntVar(0,B[k] if C[k][i][j] else 0,'') \ if Z else s.NumVar(0,B[k] if C[k][i][j] else 0,'') \ for j in range(n)] for i in range(n)] for k in range(K)] for k in range(K): for i in range(n): s.Add(C[k][i][-1] - C[k][-1][i] == sum(x[k][i][j] for j in range(n)) - sum(x[k][j][i] for j in range(n))) if D: for i in range(n): for j in range(n): s.Add(sum(x[k][i][j] for k in range(K)) <= \ D if type(D) in [int,float] else D[i][j]) Cost = s.Sum(x[k][i][j]*C[k][i][j] \ for i in range(n) for j in range(n) for k in range(K)) s.Minimize(Cost) rc = s.Solve() return rc, ObjVal(s), SolVal(x)
for j in Teams[e]: s.Add(sum(x[i][j][w] for w in range(nbWeeks)) == nbInter) for w in range(nbWeeks): for i in range(nbTeams): s.Add( sum(x[i][j][w] for j in range(nbTeams) if i < j) + sum(x[j][i][w] for j in range(nbTeams) if j < i) <= nbPerWeek) Value = [ w * x[i][j][w] for Div in Teams for i in Div for j in Div for w in range(nbWeeks) if i < j ] s.Maximize(s.Sum(Value)) rc, Cal = s.Solve(), [] if rc == 0: Cal = [[(i, j) for i in range(nbTeams - 1) for j in range(i + 1, nbTeams) if SolVal(x[i][j][w]) > 0] for w in range(nbWeeks)] return rc, ObjVal(s), Cal import time def add_intra(s, Teams, nbWeeks, nbIntra, x): for Div in Teams: for i in Div: for j in Div: if i < j: s.Add(sum(x[i][j][w] for w in range(nbWeeks)) == nbIntra)