Applying Linear Optimization Using GLPK

Embed Size (px)

Citation preview

Applying Linear Optimization Using GLPK

CCISDevt Sharing Session

Applying Linear OptimizationUsing GLPK

Outline

What is Linear Optimization?

What is GLPK?

Available Bindings

GNU MathProg Scripting

Examples:

Trans-shipment (the standard example)

Time Table Scheduling (from school days...)

Allocating HDB Flats by Combinatorial Auction

Linear Optimization? What?

The minimization of functions of the form

c1 x1 + c2 x2 + + cn xn

by varying decision variables xi,subject to constraints of the form

ai1 x1 + ai2 x2 + + ain xn = bi orai1 x1 + ai2 x2 + + ain xn bi

where the other coefficients aij, bi and ci are fixed.

Linear Optimization? What?

A typical example: The Diet Problem. By varying the intake of each type of food (e.g.: chicken rice, durian, cheese cake), minimize

(The Total Cost of Food)

subject to

(Non-negativity of the intakes of each type.)(Satisfaction of minimum nutritional requirements)

Linear Optimization? What?

A secondary school example. By varying decision variables x and y, maximize

2 x + y

subject to

x 0, x 1,y 0, y 1,x + y 1.5

Linear Optimization? What?

A secondary school example. By varying decision variables x and y, maximize

2 x + y

subject to

x 0, x 1,y 0, y 1,x + y 1.5

Linear Optimization? What?

A secondary school example. By varying decision variables x and y, maximize

2 x + y

subject to

x 0, x 1,y 0, y 1,x + y 1.5

Linear Optimization? What?

A secondary school example. By varying decision variables x and y, maximize

2 x + y

subject to

x 0, x 1,y 0, y 1,x + y 1.5

Linear Optimization? What?

A more useful sounding example: Simple Commodity Flow. By varying the number of TV sets being transferred along each road in a road network, minimize

(Total Distance each TV set is moved through)

subject to

(Non-negativity of the flows)(Sum of Inflows = Sum of Outflows at each junction where Stock counts as inflow & demand, outflow.)

Linear Optimization? What?

Perhaps one of the most widely used mathematical techniques:

Network flow / multi-commodity flow problems

Project Management

Production Planning, etc...

Used in Mixed Integer Linear Optimization for:

Airplane scheduling

Facility planning

Timetabling, etc...

Linear Optimization? What?

Solution methods:

Simplex-based algorithmsNon-polynomial-time algorithm

Performs much better in practice than predicted by theory

Interior-point methodsPolynomial-time algorithm

Also performs better in practice than predicted by theory

What is GLPK

GLPK: GNU Linear Programming Kit

An open-source, cross-platform software package for solving large-scale Linear Optimization and Mixed Integer Linear Optimization problems.

Comes bundled with the GNU MathProg scripting language for rapid development.

URL: http://www.gnu.org/software/glpk/

GUSEK (a useful front-end): http://gusek.sourceforge.net/gusek.html

Bindings for GLPK

GLPK bindings exist for:

C, C++ (with distribution)

Java (with distribution)

C#

Python, Ruby, Erlang

MATLAB, R

even Lisp.

(And probably many more.)

Scripting with GNU MathProg

For expressing optimization problems in a compact, human readable format.

Used to generate problems for computer solution.

Usable in production as problem generation is typically a small fraction of solution time and problem generation (in C/C++/Java/etc) via the API takes about as long.

Hence, rapid development as opposed to rapid prototyping.

Scripting with GNU MathProg

Parts of a script:

Model: Description of objective function and constraints to be satisfied

Data: Parameters that go into the model

Data can be acquired from a database (e.g. via ODBC).

Post processed solution can be written to a database.

Scripting with GNU MathProg

Model File (For choosing a meet-up date)

set Peoples;set Days;set Meals;... # PrefInd[...] defined herevar x{d in Days, m in Meals} binary;maximize Participation_and_Preference: sum{p in Peoples, d in Days, m in Meals} PrefInd[p,d,m]*x[d,m];s.t.one_meal: sum{d in Days, m in Meals} x[d,m] = 1;solve;... # Post-processing hereend;

Scripting with GNU MathProg

Data File (for the above model)

data;set Peoples := JC LZY FYN MJ;set Days := Mon Tue Wed Thu Fri Sat Sun;set Meals := Lunch Dinner;# Last element is 1 if preferred, 0 if otherwiseset Prefs := (JC, Mon, Dinner, 1),(JC, Tue, Dinner, 0),... # Rest of preference data;

end;

Scripting with GNU MathProg

Good student practice:

Decouple model from data (separate files)

Write script to generate data file from sources

Reasonable production practice

Decouple model from data

Acquire data from and write output to database

Abuse: Read statements can be used to write to the database (e.g.: a currently working flag)

Examples

Trans-shipment (the standard example)

A source to destination network flow problem

Data: Quantity at source, Demand at destination

Time Table Scheduling (from school days...)

Respect requirements, Maximize preference

Support alternate sessions and multiple time slots

A Combinatorial Auction for HDB Flat Allocation

Allocation and pricing of HDB flats

Efficiently solvable (Surprise!)

Example: Trans-shipment

Model File

set I;/* canning plants */param a{i in I};/* capacity of plant i */set J;/* markets */param b{j in J};/* demand at market j */param d{i in I, j in J};/* distance in thousands of miles */param f;/* freight in dollars per case per thousand miles */param c{i in I, j in J} := f * d[i,j] / 1000;/* transport cost */var x{i in I, j in J} >= 0;/* shipment quantities in cases */minimize cost: sum{i in I, j in J} c[i,j] * x[i,j]; /* total costs */s.t. supply{i in I}: sum{j in J} x[i,j] = b[j];/* satisfy demand */solve;# < Post-processing code >end;

Example: Trans-shipment

Data File

data;set I := Seattle San-Diego;param a := Seattle 350San-Diego 600;

set J := New-York Chicago Topeka;param b := New-York 325Chicago 300Topeka 275;

param d :New-YorkChicagoTopeka :=Seattle2.51.71.8San-Diego2.51.81.4 ;

param f := 90;end;

Example: Trans-shipment

Sample Output

GLPK Simplex Optimizer, v4.476 rows, 6 columns, 18 non-zeros...OPTIMAL SOLUTION FOUND***************************************************POST SOLVE(Post-processed Output)************************************************Flow[ Seattle -> New-York ] = 50.000000Flow[ Seattle -> Chicago ] = 300.000000Flow[ Seattle -> Topeka ] = 0.000000Flow[ San-Diego -> New-York ] = 275.000000Flow[ San-Diego -> Chicago ] = 0.000000Flow[ San-Diego -> Topeka ] = 275.000000

Example: Trans-shipment

Using ODBC as a Data Source

table tbl_plants IN 'ODBC' 'dsn=demo_tpt' 'plants' :I