我是约束编程的初学者,我在我的 c# 程序中使用Google or-tools 库。
我想向我的求解器添加以下约束:
((t1 >= 12 && t1 <= 15) || (t2 >= 16 && t2 <= 18)) && ( t1 + t2 ) < 30
所以我用c#写了下面一段代码:
var solver = new Solver("My_CP_Colver");
var t1 = solver.MakeIntVar(12, 20,"t1");
var t2 = solver.MakeIntVar(12, 20,"t2");
solver.Add(???)//<-((t1 >= 12 && t1 <= 15)||(t2 >= 16 && t2 <= 18)) && ( t1 + t2 ) < 30
Run Code Online (Sandbox Code Playgroud)
请帮助做出上述限制?
设置
我使用 google OR 工具作为约束编程求解器:
from ortools.sat.python import cp_model
Run Code Online (Sandbox Code Playgroud)
我定义了以下 BoolVars
model = cp_model.CpModel()
a = model.NewBoolVar("a")
b = model.NewBoolVar("b")
c = model.NewBoolVar("c")
d = model.NewBoolVar("d")
e = model.NewBoolVar("e")
f = model.NewBoolVar("f")
g = model.NewBoolVar("g")
Run Code Online (Sandbox Code Playgroud)
问题
我需要向模型添加复杂的布尔约束。就像是
(a || b) && (d || e) == g
Run Code Online (Sandbox Code Playgroud)
如何将这样的复杂布尔约束添加到模型中?
PS:我无法立即在网上找到此信息,但根据我在此处的相关问题和此处另一个人的另一个相关问题上得到的答案找到了解决方案。我以问答的形式总结了我的发现,希望它们对某人有用。