Skip to content
Skills Library
Data & analytics · Analysis

integer-programming

/integer-programming

Integer Programming for MCM/ICM competitions. Use when decision variables must be integers (0/1 decisions, discrete quantities) - knapsack, assignment, scheduling, facility location. Extends linear programming with integrality constraints.

Licence
MIT
Last repository change
27.06.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
8
Scripts
3
HTML comments
0
URLs in SKILL.md
2
Deprecation

No signals detected. Repository changed on 27.06.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What to check before installing
  • 3 scripts in this package. Review them before installing, as Claude may execute them.

Scripts: examples/beibao.py, examples/youxi.py, examples/zhipai.py

What it does and when to use it

Integer Programming for MCM/ICM competitions. Use when decision variables must be integers (0/1 decisions, discrete quantities) - knapsack, assignment, scheduling, facility location. Extends linear programming with integrality constraints.

Original author description

Integer Programming for MCM/ICM competitions. Use when decision variables must be integers (0/1 decisions, discrete quantities) - knapsack, assignment, scheduling, facility location. Extends linear programming with integrality constraints.

How it works

  1. When to Use
  2. Quick Start
  3. Standard Form
  4. Examples in Skill
  5. Competition Tips
  6. Advanced: Modeling Tricks
  7. Comparison with Other Methods
  8. References
  9. Time Budget
  10. Related Skills
Original SKILL.md headings · 231 lines, 981 words
SKILL.md preview
---
name: integer-programming
description: Integer Programming for MCM/ICM competitions. Use when decision variables must be integers (0/1 decisions, discrete quantities) - knapsack, assignment, scheduling, facility location. Extends linear programming with integrality constraints.
---

# Integer Programming

Solve optimization problems where some or all decision variables must take integer values (not continuous).

## When to Use

- **0-1 decisions**: Select/don't select (project selection, facility location)
- **Discrete quantities**: Number of units (production, staffing - must be whole numbers)
- **Assignment problems**: Assign tasks to workers (each task to exactly one worker)
- **Scheduling**: Shift scheduling, course timetabling
- **Knapsack variants**: Select items with discrete choices
- **Network design**: Select edges/nodes in a network

**Key difference from linear programming**: Variables are restricted to integers, making problems NP-hard (much harder to solve).

## Quick Start

### Python (scipy.optimize.milp)

```python
import numpy as np
from scipy.optimize import milp, Bounds, LinearConstraint

# 0-1 Knapsack: maximize profit within weight limit
profit = np.array([540, 200, 180, 350, 60])
weight = np.array([6, 3, 4, 5, 1])
capacity = 10

n = len(profit)
c = -profit  # Negate for maximization

# Constraint: weight @ x <= capacity
A = weight.reshape(1, -1)
b = np.array([capacity])
constraints = LinearConstraint(A, lb=-np.inf, ub=b)

# Variables: 0 or 1 (binary)
bounds = Bounds(lb=np.zeros(n), ub=np.ones(n))
integrality = np.ones(n, dtype=int)  # All integer

result = milp(c=c, integrality=integrality, 
              constraints=[constraints], bounds=bounds)

if result.success:
    x = np.round(result.x).astype(int)
    print(f"Selected items: {x}")
    print(f"Total profit: {-result.fun}")
    print(f"Total weight: {weight @ x}")
```

### MATLAB (intlinprog)

```matlab
% 0-1 Knapsack
profit = [540; 200; 180; 350; 60];
weight = [6; 3; 4; 5; 1];
capacity = 10;

n = length(profit);
f = -profit;  % Negate for maximization

% Constraint: weight' * x <= capacity
A = weight';
b = capacity;

% Variables: 0 or 1
lb = zeros(n, 1);
ub = ones(n, 1);
intcon = 1:n;  % All variables are integers

[x, fval] = intlinprog(f, intcon, A, b, [], [], lb, ub);

fprintf('Selected items: %s\n', mat2str(x));
fprintf('Total profit: %.2f\n', -fval);
fprintf('Total weight: %.2f\n', weight' * x);
```

## Standard Form

**Maximize**: $z = c^T x$

**Subject to**:
- $Ax \leq b$ (linear constraints)
- $Aeq \cdot x = beq$ (equality constraints)
- $x_i \in \mathbb{Z}$ for $i \in I$ (integrality constraints)
- $x_i \in \{0, 1\}$ for binary variables

**Types**:
1. **Pure IP**: All variables are integers
2. **Mixed IP (MIP)**: Some variables are integers, some continuous
3. **Binary IP (BIP)**: All variables are 0 or 1

## Examples in Skill

See `examples/` folder:

- **`beibao.py` / `beibao.m`**: 0-1 knapsack problem (select items to maximize profit within weight limit)
- **`youxi.py` / `youxi.mlx`**: Game strategy optimization (discrete choices)
- **`zhipai.py` / `zhipai.m`**: Assignment problem (assign tasks to workers, minimize cost)

## Competition Tips

### Problem Recognition (Day 1)

**IP indicators**:
- "Select", "choose", "assign", "schedule" (discrete decisions)
- "Integer number of...", "whole units"
- "Either... or..." (binary choice)
- "Each task to exactly one..." (assignment)

**Common IP types**:
1. **0-1 Knapsack**: Select subset with maximum value
2. **Assignment**: One-to-one matching (Hungarian algorithm for special case)
3. **Set covering**: Minimum sets to cover all elements
4. **Facility location**: Where to build facilities (binary + continuous)
5. **Traveling Salesman**: Visit all cities once (very hard!)

### Formulation Steps (Day 1-2)

1. **Define decision variables**: 
   - Binary: $x_i \in \{0, 1\}$ (select item i or not)
   - Integer: $x_i \in \mathbb{Z}^+$ (number of units)

2. **Write objective**: Same as linear programming

3. **Add constraints**:
   - Resource limits (like LP)
   - Logical constraints: "If A then B" → $x_A \leq x_B$
   - Exactly one: $\sum x_i = 1$

4. **Specify integrality**: Which variables must be integers?

### Implementation Template

```python
from scipy.optimize import milp, Bounds, LinearConstraint
import numpy as np

# Decision variables: x = [x1, x2, ..., xn]
n = 10

# Objective: minimize c @ x (negate for maximization)
c = np.array([...])

# Inequality: A @ x <= b
A = np.array([[...], [...]])
b = np.array([...])
constraints = LinearConstraint(A, lb=-np.inf, ub=b)

# Bounds: 0 <= x <= ub
bounds = Bounds(lb=np.zeros(n), ub=np.array([...]))

# Integrality: 1 for integer, 0 for continuous
integrality = np.ones(n, dtype=int)  # All integer

# Solve
result = milp(c=c, integrality=integrality,
              constraints=[constraints], bounds=bounds)

if result.success:
    x = np.round(result.x).astype(int)
    print(f"Solution: {x}")
    print(f"Objective: {result.fun}")
else:
    print("No solution found")
```

### Validation (Day 3)

- **Check integrality**: Ensure solution is truly integer (use `np.round`)
- **Verify constraints**: Manually check feasibility
- **Sensitivity**: How does solution change with small parameter changes?
- **Relaxation comparison**: Solve LP relaxation (drop integrality) - gives upper bound

### Common Pitfalls

1. **Computational complexity**: IP is NP-hard - large problems (n > 1000) may be slow
2. **Infeasibility**: Integer constraints can make feasible LP infeasible
3. **Gap**: LP relaxation gives bound, but IP solution may be far from it
4. **Solver limits**: Set time limits (`options={'time_limit': 300}`)
5. **Rounding LP solution**: Don't just round LP solution - may be infeasible or suboptimal

## Advanced: Modeling Tricks

### Logical Constraints

**"If $x_1 = 1$ then $x_2 = 1$"**: $x_1 \leq x_2$

**"At most k of n"**: $\sum_{i=1}^n x_i \leq k$

**"Exactly one"**: $\sum_{i=1}^n x_i = 1$

### Big-M Method

For conditional constraints: "If $x = 1$ then $ay \leq b$"

$$ay \leq b + M(1 - x)$$

where $M$ is a large constant.

## Comparison with Other Methods

| Method | Variables | Complexity | When to Use |
|--------|-----------|------------|-------------|
| **Linear Programming** | Continuous | Polynomial | Fractional solutions OK |
| **Integer Programming** | Integer | NP-hard | Discrete decisions required |
| **Dynamic Programming** | N/A | Pseudo-polynomial | Knapsack with small capacity |
| **Genetic Algorithm** | Any | Heuristic | Very large IP (approximate solution) |

## References

- `references/10-整数规划和0-1规划模型【微信公众号丨B站:数模加油站】.pdf`: Complete tutorial
- SciPy docs: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.milp.html
- MATLAB docs: https://www.mathworks.com/help/optim/ug/intlinprog.html

## Time Budget

- **Formulation**: 30-60 min (define variables and constraints)
- **Implementation**: 20-40 min
- **Solving**: 10 min - 1 hour (depends on problem size!)
- **Validation**: 20-30 min
- **Total**: 1.5-3 hours

## Related Skills

- `linear-programming`: Continuous version (LP relaxation)
- `dynamic-programming`: Alternative for knapsack problems
- `genetic-algorithm`: Heuristic for large-scale IP
- `shortest-path`: Special case of network IP
Files in this package (8)
  • SKILL.md 7 KB
  • examples/beibao.m 1 KB
  • examples/beibao.py 1 KB
  • examples/youxi.mlx 4 KB
  • examples/youxi.py 1 KB
  • examples/zhipai.m 1 KB
  • examples/zhipai.py 2 KB
  • references/10-整数规划和0-1规划模型【微信公众号丨B站:数模加油站】.pdf 1,023 KB

Versions

The database has no other skills named integer-programming.

Database snapshot: 13.11.2026 · package version a82433d3d6b7 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100linear-programming
    Linear Programming solver for MCM/ICM competitions. Use when optimizing linear objective functions subject to linear constraints (resource allocation, production planning, transportation problems). Supports simplex method, interior-point, and graphical solutions.
  • 100nonlinear-programming
    Nonlinear Programming for MCM/ICM competitions. Use when objective function or constraints contain nonlinear terms (x², xy, sin(x), sqrt(x)). Handles engineering design, optimal control, parameter fitting. Requires initial guess and may find local optima.
  • 100nvidia-cuopt-numerical-optimization-api-python
    Solve Linear Programming (LP), Mixed-Integer Linear Programming (MILP), and Quadratic Programming (QP, beta) with the Python API. Use when the user asks about optimization with linear or quadratic objectives, linear constraints, integer variables, scheduling, resource…
  • 91optimization-modeling
    When the user wants to build optimization models, solve mathematical programming problems, or find optimal solutions. Also use when the user mentions "linear programming," "integer programming," "mixed-integer programming," "constraint optimization," "mathematical optimization,"…
  • 100mcm-master
    Full-stack autonomous math modeling agent for MCM/ICM (美国大学生数学建模竞赛). Handles team control number, problem choice (A–F), English academic writing, mcmthesis LaTeX template, and optional practical deliverable (memo/letter/report). Use when the user provides an MCM/ICM problem and…
  • 100mcm-o-guide
    MCM/ICM 优秀论文写作全流程指南——基于 502 篇历年获奖论文(2004–2024)语料库的结构化分析与评委点评提炼。覆盖:摘要逐句模板、6 类题型模型选择矩阵、25 页页预算分配、图表黄金法则、灵敏度/稳健性分析范式、全文一致性检查清单。当用户准备美赛、写论文、打磨论文、查结构、选模型、做摘要、看评委标准、读优秀论文时使用。也适用于任何需要高质量数学建模论文写作的场景。

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.