如何使用 Add Multiplication Equality 添加约束?

How to use AddMultiplicationEquality to add constrain?

例如,我有下面的数字列表。并且限制是相邻两个数的差距应该是正数或负数,或者两个数之一等于零,也就是说一正一负(例如3和-1)是不可接受的。

List numbers: [15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 18, 17, 20, 19, 22, 25, 25, 25, 25, 25, 25, 25, 25, 22, 20, 23, 22, 21, 18, 15, 15, 15, 15, 15, 15, 15, 18, 21, 24, 24, 24, 27, 30]

Numbers' gap: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, -1, 3, -1, 3, 3, 0, 0, 0, 0, 0, 0, 0, -3, -2, 3, -1, -1, -3, -3, 0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 3, 3]

所以我尝试使用AddMultiplicationEquality来添加约束

target=model.NewIntVar(-999999,999999,'target_%s_%s'%(i, i+1))
model.AddMultiplicationEquality(target, [gap[i],gap[i+1]])
model.Add(target>=0)

但是我得到了下面的结果。我该如何解决这个问题?提前致谢。

Not supported
*** Check failure stack trace: ***
@        0x10956c8ed  google::LogMessage::Fail()
@        0x10956b29e  google::LogMessage::SendToLog()
@        0x10956bebf  google::LogMessage::Flush()
@        0x109570c19  google::LogMessageFatal::~LogMessageFatal()
@        0x10956cf55  google::LogMessageFatal::~LogMessageFatal()
@        0x10a7948e4  _ZZN19operations_research3sat17ProductConstraintEN3gtl7IntTypeINS0_20IntegerVariable_tag_EiEES4_S4_ENKUlPNS0_5ModelEE_clES6_
@        0x10a787dfc  operations_research::sat::LoadIntProdConstraint()
@        0x10a7916da  operations_research::sat::LoadConstraint()
@        0x10a7c36e1  operations_research::sat::CpModelPresolver::Probe()
@        0x10a7ceba8  operations_research::sat::CpModelPresolver::Presolve()
@        0x10a7ce756  operations_research::sat::PresolveCpModel()
@        0x10a7e3f7a  operations_research::sat::SolveCpModel()

如评论中所见,7.7 后已修复此问题

现在,如果不使用乘法的结果,使用AddMultiplicationEquality是低效的。

对于每个间隙编号,我会创建两个布尔变量。

is_gt_zero[i] = model.NewBoolVar('')
model.Add(gap[i] > 0).OnlyEnforceIf(is_gt_zero[i])
model.Add(gap[i] <= 0).OnlyEnforceIf(is_gt_zero[i].Not())
is_lt_zero[i] = model.NewBoolVar('')
model.Add(gap[i] < 0).OnlyEnforceIf(is_lt_zero[i])
model.Add(gap[i] >= 0).OnlyEnforceIf(is_lt_zero[i].Not())

然后强制兼容性。

model.AddImplication(is_gt_zero[i], is_lt_zero[i + 1].Not())
model.AddImplication(is_lt_zero[i], is_gt_zero[i + 1].Not())