【问题标题】:DOcplexException: Expecting sequence of constraints, got: True at position 0DOcplexException:期望约束序列,得到:在位置 0 处为真
【发布时间】:2019-11-21 21:12:55
【问题描述】:

我在 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

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

【问题讨论】:

    标签: cplex docplex docplexcloud


    【解决方案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 中被移除。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2021-01-09
        • 1970-01-01
        • 2021-10-09
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        • 1970-01-01
        • 2020-08-16
        • 1970-01-01
        相关资源
        最近更新 更多