Converter tool

Linear and Integer Programming Solver

Write a linear or mixed-integer model in plain text or a CSV matrix and get the optimum, shadow prices, reduced costs and a reason when it is infeasible or unbounded.

Plain text or CSV matrix

1. The model

Syntax

Start with maximize or minimize and a linear expression such as 3 x + 2.5 y - z. Then subject to, and one constraint per line, optionally named: c1: x + y <= 10. Use <=, >= or =, with variables on either side, or a range such as 2 <= x + y <= 8.

Variables are at least 0 unless a bounds line says otherwise: 0 <= x <= 40, x >= -5, x <= 9, x = 3, or x free (use -inf and inf for open ends). List whole-number variables under integer and yes/no variables under binary. A line may end in a comment after # or //.

A CSV matrix has a label column, one column per variable, a type column (<=, >= or =) and an rhs column. A row labelled max or min is the objective; rows labelled lower, upper, integer or binary set bounds and integer variables.

Limits: 600 variables and 600 constraints, solved with a dense simplex method in your browser.

Linear and Integer Programming Solver

Operator: Abdulla Al Fahad. How tools are built and checked

A linear program asks for the best value of a linear objective under linear limits, and the interesting part of the answer is rarely the optimum alone. Which limits are binding, what one more unit of a scarce resource would be worth, and which unused option would need to get cheaper before it is chosen are all in the dual figures, and the solver here reports them.

The page has its own simplex and branch and bound code, written for this site rather than wrapped around an existing library, and it runs in a worker so a long integer search can be cancelled. Models up to 600 variables and 600 constraints are accepted.

Key features

  • A readable model format with named constraints, ranges, bounds, free variables, integer and binary sections, and comments
  • A CSV matrix input with one column per variable, a type and an rhs column, and objective, lower, upper, integer and binary rows
  • Two-phase simplex with bounded variables, switching to Bland's rule when it stalls on degenerate pivots
  • Shadow price and slack for every constraint, and reduced cost for every variable, for linear models
  • Branch and bound for integer and binary models with a time budget and a node limit, reporting the incumbent, the bound and the gap
  • A conflicting set of constraints when a model is infeasible, and the direction of the unbounded ray when it is unbounded
  • The row activity and slack of each constraint, and for integer models the objective of the relaxation so you can see what integrality costs

The model syntax, how the simplex and branch and bound steps work, and their limits

A model starts with maximize or minimize and a linear expression, then subject to and one constraint per line. A constraint may have a name and variables on both sides, and a range such as 2 <= x + y <= 8 (or 8 >= x + y >= 2) becomes two constraints; a range whose two operators point different ways, or whose low end is above its high end, is rejected. Coefficients may be written 3x, 3 x or 3*x. Products of variables, division and brackets are rejected with the line number.

Every variable is at least 0 and has no upper limit unless a bounds line changes it: 0 <= x <= 40, x >= -5, x <= 9, x = 3 or x free, with inf and -inf for open ends. Names listed under integer must be whole numbers, and names under binary are integers from 0 to 1. Comments start with #, // or a backslash.

In the CSV matrix, a row labelled max or min holds the objective, rows labelled lower and upper set bounds, integer and binary mark integer variables with a 1, and every other row is a constraint whose type is <=, >= or = and whose right side is in the rhs column. Empty cells are zero.

How it works

Each variable is shifted to start at 0, split in two if it is free, or reflected if only its upper bound is finite, and each finite upper bound becomes a row. Rows get slack, surplus or artificial columns and a dense tableau is built, with rows scaled by powers of two for stability.

Phase 1 minimises the artificial variables. If their sum cannot reach 0 the model is infeasible, and the constraints whose rows carry non-zero dual values in that phase are the conflicting set. Phase 2 optimises the real objective; if an entering column has no positive entry in any row, the objective is unbounded along that column.

Shadow prices are the dual values of the final tableau mapped back through the scaling and any sign flips, expressed as the change in the objective per unit increase in the right side. They are valid for small changes that keep the same set of binding constraints.

For integer models each node solves the relaxation with tightened bounds, branches on the variable furthest from a whole number, explores the nearer rounding first and prunes nodes whose bound cannot beat the incumbent.

When all objective coefficients are whole numbers on integer variables, a node must beat the incumbent by at least 1.

Common use cases

  • A student solves the P&T transportation problem from a textbook, three canneries and four warehouses, and gets a shipping cost of 152,535 with the plan 20 and 55 truckloads from the first cannery.
  • An analyst decides which of four capital projects to fund under a 10 million budget with dependencies, using binary variables, and finds 14 million of net present value from the first two.
  • A production planner reads the shadow price of an overtime constraint to see what an extra hour is worth before negotiating it.

How to use this tool

  1. Type or paste the model, or load one of the examples.
  2. For integer models set the search time and node limit.
  3. Press Solve; integer searches show the node count, the incumbent and the bound, and can be cancelled (a cancelled solve says so and keeps no result).
  4. Read the status, the objective, the variable values and the constraint table.
  5. Download the variables, constraints or the workbook.

Example

The Stigler diet with 15 foods: minimise annual food cost for nine nutrient minimums

minimize annual_cost: 365 Wheat_Flour_Enriched + 365 Macaroni + ... + 365 Navy_Beans_Dried
subject to
  calories: 44.7 Wheat_Flour_Enriched + 11.6 Macaroni + ... >= 3
  protein: 1411 Wheat_Flour_Enriched + 418 Macaroni + ... >= 70
  ... (calcium, iron, vitamins A, B1, B2, niacin and C)

Optimum, spend per food and the binding nutrients

Optimal, minimum annual cost 39.66173 dollars (1939 prices)
Variables, in dollars per day: Wheat_Flour_Enriched 0.029519, Liver_Beef 0.001893, Cabbage 0.011214, Spinach 0.005008, Navy_Beans_Dried 0.061029; all other foods 0 (times 365 that is 10.77, 0.69, 4.09, 1.83 and 22.28 dollars a year)
Binding nutrients with shadow prices: calories 3.1993, calcium 11.5843, vitamin A 0.1461, vitamin B2 5.9707, vitamin C 0.0526; protein, iron, B1 and niacin have slack and a shadow price of 0

Tips for best results

  • Name constraints. Shadow prices, slack and the infeasibility report all use the names.
  • Check the reduced cost of an option you left out. It is the amount its cost must fall before the model would use it.
  • Give integer models tight bounds on every integer variable; a variable bounded between 0 and 5 branches far less than one with no limit.
  • Use ranges for two-sided limits rather than two lines, so the pair shares a name.
  • Compare an integer model's objective with the relaxation shown beside it; a small difference means integrality barely matters.

Common pitfalls

Branch and bound can take exponentially long. When the budget runs out the page shows the best solution found, the best bound and the gap, which is not a proof of optimality.

Shadow prices and reduced costs are not shown for integer models, because enforcing integrality changes the meaning of the dual values.

The dense tableau suits models with up to a few hundred rows and columns. Larger or very sparse models are slower than a sparse solver would be.

Models with very large and very small coefficients together can lose precision; scaling the units, for example to thousands, helps.

Common errors and how to fix them

Line 4: the constraint name "a" is used twice.
Constraint names must be unique. Rename one or leave the name out and let the page number it.
Line 1: only linear terms are supported (found "*y" after x).
The model has a product of two variables. Linearise it or use binary variables and constraints instead.
Line 3: one side of the comparison is empty.
A constraint needs an expression or number on both sides of <=, >= or =.
The bounds of x are contradictory (5 > 3).
The lower bound is above the upper bound; check the bounds section and any integer or binary declarations.
The objective coefficient of x has the value 1.000e+308, which is beyond the 1e+15 this solver can hold exactly.
A coefficient, right side or finite bound above 1e15 is refused rather than solved with overflowing arithmetic. Rescale the units, for example to millions, and solve again.
Line 3: the range runs from 8 up to 2, which is empty.
In 8 <= x + y <= 2 the low end is above the high end. Swap them, or write 8 >= x + y >= 2.
The matrix needs a "type" column (<=, >= or =) and an "rhs" column.
Name the columns exactly type and rhs, and put one column per variable between the label column and them.

Best practices

  • Keep a model's units close to one another, so coefficients stay within a few orders of magnitude.
  • State the objective sense and the units next to the optimum when reporting it.
  • Re-solve with the right side changed by a small amount to confirm a shadow price you intend to rely on.
  • Save the model text with the answer. It is the complete, reproducible statement of the problem.

Checked against published optima

The Stigler diet gives 39.66173 dollars a year here. The Google OR-Tools documentation prints 39.6617 for the full 77-food data, with the same five foods in the same amounts; this page's example keeps 15 of the foods, including those five. Wyndor Glass gives 36 at 2 doors and 6 windows, the P&T transportation problem gives 152,535 as in Hillier and Lieberman, and the California Manufacturing capital-budgeting model gives 14 with projects 1 and 2.

Reading shadow prices

In the Wyndor Glass product-mix example from Hillier and Lieberman, maximising 3 doors + 5 windows with the plant limits x1 ≤ 4, 2x2 ≤ 12 and 3x1 + 2x2 ≤ 18 gives 36 at 2 doors and 6 windows, and shadow prices of 0, 1.5 and 1. The first plant has slack, so another hour there is worth nothing; the other two are binding, and each extra unit of their capacity is worth 1.5 and 1.

A constraint with a shadow price of 0 and slack above 0 is not limiting the answer. A constraint with a non-zero shadow price is, and the price holds only until the set of binding constraints changes.

Frequently asked questions

Can it solve nonlinear problems?

No. Products of variables, powers and divisions are rejected. Only linear and mixed-integer linear models are solved.

Why is there no shadow price for my integer model?

Dual values belong to a linear problem. For an integer model the page reports the objective and the solution, plus the root relaxation's objective so you can see how much integrality costs.

What does the gap mean when a search stops early?

It is the difference between the best integer solution found and the best bound still open, as a share of the solution. A gap of 0 means the solution is proven optimal.