2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2021-06-08 10:41:40 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2025-11-05 11:34:49 +01:00
|
|
|
#ifndef ORTOOLS_SAT_CP_MODEL_MAPPING_H_
|
|
|
|
|
#define ORTOOLS_SAT_CP_MODEL_MAPPING_H_
|
2021-06-08 10:41:40 +02:00
|
|
|
|
2025-03-07 10:33:36 +01:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <utility>
|
2021-06-08 10:41:40 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "absl/container/flat_hash_set.h"
|
2023-05-24 11:42:11 +02:00
|
|
|
#include "absl/log/check.h"
|
2021-06-08 10:41:40 +02:00
|
|
|
#include "ortools/base/logging.h"
|
|
|
|
|
#include "ortools/base/strong_vector.h"
|
|
|
|
|
#include "ortools/sat/cp_model.pb.h"
|
|
|
|
|
#include "ortools/sat/cp_model_utils.h"
|
2024-11-29 14:27:36 +01:00
|
|
|
#include "ortools/sat/integer_base.h"
|
2024-12-04 17:47:10 +01:00
|
|
|
#include "ortools/sat/linear_constraint.h"
|
2021-06-08 10:41:40 +02:00
|
|
|
#include "ortools/sat/model.h"
|
|
|
|
|
#include "ortools/sat/sat_base.h"
|
2022-02-07 14:31:18 +01:00
|
|
|
#include "ortools/util/strong_integers.h"
|
2021-06-08 10:41:40 +02:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
namespace sat {
|
|
|
|
|
|
|
|
|
|
// For an optimization problem, this contains the internal integer objective
|
|
|
|
|
// to minimize and information on how to display it correctly in the logs.
|
|
|
|
|
struct ObjectiveDefinition {
|
|
|
|
|
double scaling_factor = 1.0;
|
|
|
|
|
double offset = 0.0;
|
|
|
|
|
IntegerVariable objective_var = kNoIntegerVariable;
|
|
|
|
|
|
|
|
|
|
// The objective linear expression that should be equal to objective_var.
|
|
|
|
|
// If not all proto variable have an IntegerVariable view, then some vars
|
|
|
|
|
// will be set to kNoIntegerVariable. In practice, when this is used, we make
|
|
|
|
|
// sure there is a view though.
|
|
|
|
|
std::vector<IntegerVariable> vars;
|
|
|
|
|
std::vector<IntegerValue> coeffs;
|
|
|
|
|
|
|
|
|
|
// List of variable that when set to their lower bound should help getting a
|
|
|
|
|
// better objective. This is used by some search heuristic to preferably
|
|
|
|
|
// assign any of the variable here to their lower bound first.
|
|
|
|
|
absl::flat_hash_set<IntegerVariable> objective_impacting_variables;
|
|
|
|
|
|
|
|
|
|
double ScaleIntegerObjective(IntegerValue value) const {
|
|
|
|
|
return (ToDouble(value) + offset) * scaling_factor;
|
|
|
|
|
}
|
2024-03-21 11:34:50 +01:00
|
|
|
|
|
|
|
|
double ScaleObjective(double value) const {
|
|
|
|
|
return (value + offset) * scaling_factor;
|
|
|
|
|
}
|
2021-06-08 10:41:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Holds the mapping between CpModel proto indices and the sat::model ones.
|
|
|
|
|
//
|
|
|
|
|
// This also holds some information used when loading a CpModel proto.
|
|
|
|
|
class CpModelMapping {
|
|
|
|
|
public:
|
|
|
|
|
// Returns true if the given CpModelProto variable reference refers to a
|
|
|
|
|
// Boolean variable. Such variable will always have an associated Literal(),
|
|
|
|
|
// but not always an associated Integer().
|
|
|
|
|
bool IsBoolean(int ref) const {
|
|
|
|
|
DCHECK_LT(PositiveRef(ref), booleans_.size());
|
|
|
|
|
return booleans_[PositiveRef(ref)] != kNoBooleanVariable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IsInteger(int ref) const {
|
|
|
|
|
DCHECK_LT(PositiveRef(ref), integers_.size());
|
|
|
|
|
return integers_[PositiveRef(ref)] != kNoIntegerVariable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sat::Literal Literal(int ref) const {
|
|
|
|
|
DCHECK(IsBoolean(ref));
|
|
|
|
|
return sat::Literal(booleans_[PositiveRef(ref)], RefIsPositive(ref));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntegerVariable Integer(int ref) const {
|
|
|
|
|
DCHECK(IsInteger(ref));
|
|
|
|
|
const IntegerVariable var = integers_[PositiveRef(ref)];
|
|
|
|
|
return RefIsPositive(ref) ? var : NegationOf(var);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(user): We could "easily" create an intermediate variable for more
|
|
|
|
|
// complex linear expression. We could also identify duplicate expressions to
|
|
|
|
|
// not create two identical integer variable.
|
2021-10-27 13:22:27 +02:00
|
|
|
AffineExpression Affine(const LinearExpressionProto& exp) const {
|
2021-06-08 10:41:40 +02:00
|
|
|
CHECK_LE(exp.vars().size(), 1);
|
|
|
|
|
if (exp.vars().empty()) {
|
|
|
|
|
return AffineExpression(IntegerValue(exp.offset()));
|
|
|
|
|
}
|
|
|
|
|
return AffineExpression(Integer(exp.vars(0)), IntegerValue(exp.coeffs(0)),
|
|
|
|
|
IntegerValue(exp.offset()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntervalVariable Interval(int i) const {
|
|
|
|
|
CHECK_GE(i, 0);
|
|
|
|
|
CHECK_LT(i, intervals_.size());
|
|
|
|
|
CHECK_NE(intervals_[i], kNoIntervalVariable);
|
|
|
|
|
return intervals_[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename List>
|
|
|
|
|
std::vector<IntegerVariable> Integers(const List& list) const {
|
|
|
|
|
std::vector<IntegerVariable> result;
|
2024-01-10 16:42:04 +01:00
|
|
|
result.reserve(list.size());
|
2021-06-08 10:41:40 +02:00
|
|
|
for (const auto i : list) result.push_back(Integer(i));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ProtoIndices>
|
|
|
|
|
std::vector<sat::Literal> Literals(const ProtoIndices& indices) const {
|
|
|
|
|
std::vector<sat::Literal> result;
|
2024-01-10 16:42:04 +01:00
|
|
|
result.reserve(indices.size());
|
2021-06-08 10:41:40 +02:00
|
|
|
for (const int i : indices) result.push_back(CpModelMapping::Literal(i));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 14:53:05 +01:00
|
|
|
template <typename List>
|
|
|
|
|
std::vector<AffineExpression> Affines(const List& list) const {
|
|
|
|
|
std::vector<AffineExpression> result;
|
2024-01-10 16:42:04 +01:00
|
|
|
result.reserve(list.size());
|
2021-11-25 14:53:05 +01:00
|
|
|
for (const auto& i : list) result.push_back(Affine(i));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 10:41:40 +02:00
|
|
|
template <typename ProtoIndices>
|
|
|
|
|
std::vector<IntervalVariable> Intervals(const ProtoIndices& indices) const {
|
|
|
|
|
std::vector<IntervalVariable> result;
|
2024-01-10 16:42:04 +01:00
|
|
|
result.reserve(indices.size());
|
2021-06-08 10:41:40 +02:00
|
|
|
for (const int i : indices) result.push_back(Interval(i));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Depending on the option, we will load constraints in stages. This is used
|
|
|
|
|
// to detect constraints that are already loaded. For instance the interval
|
|
|
|
|
// constraints and the linear constraint of size 1 (encodings) are usually
|
|
|
|
|
// loaded first.
|
|
|
|
|
bool ConstraintIsAlreadyLoaded(const ConstraintProto* ct) const {
|
|
|
|
|
return already_loaded_ct_.contains(ct);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if the given constraint is a "half-encoding" constraint. That
|
|
|
|
|
// is, if it is of the form (b => size 1 linear) but there is no (<=) side in
|
|
|
|
|
// the model. Such constraint are detected while we extract integer encoding
|
|
|
|
|
// and are cached here so that we can deal properly with them during the
|
|
|
|
|
// linear relaxation.
|
|
|
|
|
bool IsHalfEncodingConstraint(const ConstraintProto* ct) const {
|
|
|
|
|
return is_half_encoding_ct_.contains(ct);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Note that both these functions returns positive reference or -1.
|
|
|
|
|
int GetProtoVariableFromBooleanVariable(BooleanVariable var) const {
|
|
|
|
|
if (var.value() >= reverse_boolean_map_.size()) return -1;
|
|
|
|
|
return reverse_boolean_map_[var];
|
|
|
|
|
}
|
|
|
|
|
int GetProtoVariableFromIntegerVariable(IntegerVariable var) const {
|
2025-09-26 15:52:20 +02:00
|
|
|
DCHECK(VariableIsPositive(var));
|
|
|
|
|
const PositiveOnlyIndex index = GetPositiveOnlyIndex(var);
|
|
|
|
|
if (index >= reverse_integer_map_.end_index()) return -1;
|
|
|
|
|
return reverse_integer_map_[index];
|
2021-06-08 10:41:40 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-30 17:28:08 +02:00
|
|
|
// This one should only be used when we have a mapping.
|
|
|
|
|
int GetProtoLiteralFromLiteral(sat::Literal lit) const {
|
|
|
|
|
const int proto_var = GetProtoVariableFromBooleanVariable(lit.Variable());
|
|
|
|
|
DCHECK_NE(proto_var, -1);
|
|
|
|
|
return lit.IsPositive() ? proto_var : NegatedRef(proto_var);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 10:41:40 +02:00
|
|
|
const std::vector<IntegerVariable>& GetVariableMapping() const {
|
|
|
|
|
return integers_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinearExpression GetExprFromProto(
|
|
|
|
|
const LinearExpressionProto& expr_proto) const {
|
|
|
|
|
LinearExpression expr;
|
|
|
|
|
expr.vars = Integers(expr_proto.vars());
|
|
|
|
|
for (int j = 0; j < expr_proto.coeffs_size(); ++j) {
|
|
|
|
|
expr.coeffs.push_back(IntegerValue(expr_proto.coeffs(j)));
|
|
|
|
|
}
|
|
|
|
|
expr.offset = IntegerValue(expr_proto.offset());
|
|
|
|
|
return CanonicalizeExpr(expr);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-07 10:33:36 +01:00
|
|
|
// Returns the min/max activity of the linear constraint under the current
|
|
|
|
|
// integer_trail bounds.
|
|
|
|
|
std::pair<int64_t, int64_t> ComputeMinMaxActivity(
|
|
|
|
|
const LinearConstraintProto& proto, IntegerTrail* integer_trail) {
|
|
|
|
|
int64_t sum_min = 0;
|
|
|
|
|
int64_t sum_max = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < proto.vars_size(); ++i) {
|
|
|
|
|
const int64_t coeff = proto.coeffs(i);
|
|
|
|
|
const IntegerVariable var = this->Integer(proto.vars(i));
|
|
|
|
|
const int64_t lb = integer_trail->LowerBound(var).value();
|
|
|
|
|
const int64_t ub = integer_trail->UpperBound(var).value();
|
|
|
|
|
if (coeff >= 0) {
|
|
|
|
|
sum_min += coeff * lb;
|
|
|
|
|
sum_max += coeff * ub;
|
|
|
|
|
} else {
|
|
|
|
|
sum_min += coeff * ub;
|
|
|
|
|
sum_max += coeff * lb;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {sum_min, sum_max};
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 10:41:40 +02:00
|
|
|
// For logging only, these are not super efficient.
|
|
|
|
|
int NumIntegerVariables() const {
|
|
|
|
|
int result = 0;
|
|
|
|
|
for (const IntegerVariable var : integers_) {
|
|
|
|
|
if (var != kNoIntegerVariable) result++;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
int NumBooleanVariables() const {
|
|
|
|
|
int result = 0;
|
|
|
|
|
for (const BooleanVariable var : booleans_) {
|
|
|
|
|
if (var != kNoBooleanVariable) result++;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the number of variables in the loaded proto.
|
|
|
|
|
int NumProtoVariables() const { return integers_.size(); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
friend void LoadVariables(const CpModelProto& model_proto,
|
|
|
|
|
bool view_all_booleans_as_integers, Model* m);
|
|
|
|
|
friend void ExtractEncoding(const CpModelProto& model_proto, Model* m);
|
|
|
|
|
|
|
|
|
|
// Note that only the variables used by at least one constraint will be
|
|
|
|
|
// created, the other will have a kNo[Integer,Interval,Boolean]VariableValue.
|
|
|
|
|
std::vector<IntegerVariable> integers_;
|
|
|
|
|
std::vector<IntervalVariable> intervals_;
|
|
|
|
|
std::vector<BooleanVariable> booleans_;
|
|
|
|
|
|
|
|
|
|
// Recover from a IntervalVariable/BooleanVariable its associated CpModelProto
|
|
|
|
|
// index. The value of -1 is used to indicate that there is no correspondence
|
|
|
|
|
// (i.e. this variable is only used internally).
|
2024-07-12 13:56:11 +02:00
|
|
|
util_intops::StrongVector<BooleanVariable, int> reverse_boolean_map_;
|
2025-09-26 15:52:20 +02:00
|
|
|
util_intops::StrongVector<PositiveOnlyIndex, int> reverse_integer_map_;
|
2021-06-08 10:41:40 +02:00
|
|
|
|
|
|
|
|
// Set of constraints to ignore because they were already dealt with by
|
|
|
|
|
// ExtractEncoding().
|
|
|
|
|
absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
|
|
|
|
|
absl::flat_hash_set<const ConstraintProto*> is_half_encoding_ct_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace sat
|
|
|
|
|
} // namespace operations_research
|
|
|
|
|
|
2025-11-05 11:34:49 +01:00
|
|
|
#endif // ORTOOLS_SAT_CP_MODEL_MAPPING_H_
|