How to Model an Integer Linear Program (ILP): From Idea to a Solver‑Ready Formulation
Modeling is where ILPs succeed or fail. This guide teaches a repeatable workflow, reusable constraint templates, and solver-aware checks so your formulation matches reality—not just the math.
Learn by doing: test your formulation in our Integer Linear Programming (ILP/MILP) Calculator. To diagnose formulation strength, solve the LP relaxation in the Linear Programming calculator.
On this page
- Quick takeaway: the modeling mindset
- What “modeling an ILP” means (and why models fail)
- Before equations: sets, parameters, and units (the missing foundation)
- A solver-aware modeling workflow (artifacts + checks)
- How to choose variables, domains, and bounds
- How to build an objective that behaves well (scaling + penalties)
- Constraint templates you’ll reuse constantly
- How to model logic (if-then, either-or) linearly
- Big M: how to pick M, and how to detect “too big/too small”
- LP relaxation diagnostics (predict difficulty, fix slow models)
- How to interpret solver output (incumbent, bound, gap)
- Worked modeling examples (numeric + validation)
- Debugging checklist (infeasible/unbounded/weird results)
- Common mistakes (and how to avoid them)
- Questions people ask about ILP modeling (PAA)
Quick takeaway: A correct ILP formulation is a translation of decisions into variables, and rules into constraint families. The professional workflow is: (1) write the “problem sentence,” (2) define sets/data/units, (3) choose tight bounds early, (4) build constraints from templates, and (5) validate with the LP relaxation before tuning solver settings. If you’re brand new, start with the ILP beginner’s guide.
What “modeling an ILP” really means (and why models fail)
Concept
Modeling is choosing what the solver is allowed to do
An ILP solver is extremely literal. It does not infer “common sense.” If a rule is not written as a constraint, the solver treats it as optional. That’s why many “solver mistakes” are actually modeling omissions.
| Failure mode | What you see | Typical cause | Fast fix |
|---|---|---|---|
| Wrong problem | Solution is feasible but nonsensical | Missing rule, wrong inequality direction, wrong objective weights/units | Rewrite each constraint in English; add the missing constraint family |
| Infeasible | No feasible solution exists | Contradicting constraints, too-tight bounds, Big‑M too small | Solve the LP relaxation; inspect equalities and bounds first |
| Slow / big gap | Stops early, large branch-and-bound tree, weak bound | Loose bounds, oversized Big‑M, symmetry, weak relaxation | Compute LP relaxation bound; tighten formulation before “running longer” |
If you’re deciding whether your model should be pure ILP or mixed (MILP), see ILP vs MILP. For many problems, forcing everything to be integer is a common reason for slow solves.
Before equations: sets, parameters, and units (the missing foundation)
Beginner → intermediate
Most “bad ILPs” are actually “bad data definitions”
Good models separate what the solver can choose (variables) from what the world provides (data/parameters). Before writing constraints, write down: (1) your index sets, (2) your parameters with units, and (3) reasonable bounds implied by physics/business rules.
| Modeling object | Symbol | Example | Unit / meaning | Why it matters for ILP |
|---|---|---|---|---|
| Set of items/projects | i ∈ I |
Projects 1..n | Index only | Defines how constraints repeat (“for each i”) |
| Costs | cost_i |
$ per project | Currency | Objective coefficients + budget constraints |
| Capacity | cap_j |
Units/day | Quantity | Gives tight bounds (smaller Big‑M, faster solve) |
| Demand/requirements | d_k |
Customer demand | Quantity | Prevents “solver chooses to serve nobody” mistakes |
| Time limits / hours | h_t |
Labor hours | Time | Creates feasibility; often interacts with equalities |
A solver-aware ILP modeling workflow (artifacts + checks)
Workflow
The “artifact” approach: what to produce at each step
Professionals don’t jump from story to equations in one shot. They create small artifacts that are easy to verify, then solve a tiny instance as early as possible.
| Step | What you produce | Sanity check (before solving) |
|---|---|---|
| 1. Problem sentence | “Choose ___ to optimize ___ subject to ___.” | Would a domain expert agree this is the goal? |
| 2. Sets & data | Index sets + parameters + units | Any implied maxima/minima you can turn into bounds? |
| 3. Variable table | Meaning, domain, bounds, “what does 1 mean?” | Can you interpret every variable without math jargon? |
| 4. Objective | A single score to optimize (cost/profit/time) | Coefficients make sense in one consistent unit? |
| 5. Constraint families | Budget/capacity/logic/coverage families | Each business rule maps to a written constraint family? |
| 6. LP relaxation test | Relax integrality, solve as LP | Feasible? Bound realistic? If not, tighten formulation. |
How to choose variables, domains, and bounds
Beginner level
Decisions become variables; data stays parameters
Start by asking: “What can the optimizer choose?” Those choices are variables. Everything else—costs, capacities, time limits—is data (parameters).
- Binary
x∈{0,1}: choose/assign/open/activate. - Integer
x∈ℤ: counts, batches, number of units. - Continuous
x∈ℝ: flows, quantities, time, energy.
Intermediate
A variable table template (steal this)
A “variable table” prevents ambiguity and makes debugging much faster. You can build this in your notes before writing any constraints:
| Variable | Meaning (plain English) | Domain | Lower/Upper bound | Interpretation test |
|---|---|---|---|---|
x_i |
1 if item i is selected | Binary | 0 to 1 | “What does x_i=1 mean?” |
y_j |
1 if facility j is open | Binary | 0 to 1 | Does “partially open” make sense? (If not, binary is right.) |
f_{j,k} |
Units shipped from j to k | Continuous | 0 to cap_j |
Can shipments exceed capacity? If not, cap gives a bound. |
Advanced (simplified)
Bounds are not optional—they define your feasible region and your Big‑M
Tight bounds shrink the search space and strengthen LP relaxations (which improves pruning in branch-and-bound). They also let you derive smaller (safer) Big‑M values. “Use M = 1,000,000” is not a modeling strategy—it’s usually a performance bug.
If you don’t know a bound, derive one from physical limits: maximum demand, maximum production, maximum time, maximum inventory, or a hard business limit. If you can’t justify a bound in words, it’s rarely a good Big‑M.
How to build an objective that behaves well (scaling + penalties)
Objective design
Don’t hide constraints inside the objective unless you truly mean “soft”
A clean modeling habit is separating: hard rules (must always hold → constraints) from preferences (nice to have → penalties in the objective).
minimize/maximize Z = Σ c_i x_i + Σ p_k s_k
Here s_k can represent violation/slack variables for “soft constraints.”
This is valid modeling, but only if penalty weights p_k represent real trade-offs.
Practical insight
When is it better to use a constraint instead of a penalty?
If violating a rule would be unacceptable in the real world (safety, legal compliance, service-level minimums), write it as a constraint. Penalties are best for “nice-to-have” objectives like balancing workload or reducing overtime, where small violations are acceptable and you want the model to choose the least-bad tradeoff.
Constraint templates you’ll reuse constantly
Templates
A compact pattern library (the “grammar” of most ILPs)
| Use case | Template | What it enforces | Typical variables |
|---|---|---|---|
| Budget / knapsack | Σ cost_i x_i ≤ Budget |
Don’t exceed a resource limit | Binary selection |
| Exactly / at most / at least one | Σ x_i = 1, ≤ 1, ≥ 1 |
Choice and exclusivity rules | Binary |
| Assignment | Σ_j x_{i,j} = 1 ∀i |
Each i gets exactly one option | Binary matrix |
| Coverage / minimum requirement | Σ a_i x_i ≥ R |
Meet a minimum threshold | Binary or integer |
| Flow / balance | inflow(v) − outflow(v) = supply(v) |
Conservation of flow | Continuous flows |
| On/off linking | 0 ≤ x ≤ U·y |
x must be 0 if y=0; x allowed if y=1 | Continuous x, binary y |
| Cardinality (choose exactly k) | Σ x_i = k |
Select exactly k items | Binary |
| Absolute value linearization | t ≥ x, t ≥ -x |
t ≥ |x| without nonlinearity |
Continuous t |
| Max / min modeling (common trick) | z ≥ a, z ≥ b (z = max) |
Represent max/min with linear constraints | Continuous z |
If your model is primarily shipping/flow with linear costs, test the specialized structure with the Transportation Problem calculator before building a full ILP/MILP. Many flow problems are naturally “LP-friendly” and solve extremely fast.
How to model logic (if-then, either-or) linearly
Logic modeling
Implication with binaries (often no Big‑M needed)
If x and y are binary and you want “if x then y”, use:
x ≤ y
This eliminates the forbidden case (x=1, y=0). It’s one of the cleanest logic constraints you can write.
How do you model AND / OR with binaries?
Let z represent z = (x AND y) for binaries. A common linearization is:
z ≤ x
z ≤ y
z ≥ x + y - 1
x,y,z ∈ {0,1}
For z = (x OR y):
z ≥ x
z ≥ y
z ≤ x + y
x,y,z ∈ {0,1}
Either-or: exactly one option must be chosen
x + y = 1
Big M: how to pick M, and how to detect “too big/too small”
Big‑M method
Big‑M is a bound disguised as logic
Big‑M works only because M is (implicitly) an upper bound on something. Oversized M weakens the LP relaxation and can make models slow or numerically unstable. Undersized M can cut off valid solutions and create “false infeasibility.”
A repeatable method to choose M (what professionals actually do)
- Start from reality: what is the maximum possible value of the expression you’re trying to “turn off”?
- Use constraint-specific M: different constraints often have different valid bounds—avoid one global mega-M.
- Prefer derived bounds: use capacity, demand, or time windows to compute the smallest safe M.
- Validate with LP relaxation: if the relaxed model becomes “too good to be true,” M is likely too large (or bounds are missing).
y and production x, use 0 ≤ x ≤ 120y.
Using 0 ≤ x ≤ 1,000,000y rarely adds realism—and often makes the relaxation weak.
How do you know if M is too small?
You’ll often see infeasibility or suspiciously poor objective values because valid solutions are being cut off. A diagnostic test: temporarily increase M. If feasibility or objective quality changes materially, your original M was likely invalid.
How do you know if M is too large?
Your LP relaxation bound becomes overly optimistic (far from any integer-feasible solution), and branch-and-bound struggles to prune. Use the LP relaxation diagnostic below to confirm.
LP relaxation diagnostics (predict difficulty, fix slow models)
Diagnostics
The fastest “model health check”: compare ILP vs LP relaxation
The LP relaxation bound is the objective value of your model after relaxing integrality (binaries become 0 ≤ x ≤ 1, integers become continuous).
It’s the same diagnostic tool solvers use inside
branch-and-bound.
- Solve the integer model in the ILP calculator → get an incumbent objective.
- Relax integrality and solve in the LP calculator → get the LP bound.
- Compare: if the LP bound is unrealistically better, tighten bounds, shrink Big‑M, or strengthen constraints.
How to interpret solver output (incumbent, bound, gap)
Interpretation
What to read when the solver stops early
Solvers track a best feasible solution (the incumbent) and a best proven bound (often from relaxations). The difference becomes the optimality gap.
Minimization: BestBound ≤ Optimum ≤ Incumbent Maximization: Incumbent ≤ Optimum ≤ BestBound
Practical takeaway: even if you stop early, you can report a certified interval (“the optimum lies between A and B”). For a dedicated interpretation guide, see optimality gap in MIP.
Worked modeling examples (numeric + validation)
Worked example (numeric)
Example 1: project selection under a budget (0–1 ILP you can verify by hand)
Scenario: You can fund projects A, B, C. Budget = 10. Costs: A=6, B=5, C=4. Values: A=9, B=8, C=6.
Model (x_A, x_B, x_C ∈ {0,1}):
maximize 9xA + 8xB + 6xC
subject to 6xA + 5xB + 4xC ≤ 10
xA, xB, xC ∈ {0,1}
Why each line matters:
- The objective encodes the definition of “best” (total value).
- The budget constraint blocks impossible combinations (like picking all three: cost 15).
- Binaries ensure “pick or don’t pick”—no fractional projects.
Validation (quick manual check):
- A+B cost 11 (infeasible)
- A+C cost 10, value 15 (feasible)
- B+C cost 9, value 14 (feasible)
- Best feasible is A+C with value 15
Worked example (linking + Big‑M as capacity)
Example 2: facility open/close with shipping (Big‑M done right)
Story: Decide which facilities to open, then ship to customers.
Variables: open decisions y_j∈{0,1}, shipment quantities x_{j,k}≥0.
minimize Σ fixed_j y_j + Σ shipCost_{j,k} x_{j,k}
subject to Σ_j x_{j,k} ≥ demand_k ∀k
Σ_k x_{j,k} ≤ cap_j · y_j ∀j
y_j ∈ {0,1}, x_{j,k} ≥ 0
Interpretation: The linking constraint Σ_k x_{j,k} ≤ cap_j·y_j says:
if y_j=0, then shipments must be 0; if y_j=1, shipments are allowed up to real capacity.
Here, cap_j is your Big‑M (but it’s meaningful and tight).
Worked example (logic)
Example 3: “Either outsource or build capacity” (either–or logic)
Scenario: You must satisfy demand of 100 units. You can either (a) outsource units at $7/unit, or (b) build a machine (fixed $300) and produce in-house at $3/unit with capacity 120. You want the cheaper plan.
Let y = 1 if you build the machine, else 0.
Let x be in-house production, o outsourced units.
minimize 300y + 3x + 7o
subject to x + o = 100
0 ≤ x ≤ 120y
o ≥ 0
y ∈ {0,1}
Why the linking constraint is the key: 0 ≤ x ≤ 120y forces x=0 when y=0.
Without it, the solver could produce without paying the fixed cost—an extremely common beginner error.
Quick interpretation:
- If you don’t build (
y=0):x=0,o=100, cost = 700 - If you build (
y=1): choosex=100,o=0, cost = 300 + 300 = 600
So building is optimal here. If demand dropped to 40, outsourcing might win—this is a good “what-if” test for your model.
Debugging checklist (infeasible/unbounded/weird results)
Debugging
Why is my ILP infeasible?
- Check bounds first: are you accidentally forcing something like
x ≥ 1andx ≤ 0? - Check equalities: “exactly one” and “exactly meet demand” constraints clash easily with tight capacities.
- Solve the LP relaxation: if the relaxation is infeasible too, the issue is constraints/data (not integrality).
- Check Big‑M validity: if LP is feasible but ILP is infeasible, logic/integrality constraints may be too strict (or M too small).
Debugging
Why is my model unbounded?
Unbounded usually means you’re maximizing something with no upper bound (or minimizing with no lower bound), often due to a missing constraint, wrong inequality direction, or missing variable bounds. Add real limits (capacity, demand, conservation) and verify signs carefully.
Debugging
Why is my solution feasible but not what I meant?
This almost always indicates a missing business rule. The fastest fix is to re-write the story sentence and add the missing rule as a constraint family. Common missing pieces:
- Missing linking constraints (binaries not connected to quantities)
- Wrong “one-of” logic (you meant “exactly one,” wrote “at least one”)
- Unit mismatch (objective mixes dollars/hours/penalties without conversion)
Common modeling mistakes (and how to avoid them)
Mistakes
High-frequency mistakes (with quick prevention)
- Missing bounds: makes Big‑M huge and relaxations weak. Add realistic maxima early.
- Wrong inequality direction: silently flips feasibility. Re-read each constraint as an English sentence.
- Over-integerization: forcing continuous quantities to be integer can explode runtime (see ILP vs MILP).
- Oversized Big‑M: causes slow branch-and-bound and large gaps; diagnose with LP relaxation bound.
- Coefficient scaling issues: huge magnitude differences can cause numerical instability and weak progress.
Questions people ask about ILP modeling (PAA)
Questions people ask
How do I choose decision variables in an ILP model?
Choose variables that directly represent the decision you will implement. If a manager could read your variable definition and say “yes, that’s exactly what we choose,” you’re on the right track. Avoid defining variables that represent intermediate math artifacts unless they are needed to keep constraints linear (e.g., absolute value or max variables).
Questions people ask
What is the difference between a constraint and a penalty in ILP?
A constraint is a rule that must always hold (budget cap, legal requirement, safety). A penalty is a preference: you allow violation but charge for it in the objective using slack/violation variables. If violating the rule would make the solution unusable, model it as a constraint.
Questions people ask
How do I linearize an if-then constraint?
If both sides are binary, “if x then y” is often simply x ≤ y.
If a binary controls a continuous quantity, use linking constraints like 0 ≤ q ≤ U·y.
When you do use Big‑M, derive M from a real bound (capacity, demand, time limit) instead of a huge constant.
Questions people ask
Why is my MILP/ILP solver so slow even for a small model?
“Small” in number of variables is not the same as “easy.” The strongest predictor is the LP relaxation tightness. If the LP relaxation bound is far from your best integer solution, branch-and-bound has weak pruning power and may explore many nodes. Use LP relaxation bound as your first diagnostic before tuning solver settings.
Questions people ask
What happens if I relax integrality (binary to continuous)?
You get the LP relaxation: the same constraints but with a larger feasible set. That solution provides a bound on the integer optimum and is the core engine behind branch-and-bound. Practically, it tells you whether your formulation is “tight” (good) or “loose” (needs better bounds/Big‑M/constraints).
Questions people ask
How do I know whether to stop early when the solver reports an optimality gap?
Convert the incumbent and best bound into a certified interval and ask: (1) is the incumbent implementable and stable under small changes? (2) is the remaining gap small enough that further improvement isn’t worth time or complexity? If the gap is large and the LP bound is unrealistic, refactor the model first (bounds + Big‑M + strengthening) rather than only increasing time. See optimality gap in MIP.
Next step
Test your model (and learn faster)
Paste your formulation into the Integer Linear Programming Calculator and review feasibility and objective. Then compute the relaxation with the LP calculator to diagnose whether your formulation is tight.
Disclaimer
This content and the associated calculators are provided for educational and informational purposes only. Optimization results depend entirely on your input data and modeling assumptions. They do not constitute professional, legal, financial, operational, or safety advice. Always validate constraints, units, and feasibility against your real-world requirements, and consult a qualified operations research professional for high-stakes decisions.