Skip to content

Commit

Permalink
[CP-SAT] polish code
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jul 20, 2023
1 parent a273b95 commit 36d137c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
23 changes: 4 additions & 19 deletions ortools/sat/cp_model_lns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,10 @@ NeighborhoodGeneratorHelper::GetActiveRectangles(
for (int i = 0; i < ct.x_intervals_size(); ++i) {
const int x_i = ct.x_intervals(i);
const int y_i = ct.y_intervals(i);
if (!active_intervals_set.contains(x_i) &&
!active_intervals_set.contains(y_i)) {
continue;
if (active_intervals_set.contains(x_i) ||
active_intervals_set.contains(y_i)) {
active_rectangles.push_back({x_i, y_i});
}
active_rectangles.push_back({x_i, y_i});
}
}

Expand Down Expand Up @@ -2045,24 +2044,10 @@ Neighborhood RandomRectanglesPackingNeighborhoodGenerator::Generate(
helper_.GetActiveRectangles(initial_solution);
GetRandomSubset(1.0 - difficulty, &rectangles_to_freeze, random);

// ConstraintToVar() is not initialised for intervals. We need to parse them
// manually.
auto insert_vars_from_intervals =
[this](int i, absl::flat_hash_set<int>& vars_to_freeze) {
const ConstraintProto& ct = helper_.ModelProto().constraints(i);
for (const int lit : ct.enforcement_literal()) {
const int var = PositiveRef(lit);
vars_to_freeze.insert(var);
}
for (const int var : ct.interval().start().vars()) {
vars_to_freeze.insert(var);
}
for (const int var : ct.interval().end().vars()) {
vars_to_freeze.insert(var);
}
for (const int var : ct.interval().size().vars()) {
vars_to_freeze.insert(var);
}
for (const int var : UsedVariables(ct)) vars_to_freeze.insert(var);
};

absl::flat_hash_set<int> variables_to_freeze;
Expand Down
6 changes: 3 additions & 3 deletions ortools/sat/cp_model_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3106,7 +3106,7 @@ class LnsSolver : public SubSolver {
// Copy the rest of the model and overwrite the name.
CopyEverythingExceptVariablesAndConstraintsFieldsIntoContext(
helper_->ModelProto(), context.get());
lns_fragment.set_name(absl::StrCat("lns_", task_id));
lns_fragment.set_name(absl::StrCat("lns_", task_id, "_", source_info));

// Overwrite solution hinting.
if (neighborhood.delta.has_solution_hint()) {
Expand Down Expand Up @@ -3153,7 +3153,7 @@ class LnsSolver : public SubSolver {
// TODO(user): export the delta too if needed.
const std::string lns_name =
absl::StrCat(absl::GetFlag(FLAGS_cp_model_dump_prefix),
lns_fragment.name(), "_", source_info, ".pb.txt");
lns_fragment.name(), ".pb.txt");
LOG(INFO) << "Dumping LNS model to '" << lns_name << "'.";
CHECK(WriteModelProtoToFile(lns_fragment, lns_name));
}
Expand Down Expand Up @@ -3230,7 +3230,7 @@ class LnsSolver : public SubSolver {
if (absl::GetFlag(FLAGS_cp_model_dump_problematic_lns)) {
const std::string name =
absl::StrCat(absl::GetFlag(FLAGS_cp_model_dump_prefix),
debug_copy.name(), "_", source_info, ".pb.txt");
debug_copy.name(), ".pb.txt");
LOG(INFO) << "Dumping problematic LNS model to '" << name << "'.";
CHECK(WriteModelProtoToFile(debug_copy, name));
}
Expand Down

0 comments on commit 36d137c

Please sign in to comment.