Optimization pyomo模型约束规划;发送+;更多=金钱;任务

Optimization pyomo模型约束规划;发送+;更多=金钱;任务,optimization,constraints,solver,pyomo,Optimization,Constraints,Solver,Pyomo,试图构建一个pyomo模型来解决“发送+更多=金钱”任务 模型建筑: from pyomo.environ import * m = ConcreteModel() m.letters = Set(initialize = ['S', 'E', 'N', 'D', 'M', 'O', 'R', 'Y']) m.tens = Set(initialize = ['C1', 'C2', 'C3', 'C4']) m.let_val = Var(m.letters, bounds=(0,9

试图构建一个pyomo模型来解决“发送+更多=金钱”任务

模型建筑:

from pyomo.environ import *    
m = ConcreteModel()

m.letters = Set(initialize = ['S', 'E', 'N', 'D', 'M', 'O', 'R', 'Y'])
m.tens = Set(initialize = ['C1', 'C2', 'C3', 'C4'])

m.let_val = Var(m.letters, bounds=(0,9), initialize=9, within=Integers)
m.ten_val = Var(m.tens, within=Binary)

m.C1 = Constraint(expr =  m.ten_val['C3'] + m.let_val['S'] + m.let_val['M'] == m.let_val['O'] + m.ten_val['C4']*10)
m.C2 = Constraint(expr =  m.ten_val['C2'] + m.let_val['E'] + m.let_val['O'] == m.let_val['N'] + m.ten_val['C3']*10)
m.C3 = Constraint(expr =  m.ten_val['C1'] + m.let_val['N'] + m.let_val['R'] == m.let_val['E'] + m.ten_val['C2']*10)
m.C4 = Constraint(expr =  m.let_val['D'] + m.let_val['E'] == m.let_val['Y'] + m.ten_val['C1']*10)
m.C5 = Constraint(expr =  m.let_val['M'] == m.ten_val['C4'])
m.C6 = Constraint(expr =  m.let_val['M'] == 1)

m.f1 = Objective(expr = m.let_val['S']*1000 + m.let_val['E']*100 + m.let_val['N']*10 + m.let_val['D'] + 
                 m.let_val['M']*1000 + m.let_val['O']*100 + m.let_val['R']*10 + m.let_val['E'], sense=minimize)

solver = SolverFactory('glpk')
results = solver.solve(m)

print('  ', int((value(m.let_val['S'])*1000 + value(m.let_val['E'])*100 + value(m.let_val['N'])*10 + value(m.let_val['D']))))
print('+ ', int((value(m.let_val['M'])*1000 + value(m.let_val['O'])*100 + value(m.let_val['R'])*10 + value(m.let_val['E']))))
print('=', int((value(m.let_val['M'])*10000 + value(m.let_val['O'])*1000 + value(m.let_val['N'])*100 + value(m.let_val['E'])*10 + value(m.let_val['Y']))))
“glpk”解决方案:

   9000
+  1000
= 10000
   8942
+  1057
= 10000
from pyomo.environ import *

m = ConcreteModel()

m.letters = Set(initialize = ['S', 'E', 'N', 'D', 'M', 'O', 'R', 'Y'])
m.vals = RangeSet(0, 9)
m.tens = Set(initialize = ['C1', 'C2', 'C3', 'C4'])

m.ten_val = Var(m.tens, within=Binary, initialize=0)
m.x = Var(m.letters, m.vals, within=Binary, initialize=1)

def c_rule(m, i):
    return sum(m.x[i, j] for j in m.vals) <= 1 
m.col = Constraint(m.letters, rule=c_rule) # not more than one value for the letter

def r_rule(m, j):
    return sum(m.x[i, j] for i in m.letters) <= 1
m.row = Constraint(m.vals, rule=r_rule) # not more than one value for the digit

m.C1 = Constraint(expr = m.ten_val['C4'] == m.x['M', 1])
m.C2 = Constraint(expr = m.x['M', 1] == 1)
m.C3 = Constraint(expr =  m.ten_val['C3'] + sum(m.x['S', j]*j for j in m.vals) + m.x['M', 1] == sum(m.x['O', j]*j for j in m.vals) + m.ten_val['C4']*10)
m.C4 = Constraint(expr =  m.ten_val['C2'] + sum(m.x['E', j]*j for j in m.vals) + sum(m.x['O', j]*j for j in m.vals) == sum(m.x['N', j]*j for j in m.vals) + m.ten_val['C3']*10)             
m.C5 = Constraint(expr =  m.ten_val['C1'] + sum(m.x['N', j]*j for j in m.vals) + sum(m.x['R', j]*j for j in m.vals) == sum(m.x['E', j]*j for j in m.vals) + m.ten_val['C2']*10)
m.C6 = Constraint(expr =  sum(m.x['D', j]*j for j in m.vals) + sum(m.x['E', j]*j for j in m.vals) == sum(m.x['Y', j]*j for j in m.vals) + m.ten_val['C1']*10)

m.f1 = Objective(expr =  sum(m.x['S', j]*j for j in m.vals), sense=minimize) # we are searching for working solution, no matter what to minimize

solver = SolverFactory('glpk')
results = solver.solve(m);
“ipopt”解决方案:

   9000
+  1000
= 10000
   8942
+  1057
= 10000
from pyomo.environ import *

m = ConcreteModel()

m.letters = Set(initialize = ['S', 'E', 'N', 'D', 'M', 'O', 'R', 'Y'])
m.vals = RangeSet(0, 9)
m.tens = Set(initialize = ['C1', 'C2', 'C3', 'C4'])

m.ten_val = Var(m.tens, within=Binary, initialize=0)
m.x = Var(m.letters, m.vals, within=Binary, initialize=1)

def c_rule(m, i):
    return sum(m.x[i, j] for j in m.vals) <= 1 
m.col = Constraint(m.letters, rule=c_rule) # not more than one value for the letter

def r_rule(m, j):
    return sum(m.x[i, j] for i in m.letters) <= 1
m.row = Constraint(m.vals, rule=r_rule) # not more than one value for the digit

m.C1 = Constraint(expr = m.ten_val['C4'] == m.x['M', 1])
m.C2 = Constraint(expr = m.x['M', 1] == 1)
m.C3 = Constraint(expr =  m.ten_val['C3'] + sum(m.x['S', j]*j for j in m.vals) + m.x['M', 1] == sum(m.x['O', j]*j for j in m.vals) + m.ten_val['C4']*10)
m.C4 = Constraint(expr =  m.ten_val['C2'] + sum(m.x['E', j]*j for j in m.vals) + sum(m.x['O', j]*j for j in m.vals) == sum(m.x['N', j]*j for j in m.vals) + m.ten_val['C3']*10)             
m.C5 = Constraint(expr =  m.ten_val['C1'] + sum(m.x['N', j]*j for j in m.vals) + sum(m.x['R', j]*j for j in m.vals) == sum(m.x['E', j]*j for j in m.vals) + m.ten_val['C2']*10)
m.C6 = Constraint(expr =  sum(m.x['D', j]*j for j in m.vals) + sum(m.x['E', j]*j for j in m.vals) == sum(m.x['Y', j]*j for j in m.vals) + m.ten_val['C1']*10)

m.f1 = Objective(expr =  sum(m.x['S', j]*j for j in m.vals), sense=minimize) # we are searching for working solution, no matter what to minimize

solver = SolverFactory('glpk')
results = solver.solve(m);
Ipopt更接近,但还不够好。我必须帮助求解并添加约束(C5和C6)。没有它,答案是0

我找不到关于如何声明所有字母值应该彼此不同的指令的任何信息。 如何做到这一点

如何正确使用pyomo解决此类任务?

以下是一种解决方案:

   9000
+  1000
= 10000
   8942
+  1057
= 10000
from pyomo.environ import *

m = ConcreteModel()

m.letters = Set(initialize = ['S', 'E', 'N', 'D', 'M', 'O', 'R', 'Y'])
m.vals = RangeSet(0, 9)
m.tens = Set(initialize = ['C1', 'C2', 'C3', 'C4'])

m.ten_val = Var(m.tens, within=Binary, initialize=0)
m.x = Var(m.letters, m.vals, within=Binary, initialize=1)

def c_rule(m, i):
    return sum(m.x[i, j] for j in m.vals) <= 1 
m.col = Constraint(m.letters, rule=c_rule) # not more than one value for the letter

def r_rule(m, j):
    return sum(m.x[i, j] for i in m.letters) <= 1
m.row = Constraint(m.vals, rule=r_rule) # not more than one value for the digit

m.C1 = Constraint(expr = m.ten_val['C4'] == m.x['M', 1])
m.C2 = Constraint(expr = m.x['M', 1] == 1)
m.C3 = Constraint(expr =  m.ten_val['C3'] + sum(m.x['S', j]*j for j in m.vals) + m.x['M', 1] == sum(m.x['O', j]*j for j in m.vals) + m.ten_val['C4']*10)
m.C4 = Constraint(expr =  m.ten_val['C2'] + sum(m.x['E', j]*j for j in m.vals) + sum(m.x['O', j]*j for j in m.vals) == sum(m.x['N', j]*j for j in m.vals) + m.ten_val['C3']*10)             
m.C5 = Constraint(expr =  m.ten_val['C1'] + sum(m.x['N', j]*j for j in m.vals) + sum(m.x['R', j]*j for j in m.vals) == sum(m.x['E', j]*j for j in m.vals) + m.ten_val['C2']*10)
m.C6 = Constraint(expr =  sum(m.x['D', j]*j for j in m.vals) + sum(m.x['E', j]*j for j in m.vals) == sum(m.x['Y', j]*j for j in m.vals) + m.ten_val['C1']*10)

m.f1 = Objective(expr =  sum(m.x['S', j]*j for j in m.vals), sense=minimize) # we are searching for working solution, no matter what to minimize

solver = SolverFactory('glpk')
results = solver.solve(m);

干得好

关于所有不同约束的MIP公式,见:Williams,H.Paul和Yan,Hong(2001),《整数规划中约束满足的“所有不同”谓词的表示》,通知《计算杂志》,13(2)。96-103。@Erwin Kalvelagen。谢谢你的文章参考。有趣的是,在本文中,同样的任务也被回顾了。我还阅读了你2016年和2017年关于这个问题的其他帖子。这在理论上很清楚,但我没有足够的技能和经验在代码中实现它。因此,在实践中,这是完全不清楚的。我想知道为什么没有这个问题的代码示例。在pyomo文档中找不到任何内容。在经验交流论坛上找不到任何内容。对于初学者来说,在优化领域的实际应用太难了。