remove more usage of 0LL and 1LL; reformat code

This commit is contained in:
Laurent Perron
2019-09-12 10:11:02 +02:00
parent 8dda73dedb
commit 2dbf3895f2
8 changed files with 41 additions and 13 deletions

View File

@@ -734,7 +734,8 @@ void CpModelBuilder::AddDecisionStrategy(
}
void CpModelBuilder::AddHint(IntVar var, int64 value) {
cp_model_.mutable_solution_hint()->add_vars(GetOrCreateIntegerIndex(var.index_));
cp_model_.mutable_solution_hint()->add_vars(
GetOrCreateIntegerIndex(var.index_));
cp_model_.mutable_solution_hint()->add_values(value);
}

View File

@@ -99,18 +99,18 @@ bool PresolveContext::IsFixed(int ref) const {
bool PresolveContext::LiteralIsTrue(int lit) const {
if (!IsFixed(lit)) return false;
if (RefIsPositive(lit)) {
return domains[lit].Min() == 1ll;
return domains[lit].Min() == 1;
} else {
return domains[PositiveRef(lit)].Max() == 0ll;
return domains[PositiveRef(lit)].Max() == 0;
}
}
bool PresolveContext::LiteralIsFalse(int lit) const {
if (!IsFixed(lit)) return false;
if (RefIsPositive(lit)) {
return domains[lit].Max() == 0ll;
return domains[lit].Max() == 0;
} else {
return domains[PositiveRef(lit)].Min() == 1ll;
return domains[PositiveRef(lit)].Min() == 1;
}
}
@@ -182,7 +182,7 @@ ABSL_MUST_USE_RESULT bool PresolveContext::IntersectDomainWith(
ABSL_MUST_USE_RESULT bool PresolveContext::SetLiteralToFalse(int lit) {
const int var = PositiveRef(lit);
const int64 value = RefIsPositive(lit) ? 0ll : 1ll;
const int64 value = RefIsPositive(lit) ? 0 : 1;
return IntersectDomainWith(var, Domain(value));
}
@@ -4016,7 +4016,7 @@ bool CpModelPresolver::ProcessSetPPC() {
uint64 signature = 0;
for (const int literal : constraint_literals.back()) {
const int positive_literal = PositiveRef(literal);
signature |= (1LL << (positive_literal % 64));
signature |= (int64{1} << (positive_literal % 64));
DCHECK_GE(positive_literal, 0);
if (positive_literal >= literals_to_constraints.size()) {
literals_to_constraints.resize(positive_literal + 1);

View File

@@ -63,6 +63,7 @@ namespace Google.OrTools.Sat
{
return new SumArray(vars, coeffs);
}
public static LinearExpr ScalProd(IEnumerable<IntVar> vars, IEnumerable<long> coeffs)
{
return new SumArray(vars, coeffs);

View File

@@ -18,6 +18,7 @@
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_loader.h"
#include "ortools/sat/integer.h"
#include "ortools/sat/integer_expr.h"
#include "ortools/sat/linear_constraint.h"
#include "ortools/sat/linear_programming_constraint.h"
#include "ortools/sat/sat_base.h"
@@ -496,6 +497,7 @@ void AppendMaxRelaxation(IntegerVariable target,
// Part 2: Encode upper bound on X.
if (linearization_level < 2) return;
GenericLiteralWatcher* watcher = model->GetOrCreate<GenericLiteralWatcher>();
// For size = 2, we do this with 1 less variable.
IntegerEncoder* encoder = model->GetOrCreate<IntegerEncoder>();
if (vars.size() == 2) {
@@ -503,8 +505,21 @@ void AppendMaxRelaxation(IntegerVariable target,
const Literal y_lit =
encoder->GetOrCreateLiteralAssociatedToEquality(y, IntegerValue(1));
AppendEnforcedUpperBound(y_lit, target, vars[0], model, relaxation);
// TODO(user,user): It makes more sense to use ConditionalLowerOrEqual()
// here, but that degrades perf on the road*.fzn problem. Understand why.
IntegerSumLE* upper_bound1 = new IntegerSumLE(
{y_lit}, {target, vars[0]}, {IntegerValue(1), IntegerValue(-1)},
IntegerValue(0), model);
upper_bound1->RegisterWith(watcher);
model->TakeOwnership(upper_bound1);
AppendEnforcedUpperBound(y_lit.Negated(), target, vars[1], model,
relaxation);
IntegerSumLE* upper_bound2 = new IntegerSumLE(
{y_lit.Negated()}, {target, vars[1]},
{IntegerValue(1), IntegerValue(-1)}, IntegerValue(0), model);
upper_bound2->RegisterWith(watcher);
model->TakeOwnership(upper_bound2);
return;
}
// For each X_i, we encode y_i => X <= X_i. And at least one of the y_i is
@@ -513,6 +528,8 @@ void AppendMaxRelaxation(IntegerVariable target,
// TODO(user): Only lower bound is needed, experiment.
LinearConstraintBuilder lc_exactly_one(model, IntegerValue(1),
IntegerValue(1));
std::vector<Literal> exactly_one_literals;
exactly_one_literals.reserve(vars.size());
for (const IntegerVariable var : vars) {
if (target == var) continue;
// y => X <= X_i.
@@ -521,10 +538,18 @@ void AppendMaxRelaxation(IntegerVariable target,
IntegerVariable y = model->Add(NewIntegerVariable(0, 1));
const Literal y_lit =
encoder->GetOrCreateLiteralAssociatedToEquality(y, IntegerValue(1));
AppendEnforcedUpperBound(y_lit, target, var, model, relaxation);
IntegerSumLE* upper_bound_constraint = new IntegerSumLE(
{y_lit}, {target, var}, {IntegerValue(1), IntegerValue(-1)},
IntegerValue(0), model);
upper_bound_constraint->RegisterWith(watcher);
model->TakeOwnership(upper_bound_constraint);
exactly_one_literals.push_back(y_lit);
CHECK(lc_exactly_one.AddLiteralTerm(y_lit, IntegerValue(1)));
}
model->Add(ExactlyOneConstraint(exactly_one_literals));
relaxation->linear_constraints.push_back(lc_exactly_one.Build());
}

View File

@@ -116,7 +116,7 @@ struct ConstraintScaler {
double max_scaling_factor = 0.0;
double wanted_precision = 1e-6;
int64 scaling_target = 1LL << 50;
int64 scaling_target = int64{1} << 50;
std::vector<double> coefficients;
std::vector<double> lower_bounds;
std::vector<double> upper_bounds;
@@ -313,7 +313,7 @@ bool ConvertMPModelProtoToCpModelProto(const SatParameters& params,
<< kSmallDomainSize << " values.";
ConstraintScaler scaler;
const int64 kScalingTarget = 1LL << params.mip_max_activity_exponent();
const int64 kScalingTarget = int64{1} << params.mip_max_activity_exponent();
scaler.wanted_precision = kWantedPrecision;
scaler.scaling_target = kScalingTarget;

View File

@@ -906,8 +906,8 @@ class CpModel(object):
Args:
variables: A list of variables.
tuples_list: A list of forbidden tuples. Each tuple must have the same
length as the variables, and the *i*th value of a tuple corresponds
to the *i*th variable.
length as the variables, and the *i*th value of a tuple corresponds to
the *i*th variable.
Returns:
An instance of the `Constraint` class.

View File

@@ -502,7 +502,7 @@ void SatPresolver::SimpleBva(LiteralIndex l) {
uint64 SatPresolver::ComputeSignatureOfClauseVariables(ClauseIndex ci) {
uint64 signature = 0;
for (const Literal l : clauses_[ci]) {
signature |= (1ULL << (l.Variable().value() % 64));
signature |= (uint64{1} << (l.Variable().value() % 64));
}
DCHECK_EQ(signature == 0, clauses_[ci].empty());
return signature;

View File

@@ -152,7 +152,8 @@ void SharedResponseManager::UpdateInnerObjectiveBounds(
// Overflow.
max_integral = kMaxIntegerValue;
}
UpdatePrimalIntegral(/*max_integral=*/std::max(int64{0}, max_integral.value()));
UpdatePrimalIntegral(
/*max_integral=*/std::max(int64{0}, max_integral.value()));
}
if (lb > inner_objective_lower_bound_) {
inner_objective_lower_bound_ = lb.value();