14#ifndef OR_TOOLS_SAT_CP_MODEL_UTILS_H_
15#define OR_TOOLS_SAT_CP_MODEL_UTILS_H_
24#if !defined(__PORTABLE_PLATFORM__)
27#include "absl/log/check.h"
28#include "absl/status/status.h"
29#include "absl/strings/match.h"
30#include "absl/strings/string_view.h"
31#include "absl/types/span.h"
32#include "google/protobuf/message.h"
33#include "google/protobuf/text_format.h"
36#include "ortools/sat/cp_model.pb.h"
49 return !
ct.enforcement_literal().empty();
52 return ct.enforcement_literal(0);
65 LinearExpressionProto* output_negated_expr);
77 std::vector<int>* variables,
78 std::vector<int>* literals);
93 ConstraintProto::ConstraintCase constraint_case);
103template <
typename Set>
106 const ConstraintProto&
ct = model_proto.constraints(
index);
112template <
typename ProtoWithDomain>
114 for (
int i = 0;
i <
proto.domain_size();
i += 2) {
121template <
typename ProtoWithDomain>
123 proto->clear_domain();
132template <
typename ProtoWithDomain>
134#if defined(__PORTABLE_PLATFORM__)
136 {
proto.domain().begin(),
proto.domain().end()});
146template <
typename ProtoWithDomain>
148 std::vector<int64_t> result;
149 for (
int i = 0;
i <
proto.domain_size();
i += 2) {
150 for (int64_t v =
proto.domain(
i); v <=
proto.domain(
i + 1); ++v) {
151 CHECK_LE(result.size(), 1e6);
161 double result =
static_cast<double>(
value);
162 if (
value == std::numeric_limits<int64_t>::min())
163 result = -std::numeric_limits<double>::infinity();
164 if (
value == std::numeric_limits<int64_t>::max())
165 result = std::numeric_limits<double>::infinity();
166 result +=
proto.offset();
167 if (
proto.scaling_factor() == 0)
return result;
168 return proto.scaling_factor() * result;
174 if (
proto.integer_scaling_factor() == 0) {
177 return (
value +
proto.integer_before_offset()) *
178 proto.integer_scaling_factor() +
179 proto.integer_after_offset();
185 double result =
value;
186 if (
proto.scaling_factor() != 0) {
187 result /=
proto.scaling_factor();
189 return result -
proto.offset();
196 absl::Span<const int64_t>
solution);
215 LinearConstraintProto* linear);
219 const LinearExpressionProto& expr, int64_t
coefficient,
220 LinearConstraintProto* linear);
224 const LinearExpressionProto&
b,
225 int64_t b_scaling = 1);
228template <
class ExpressionList>
231 for (
const LinearExpressionProto& expr : exprs) {
232 for (
const int var : expr.vars()) {
234 if (unique_var == -1) {
236 }
else if (
var != unique_var) {
241 return unique_var != -1;
249 const google::protobuf::RepeatedField<T>& sequence, uint64_t seed) {
250 if (sequence.empty())
return seed;
251 return fasthash64(
reinterpret_cast<const char*
>(sequence.data()),
252 sequence.size() *
sizeof(T), seed);
257 return fasthash64(
reinterpret_cast<const char*
>(&field),
sizeof(T), seed);
267#if !defined(__PORTABLE_PLATFORM__)
291#if defined(__PORTABLE_PLATFORM__)
294 if (absl::EndsWith(filename,
"txt") ||
295 absl::EndsWith(filename,
"textproto")) {
296 std::string proto_string;
297 google::protobuf::TextFormat::Printer printer;
299 printer.PrintToString(
proto, &proto_string);
311 const BoolArgumentProto& rhs) {
312 if (absl::MakeConstSpan(lhs.literals()) !=
313 absl::MakeConstSpan(rhs.literals())) {
316 if (lhs.literals_size() != rhs.literals_size())
return false;
317 for (
int i = 0;
i < lhs.literals_size(); ++
i) {
318 if (lhs.literals(
i) != rhs.literals(
i))
return false;
325 for (
const int lit : m.literals()) {
326 h = H::combine(std::move(h),
lit);
332 const LinearConstraintProto& rhs) {
333 if (absl::MakeConstSpan(lhs.vars()) != absl::MakeConstSpan(rhs.vars())) {
336 if (absl::MakeConstSpan(lhs.coeffs()) != absl::MakeConstSpan(rhs.coeffs())) {
339 if (absl::MakeConstSpan(lhs.domain()) != absl::MakeConstSpan(rhs.domain())) {
347 for (
const int var : m.vars()) {
348 h = H::combine(std::move(h),
var);
350 for (
const int64_t coeff : m.coeffs()) {
351 h = H::combine(std::move(h), coeff);
353 for (
const int64_t
bound : m.domain()) {
354 h = H::combine(std::move(h),
bound);
static Domain FromFlatIntervals(const std::vector< int64_t > &flat_intervals)
static Domain FromFlatSpanOfIntervals(absl::Span< const int64_t > flat_intervals)
CpModelProto proto
The output proto.
absl::Status SetBinaryProto(absl::string_view filename, const google::protobuf::Message &proto, Options options)
absl::Status SetContents(absl::string_view filename, absl::string_view contents, Options options)
void SetToNegatedLinearExpression(const LinearExpressionProto &input_expr, LinearExpressionProto *output_negated_expr)
Fills the target as negated ref.
constexpr uint64_t kDefaultFingerprintSeed
Default seed for fingerprints.
uint64_t FingerprintRepeatedField(const google::protobuf::RepeatedField< T > &sequence, uint64_t seed)
double UnscaleObjectiveValue(const CpObjectiveProto &proto, double value)
Removes the objective scaling and offset from the given value.
bool RefIsPositive(int ref)
int64_t ComputeInnerObjective(const CpObjectiveProto &objective, absl::Span< const int64_t > solution)
bool operator==(const BoolArgumentProto &lhs, const BoolArgumentProto &rhs)
bool HasEnforcementLiteral(const ConstraintProto &ct)
Small utility functions to deal with half-reified constraints.
int CombineSeed(int base_seed, int64_t delta)
We assume delta >= 0 and we only use the low bit of delta.
bool WriteModelProtoToFile(const M &proto, absl::string_view filename)
bool DomainInProtoContains(const ProtoWithDomain &proto, int64_t value)
void ApplyToAllIntervalIndices(const std::function< void(int *)> &f, ConstraintProto *ct)
bool SafeAddLinearExpressionToLinearConstraint(const LinearExpressionProto &expr, int64_t coefficient, LinearConstraintProto *linear)
Same method, but returns if the addition was possible without overflowing.
uint64_t FingerprintSingleField(const T &field, uint64_t seed)
double ScaleObjectiveValue(const CpObjectiveProto &proto, int64_t value)
Scales back a objective value to a double value from the original model.
bool ConvertCpModelProtoToCnf(const CpModelProto &cp_model, std::string *out)
uint64_t FingerprintExpression(const LinearExpressionProto &lin, uint64_t seed)
Returns a stable fingerprint of a linear expression.
uint64_t FingerprintModel(const CpModelProto &model, uint64_t seed)
Returns a stable fingerprint of a model.
bool ExpressionIsAffine(const LinearExpressionProto &expr)
Checks if the expression is affine or constant.
std::vector< int > UsedVariables(const ConstraintProto &ct)
std::vector< int > UsedIntervals(const ConstraintProto &ct)
Returns the sorted list of interval used by a constraint.
void FillDomainInProto(const Domain &domain, ProtoWithDomain *proto)
Serializes a Domain into the domain field of a proto.
H AbslHashValue(H h, const IntVar &i)
– ABSL HASHING SUPPORT --------------------------------------------------—
Domain ReadDomainFromProto(const ProtoWithDomain &proto)
Reads a Domain from the domain field of a proto.
bool ExpressionsContainsOnlyOneVar(const ExpressionList &exprs)
Returns true if there exactly one variable appearing in all the expressions.
void SetupTextFormatPrinter(google::protobuf::TextFormat::Printer *printer)
absl::string_view ConstraintCaseName(ConstraintProto::ConstraintCase constraint_case)
bool ExpressionContainsSingleRef(const LinearExpressionProto &expr)
Returns true if a linear expression can be reduced to a single ref.
int64_t LinearExpressionGcd(const LinearExpressionProto &expr, int64_t gcd)
void ApplyToAllLiteralIndices(const std::function< void(int *)> &f, ConstraintProto *ct)
void DivideLinearExpression(int64_t divisor, LinearExpressionProto *expr)
void AddLinearExpressionToLinearConstraint(const LinearExpressionProto &expr, int64_t coefficient, LinearConstraintProto *linear)
int GetSingleRefFromExpression(const LinearExpressionProto &expr)
int64_t ScaleInnerObjectiveValue(const CpObjectiveProto &proto, int64_t value)
Similar to ScaleObjectiveValue() but uses the integer version.
int EnforcementLiteral(const ConstraintProto &ct)
int NegatedRef(int ref)
Small utility functions to deal with negative variable/literal references.
bool LinearExpressionProtosAreEqual(const LinearExpressionProto &a, const LinearExpressionProto &b, int64_t b_scaling)
Returns true iff a == b * b_scaling.
IndexReferences GetReferencesUsedByConstraint(const ConstraintProto &ct)
void InsertVariablesFromConstraint(const CpModelProto &model_proto, int index, Set &output)
Insert variables in a constraint into a set.
void ApplyToAllVariableIndices(const std::function< void(int *)> &f, ConstraintProto *ct)
std::vector< int64_t > AllValuesInDomain(const ProtoWithDomain &proto)
In SWIG mode, we don't want anything besides these top-level includes.
uint64_t fasthash64(const void *buf, size_t len, uint64_t seed)
std::vector< int > variables
std::vector< int > literals