You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在OR-Tools求解器中计算数组元素并为切割下料问题设置切割次数限制

Fixing Cutting Operation Limit in OR-Tools Cutting Stock Problem

I get exactly what you're dealing with—hardware constraints throwing a wrench into your perfectly optimized cutting plan is always a pain. Let's break down how to add that max 7 cutting operations constraint using OR-Tools' solver.Add() method, since you can't debug the solver internals directly.

Step 1: Define Variables for Cutting Operations per Roll

First, we need to track how many cuts are made on each large roll. For each roll j, create an integer variable cut_ops_j that represents the number of cuts performed on that roll:

# Calculate a safe upper bound for cuts per roll (max possible cuts using the smallest small roll)
w_min = min(d[1] for d in demands)
max_cuts_per_roll = (parent_width // w_min) - 1  # Cuts = number of pieces - 1

# Variable to track cuts per roll: cut_ops_j is 0 if roll j isn't used
cut_ops = [solver.IntVar(0, max_cuts_per_roll, f'cut_ops_{j}') for j in range(k[1])]

For each roll j, the number of cuts equals (number of small pieces cut from roll j) - 1 only if the roll is used (i.e., y[j] = 1). If the roll isn't used (y[j] = 0), cuts should be 0. We translate this into linear constraints (required for OR-Tools' MILP solver):

M = max_cuts_per_roll + 1  # A sufficiently large constant for "big-M" constraints

for j in range(k[1]):
    total_pieces_j = sum(x[i][j] for i in range(num_orders))
    
    # Enforce cuts = total_pieces_j - 1 when roll j is used
    solver.Add(cut_ops[j] >= total_pieces_j - 1 - M * (1 - y[j]))
    solver.Add(cut_ops[j] <= total_pieces_j - 1 + M * (1 - y[j]))
    
    # Enforce cuts = 0 when roll j is not used
    solver.Add(cut_ops[j] <= M * y[j])

Note: I noticed your code references y[j] but doesn't define it—don't forget to initialize it as a 0-1 integer variable first:

y = [solver.IntVar(0, 1, f'y_{j}') for j in range(k[1])]

Step 3: Add the Global Cut Limit Constraint

Finally, add a constraint that caps the total number of cutting operations across all rolls at 7:

# Total cutting operations cannot exceed 7
solver.Add(sum(cut_ops) <= 7)

Quick Tips for Smooth Execution

  • Big-M Value: Using max_cuts_per_roll + 1 ensures the constraint doesn't interfere with valid solutions while keeping the solver efficient.
  • Objective Alignment: Keep your existing objective (like minimizing unused width or number of rolls) intact—these new constraints will work alongside it to find feasible, hardware-compliant solutions.

This setup will force the solver to generate plans where total cutting operations never exceed 7, matching your hardware's limitations.

内容的提问来源于stack exchange,提问作者Parth Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 05:57:46