我在 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
请帮忙。我真的不明白为什么这是一个问题。谢谢