CP-SAT: NoOverlap2D energy cut generator never emits a cut (`rectangles.clear()` is inside the per-rectangle loop)
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
- Domain
- backend, testing-qa
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 >= 2when 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_testandscheduling_cuts_testpass onb11de38with 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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from google/or-tools
-
Difficulty 5/5 Over a week Newbie friendliness 42/100
Maintainers usually reply within 1 day
-
CP-SAT: TimeTableEdgeFinding can report false INFEASIBLE for large cumulative scheduling modelsPossibly taken @lperron claimed this today. OpenBug Solver: CP-SAT Solver
google/or-tools#5672 · 2 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
Python (v10.0-rc.1): Model.restore_assignment() and Assignment.objective() take ownership of C++-owned objects (segfault)Possibly taken @Mizux claimed this today. OpenLang: Python Solver: Routing & CP Solver
google/or-tools#5670 · 1 assignee ·
Maintainers usually reply within 1 day
-
[CP-SAT] Duplicate timer registration in StartDirectSolver when UserTimer aliases WallTimerPossibly taken @lperron claimed this 1 day ago. OpenSolver: CP-SAT Solver
Difficulty 3/5 Half a day Newbie friendliness 85/100
google/or-tools#5659 · 5 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Maintainers usually reply within 1 day
Similar issues
-
needs-kind
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
villagesql/villagesql-server#1702 · 1 comment ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
microsoft/vcpkg-tool#2191 · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
harfbuzz/harfbuzz#6966 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
upstream update
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
conan-io/conan-center-index#31597 ·
Maintainers usually reply within 1 day