Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using JuMP
- using MathProgBase
- using GLPKMathProgInterface
- # Create Mixed Integer Programming GLPK solver
- m = Model(solver = GLPKSolverMIP())
- # x[1] = S, x[2] = E, x[3] = N, x[4] = D, x[5] = M, x[6] = O, x[7] = R, x[8] = Y
- @variable(m, 0 <= x[i=1:8] <= 9, Int)
- # y[i,j] == 1 iff x[i] == j, 0 otherwise
- @variable(m, y[i=1:8,j=0:9], Bin)
- # Enforce sum consistency
- @constraint(m, 1000*x[1] + 100x[2] + 10x[3] + 1x[4] + 1000x[5] + 100x[6] + 10x[7] + 1x[2] == 10000x[5] + 1000x[6] + 100x[7] + 10x[2] + 1x[8])
- # Enforce that y[i,j] <=> x[i] == j
- @constraint(m, [i=1:8], sum([j*y[i,j] for j=0:9]) == x[i])
- # Enforce that each i has only one j associated to it (each letter has only one value)
- @constraint(m, [i=1:8], sum([y[i,j] for j=0:9]) == 1)
- # Enforce that eah j has at most one i associated to it (each digit appears in at most one letter)
- @constraint(m, [j=0:9], sum([y[i,j] for i=1:8]) <= 1)
- # Enforce that S >= 1 and M >= 1 because they are the leftmost digits of their respective words
- @constraint(m, x[1] >= 1)
- @constraint(m, x[5] >= 1)
- print(m)
- status = solve(m)
- println("S = ", getvalue(x[1]))
- println("E = ", getvalue(x[2]))
- println("N = ", getvalue(x[3]))
- println("D = ", getvalue(x[4]))
- println("M = ", getvalue(x[5]))
- println("O = ", getvalue(x[6]))
- println("R = ", getvalue(x[7]))
- println("Y = ", getvalue(x[8]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement