如何获取无效的OR-Tools CP-SAT模型的详细错误消息?

huangapple go评论69阅读模式
英文:

How to get detailed error messages for invalid OR-Tools CP-SAT models?

问题

我正在尝试使用CP-SAT求解器重新创建Google OR-Tools的Stigler Diet示例,而不是使用线性求解器,但结果显示状态为MODEL_INVALID。我不知道如何获取详细的错误消息或获取有关为什么模型无效的任何其他详细信息。

查看Python源代码时,在MODEL_INVALID定义旁边的代码注释中说:“给定的CpModelProto未通过验证步骤。您可以通过调用ValidateCpModel(model_proto)来获取详细的错误信息。” 但是,我无法弄清楚如何访问ValidateCpModel(),它在文档或or-tools存储库中的任何示例中都没有被引用。

供参考,这是我正在尝试运行的程序:

from ortools.sat.python import cp_model
import sys

# 每种营养素的最小和最大值
nutritional_requirements = [
    [1, 2],  # 维生素A
    [1, 2],  # 维生素B
    [1, 2],  # 维生素C
]

# 每种食物的营养价值
foods = [
  # 维生素
  #  A  B  C
    [1, 0, 0],  # 食物A
    [0, 1, 0],  # 食物B
    [0, 0, 1],  # 食物C
]

model = cp_model.CpModel()

quantity_of_food = [model.NewIntVar(0, sys.maxsize, str(i)) for i in range(len(foods))]

for i, nutrient in enumerate(nutritional_requirements):
    model.Add(
        sum([food[i] * quantity_of_food[i] for food in foods]) >= nutrient[0]
    )
    model.Add(
        sum([food[i] * quantity_of_food[i] for food in foods]) <= nutrient[1]
    )

model.Minimize(sum(quantity_of_food))

solver = cp_model.CpSolver()
status = solver.Solve(model)

outcomes = [
    "UNKNOWN",
    "MODEL_INVALID",
    "FEASIBLE",
    "INFEASIBLE",
    "OPTIMAL",
]

print(outcomes[status])  # 输出 "MODEL_INVALID"

对我来说,这个程序看起来相当简单。我如何找到详细的错误消息,解释为什么模型无效?

英文:

I'm trying to recreate the Google OR-Tools Stigler Diet example using the CP-SAT solver instead of the linear solver, and it results in a status of MODEL_INVALID. I don't know how to get detailed error messages or get any additional details as to why the model is invalid.

Looking into the Python source code, a code comment next to the definition of MODEL_INVALID says "The given CpModelProto didn't pass the validation step. You can get a detailed error by calling ValidateCpModel(model_proto)." But, I can't figure out how to access ValidateCpModel() and it's not referenced in the docs or any examples in the or-tools repo.

For reference, here's the program I'm trying to run:

from ortools.sat.python import cp_model
import sys


# Minimum and maximum values for each nutrient
nutritional_requirements = [  
    [1, 2],  # Vitamin A
    [1, 2],  # Vitamin B
    [1, 2],  # Vitamin C
]

# Nutritional value for each food
foods = [
  # Vitamins
  #  A  B  C
    [1, 0, 0],  # Food A
    [0, 1, 0],  # Food B
    [0, 0, 1],  # Food C
]

model = cp_model.CpModel()

quantity_of_food = [model.NewIntVar(0, sys.maxsize, str(i)) for i in range(len(foods))]

for i, nutrient in enumerate(nutritional_requirements):
    model.Add(
        sum([food[i] * quantity_of_food[i] for food in foods]) &gt;= nutrient[0]
    )
    model.Add(
        sum([food[i] * quantity_of_food[i] for food in foods]) &lt; nutrient[1]
    )

model.Minimize(sum(quantity_of_food))

solver = cp_model.CpSolver()
status = solver.Solve(model)

outcomes = [
    &quot;UNKNOWN&quot;,
    &quot;MODEL_INVALID&quot;,
    &quot;FEASIBLE&quot;,
    &quot;INFEASIBLE&quot;,
    &quot;OPTIMAL&quot;,
]

print(outcomes[status])  # Prints &quot;MODEL_INVALID&quot;

This program seems pretty simple to me. How do I find a detailed error message explaining why the model is invalid?

答案1

得分: 3

有两个选项:
- solver.parameters.log_search_progress = True


- print(model.Validate())

输出

```plaintext
无效模型:变量#0的域不在[kint64min + 2,kint64max - 1]范围内。名称:“0”域:0域:9223372036854775807

CP-SAT求解器拒绝可能导致整数算术溢出的模型。
特别是,变量的域应远离int64max。

将变量的域尽量减小是一个良好的实践。这样可以简化之后的求解过程。


<details>
<summary>英文:</summary>

There are two options:
  - solver.parameters.log_search_progress = True

or

  - print(model.Validate())



outputs 

Invalid model: var #0 domain do not fall in [kint64min + 2, kint64max - 1]. name: "0" domain: 0 domain: 9223372036854775807


CP-SAT solvers rejects models that can overflow integer arithmetic.
In particular, domains of variables should stay away from int64max.

It is good practice to reduce the domain of variables as much as possible. It simplifies solving afterwards.

</details>



huangapple
  • 本文由 发表于 2023年7月17日 12:49:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定