Processing math: 100%
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

asked 5 years ago

Cyrille gravatar image

SEND+MORE=MONEY (one more time)

I have some heavy problems of formulation with Sagemath. It's certainly a formulation problem :

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/pdf/10.1287/ited.2016.0163. 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 x66 to x73 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.

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/pdf/10.1287/ited.2016.0163. 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 x66 to x73 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

xij={1if letter i(i=1,8) is associated to th digit j(j=0,,9)0sinon

We need also 4 variables Ck for the retention. From this one can write S=91jx1j, E=91jx2j and so on. For the constraints one have 17 general constraints

--- each letter is associated to a unic digit 9j=0xij=1, for i=1,8
--- each digit is associated to a only one letter 8i=0xij1, for j=1,9

Then one hase the pure addition constraints :

D+E=Y+10C19j=0jx2j+9j=0jx4j=9j=0jx8j+10C1

C1+R+N=Y+10C2C1+9j=0jx3j+9j=0jx4j=9j=0jx2j+10C2

C2+E+O=N+10C3C2+9j=0jx2j+9j=0jx6j=9j=0jx3j+10C3

C3+S+M=O+10C4C3+9j=0jx1j+9j=0jx5j=9j=0jx6j+10C4

C4=MC4=9j=0jx5j

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)

click to hide/show revision 3
retagged

updated 1 year ago

FrédéricC gravatar image

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/pdf/10.1287/ited.2016.0163. 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 x66 to x73 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

xij={1if letter i(i=1,8) is associated to th digit j(j=0,,9)0sinon

We need also 4 variables Ck for the retention. From this one can write S=91jx1j, E=91jx2j and so on. For the constraints one have 17 general constraints

--- each letter is associated to a unic digit 9j=0xij=1, for i=1,8
--- each digit is associated to a only one letter 8i=0xij1, for j=1,9

Then one hase the pure addition constraints :

D+E=Y+10C19j=0jx2j+9j=0jx4j=9j=0jx8j+10C1

C1+R+N=Y+10C2C1+9j=0jx3j+9j=0jx4j=9j=0jx2j+10C2

C2+E+O=N+10C3C2+9j=0jx2j+9j=0jx6j=9j=0jx3j+10C3

C3+S+M=O+10C4C3+9j=0jx1j+9j=0jx5j=9j=0jx6j+10C4

C4=MC4=9j=0jx5j

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)