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

Aperta Adatta ai principianti
#5,674 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp

Direzione di ricerca

Leggi ortools/sat/diffn_cuts.cc intorno a CreateNoOverlap2dEnergyCutGenerator() e confronta il suo ciclo per componente con CreateNoOverlap2dCompletionTimeCutGenerator() nello stesso file. Sposta la chiamata a rectangles.clear() nel ciclo per componente, quindi esegui diffn_test e scheduling_cuts_test; verifica che il riproduttore Python registri un taglio energetico NoOverlap2D e mantenga l’ottimo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C++
Stelle
14.5k
Fork
2.6k
Merge medio
9h 43m
PR unite (30g)
53

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google/or-tools

Tutte le issue di google/or-tools

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.