Advertisement
Guest User

Untitled

a guest
May 1st, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.34 KB | None | 0 0
  1. using JuMP
  2. using MathProgBase
  3. using GLPKMathProgInterface
  4.  
  5. # Create Mixed Integer Programming GLPK solver
  6. m = Model(solver = GLPKSolverMIP())
  7.  
  8. # x[1] = S, x[2] = E, x[3] = N, x[4] = D, x[5] = M, x[6] = O, x[7] = R, x[8] = Y
  9. @variable(m, 0 <= x[i=1:8] <= 9, Int)
  10.  
  11. # y[i,j] == 1 iff x[i] == j, 0 otherwise
  12. @variable(m, y[i=1:8,j=0:9], Bin)
  13.  
  14. # Enforce sum consistency
  15. @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])
  16.  
  17. # Enforce that y[i,j] <=> x[i] == j
  18. @constraint(m, [i=1:8], sum([j*y[i,j] for j=0:9]) == x[i])
  19.  
  20. # Enforce that each i has only one j associated to it (each letter has only one value)
  21. @constraint(m, [i=1:8], sum([y[i,j] for j=0:9]) == 1)
  22. # Enforce that eah j has at most one i associated to it (each digit appears in at most one letter)
  23. @constraint(m, [j=0:9], sum([y[i,j] for i=1:8]) <= 1)
  24.  
  25. # Enforce that S >= 1 and M >= 1 because they are the leftmost digits of their respective words
  26. @constraint(m, x[1] >= 1)
  27. @constraint(m, x[5] >= 1)
  28.  
  29. print(m)
  30.  
  31. status = solve(m)
  32.  
  33. println("S = ", getvalue(x[1]))
  34. println("E = ", getvalue(x[2]))
  35. println("N = ", getvalue(x[3]))
  36. println("D = ", getvalue(x[4]))
  37.  
  38. println("M = ", getvalue(x[5]))
  39. println("O = ", getvalue(x[6]))
  40. println("R = ", getvalue(x[7]))
  41.  
  42. println("Y = ", getvalue(x[8]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement