使用逻辑约束
描述在 OPL 中如何基于 CPLEX 解算引擎自动变换逻辑约束。
您在约束有哪些?中已经了解到如何表示此问题的逻辑约束。 但是,您还应该再来了解一下,因为它们描述了有关 OPL 中基于 CPLEX 解算引擎的逻辑约束和自动变换的要点。
forall( m in Months ) {
// Using some constraints as boolean expressions to state that at least
// 2 of the given 5 constraints must be true.
ctUse7:
(Use[m]["v1"] == 0) + (Use[m]["v2"] == 0) + (Use[m]["o1"] == 0) +
(Use[m]["o2"] == 0) + (Use[m]["o3"] == 0) >= 2;
// Using the "or" operator, set each Use variable to be
// zero or greater than 20.
forall( p in Products )
ctUse8:
(Use[m][p] == 0) || (Use[m][p] >= 20);
// Using "or" and "implication" operator, set that if one of 2 given products
// is used more than 20 then a third one must be used more than 20 too.
ctUse9:
(Use[m]["v1"] >= 20) || (Use[m]["v2"] >= 20) => Use[m]["o3"] >= 20;
例如,假设这样一种约束,即混合产品在一个批次中不能使用三种以上类型的油。 在此约束下,许多程序员可能会自然地编写以下语句(或类似语句):
(use[i][v1] != 0)
+ (use[i][v2] != 0)
+ (use[i][o1] != 0)
+ (use[i][o2] != 0)
+ (use[i][o3] != 0)
<= 3;
该语句表示相同的约束,而没有更改问题的解法集。 但是,公式是不同的,可能会导致不同的运行时间和不同的用于搜索树的内存量。 换言之,如果存在逻辑英语表达式,可能存在用于表示的多种逻辑约束,并且不同的逻辑约束在计算时间和内存方面有不同的表现。
《语言参考手册》约束部分中的“CPLEX 逻辑约束”引入了超负荷逻辑运算符,您可以用于在 OPL 中组合线性或分段线性约束。 在该示例中,请注意这些逻辑约束中出现的逻辑运算符 ==、>= 和 ||。