SCPSolver

20
SCPSolver Linear Programming in Java made easy

description

Presenting s

Transcript of SCPSolver

Page 1: SCPSolver

SCPSolver

Linear Programming in Java madeeasy

Page 2: SCPSolver

Overview

• What is Linear Programming?

• Why one would like to use it?

• What is SCPSolver (not)?

• Demo

– Modelling for Beginners

– Modelling for Advanced Users

Hannes Planatscher 2

Page 3: SCPSolver

Linear Optimization

• „Linear Programming is a technique for the optimization of a linear objective function subject to linear constraints.“

Hannes Planatscher 3

n

i

ii xc1

max

1

,1n

ij i j

i

a x b j m

subject to m linear constraints

n-dimensional linear objective function

Page 4: SCPSolver

Constraints define halfspaces

Hannes Planatscher 4

x1

x2

Page 5: SCPSolver

Constraints define halfspaces

Hannes Planatscher 5

x1

x2

Page 6: SCPSolver

Feasible region

Hannes Planatscher 6

x1

x2

feasible region

Page 7: SCPSolver

Location of global optima

Hannes Planatscher 7

x1

x2

feasible regionpotential optimum

potential optimum

potential optimum

potential optimum

If the linear program is

feasible and not

unbounded, the

optimum can be found

at a vertex, in special

cases also at an

edge, of the polytope.

Page 8: SCPSolver

Location of global optima

Hannes Planatscher 8

x1

x2

optimum

If the linear program is

feasible and not

unbounded, the

optimum can be found

at a vertex, in special

cases also at an

edge, of the polytope.

Page 9: SCPSolver

Location of global optima

9

x1

x2

optimum

If the linear program is

feasible and not

unbounded, the

optimum can be found

at a vertex, in special

cases also at an

edge, of the polytope.

Page 10: SCPSolver

Simplex

10

x1

x2

feasible regionpotential optimum

optimum

potential optimum

potential optimum

The Simplex

Algorithm visits one

vertex after another

until the optimum is

found.

Page 11: SCPSolver

Interior Point

11

x1

x2

feasible regionpotential optimum

optimum

potential optimum

potential optimum

Interior point

algorithms try find to

optimum starting from

within the feasible

region.

Page 12: SCPSolver

Integer Variables

12

x1

x2

Page 13: SCPSolver

Why use Linear Programming?

• Even if the Simplex Algorithm has exponentialworst case complexity, it is very fast in mostreal world scenarios.

• Very efficient solvers, which can solve linear (integer) programs with several 100.000 variables and constraints, exist.

• If possible, always try to reformulate youroptimization problem as a Linear Program.

Hannes Planatscher 13

Page 14: SCPSolver

Linear Programming in Java

• Using existing Linear Programming Solvers in Java programs is a pain, because of:

– platform dependency (.dll, .so, .jnilib)

– bad API s

– difficult deployment (where to put the libraries)

• You have to stick with its (usually bad) API, once you have chosen a solver.

Hannes Planatscher 14

Page 15: SCPSolver

SCPSolver

• Main goals– make it easy to develop

– keep the API lean

– get „platformindependent“

– automate binary librarydeployment

– separate modelling fromthe solver

• Not a goal: Write a newSolver in Java!

Hannes Planatscher 15

Page 16: SCPSolver

„Low-level“-Modelling

Hannes Planatscher 16

LinearProgram lp = new LinearProgram(new double[]{5.0,10.0});

lp.setMinProblem(true);

lp.addConstraint(new LinearBiggerThanEqualsConstraint(new double[]{3.0,1.0}, 8.0, "c1"));

lp.addConstraint(new LinearBiggerThanEqualsConstraint(new double[]{0.0,4.0}, 4.0, "c2"));

lp.addConstraint(new LinearSmallerThanEqualsConstraint(new double[]{2.0,0.0}, 2.0, "c3"));

LinearProgramSolver solver = SolverFactory.newDefault();

double[] sol = solver.solve(lp);

1 2min5.0 10.0x x

1 23.0 1.0 8.0x x

24.0 4.0x

12.0 2.0x

Page 17: SCPSolver

„High-Level“-Modelling

Hannes Planatscher 17

LPWizard lpw = new LPWizard();

lpw.plus("x1",5.0).plus("x2",10.0);

lpw.addConstraint("c1",8,"<=").plus("x1",3.0).plus("x2",1.0);

lpw.addConstraint("c2",4,"<=").plus("x2",4.0);

lpw.addConstraint("c3", 2, ">=").plus("x1",2.0);

System.out.println(lpw.solve());

1 2min5.0 10.0x x

1 23.0 1.0 8.0x x

24.0 4.0x

12.0 2.0x

Page 18: SCPSolver

Solver Packs

• Solvers

– lpsolve (Open Source)

– GLPK (Open Source)

– CPLEX (closed

• Platforms

– Linux 32/64-bit (tested: Ubuntu, Scientific Linux)

– Mac Os X 64-bit (> 10.5)

– Windows 32-bit(tested: XP)

Hannes Planatscher 18

solver_x86.dll

solver_x86.so

solver_x64.jnilib

JNI Interface to solver

Solver Pack Jar

Page 19: SCPSolver

Solver Packs

• All libraries are asstatically linked aspossible

• Minimal dependencies on execution environment.

• Advantage– packs runs instantly on

many systems

• Disadvantage– the solver packs are pretty

large (lpsolve 1.3 MB, GLPK 2.3 MB, CPLEX 19 MB)

Hannes Planatscher 19

solver_x86.dll

solver_x86.so

solver_x86.jnilib

JNI Interface to solver

Solver Pack Jar

Page 20: SCPSolver

Farming

• Need to decide how much tons of wheat and/or rye toproduce.

• Rye earns 198 Euro/ton, wheat 226 Euro/ton• Rye needs 0.2 ha/ton, wheat 0.15 ha/ton• Rye needs 50 labour-units/ton, wheat 80 labour-

units/ton.• 50 ha and 5000 labour-units available• There are two barns, which can store up to 30 and 65

tons of one type of crop.• Because of EU-Regulations we have to produce exact

integer tons of both crops.

Hannes Planatscher 20