Skip to content

Commit 66dd2cb

Browse files
committed
[CP-SAT] improve code
1 parent 71c94a8 commit 66dd2cb

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

ortools/sat/cp_model_presolve.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12889,6 +12889,13 @@ bool ModelCopy::CopyLinear(const ConstraintProto& ct) {
1288912889

1289012890
DCHECK(!non_fixed_variables_.empty());
1289112891

12892+
if (non_fixed_variables_.size() == 1 && ct.enforcement_literal().empty()) {
12893+
context_->UpdateRuleStats("linear1: x in domain");
12894+
return context_->IntersectDomainWith(
12895+
non_fixed_variables_[0],
12896+
new_rhs.InverseMultiplicationBy(non_fixed_coefficients_[0]));
12897+
}
12898+
1289212899
ConstraintProto* new_ct = context_->working_model->add_constraints();
1289312900
FinishEnforcementCopy(new_ct);
1289412901
LinearConstraintProto* linear = new_ct->mutable_linear();

ortools/sat/cp_model_solver.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -810,12 +810,12 @@ void RestrictObjectiveUsingHint(CpModelProto* model_proto) {
810810
}
811811
}
812812

813-
// Returns false if there is a complete solution hint that is infeasible, or
814-
// true otherwise.
815-
bool TestSolutionHintForFeasibility(const CpModelProto& model_proto,
816-
SolverLogger* logger = nullptr,
817-
SharedResponseManager* manager = nullptr) {
818-
if (!model_proto.has_solution_hint()) return true;
813+
// Returns true iff there is a hint, and (ignoring fixed variables) if it is
814+
// complete and feasible.
815+
bool SolutionHintIsCompleteAndFeasible(
816+
const CpModelProto& model_proto, SolverLogger* logger = nullptr,
817+
SharedResponseManager* manager = nullptr) {
818+
if (!model_proto.has_solution_hint()) return false;
819819

820820
int num_active_variables = 0;
821821
int num_hinted_variables = 0;
@@ -837,7 +837,7 @@ bool TestSolutionHintForFeasibility(const CpModelProto& model_proto,
837837
logger, "The solution hint is incomplete: ", num_hinted_variables,
838838
" out of ", num_active_variables, " non fixed variables hinted.");
839839
}
840-
return true;
840+
return false;
841841
}
842842

843843
std::vector<int64_t> solution(model_proto.variables_size(), 0);
@@ -1350,7 +1350,7 @@ class LnsSolver : public SubSolver {
13501350
bool hint_feasible_before_presolve = false;
13511351
if (lns_parameters_.debug_crash_if_presolve_breaks_hint()) {
13521352
hint_feasible_before_presolve =
1353-
TestSolutionHintForFeasibility(lns_fragment, /*logger=*/nullptr);
1353+
SolutionHintIsCompleteAndFeasible(lns_fragment, /*logger=*/nullptr);
13541354
}
13551355

13561356
// If we use a hint, we will restrict the objective to be <= to the one
@@ -1389,7 +1389,8 @@ class LnsSolver : public SubSolver {
13891389

13901390
if (lns_parameters_.debug_crash_if_presolve_breaks_hint() &&
13911391
hint_feasible_before_presolve &&
1392-
!TestSolutionHintForFeasibility(lns_fragment, /*logger=*/nullptr)) {
1392+
!SolutionHintIsCompleteAndFeasible(lns_fragment,
1393+
/*logger=*/nullptr)) {
13931394
LOG(FATAL) << "Presolve broke a feasible LNS hint. The model name is '"
13941395
<< lns_fragment.name()
13951396
<< "' (use the --cp_model_dump_submodels flag to dump it).";
@@ -2372,7 +2373,7 @@ CpSolverResponse SolveCpModel(const CpModelProto& model_proto, Model* model) {
23722373
bool hint_feasible_before_presolve = false;
23732374
if (!context->ModelIsUnsat()) {
23742375
hint_feasible_before_presolve =
2375-
TestSolutionHintForFeasibility(model_proto, logger);
2376+
SolutionHintIsCompleteAndFeasible(model_proto, logger);
23762377
}
23772378

23782379
// If the objective was a floating point one, do some postprocessing on the
@@ -2708,11 +2709,11 @@ CpSolverResponse SolveCpModel(const CpModelProto& model_proto, Model* model) {
27082709
// different.
27092710
bool hint_feasible_after_presolve;
27102711
if (!params.enumerate_all_solutions()) {
2711-
hint_feasible_after_presolve = TestSolutionHintForFeasibility(
2712+
hint_feasible_after_presolve = SolutionHintIsCompleteAndFeasible(
27122713
*new_cp_model_proto, logger, shared_response_manager);
27132714
} else {
27142715
hint_feasible_after_presolve =
2715-
TestSolutionHintForFeasibility(*new_cp_model_proto, logger, nullptr);
2716+
SolutionHintIsCompleteAndFeasible(*new_cp_model_proto, logger, nullptr);
27162717
}
27172718

27182719
if (hint_feasible_before_presolve && !hint_feasible_after_presolve &&

0 commit comments

Comments
 (0)