CP-SAT: NoOverlap2D energy cut generator never emits a cut (`rectangles.clear()` is inside the per-rectangle loop)

Open Beginner friendly
#5,674 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp

Research direction

Read ortools/sat/diffn_cuts.cc around CreateNoOverlap2dEnergyCutGenerator() and compare its per-component loop with CreateNoOverlap2dCompletionTimeCutGenerator() in the same file. Move the rectangles.clear() call to the component loop, then run diffn_test and scheduling_cuts_test; verify the Python reproducer logs a NoOverlap2D energy cut and preserves the optimum.

Written by the indexing model from the issue text.

Description

Version: main at b11de38 (2026-11-30), and the released wheel ortools==9.15.6755 (Python reproducer). Linux x86-64, GCC 13.3, Python 3.13.

Summary

CreateNoOverlap2dEnergyCutGenerator() never adds a cut. rectangles.clear() runs once per rectangle instead of once per connected component, so rectangles holds at most one rectangle, if (rectangles.size() <= 1) continue; always skips, and GenerateNoOverlap2dEnergyCut() is never called.

Where

ortools/sat/diffn_cuts.cc, line 353 on b11de38:

    for (const auto& component : helper->connected_components()) {
      for (const int rect : component) {
        rectangles.clear();               // cleared for every rectangle
        if (helper->IsAbsent(rect)) continue;
        ...
        rectangles.push_back(rect);
      }

      if (rectangles.size() <= 1) continue;   // therefore always true
      GenerateNoOverlap2dEnergyCut(...);

CreateNoOverlap2dCompletionTimeCutGenerator() in the same file (line 635) clears the vector once per component.

Reproducer (Python)
import re
import ortools
from ortools.sat.python import cp_model

W, H = 8, 4
# (width, height, value, x_max, y_max): the first boxes only fit in the 3x3 corner [0,3)x[0,3).
BOXES = [(2, 2, 5 + i, 1, 1) for i in range(4)] + [(2, 2, 3, W - 2, H - 2)] * 3

m = cp_model.CpModel()
xs, ys, value = [], [], []
for i, (w, h, val, x_max, y_max) in enumerate(BOXES):
    p = m.new_bool_var(f"p{i}")
    x = m.new_int_var(0, x_max, f"x{i}"); y = m.new_int_var(0, y_max, f"y{i}")
    xs.append(m.new_optional_fixed_size_interval_var(x, w, p, f"ix{i}"))
    ys.append(m.new_optional_fixed_size_interval_var(y, h, p, f"iy{i}"))
    value.append(val * p)
m.add_no_overlap_2d(xs, ys)
m.maximize(sum(value))

print(ortools.__version__)
for presolve in (False, True):
    s = cp_model.CpSolver()
    s.parameters.num_workers = 1
    s.parameters.linearization_level = 2  # the NoOverlap2D energy cut generator is only added at level 2
    s.parameters.cp_model_presolve = presolve
    s.parameters.log_search_progress = True
    s.parameters.log_to_stdout = False
    s.parameters.log_to_response = True
    status = s.solve(m)
    log = s.response_proto.solve_log
    energy = sorted(set(re.findall(r"NoOverlap2d[XY]Energy\w*", log)))
    print(f"presolve={presolve}: {s.status_name(status)} obj={s.objective_value:.0f}, "
          f"NoOverlap2D energy cuts in log: {energy or 'none'}")

Output with ortools==9.15.6755:

9.15.6755
presolve=False: OPTIMAL obj=17, NoOverlap2D energy cuts in log: none
presolve=True: OPTIMAL obj=17, NoOverlap2D energy cuts in log: none

Output of a build that includes the change below (same optimum):

presolve=False: OPTIMAL obj=17, NoOverlap2D energy cuts in log: ['NoOverlap2dXEnergy_optional']
presolve=True: OPTIMAL obj=17, NoOverlap2D energy cuts in log: ['NoOverlap2dXEnergy_optional']
Suggested fix
--- a/ortools/sat/diffn_cuts.cc
+++ b/ortools/sat/diffn_cuts.cc
@@ -349,8 +349,8 @@
     std::vector<int> rectangles;
     rectangles.reserve(num_rectangles);
     for (const auto& component : helper->connected_components()) {
+      rectangles.clear();
       for (const int rect : component) {
-        rectangles.clear();
         if (helper->IsAbsent(rect)) continue;
         // We do not consider rectangles controlled by 2 different unassigned
         // enforcement literals.
Impact and verification
  • Performance only: the generator adds valid cuts, so answers do not change. It is created at linearization_level >= 2 when some box is optional or has a variable size (linear_relaxation.cc, line 2454). Default multi-worker runs include level-2 workers: with default parameters and 8 workers, our random packing models logged these cuts in 13 of 20 solves after the change, and in 0 before.
  • With the change, every cut generated on 148 small random instances was checked against all integer solutions of its instance (57 cuts, none invalid).
  • 80 random packing instances, presolve on and off (160 solves) at linearization level 2: every optimum equals the brute-force optimum; 72 solves used energy cuts.
  • diffn_test and scheduling_cuts_test pass on b11de38 with the change.

Found during an AI-assisted audit (source review, regression tests, UBSan, independent oracles). Every output above comes from an actual run.

Dominant language
C++
Stars
14.5k
Forks
2.6k
Avg merge
9h 43m
Merged PRs (30d)
53

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from google/or-tools

All issues in google/or-tools

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.