3232#include " absl/log/vlog_is_on.h"
3333#include " absl/numeric/bits.h"
3434#include " absl/types/span.h"
35+ #include " ortools/base/stl_util.h"
3536#include " ortools/sat/2d_distances_propagator.h"
3637#include " ortools/sat/2d_mandatory_overlap_propagator.h"
3738#include " ortools/sat/2d_orthogonal_packing.h"
@@ -276,11 +277,11 @@ void AddNonOverlappingRectangles(const std::vector<IntervalVariable>& x,
276277 DCHECK_EQ (sat_solver->CurrentDecisionLevel (), 0 );
277278
278279 for (int i = 0 ; i < num_boxes; ++i) {
279- if (repository->IsOptional (x[i])) continue ;
280- if (repository->IsOptional (y[i])) continue ;
280+ if (repository->IsAbsent (x[i])) continue ;
281+ if (repository->IsAbsent (y[i])) continue ;
281282 for (int j = i + 1 ; j < num_boxes; ++j) {
282- if (repository->IsOptional (x[j])) continue ;
283- if (repository->IsOptional (y[j])) continue ;
283+ if (repository->IsAbsent (x[j])) continue ;
284+ if (repository->IsAbsent (y[j])) continue ;
284285
285286 // At most one of these two x options is true.
286287 const Literal x_ij = repository->GetOrCreatePrecedenceLiteral (
@@ -307,7 +308,21 @@ void AddNonOverlappingRectangles(const std::vector<IntervalVariable>& x,
307308 }
308309
309310 // At least one of the 4 options is true.
310- if (!sat_solver->AddProblemClause ({x_ij, x_ji, y_ij, y_ji})) {
311+ std::vector<Literal> clause = {x_ij, x_ji, y_ij, y_ji};
312+ if (repository->IsOptional (x[i])) {
313+ clause.push_back (repository->PresenceLiteral (x[i]).Negated ());
314+ }
315+ if (repository->IsOptional (y[i])) {
316+ clause.push_back (repository->PresenceLiteral (y[i]).Negated ());
317+ }
318+ if (repository->IsOptional (x[j])) {
319+ clause.push_back (repository->PresenceLiteral (x[j]).Negated ());
320+ }
321+ if (repository->IsOptional (y[j])) {
322+ clause.push_back (repository->PresenceLiteral (y[j]).Negated ());
323+ }
324+ gtl::STLSortAndRemoveDuplicates (&clause);
325+ if (!sat_solver->AddProblemClause (clause)) {
311326 return ;
312327 }
313328 }
0 commit comments