首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PYOMO,组合集

PYOMO,组合集
EN

Stack Overflow用户
提问于 2022-09-03 09:25:28
回答 1查看 29关注 0票数 0

我希望你在PYOMO LP方面提供帮助。我不知道我做错了什么,所以任何反馈都会有帮助:

我的数据集如下所示:

代码语言:javascript
复制
#The demand for each order
demand= {782912: 808, 782913: 3188, 782914: 2331, 782915: 847, 782916: 2163,789954:5643}

#The cost per unit produced for each order based in which factory chosen
total cost= { (782912, 'PLANT16'): 0.46, (782913, 'PLANT16'): 0.46, (782914, 'PLANT16'): 0.46, (782915, 'PLANT16'): 0.46, (782916, 'PLANT16'): 0.46}, (789954,'PLANT05'):0.90,(789954,'PLANT07'):0.91,(789954,'PLANT08'):1.13,(789954,'PLANT10'):0.12}

#The capacity of each factory
supply= {'PLANT05': 531,'PLANT07': 841,'PLANT08': 1107,'PLANT10': 981,'PLANT16': 2313}

#定义模型

代码语言:javascript
复制
model=pyo.ConcreteModel()
#Sets
model.i=pyo.Set(initialize=demand.keys())#orders
model.j=pyo.Set(initialize=supply.keys()) #factories
model.select_combos = pyo.Set(within = model.i * model.j, initialize = total_costs_per_unit.keys())
#Parameters

model.p=pyo.Param(model.i,model.j,initialize=total_costs_per_unit) # here goes the cost dictionary
p=model.p

model.d=pyo.Param(model.i,initialize=demand)
d=model.d

model.s=pyo.Param(model.j,initialize=supply)
s=model.s

#Decision variable
model.x=pyo.Var(model.i,model.j,within=pyo.NonNegativeReals)
x=model.x

#Objective function
def obj_rul(model):

  return sum(p[i,j]*x[i,j]  for i,j in model.select_combos)
#warning , not all combinations of i,j exist in my model.p, as they would not be valid solutions for the problem
model.Obj=pyo.Objective(rule=obj_rul,sense=pyo.minimize)

#Constraints

def Const1(model,i):
  return sum(x[i,j] for j in model.j)>=d[i]
model.condemand=pyo.Constraint(model.i,rule=Const1)

def Const5(model,j):
  return sum(x[i,j] for i in model.i)<=s[j]
model.consupply=pyo.Constraint(model.j,rule=Const5)

#Solving
Solver=SolverFactory('glpk')

results=Solver.solve(model)

print(results)
print('Obj funct=',model.Obj())
for i in model.i:
  for j in model.j:
    print('for order',i," from plant ",j, " is sent ",x[i,j]())

我收到的错误消息是:

代码语言:javascript
复制
ERROR:pyomo.core:evaluating object as numeric value: x[782912,PLANT16]
    (object: <class 'pyomo.core.base.var._GeneralVarData'>)
No value for uninitialized NumericValue object x[782912,PLANT16]

总之,我认为如果我有‘订单’和‘植物’的所有组合,它就不会有任何问题。因为现在我不知道如何解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-04 05:26:10

你在正确的轨道上,你的怀疑是正确的。

因为模型中根本没有使用i, j的某些组合,所以它们永远不会被初始化,当您试图打印它们的值时,您会遇到问题。在您的模型中还有其他问题处理相同的事情。

所以你有几个选择。

您有一个“隐含的”约束,如果您没有生产成本,那么它不能在该工厂,所以您可以添加到您的数据,并在“生产上限”为每一个项目-工厂组合.不是真正的乐趣。

或者你可以为“非法”物品的生产投入很大的成本,这样解决者就不会去挑选它们。

但是您已经完成了部分工作,我认为这是最好的解决方案,就是创建一组i, j的“合法组合”。你只是没有把它带到终点线。您可以/应该使用这个子集作为自然遵循它的事物的基础,比如xp。看看我的密码。您只需要小心只使用合法的i, j组合的总和,这很容易通过筛选它们来完成,我在更新的约束中显示了这一点。

注意:我把你所有的能力乘以10倍,因为最初的模型是不可行的。是的,这是运行的,您应该能够根据需要修改它。

代码

代码语言:javascript
复制
import pyomo.environ as pyo

#The demand for each order
demand= {782912: 808, 782913: 3188, 782914: 2331, 782915: 847, 782916: 2163,789954:5643}

#The cost per unit produced for each order based in which factory chosen
production_cost= {  (782912, 'PLANT16'): 0.46, 
                    (782913, 'PLANT16'): 0.46, 
                    (782914, 'PLANT16'): 0.46, 
                    (782915, 'PLANT16'): 0.46, 
                    (782916, 'PLANT16'): 0.46, 
                    (789954, 'PLANT05'): 0.90,
                    (789954, 'PLANT07'): 0.91,
                    (789954, 'PLANT08'): 1.13,
                    (789954, 'PLANT10'): 0.12}

#The capacity of each factory
supply= {'PLANT05': 5310,'PLANT07': 8410,'PLANT08': 11070,'PLANT10': 9810,'PLANT16': 23130}

model=pyo.ConcreteModel()

#Sets
model.i=pyo.Set(initialize=demand.keys()) #orders
model.j=pyo.Set(initialize=supply.keys()) #factories
model.select_combos = pyo.Set(within = model.i * model.j, initialize = production_cost.keys())

#Parameters
model.p=pyo.Param(model.select_combos, initialize=production_cost) # here goes the cost dictionary
# p=model.p  <-- I wouldn't rename things...confusing

model.d=pyo.Param(model.i,initialize=demand)
# d=model.d

model.s=pyo.Param(model.j,initialize=supply)
# s=model.s

#Decision variable
model.x=pyo.Var(model.select_combos, within=pyo.NonNegativeReals)
# x=model.x

#Objective function
# def obj_rul(model):
#   return sum(p[i,j]*x[i,j]  for i,j in model.select_combos)
#warning , not all combinations of i,j exist in my model.p, as they would not be valid solutions for the problem

# your objective is a simple sum() expression, so you don't need the complication of a rule-function combo...
model.Obj=pyo.Objective(expr=sum(model.p[i,j]*model.x[i,j] for i,j in model.select_combos),sense=pyo.minimize)

#Constraints

def Const1(model,i):
  return sum(model.x[i,j] for j in model.j if (i,j) in model.select_combos)>=model.d[i]
model.condemand=pyo.Constraint(model.i,rule=Const1)

def Const5(model,j):
  return sum(model.x[i,j] for i in model.i if (i,j) in model.select_combos)<=model.s[j]
model.consupply=pyo.Constraint(model.j,rule=Const5)

#Solving
Solver=pyo.SolverFactory('glpk')

results=Solver.solve(model)

print(results)
#print('Obj funct=',model.Obj())
for i, j in model.select_combos:
  print('for order',i," from plant ",j, " is sent ", pyo.value(model.x[i,j]))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73591086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档