【发布时间】:2021-09-08 05:06:16
【问题描述】:
我正在对重新优化模型进行建模,并且我想包含一个约束以减少初始解决方案与重新优化解决方案之间的距离。我正在安排人员安排,为此我想在重新优化的解决方案中对与初始解决方案不同的每个分配进行处罚。
在开始之前,我是优化模型的新手,我构建约束的方式可能是错误的。
#1 Extract the data from the initial solution of my main variable
ModelX_DictExtVal = model.x.extract_values()
# 2 Create a new binary variable which activate when the main variable `ModelX_DictExtVal[x,s,d]` of the initial
#solution is =1 (an employee n works days d and sifht s) and the value of `model.x[n,s,d]` of the reoptimized solution are different.
model.alpha_distance = Var(model.N_S_D, within=Binary)
#3 Model a constraint to activate my variable.
def constraint_distance(model, n, s, d):
v = ModelX_DictExtVal[n,s,d]
if v == 1 and ModelX_DictExtVal[n,s,d] != model.x[n,s,d]:
return model.alpha_distance[n,s,d] == 1
elif v == 0:
return model.alpha_distance[n,s,d] == 0
model.constraint_distance = Constraint(model.N_S_D, rule = constraint_distance)
#4 Penalize in my objective function every time the varaible is equal to one
ObjFunction = Objective(expr = sum(model.alpha_distance[n,s,d] * WeightDistance
for n in model.N for s in model.S for d in model.D))
问题:我不确定我在第 3 部分中在做什么,当v == 1 时出现索引错误。
ERROR: Rule failed when generating expression for constraint
constraint_distance with index (0, 'E', 6): ValueError: Constraint
'constraint_distance[0,E,6]': rule returned None
我想知道,如果模型在重新优化阶段而不是新分配期间保持model.x [n, s, d] 的初始解决方案的值来进行比较ModelX_DictExtVal [n, s, d]! = model.x [n, s, d] ,那么我正在重复使用相同的模型进行重新优化。 ..
【问题讨论】:
标签: python optimization model pyomo