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

CVXOPT调用GLPK ILP遇LP松弛原问题不可行,求变量配置指导

Fixing "LP relaxation is primal infeasible" in CVXOPT+GLPK for Your Cutting Stock Problem

Got it, let's work through this issue—since your model works smoothly in AMPL but throws a primal infeasible LP relaxation error when using CVXOPT with GLPK's ILP module, the problem almost certainly lies in how you're defining integer and binary variables. Here's how to get this right:

1. Understand GLPK ILP's Variable Type Markers

GLPK's ILP interface distinguishes between two variable types explicitly, and mixing these up is a super common pitfall:

  • Binary variables: Use the B parameter to pass a set of variable indices. GLPK automatically enforces 0 ≤ x ≤ 1 and integer values for these variables—you don't need to add manual constraints for this. This matches exactly with AMPL's var x binary; declaration.
  • General integer variables: Use the I parameter for variables that need to be integers but aren't restricted to 0/1 (e.g., counts of stock pieces that can be 2, 3, etc.).

A lot of folks mistakenly use I for binary variables and add manual x ≤ 1 constraints, but this can lead to typos (like missing the constraint or using the wrong inequality direction) that break the LP relaxation.

2. Align Variable Types with Your AMPL Model

Pull up your AMPL code and cross-reference every variable declaration:

  • For every binary variable in AMPL, map its index to the B set in CVXOPT's ilp() call.
  • For every integer variable (non-binary) in AMPL, map its index to the I set.
  • Never overlap indices between B and I—GLPK will throw errors or misbehave if you do.

Example Code Snippet

Suppose your variable vector x has:

  • Indices 0-4: Binary variables (matches AMPL's binary declarations)
  • Indices 5-7: General integer variables (matches AMPL's integer declarations)

Your ilp() call should look like this:

from cvxopt import matrix
from cvxopt.glpk import ilp

# Define your objective, constraints as matrices (c, G, h, A, b)
# ... (double-check these match exactly what you had in AMPL!)

# Set variable types
binary_indices = set(range(5))  # Indices 0-4 are binary
integer_indices = set(range(5, 8))  # Indices 5-7 are general integers

# Run ILP solver
status, solution = ilp(c, G, h, A, b, I=integer_indices, B=binary_indices)

3. Debug the LP Relaxation

If you still get the infeasible error, narrow down the issue step by step:

  • First, run the model as a pure LP (remove I and B parameters entirely). If this is infeasible, your constraint matrices (G, h, A, b) don't match AMPL's model—double-check coefficients, inequality directions, and right-hand side values.
  • If the pure LP is feasible, gradually add back variable type constraints (first binary, then integer) to see which change triggers the infeasibility. This will tell you exactly which variable's type is misconfigured.

4. Avoid Common Mistakes

  • Don't add manual x ≤ 1 constraints for variables marked with B—GLPK already handles this, and redundant constraints can sometimes cause numerical issues.
  • Ensure your variable bounds (if any) match AMPL: If AMPL had var x ≥ 0;, confirm your CVXOPT constraints enforce the same (GLPK defaults to non-negative variables, but it's worth verifying).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:38:03