def main(): s = newSolver('Test reify force', True) x = [s.NumVar(0, 10, ''), s.NumVar(0, 10, '')] delta_1 = reify_force(s, [1, 1], x, 3, None, '==') delta_2 = reify_force(s, [1, 1], x, 5, None, '==') s.Add(delta_1 + delta_2 == 1) s.Maximize(2 * x[0] + 3 * x[1]) s.Solve() print([ SolVal(delta_1) == 0, SolVal(delta_2) == 1, abs(SolVal(x[0])) < 0.0001, abs(SolVal(x[1]) - 5) < 0.0001 ]) s = newSolver('Test reify force', True) x = [s.NumVar(0, 10, ''), s.NumVar(0, 10, '')] delta_1 = reify_force(s, [1, 1], x, 3, None, '==') delta_2 = reify_force(s, [1, 1], x, 5, None, '==') s.Add(delta_1 + delta_2 == 1) s.Minimize(2 * x[0] + 3 * x[1]) s.Solve() print([ SolVal(delta_1) == 1, SolVal(delta_2) == 0, abs(SolVal(x[0]) - 3) < 0.0001, abs(SolVal(x[1])) < 0.0001 ])
def solve_model(Part, Target, Cost, Inventory, D, SC, SL): s = newSolver('Multi-period soap blending problem') Oils = range(len(Part)) Periods, Acids = 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 = SolVal(Buy), SolVal(Blnd), SolVal(Hold), SolVal(Acid) CP, CS, P = SolVal(CostP), SolVal(CostS), SolVal(Prod) return rc, ObjVal(s), B, L, H, P, A, CP, CS
def solve_model(Teams, params): (nbIntra, nbInter, nbPerWeek, nbWeeks) = params nbTeams = sum([1 for sub in Teams for e in sub]) nbDiv, Cal = len(Teams), [] s = newSolver('Sports schedule', True) x = [[[s.IntVar(0, 1, '') if i < j else None for _ in range(nbWeeks)] for j in range(nbTeams)] for i in range(nbTeams - 1)] 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) for d in range(nbDiv - 1): for e in range(d + 1, nbDiv): for i in Teams[d]: 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=[x[i][j][w] for Div in Teams for i in Div for j in Div \ for w in range(nbWeeks-len(Div)*nbIntra//nbPerWeek,nbWeeks)\ if i<j] s.Maximize(sum(Value)) rc = 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
def solve_model(D,Start=None, End=None): s,n = newSolver('Shortest path problem'),len(D) if Start is None: Start,End = 0,len(D)-1 G = [[s.NumVar(0,1 if D[i][j] else 0,'') for j in range(n)] for i in range(n)] for i in range(n): if i == Start: s.Add(1 == sum(G[Start][j] for j in range(n))) s.Add(0 == sum(G[j][Start] for j in range(n))) elif i == End: s.Add(1 == sum(G[j][End] for j in range(n))) s.Add(0 == sum(G[End][j] for j in range(n))) else: s.Add(sum(G[i][j] for j in range(n))==sum(G[j][i] for j in range(n))) Distance = s.Sum(G[i][j]*D[i][j] for i in range(n) for j in range(n)) s.Minimize(Distance) rc = s.Solve() Path,Cost,Cumul,node=[Start],[0],[0],Start while rc == 0 and node != End and len(Path)<n: next = [i for i in range(n) if SolVal(G[node][i]) == 1][0] Path.append(next) Cost.append(D[node][next]) Cumul.append(Cumul[-1]+Cost[-1]) node = next return rc,ObjVal(s),Path,Cost,Cumul
def solve_model(S, I, R, P): s = newSolver('Staff Scheduling', True) nbS, nbI, nbSets, nbPairs = len(S), len(I), len(R), len(P) nbC, nbT = S[-1][1] + 1, 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])): xip1k0, xip1k1 = x[i][P[p][1][k][0]], x[i][P[p][1][k][1]] reify(s, [1, 1], [xip1k0, xip1k1], 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(), ss_ret(x, z, nbI, nbSets, nbS, nbPairs, I, S, R, P) return rc, SolVal(x), xs, ObjVal(s)
def solve_model(D): s = newSolver('Job Shop Scheduling', True) nJ, nM = len(D), len(D[0]) M = sum([D[i][k][1] for i in range(nJ) for k in range(nM)]) x = [[s.NumVar(0, M, '') for k in range(nM)] for i in range(nJ)] p = [[D[i][k][0] for k in range(nM)] for i in range(nJ)] d = [[D[i][k][1] for k in range(nM)] for i in range(nJ)] z = [[[s.IntVar(0,1,'') for k in range(nM)] \ for j in range(nJ)] for i in range(nJ)] T = s.NumVar(0, M, '') for i in range(nJ): for k in range(nM): s.Add(x[i][p[i][k]] + d[i][k] <= T) for j in range(nJ): if i != j: s.Add(z[i][j][k] == 1 - z[j][i][k]) s.Add(x[i][p[i][k]] + d[i][k] - M*z[i][j][p[i][k]] \ <= x[j][p[i][k]]) s.Add(x[j][p[j][k]] + d[j][k] - M*z[j][i][p[j][k]] \ <= x[i][p[j][k]]) for k in range(nM - 1): s.Add(x[i][p[i][k]] + d[i][k] <= x[i][p[i][k + 1]]) s.Minimize(T) rc = s.Solve() return rc, SolVal(T), SolVal(x)
def solve_model_big(Teams,params): (nbIntra,nbInter,nbPerWeek,nbWeeks) = params nbTeams = sum([1 for sub in Teams for e in sub]) nbDiv,cuts = len(Teams),[] for iter in range(2): s = newSolver('Sports schedule', False) x = [[[s.NumVar(0,1,'') if i<j else None for _ in range(nbWeeks)] for j in range(nbTeams)] for i in range(nbTeams-1)] basic_model(s,Teams,nbTeams,nbWeeks,nbPerWeek,nbIntra,\ nbDiv,nbInter,cuts,x) rc = s.Solve() bounds = {(3,1):1, (4,1):2, (5,1):2, (5,3):7} if nbPerWeek <= 3: for w in range(nbWeeks): for i in range(nbTeams-2): for j in range(i+1,nbTeams-1): for k in range(j+1,nbTeams): b = bounds.get((3,nbPerWeek),1000) if sum([SolVal(x[p[0]][p[1]][w]) \ for p in pairs([i,j,k],[])])>b: cuts.append([[i,j,k],[w,b]]) for l in range(k+1,nbTeams): b = bounds.get((4,nbPerWeek),1000) if sum([SolVal(x[p[0]][p[1]][w]) \ for p in pairs([i,j,k,l],[])])>b: cuts.append([[i,j,k,l],[w,b]]) for m in range(l+1, nbTeams): b = bounds.get((5,nbPerWeek),1000) if sum([SolVal(x[p[0]][p[1]][w]) \ for p in pairs([i,j,k,l,m],[])])>b: cuts.append([[i,j,k,l,m],[w,b]]) else: break s = newSolver('Sports schedule', True) x = [[[s.IntVar(0,1,'') if i<j else None for _ in range(nbWeeks)] for j in range(nbTeams)] for i in range(nbTeams-1)] basic_model(s,Teams,nbTeams,nbWeeks,nbPerWeek,nbIntra,\ nbDiv,nbInter,cuts,x) 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
def get_new_pattern(l, w): s = newSolver('Cutting stock slave', True) n = len(l) a = [s.IntVar(0, 100, '') for i in range(n)] Cost = sum(l[i] * a[i] for i in range(n)) s.Maximize(Cost) s.Add(sum(w[i] * a[i] for i in range(n)) <= 100) rc = s.Solve() return SolVal(a), ObjVal(s)
def solve_model(D,C=None): s = newSolver('Set Packing', True) nbRosters,nbCrew = len(D),max([e for d in D for e in d])+1 S = [s.IntVar(0,1,'') for i in range(nbRosters)] for j in range(nbCrew): s.Add(1 >= sum(S[i] for i in range(nbRosters) if j in D[i])) s.Maximize(s.Sum(S[i]*(1 if C==None else C[i]) \ for i in range(nbRosters))) rc = s.Solve() Rosters=[i for i in range(nbRosters)if S[i].SolutionValue()>0] return rc,s.Objective().Value(),Rosters
def solve_diet(N): s = newSolver('Diet') nbF,nbN = len(N)-2, len(N[0])-3 FMin,FMax,FCost,NMin,NMax = nbN,nbN+1,nbN+2,nbF,nbF+1 f = [s.NumVar(N[i][FMin], N[i][FMax],'') for i in range(nbF)] for j in range(nbN): s.Add(N[NMin][j]<=s.Sum([f[i]*N[i][j] for i in range(nbF)])) s.Add(s.Sum([f[i]*N[i][j] for i in range(nbF)])<=N[NMax][j]) s.Minimize(s.Sum([f[i]*N[i][FCost] for i in range(nbF)])) rc = s.Solve() return rc,ObjVal(s),SolVal(f)
def solve_maxrook(n): s = newSolver('Maxrook',True) x = [[s.IntVar(0,1,'') for _ in range(n)] for _ in range(n)] for i in range(n): k_out_of_n(s,1,get_row(x,i),'<=') k_out_of_n(s,1,get_column(x,i),'<=') Count = s.Sum(x[i][j] for i in range(n) for j in range(n)) s.Maximize(Count) rc = s.Solve() xx = [[[' ','R'][int(SolVal(x[i][j]))] for j in range(n)] for i in range(n) ] return rc,xx
def solve_smm(): s = newSolver('Send more money', True) ALL = [S, E, N, D, M, O, R, Y] = [newIntVar(s, 0, 9) for k in range(8)] s.Add(1000 * S[0] + 100 * E[0] + 10 * N[0] + D[0] + 1000 * M[0] + 100 * O[0] + 10 * R[0] + E[0] == 10000 * M[0] + 1000 * O[0] + 100 * N[0] + 10 * E[0] + Y[0]) all_different(s, ALL) neq(s, S, 0) neq(s, M, 0) rc = s.Solve() return rc, SolVal([a[0] for a in ALL])
def solve_diet(N): s = newSolver('Diet') nbF,nbN = len(N)-2, len(N[0])-3 FMin,FMax,FCost,NMin,NMax = nbN,nbN+1,nbN+2,nbF,nbF+1 f = [s.NumVar(N[i][FMin], N[i][FMax],'f[%i]'%i) for i in range(nbF)] for j in range(nbN): s.Add(N[NMin][j] <= s.Sum([f[i]*N[i][j] for i in range(nbF)])) s.Add(s.Sum([f[i]*N[i][j] for i in range(nbF)]) <= N[NMax][j]) s.Minimize(s.Sum([f[i]*N[i][FCost] for i in range(nbF)])) rc=s.Solve() return rc,ObjVal(s),SolVal(f)
def solve_smm(): s = newSolver('Send more money',True) ALL = [S,E,N,D,M,O,R,Y] = [newIntVar(s,0,9) for k in range(8)] s.Add( 1000*S[0]+100*E[0]+10*N[0]+D[0] + 1000*M[0]+100*O[0]+10*R[0]+E[0] == 10000*M[0]+1000*O[0]+100*N[0]+10*E[0]+Y[0]) all_different(s,ALL) neq(s,S,0) neq(s,M,0) rc = s.Solve() return rc,SolVal([a[0] for a in ALL])
def minimize_piecewise_linear_convex(Points,B): s,n = newSolver('Piecewise'),len(Points) x = s.NumVar(Points[0][0],Points[n-1][0],'x') l = [s.NumVar(0.0,1,'l[%i]' % (i,)) for i in range(n)] s.Add(1 == sum(l[i] for i in range(n))) 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) s.Solve() R = [l[i].SolutionValue() for i in range(n)] return R
def solve_maxrook(n): s = newSolver('Maxrook', True) x = [[s.IntVar(0, 1, '') for _ in range(n)] for _ in range(n)] for i in range(n): k_out_of_n(s, 1, get_row(x, i), '<=') k_out_of_n(s, 1, get_column(x, i), '<=') Count = s.Sum(x[i][j] for i in range(n) for j in range(n)) s.Maximize(Count) rc = s.Solve() xx = [[[' ', 'R'][int(SolVal(x[i][j]))] for j in range(n)] for i in range(n)] return rc, xx
def minimize_piecewise_linear(Points,B,convex=True): s,n = newSolver('Piecewise', True),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 minimize_piecewise_linear_convex(Points, B): s, n = newSolver('Piecewise'), len(Points) x = s.NumVar(Points[0][0], Points[n - 1][0], 'x') l = [s.NumVar(0.0, 1, 'l[%i]' % (i, )) for i in range(n)] s.Add(1 == sum(l[i] for i in range(n))) 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) s.Solve() R = [l[i].SolutionValue() for i in range(n)] return R
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_master(C, b, integer=False): t = 'Cutting stock master problem' m, n, u = len(C), len(C[0]), [] s = newSolver(t, integer) y = [s.IntVar(0, 1000, '') for j in range(n)] # right bound? Cost = sum(y[j] for j in range(n)) s.Minimize(Cost) for i in range(m): u.append(s.Add(sum(C[i][j] * y[j] for j in range(n)) >= b[i])) rc = s.Solve() y = [int(ceil(e.SolutionValue())) for e in y] return rc, y, [0 if integer else u[i].DualValue() \ for i in range(m)]
def solve_model(D): s = newSolver('Transshipment problem') n = 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(n)] for i in range(n): s.Add(D[i][-1] - D[-1][i] == \ sum(G[i][j] for j in range(n))-sum(G[j][i]for j in range(n))) Cost = s.Sum(G[i][j] * D[i][j] for i in range(n) for j in range(n)) s.Minimize(Cost) rc = s.Solve() return rc, ObjVal(s), SolVal(G)
def solve_model(C,S,T,net=True): s,n = newSolver('Maxflow flow problem'),len(C) x = [[s.NumVar(0,C[i][j],'') for j in range(n)] for i in range(n)] B = sum(C[i][j] for i in range(n) for j in range(n)) Flowout,Flowin = s.NumVar(0,B,''),s.NumVar(0,B,'') for i in range(n): if i not in S and i not in T: s.Add(sum(x[i][j] for j in range(n)) == sum(x[j][i] for j in range(n))) s.Add(Flowout == s.Sum(x[i][j] for i in S for j in range(n))) s.Add(Flowin == s.Sum(x[j][i] for i in S for j in range(n))) s.Maximize(Flowout - 2*Flowin if net else Flowout) rc = s.Solve() return rc,SolVal(Flowout),SolVal(Flowin),SolVal(x)
def solve_model(D): s = newSolver('Mincost flow problem') 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(C,S,T,unique=True): s,n = newSolver('Maximum flow problem'),len(C) x=[[s.NumVar(0,C[i][j],'')for j in range(n)]for i in range(n)] B=sum(C[i][j] for i in range(n) for j in range(n)) Flowout,Flowin = s.NumVar(0,B,''),s.NumVar(0,B,'') for i in range(n): if i not in S and i not in T: s.Add(sum(x[i][j] for j in range(n)) == \ sum(x[j][i] for j in range(n))) s.Add(Flowout == s.Sum(x[i][j] for i in S for j in range(n))) s.Add(Flowin == s.Sum(x[j][i] for i in S for j in range(n))) s.Maximize(Flowout-2*Flowin if unique else Flowout-Flowin) rc = s.Solve() return rc,SolVal(Flowout),SolVal(Flowin),SolVal(x)
def solve_model(D): s = newSolver('Mincost flow problem') 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_tree_model(D,Start=None): s,n = newSolver('Shortest paths tree problem'),len(D) Start = 0 if Start is None else Start G = [[s.NumVar(0,min(n,D[i][j]),'') for j in range(n)] for i in range(n)] for i in range(n): if i == Start: s.Add(n-1 == sum(G[Start][j] for j in range(n))) s.Add(0 == sum(G[j][Start] for j in range(n))) else: s.Add(sum(G[j][i] for j in range(n))-sum(G[i][j] for j in range(n))==1) Distance = s.Sum(G[i][j]*D[i][j] for i in range(n) for j in range(n)) s.Minimize(Distance) rc = s.Solve() Tree = [[i,j, D[i][j]] for i in range(n) for j in range(n) if SolVal(G[i][j])>0] return rc,ObjVal(s),Tree
def solve_classification(A,B): n,ma,mb=len(A[0]),len(A),len(B) s = newSolver('Classification') ya = [s.NumVar(0,99,'') for _ in range(ma)] yb = [s.NumVar(0,99,'') for _ in range(mb)] a = [s.NumVar(-99,99,'') for _ in range(n+1)] for i in range(ma): s.Add(ya[i] >= a[n]+1-s.Sum(a[j]*A[i][j] for j in range(n))) for i in range(mb): s.Add(yb[i] >= s.Sum(a[j]*B[i][j] for j in range(n))-a[n]+1 ) Agap = s.Sum(ya[i] for i in range(ma)) Bgap = s.Sum(yb[i] for i in range(mb)) s.Minimize(Agap+Bgap) rc = s.Solve() return rc,ObjVal(s),SolVal(a)
def solve_model(D): s = newSolver('Transshipment problem') n = 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(n)] #for i in range(n): # for j in range(n): # if D[i][j]==0: # s.Add(G[i][j] == 0) for i in range(n): s.Add(D[i][-1] - D[-1][i] == sum(G[i][j] for j in range(n)) - sum(G[j][i] for j in range(n))) Cost = s.Sum(G[i][j]*D[i][j] for i in range(n) for j in range(n)) s.Minimize(Cost) rc = s.Solve() return rc,ObjVal(s),SolVal(G)
def solve_maxpiece(n,piece): s = newSolver('Maxpiece',True) x = [[s.IntVar(0,1,'') for _ in range(n)] for _ in range(n)] for i in range(n): if piece in ['R' ,'Q']: k_out_of_n(s,1,get_row(x,i),'<=') k_out_of_n(s,1,get_column(x,i),'<=') if piece in ['B', 'Q']: for j in range(n): if i in [0,n-1] or j in [0,n-1]: k_out_of_n(s,1,get_ne(x,i,j,n),'<=') k_out_of_n(s,1,get_se(x,i,j,n),'<=') Count = s.Sum(x[i][j] for i in range(n) for j in range(n)) s.Maximize(Count) rc = s.Solve() xx = [[[' ',piece][int(SolVal(x[i][j]))] for j in range(n)] for i in range(n) ] return rc,xx
def solve_model_eliminate(D, Subtours=[]): s, n = newSolver('TSP', True), len(D) x = [[s.IntVar(0,0 if D[i][j] is None else 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]*(0 if D[i][j] is None else 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_maxpiece(n, piece): s = newSolver('Maxpiece', True) x = [[s.IntVar(0, 1, '') for _ in range(n)] for _ in range(n)] for i in range(n): if piece in ['R', 'Q']: k_out_of_n(s, 1, get_row(x, i), '<=') k_out_of_n(s, 1, get_column(x, i), '<=') if piece in ['B', 'Q']: for j in range(n): if i in [0, n - 1] or j in [0, n - 1]: k_out_of_n(s, 1, get_ne(x, i, j, n), '<=') k_out_of_n(s, 1, get_se(x, i, j, n), '<=') Count = s.Sum(x[i][j] for i in range(n) for j in range(n)) s.Maximize(Count) rc = s.Solve() xx = [[[' ', piece][int(SolVal(x[i][j]))] for j in range(n)] for i in range(n)] return rc, xx
def solve_model(D, W, symmetry_break=False, knapsack=True): s = newSolver('Bin Packing', True) nbC, nbP = len(D), sum([P[0] for P in D]) w = [e for sub in [[d[1]] * d[0] for d in D] for e in sub] nbT, nbTmin = bound_trucks(w, W) x = [[[s.IntVar(0,1,'') for _ in range(nbT)] \ for _ in range(d[0])] for d in D] y = [s.IntVar(0, 1, '') for _ in range(nbT)] for k in range(nbT): sxk = sum(D[i][1]*x[i][j][k] \ for i in range(nbC) for j in range(D[i][0])) s.Add(sxk <= W * y[k]) for i in range(nbC): for j in range(D[i][0]): s.Add(sum([x[i][j][k] for k in range(nbT)]) == 1) if symmetry_break: for k in range(nbT - 1): s.Add(y[k] >= y[k + 1]) for i in range(nbC): for j in range(D[i][0]): for k in range(nbT): for jj in range(max(0, j - 1), j): s.Add(sum(x[i][jj][kk] \ for kk in range(k+1)) >= x[i][j][k]) for jj in range(j + 1, min(j + 2, D[i][0])): s.Add(sum(x[i][jj][kk] \ for kk in range(k,nbT))>=x[i][j][k]) if knapsack: s.Add(sum(W * y[i] for i in range(nbT)) >= sum(w)) s.Add(sum(y[k] for k in range(nbT)) >= nbTmin) s.Minimize(sum(y[k] for k in range(nbT))) rc = s.Solve() P2T = [[ D[i][1], [ k for j in range(D[i][0]) for k in range(nbT) if SolVal(x[i][j][k]) > 0 ] ] for i in range(nbC)] T2P=[[k, [(i,j,D[i][1]) \ for i in range(nbC) for j in range(D[i][0])\ if SolVal(x[i][j][k])>0]] for k in range(nbT)] return rc, ObjVal(s), P2T, T2P
def solve_model(D,deg=1,objective=0): s,n = newSolver('Polynomial fitting'),len(D) b = s.infinity() a = [s.NumVar(-b,b,'a[%i]' % i) for i in range(1+deg)] u = [s.NumVar(0,b,'u[%i]' % i) for i in range(n)] v = [s.NumVar(0,b,'v[%i]' % i) for i in range(n)] e = s.NumVar(0,b,'e') for i in range(n): s.Add(D[i][1]==u[i]-v[i]+sum(a[j]*D[i][0]**j for j in range(1+deg))) for i in range(n): s.Add(u[i] <= e) s.Add(v[i] <= e) if objective: Cost = e else: Cost = sum(u[i]+v[i] for i in range(n)) s.Minimize(Cost) rc = s.Solve() return rc,ObjVal(s),SolVal(a)
def solve_model(M, nf, Q=None, P=None, no_part=False): s = newSolver('Staffing', True) nbt, n = len(M) - 1, len(M[0]) - 1 B = sum(M[t][-1] for t in range(len(M) - 1)) x = [s.IntVar(0, B, '') 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_model(D, F): s = newSolver('Facility location problem', True) m, n = len(D) - 1, len(D[0]) - 1 B = sum(D[-1][j]*max(D[i][j] \ for i in range(m)) for j in range(n)) 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)] Fcost, Dcost = s.NumVar(0, B, ''), s.NumVar(0, B, '') 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))) s.Add(sum(y[i] * F[i] for i in range(m)) == Fcost) s.Add(sum(x[i][j]*D[i][j] \ for i in range(m) for j in range(n)) == Dcost) s.Minimize(Dcost + Fcost) rc = s.Solve() return rc,ObjVal(s),SolVal(x),SolVal(y),\ SolVal(Fcost),SolVal(Dcost)
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, '') for j in range(nF)] for i in range(nR)] R = [s.NumVar(0, C[i][Rmax], '') for i in range(nR)] F = [s.NumVar(D[j][Fmin], D[j][Fmax], '') 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_sudoku(G): s,n,x = newSolver('Sudoku',True),len(G),[] for i in range(n): row=[] for j in range(n): if G[i][j] == None: v=[s.IntVar(1,n+1,'')]+[s.IntVar(0,1,'') for _ in range(n)] s.Add(v[0] == sum(k*v[k] for k in range(1,n+1))) else: v=[G[i][j]]+[0 if k!=G[i][j] else 1 for k in range(1,n+1)] row.append(v) x.append(row) for i in range(n): all_diff(s,get_row(x,i)) all_diff(s,get_column(x,i)) for i in range(3): for j in range(3): all_diff(s,get_subgrid(x,i,j)) rc = s.Solve() return rc,[[SolVal(x[i][j][0]) for j in range(n)] for i in range(n)]
def solve_model(D,deg=1,objective=0): s,n = newSolver('Polynomial fitting'),len(D) b = s.infinity() a = [s.NumVar(-b,b,'a[%i]' % i) for i in range(1+deg)] u = [s.NumVar(0,b,'u[%i]' % i) for i in range(n)] v = [s.NumVar(0,b,'v[%i]' % i) for i in range(n)] e = s.NumVar(0,b,'e') for i in range(n): s.Add(D[i][1]==u[i]-v[i]+sum(a[j]*D[i][0]**j \ for j in range(1+deg))) for i in range(n): s.Add(u[i] <= e) s.Add(v[i] <= e) if objective: Cost = e else: Cost = sum(u[i]+v[i] for i in range(n)) s.Minimize(Cost) rc = s.Solve() return rc,ObjVal(s),SolVal(a)
def solve_lady_or_tiger(): s = newSolver('Lady or tider', True) Rooms = range(1, 10) R = [None] + [newIntVar(s, 0, 2) for _ in Rooms] S = [None] + [s.IntVar(0, 1, '') for _ in Rooms] i_empty, i_lady, i_tiger = 1, 2, 3 k_out_of_n(s, 1, [R[i][i_lady] for i in Rooms]) for i in Rooms: reify_force(s, [1], [R[i][i_tiger]], 0, S[i], '<=') reify_raise(s, [1], [R[i][i_lady]], 1, S[i], '>=') reify(s, [1] * 5, [R[i][i_lady] for i in range(1, 10, 2)], 1, S[1], '>=') reify(s, [1], [R[2][i_empty]], 1, S[2], '>=') reify(s, [1, -1], [S[5], S[7]], 0, S[3], '>=') reify(s, [1], [S[1]], 0, S[4], '<=') reify(s, [1, 1], [S[2], S[4]], 1, S[5], '>=') reify(s, [1], [S[3]], 0, S[6], '<=') reify(s, [1], [R[1][i_lady]], 0, S[7], '<=') reify(s, [1, 1], [R[8][i_tiger], R[9][i_empty]], 2, S[8], '>=') reify(s, [1, -1], [R[9][i_tiger], S[6]], 1, S[9], '>=') rc = s.Solve() return rc, [SolVal(S[i]) for i in Rooms], [SolVal(R[i]) for i in Rooms]
def solve_lady_or_tiger(): s = newSolver('Lady or tider', True) Rooms = range(1,10) R = [None]+[newIntVar(s,0,2) for _ in Rooms] S = [None]+[s.IntVar(0,1,'') for _ in Rooms] i_empty,i_lady,i_tiger = 1,2,3 k_out_of_n(s,1,[R[i][i_lady] for i in Rooms]) for i in Rooms: reify_force(s,[1],[R[i][i_tiger]],0,S[i],'<=') reify_raise(s,[1],[R[i][i_lady]],1,S[i],'>=') reify(s,[1]*5,[R[i][i_lady] for i in range(1,10,2)],1,S[1],'>=') reify(s,[1],[R[2][i_empty]],1,S[2],'>=') reify(s,[1,-1],[S[5],S[7]],0,S[3],'>=') reify(s,[1],[S[1]],0,S[4],'<=') reify(s,[1,1],[S[2],S[4]],1,S[5],'>=') reify(s,[1],[S[3]],0,S[6],'<=') reify(s,[1],[R[1][i_lady]],0,S[7],'<=') reify(s,[1,1],[R[8][i_tiger],R[9][i_empty]],2,S[8],'>=') reify(s,[1,-1],[R[9][i_tiger],S[6]],1,S[9],'>=') rc = s.Solve() return rc,[SolVal(S[i]) for i in Rooms],[SolVal(R[i]) for i in Rooms]
def solve_model(C,D=None,Z=False): s = newSolver('Multi-commodity mincost flow problem', Z) 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(C[k][i][j]*x[k][i][j] if C[k][i][j] else 0\ 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)