[CP-SAT] more usage of absl::Span

This commit is contained in:
Laurent Perron
2024-02-15 14:30:17 +01:00
parent c50163eb1e
commit 303eb48e38
13 changed files with 32 additions and 26 deletions

View File

@@ -2006,6 +2006,7 @@ cc_library(
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/random:bit_gen_ref",
"@com_google_absl//absl/random:distributions",
"@com_google_absl//absl/types:span",
],
)

View File

@@ -327,9 +327,10 @@ std::unique_ptr<Graph> GenerateGraphForSymmetryDetection(
const LinearExpressionProto& target_expr =
constraint.lin_max().target();
std::vector<int64_t> target_color = color;
target_color.push_back(target_expr.offset());
const int target_node = new_node(target_color);
std::vector<int64_t> local_color = color;
local_color.push_back(target_expr.offset());
const int target_node = new_node(local_color);
local_color.pop_back();
for (int j = 0; j < target_expr.vars_size(); ++j) {
const int var = target_expr.vars(j);
@@ -339,11 +340,14 @@ std::unique_ptr<Graph> GenerateGraphForSymmetryDetection(
}
for (int i = 0; i < constraint.lin_max().exprs_size(); ++i) {
// TODO(user): We can create a node per LinearExpressionProto instead.
// This will allow to reuse node between constraint, if they share a
// common expression.
const LinearExpressionProto& expr = constraint.lin_max().exprs(i);
std::vector<int64_t> local_color = color;
local_color.push_back(expr.offset());
const int local_node = new_node(local_color);
local_color.pop_back();
for (int j = 0; j < expr.vars().size(); ++j) {
const int var = expr.vars(j);

View File

@@ -255,7 +255,7 @@ ElementEncodings::GetElementEncodedVariables() const {
// the size2 affine.
std::vector<LiteralValueValue> TryToReconcileEncodings(
const AffineExpression& size2_affine, const AffineExpression& affine,
const std::vector<ValueLiteralPair>& affine_var_encoding,
absl::Span<const ValueLiteralPair> affine_var_encoding,
bool put_affine_left_in_result, IntegerEncoder* integer_encoder) {
IntegerVariable binary = size2_affine.var;
std::vector<LiteralValueValue> terms;

View File

@@ -147,7 +147,7 @@ bool ApplyLiteralMapping(
// TODO(user): Also check for no duplicates literals + unit tests.
bool BooleanLinearExpressionIsCanonical(
const std::vector<LiteralWithCoeff>& cst) {
absl::Span<const LiteralWithCoeff> cst) {
Coefficient previous(1);
for (LiteralWithCoeff term : cst) {
if (term.coefficient < previous) return false;
@@ -453,7 +453,7 @@ void UpperBoundedLinearConstraint::AddToConflict(
}
bool UpperBoundedLinearConstraint::HasIdenticalTerms(
const std::vector<LiteralWithCoeff>& cst) {
absl::Span<const LiteralWithCoeff> cst) {
if (cst.size() != literals_.size()) return false;
int literal_index = 0;
int coeff_index = 0;

View File

@@ -127,8 +127,7 @@ Coefficient ComputeNegatedCanonicalRhs(Coefficient lower_bound,
Coefficient max_value);
// Returns true iff the Boolean linear expression is in canonical form.
bool BooleanLinearExpressionIsCanonical(
const std::vector<LiteralWithCoeff>& cst);
bool BooleanLinearExpressionIsCanonical(absl::Span<const LiteralWithCoeff> cst);
// Given a Boolean linear constraint in canonical form, simplify its
// coefficients using simple heuristics.
@@ -391,7 +390,7 @@ class UpperBoundedLinearConstraint {
const std::vector<LiteralWithCoeff>& cst);
// Returns true if the given terms are the same as the one in this constraint.
bool HasIdenticalTerms(const std::vector<LiteralWithCoeff>& cst);
bool HasIdenticalTerms(absl::Span<const LiteralWithCoeff> cst);
Coefficient Rhs() const { return rhs_; }
// Sets the rhs of this constraint. Compute the initial threshold value using

View File

@@ -25,6 +25,7 @@
#include "absl/log/check.h"
#include "absl/random/bit_gen_ref.h"
#include "absl/random/distributions.h"
#include "absl/types/span.h"
#include "ortools/sat/cp_model_mapping.h"
#include "ortools/sat/integer.h"
#include "ortools/sat/linear_programming_constraint.h"
@@ -132,7 +133,7 @@ void FillRinsNeighborhood(const std::vector<int64_t>& solution,
}
}
void FillRensNeighborhood(const std::vector<double>& relaxation_values,
void FillRensNeighborhood(absl::Span<const double> relaxation_values,
double difficulty, absl::BitGenRef random,
ReducedDomainNeighborhood& reduced_domains) {
std::vector<VarWeight> var_fractionality_pairs;

View File

@@ -243,7 +243,7 @@ std::vector<int64_t> FindPossibleDemands(const EnergyEvent& event,
// This generates the actual cut and compute its activity vs the
// available_energy_lp.
bool CutIsEfficient(
const std::vector<EnergyEvent>& events, IntegerValue window_start,
absl::Span<const EnergyEvent> events, IntegerValue window_start,
IntegerValue window_end, double available_energy_lp,
const absl::StrongVector<IntegerVariable, double>& lp_values,
LinearConstraintBuilder* temp_builder) {
@@ -1026,7 +1026,7 @@ namespace {
//
// It returns false if one event cannot start before event.start_max.
bool ComputeWeightedSumOfEndMinsForOnePermutation(
const std::vector<PermutableEvent>& events, IntegerValue capacity_max,
absl::Span<const PermutableEvent> events, IntegerValue capacity_max,
IntegerValue& sum_of_ends, IntegerValue& sum_of_weighted_ends,
std::vector<std::pair<IntegerValue, IntegerValue>>& profile,
std::vector<std::pair<IntegerValue, IntegerValue>>& new_profile) {

View File

@@ -183,7 +183,7 @@ std::vector<int> GetOrbits(
}
std::vector<int> GetOrbitopeOrbits(
int n, const std::vector<std::vector<int>>& orbitope) {
int n, absl::Span<const std::vector<int>> orbitope) {
std::vector<int> orbits(n, -1);
for (int i = 0; i < orbitope.size(); ++i) {
for (int j = 0; j < orbitope[i].size(); ++j) {

View File

@@ -59,8 +59,8 @@ std::vector<int> GetOrbits(
// Returns the orbits under the given orbitope action.
// Same results format as in GetOrbits(). Note that here, the orbit index
// is simply the row index of an element in the orbitope matrix.
std::vector<int> GetOrbitopeOrbits(
int n, const std::vector<std::vector<int>>& orbitope);
std::vector<int> GetOrbitopeOrbits(int n,
absl::Span<const std::vector<int>> orbitope);
// Given the generators for a permutation group of [0, n-1], update it to
// a set of generators of the group stabilizing the given element.

View File

@@ -277,9 +277,9 @@ int64_t ClosestMultiple(int64_t value, int64_t base) {
}
bool LinearInequalityCanBeReducedWithClosestMultiple(
int64_t base, const std::vector<int64_t>& coeffs,
const std::vector<int64_t>& lbs, const std::vector<int64_t>& ubs,
int64_t rhs, int64_t* new_rhs) {
int64_t base, absl::Span<const int64_t> coeffs,
absl::Span<const int64_t> lbs, absl::Span<const int64_t> ubs, int64_t rhs,
int64_t* new_rhs) {
// Precompute some bounds for the equation base * X + error <= rhs.
int64_t max_activity = 0;
int64_t max_x = 0;

View File

@@ -251,9 +251,9 @@ std::vector<absl::Span<int>> AtMostOneDecomposition(
// Preconditions: All coeffs are assumed to be positive. You can easily negate
// all the negative coeffs and corresponding bounds before calling this.
bool LinearInequalityCanBeReducedWithClosestMultiple(
int64_t base, const std::vector<int64_t>& coeffs,
const std::vector<int64_t>& lbs, const std::vector<int64_t>& ubs,
int64_t rhs, int64_t* new_rhs);
int64_t base, absl::Span<const int64_t> coeffs,
absl::Span<const int64_t> lbs, absl::Span<const int64_t> ubs, int64_t rhs,
int64_t* new_rhs);
// The model "singleton" random engine used in the solver.
//

View File

@@ -39,8 +39,8 @@ void ZeroHalfCutHelper::Reset(int size) {
void ZeroHalfCutHelper::ProcessVariables(
const std::vector<double>& lp_values,
const std::vector<IntegerValue>& lower_bounds,
const std::vector<IntegerValue>& upper_bounds) {
absl::Span<const IntegerValue> lower_bounds,
absl::Span<const IntegerValue> upper_bounds) {
Reset(lp_values.size());
// Shift all variables to their closest bound.

View File

@@ -18,6 +18,7 @@
#include <utility>
#include <vector>
#include "absl/types/span.h"
#include "ortools/lp_data/lp_types.h"
#include "ortools/sat/integer.h"
#include "ortools/sat/util.h"
@@ -46,8 +47,8 @@ class ZeroHalfCutHelper {
// TODO(user): This is a first implementation, both the heuristic and the
// code performance can probably be improved uppon.
void ProcessVariables(const std::vector<double>& lp_values,
const std::vector<IntegerValue>& lower_bounds,
const std::vector<IntegerValue>& upper_bounds);
absl::Span<const IntegerValue> lower_bounds,
absl::Span<const IntegerValue> upper_bounds);
void AddOneConstraint(glop::RowIndex, absl::Span<const glop::ColIndex> cols,
absl::Span<const IntegerValue> coeffs, IntegerValue lb,
IntegerValue ub);