14 #ifndef OR_TOOLS_SAT_CP_MODEL_UTILS_H_
15 #define OR_TOOLS_SAT_CP_MODEL_UTILS_H_
26 #if !defined(__PORTABLE_PLATFORM__)
27 #include "google/protobuf/text_format.h"
30 #include "absl/container/flat_hash_set.h"
31 #include "absl/strings/match.h"
32 #include "absl/strings/string_view.h"
34 #include "ortools/sat/cp_model.pb.h"
47 return !
ct.enforcement_literal().empty();
50 return ct.enforcement_literal(0);
55 LinearExpressionProto* output_negated_expr);
90 template <
typename ProtoWithDomain>
92 for (
int i = 0; i <
proto.domain_size(); i += 2) {
99 template <
typename ProtoWithDomain>
101 proto->clear_domain();
110 template <
typename ProtoWithDomain>
112 #if defined(__PORTABLE_PLATFORM__)
114 {
proto.domain().begin(),
proto.domain().end()});
124 template <
typename ProtoWithDomain>
126 std::vector<int64_t> result;
127 for (
int i = 0; i <
proto.domain_size(); i += 2) {
128 for (int64_t v =
proto.domain(i); v <=
proto.domain(i + 1); ++v) {
129 CHECK_LE(result.size(), 1e6);
139 double result =
static_cast<double>(
value);
141 result = -std::numeric_limits<double>::infinity();
143 result = std::numeric_limits<double>::infinity();
144 result +=
proto.offset();
145 if (
proto.scaling_factor() == 0)
return result;
146 return proto.scaling_factor() * result;
152 if (
proto.integer_scaling_factor() == 0) {
155 return (
value +
proto.integer_before_offset()) *
156 proto.integer_scaling_factor() +
157 proto.integer_after_offset();
163 double result =
value;
164 if (
proto.scaling_factor() != 0) {
165 result /=
proto.scaling_factor();
167 return result -
proto.offset();
174 absl::Span<const int64_t> solution);
193 LinearConstraintProto* linear);
197 const LinearExpressionProto&
b,
198 int64_t b_scaling = 1);
206 const google::protobuf::RepeatedField<T>& sequence, uint64_t seed) {
207 return fasthash64(
reinterpret_cast<const char*
>(sequence.data()),
208 sequence.size() *
sizeof(T), seed);
214 return fasthash64(
reinterpret_cast<const char*
>(&field),
sizeof(T), seed);
224 #if !defined(__PORTABLE_PLATFORM__)
248 #if defined(__PORTABLE_PLATFORM__)
251 if (absl::EndsWith(filename,
"txt")) {
252 std::string proto_string;
253 google::protobuf::TextFormat::Printer printer;
255 printer.PrintToString(
proto, &proto_string);
We call domain any subset of Int64 = [kint64min, kint64max].
static Domain FromFlatSpanOfIntervals(absl::Span< const int64_t > flat_intervals)
Same as FromIntervals() for a flattened representation (start, end, start, end, .....
int NumIntervals() const
Basic read-only std::vector<> wrapping to view a Domain as a sorted list of non-adjacent intervals.
static Domain FromFlatIntervals(const std::vector< int64_t > &flat_intervals)
This method is available in Python, Java and .NET.
absl::Status SetBinaryProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
uint64_t FingerprintRepeatedField(const google::protobuf::RepeatedField< T > &sequence, uint64_t seed)
std::vector< int > UsedVariables(const ConstraintProto &ct)
double UnscaleObjectiveValue(const CpObjectiveProto &proto, double value)
bool RefIsPositive(int ref)
std::vector< int > UsedIntervals(const ConstraintProto &ct)
void SetToNegatedLinearExpression(const LinearExpressionProto &input_expr, LinearExpressionProto *output_negated_expr)
bool HasEnforcementLiteral(const ConstraintProto &ct)
std::vector< int64_t > AllValuesInDomain(const ProtoWithDomain &proto)
bool WriteModelProtoToFile(const M &proto, absl::string_view filename)
bool DomainInProtoContains(const ProtoWithDomain &proto, int64_t value)
uint64_t FingerprintSingleField(const T &field, uint64_t seed)
double ScaleObjectiveValue(const CpObjectiveProto &proto, int64_t value)
void ApplyToAllLiteralIndices(const std::function< void(int *)> &f, ConstraintProto *ct)
bool LinearExpressionProtosAreEqual(const LinearExpressionProto &a, const LinearExpressionProto &b, int64_t b_scaling)
void ApplyToAllIntervalIndices(const std::function< void(int *)> &f, ConstraintProto *ct)
void FillDomainInProto(const Domain &domain, ProtoWithDomain *proto)
uint64_t FingerprintExpression(const LinearExpressionProto &lin, uint64_t seed)
bool ExpressionIsAffine(const LinearExpressionProto &expr)
Domain ReadDomainFromProto(const ProtoWithDomain &proto)
void ApplyToAllVariableIndices(const std::function< void(int *)> &f, ConstraintProto *ct)
int64_t ComputeInnerObjective(const CpObjectiveProto &objective, absl::Span< const int64_t > solution)
void SetupTextFormatPrinter(google::protobuf::TextFormat::Printer *printer)
IndexReferences GetReferencesUsedByConstraint(const ConstraintProto &ct)
constexpr uint64_t kDefaultFingerprintSeed
void AddLinearExpressionToLinearConstraint(const LinearExpressionProto &expr, int64_t coefficient, LinearConstraintProto *linear)
std::string ConstraintCaseName(ConstraintProto::ConstraintCase constraint_case)
int64_t ScaleInnerObjectiveValue(const CpObjectiveProto &proto, int64_t value)
int GetSingleRefFromExpression(const LinearExpressionProto &expr)
int EnforcementLiteral(const ConstraintProto &ct)
bool ExpressionContainsSingleRef(const LinearExpressionProto &expr)
uint64_t FingerprintModel(const CpModelProto &model, uint64_t seed)
Collection of objects used to extend the Constraint Solver library.
uint64_t fasthash64(const void *buf, size_t len, uint64_t seed)
Represents a closed interval [start, end].
std::vector< int > variables
std::vector< int > literals