CVXOPT调用GLPK ILP遇LP松弛原问题不可行,求变量配置指导
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
Bparameter to pass a set of variable indices. GLPK automatically enforces0 ≤ x ≤ 1and integer values for these variables—you don't need to add manual constraints for this. This matches exactly with AMPL'svar x binary;declaration. - General integer variables: Use the
Iparameter 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
binaryvariable in AMPL, map its index to theBset in CVXOPT'silp()call. - For every
integervariable (non-binary) in AMPL, map its index to theIset. - Never overlap indices between
BandI—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
binarydeclarations) - Indices 5-7: General integer variables (matches AMPL's
integerdeclarations)
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
IandBparameters 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 ≤ 1constraints for variables marked withB—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

