SEND+MORE=MONEY (one more time)
I have some heavy problems of formulation with Sagemath. It's certainly a formulation problem --- I will add the theoretrical aspect of the model at the end of the question :
I have corrected my code. May be some errors could stay. But even if I am wrong I need some information on how to write my program. You can find the source in the free paper https://pubsonline.informs.org/doi/pd.... There is also an Excel code on the internet. Here is my code
p = MixedIntegerLinearProgram(maximization=True,solver='PPL')
v = p.new_variable(nonnegative=True)# permet de définir v[i, j] aussi bien que v[i]
c = p.new_variable(nonnegative=True)# permet de définir v[i, j] aussi bien que v[i]
p.set_objective(v[0,0])
for i in range(7) : # nombre de lettres (8)
for j in range(9) : #nombre de chiffres
p.set_binary(v[i,j])
#S=sum(j*v[0,j] for j in range(9))
#E=sum(j*v[1,j] for j in range(9))
#N=sum(j*v[2,j] for j in range(9))
#D=sum(j*v[3,j] for j in range(9))
#M=sum(j*v[4,j] for j in range(9))
#O=sum(j*v[5,j] for j in range(9))
#R=sum(j*v[6,j] for j in range(9))
#Y=sum(j*v[7,j] for j in range(9))
for k in range(1,4) :
p.set_binary(c[k])
# Une lettre n'est représentée que par un chiffre
for j in range(8) :
p.add_constraint(0<= sum(v[i,j] for i in range(7)) <= 1)
# Un chiffre n'est associé qu'à une seule lettre
for i in range(7) :
p.add_constraint(sum(v[i,j] for j in range(9)) == 1)
# D + E = Y + 10 C_1
p.add_constraint(sum(j*v[3,j] for j in range(7))+sum(j*v[1,j] for j in range(9))==
sum(j*v[7,j] for j in range(7))+10*c[1])#
# C_1 + R + N = E + 10 C_2
p.add_constraint(c[1]+sum(j*v[6,j] for j in range(7))+sum(j*v[2,j] for j in range(9))==
sum(j*v[1,j] for j in range(9))+10*c[2])#
# C_2 + E + O = N + 10 C_3
p.add_constraint(c[2]+sum(j*v[5,j] for j in range(8))+sum(j*v[1,j] for j in range(9))==
sum(j*v[2,j] for j in range(9))+10*c[3])#
# C_3 + S + M = O + 10 C_4
p.add_constraint(c[3]+sum(j*v[0,j] for j in range(9)) + sum(j*v[4,j] for j in range(9))==
sum(j*v[5,j] for j in range(9))+10*c[4])#
# C_4 = M
p.add_constraint(c[4]==sum(j*v[4,j] for j in range(9)))
p.solve()
p.show()
xx=p.get_values(v)
show(xx)
for j in range(9) :
if xx[0,j]== 1 : print('S='+ `j`)
for j in range(9) :
if xx[1,j]== 1 : print('E='+ `j`)
for j in range(9) :
if xx[2,j]== 1 : print('N='+ `j`)
for j in range(9) :
if xx[3,j]== 1 : print('D='+ `j`)
for j in range(9) :
if xx[4,j]== 1 : print('M='+ `j`)
for j in range(9) :
if xx[5,j]== 1 : print('O='+ `j`)
for j in range(9) :
if xx[6,j]== 1 : print('R='+ `j`)
for j in range(9) :
if xx[7,j]== 1 : print('Y='+ `j`)
I have 8x10 = 80, v[i,j] variables v[i, j] and 4 c[j] variables. I have asked all to be binary. But when I look the printed model I see only 73 variables and from $x_{66}$ to $x_{73}$ they are not binary. More than that the (7,6) variables takes a value of 3/6. It's not particularly simple to associate the variables internal variables to the internal ones.
I need help! (Thanks by advance). The documentation is of no help for me.
MODELIZATION CLUES
In this so called "cryptarythm" one use 8 letters $S_{(1)}E_{(2)}N_{(3)}D_{(4)}M_{(5)}O_{(6)}R_{(7)}Y_{(8)}$ so we define the binary variables
$x_{ij} = \begin{cases} 1 & \text{if letter } i (i= 1, \ldots 8) \text{ is associated to th digit } j (j = 0,\ldots, 9)\\ 0 & \text{sinon} \end{cases}$
We need also 4 variables $C_k$ for the retention. From this one can write $S = \sum_{1}^9 j x_{1j }$, $E = \sum_{1}^9 j x_{2j }$ and so on. For the constraints one have 17 general constraints
--- each letter is associated to a unic digit $\sum_{j=0}^9 x_{ij} = 1, \text{ for }
i = 1, \ldots 8$
--- each digit is associated to a only one letter $\sum_{i=0}^8 x_{ij} \leq 1, \text{ for }
j = 1, \ldots 9$
Then one hase the pure addition constraints :
$D + E = Y + 10 C_1 \Longleftrightarrow \sum_{j=0}^9 j x_{2j} +\sum_{j=0}^9 j x_{4j}= \sum_{j=0}^9 j x_{8j} + 10 C_1$
$C_1 + R + N = Y + 10 C_2 \Longleftrightarrow C_1+\sum_{j=0}^9 j x_{3j} +\sum_{j=0}^9 j x_{4j}= \sum_{j=0}^9 j x_{2j} + 10 C_2$
$C_2 + E + O = N + 10 C_3 \Longleftrightarrow C_2+\sum_{j=0}^9 j x_{2j} +\sum_{j=0}^9 j x_{6j}= \sum_{j=0}^9 j x_{3j} + 10 C_3$
$C_3 + S + M = O + 10 C_4 \Longleftrightarrow C_3+\sum_{j=0}^9 j x_{1j} +\sum_{j=0}^9 j x_{5j}= \sum_{j=0}^9 j x_{6j} + 10 C_4$
$C_4 = M \Longleftrightarrow C_4 = \sum_{j=0}^9 j x_{5j}$
Here is the solution
$(S=9). (E=5). (N=6). (D=7).$
$+. (M=1). (O=0).(R=8). (E=5).$
$= (M=1). (O=0). (N=6). (E=5). (y=2)$
If all variables are binary, you can do directly
Can you give more details on the problem (input, output, objective, constraints) in text, not code.
Sorry David, I have just seen your commentarySEND+MORE = MONEY is an exemple of a game which consists to substitute a letter to a decimal digit You can find the substitution by logical reasoning. Here we try to model tje problem as a feasibility test. I will see if I could add in my question the theory.