0

我在 google collab with python 中使用 docplex

对于接下来的 LP,一些决策变量是预先确定的,需要为此求解 LP。这是一个序列问题,序列是一组给定的值。其他决策变量将在此基础上进行优化。

#Define the decision variables

x = cost.continuous_var_dict(P, name='x') # The landing time of plane i
alpha = cost.continuous_var_dict(P, name='alpha') # How much of deviation of landing before target 
landing time for plane i
beta = cost.continuous_var_dict(P, name='beta') # How much of deviation of landing after target 
landing time for plane i
delta = cost.binary_var_dict(plane_matrix,name="delta") # 1 if plane i lands before plane j; 0 o/w
z = cost.binary_var_dict(plane_matrix, name="z") # 1 if plane i and j land on same runway; 0 o/w
y = cost.binary_var_dict(plane_runway, name="y") # 1 if plane j lands on runway r; 0 o/w

所以给定的值是针对增量的,有一个约束可以满足这一点,如下所示

# Constraint 2: either plane i lands before j or j lands before i
cost.add_constraints(delta[i,j] + delta[j,i] == 1 for i in P for j in P if j!=i)

但是,我收到如下错误:

DOcplexException                          Traceback (most recent call last)
<ipython-input-23-441ca8cbb9d0> in <module>()
      3 
      4 # #Constraint 2: either i lands before j or j lands before i
----> 5 cost.add_constraints(delta[i,j] + delta[j,i] == 1 for i in P for j in P if j!=i)
      6 
      7 # #Constraint 3: Each plane can land on only one runway

 4 frames
 /usr/local/lib/python3.6/dist-packages/docplex/mp/model.py in add_constraints(self, cts, names)
 3514             return self._lfactory._new_constraint_block2(cts, names)
 3515         else:
 -> 3516             return self._lfactory._new_constraint_block1(cts)
 3517 
 3518 

/usr/local/lib/python3.6/dist-packages/docplex/mp/mfactory.py in _new_constraint_block1(self, cts)
891                     posted_cts.append(ct)
892         else:
--> 893             checker.typecheck_constraint_seq(ctseq, check_linear=True, accept_range=True)
894             for ct in ctseq:
895                 if filterfn(ct, ctname=None, check_for_trivial_ct=check_trivial, 
arg_checker=checker):

/usr/local/lib/python3.6/dist-packages/docplex/mp/tck.py in typecheck_constraint_seq(self, cts, 
check_linear, accept_range)
354         for i, ct in enumerate(checked_cts_list):
355             if not isinstance(ct, AbstractConstraint):
--> 356                 self.fatal("Expecting sequence of constraints, got: {0!r} at position {1}", 
ct, i)
357             if check_linear:
358                 if not ct.is_linear():

/usr/local/lib/python3.6/dist-packages/docplex/mp/tck.py in fatal(self, msg, *args)
229 
230     def fatal(self, msg, *args):
--> 231         self._logger.fatal(msg, args)
232 
233     def error(self, msg, *args):  # pragma: no cover

/usr/local/lib/python3.6/dist-packages/docplex/mp/error_handler.py in fatal(self, msg, args)
208         resolved_message = resolve_pattern(msg, args)
209         docplex_error_stop_here()
--> 210         raise DOcplexException(resolved_message)
211 
212     def fatal_limits_exceeded(self):

DOcplexException: Expecting sequence of constraints, got: True at position 0

请帮忙。我真的不明白为什么这是一个问题。谢谢

4

2 回答 2

1

您确定您的所有元素delta实际上都是决策变量吗?这段代码对我来说很好:

with Model() as m:
    P = range(10)
    delta = m.binary_var_dict((i,j) for i in P for j in P)
    m.add_constraints(delta[i,j] + delta[j,i] == 1 for i in P for j in P if j!=i)

但是,如果我做这样的事情(我用delta纯数字替换一些元素),我可能会引发你的错误:

with Model() as m:
    P = range(10)
    delta = m.binary_var_dict((i,j) for i in P for j in P)
    delta[0,1] = 0.5
    delta[1,0] = 0.5
    m.add_constraints(delta[i,j] + delta[j,i] == 1 for i in P for j in P if j!=i)

在这种情况下, 的一些实例delta[i,j] + delta[j,i] == 1不再包含任何决策变量,因此==运算符不会创建约束,而只是在布尔上下文中评估并给出True.

您可以将这些变量固定为一个值,但设置变量的下限和上限,而不是用数字替换变量:

delta[0,1].set_lb(0.5)
delta[0,1].set_ub(0.5)
delta[1,0].set_lb(0.5)
delta[1,0].set_ub(0.5)

这样你的陈述仍然会产生约束。仅具有固定变量的约束将在 presolve 中删除。

于 2019-11-22T09:46:04.777 回答
1

为了补充丹尼尔的回答:请注意 Model.add_constraints (final s) 需要一个带有约束的可迭代对象,因此它不会接受一个单一的约束。如果要传递可迭代或单个约束,请使用同时接受两者的“Model.add()”。

于 2019-11-22T10:47:31.053 回答