cp_sat 模型构建后是否可以修改?

Is it possible to modify a cp_sat model after construction?

我有一个模型可以找到特定的 class 个整数("Keith numbers"),它运行良好,但速度很慢,因为它需要多次构建新模型。有没有办法更新模型,特别是改变约束中的系数。换句话说,更改模型以匹配不同的 mat,而不重建整个模型?

def _construct_model(self, mat):
  model = cp_model.CpModel()
  digit = [model.NewIntVar(0, 9, f'digit[{i}]') for i in range(self.k)]

  # Creates the constraint.
  model.Add(sum([mat[i] * digit[i] for i in range(self.k)]) == 0)

  model.Add(digit[0] != 0)
  return model, digit

是的,但你是一个人。

您可以从模型中访问底层 cp_model_proto protobuf,并直接修改它。

他目前没有计划在 cp_model API 的基础上添加修改 API。