29
1 The EPLEX Library

1 The EPLEX Library. 2 Introduction lib(eplex) An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

Embed Size (px)

Citation preview

Page 1: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

1

The EPLEX Library

Page 2: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

2

Introduction

lib(eplex)

An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently to COIN’s CLP/CBC and

SYMPHONY/CLP (Open Source) XPRESS-MP, a product by Dash Associates CPLEX, a product by ILOG SA

Motivation to have this link: Use state-of-the-art linear programming tools Use ECLiPSe for modelling Build hybrid solvers

Implementation Tight coupling, using subroutine libraries

lib(eplex)

An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently to COIN’s CLP/CBC and

SYMPHONY/CLP (Open Source) XPRESS-MP, a product by Dash Associates CPLEX, a product by ILOG SA

Motivation to have this link: Use state-of-the-art linear programming tools Use ECLiPSe for modelling Build hybrid solvers

Implementation Tight coupling, using subroutine libraries

Page 3: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

3

Mathematical Programming

Making optimal decisions with limited resources ‘Programming’ in the sense of planning Model problem to optimise as an ‘objective’ function,

subject to constraints Classic example: transportation problem

Page 4: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

4

Mathematical Programming

500

300

400

200

100

400

300

10

89

7

5

11

5

35

108

7

Plantcapacity

Clientdemand

Transportationcost

minimize10 A1 + 7 A2 + 11 A3 + 8 B1 + 5 B2 + 10 B3 + 5 C1 + 5 C2 + 8 C3 + 9 D1 + 3 D2 + 7 D3

subject toA1 + A2 + A3 = 200B1 + B2 + B3 = 400C1 + C2 + C3 = 300D1 + D2 + D3 = 100

A1 + B1 + C1 + D1 500

A2 + B2 + C2 + D2 300

A3 + B3 + C3 + D3 400

A

B

C

D

1

2

3

Objective Function

Constraints

Leads to a matrix:•Each row is one constraint•Each column is one variable

Page 5: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

6

LP/MIP – A brief overview

Linear Programming: Optimisation problem formulated using linear constraints and a linear

objective function An efficient algebraic method developed to solve such problems (Simplex,

1947) Interior point (barrier) method

Mixed Integer Programming: LP problems where some of the variables are constrained to be integer No known ‘optimal’ method: generally use simplex/barrier with branch and

bound search for optimal

Quadratic Programming Quadratic objective function, linear constraints Barrier method

Page 6: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

8

Possible Uses From Within ECLiPSe

Black-boxIf problem is suitable for LP/QP/MIP as a whole

But: Only one solution, non-incremental, rounding errors

Incrementality and multiple instancesResults explicitly retrieved

Multiple independent subproblems

HybridSolvers work on (possibly overlapping) subproblems

Programmed cooperation strategy

Data-driven triggering

Low-level interface

Page 7: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

9

General Usage

Loading::- lib(eplex).

Loads interface with default solver (OSI CLP/CBC on most machines)

Differences between the underlying solvers are largely hidden

DocumentationECLiPSe Library Manual

Tutorial chapter on Eplex

Reference Manual

Page 8: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

10

Transportation Problem (black box solving)

:- lib(eplex).

main(Cost, Vars) :-Vars = [A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2, D3],Vars :: 0.0..1.0Inf,

A1 + A2 + A3 $= 200,B1 + B2 + B3 $= 400,C1 + C2 + C3 $= 300,D1 + D2 + D3 $= 100,

A1 + B1 + C1 + D1 $=< 500,A2 + B2 + C2 + D2 $=< 300,A3 + B3 + C3 + D3 $=< 400,

optimize(min( 10*A1 + 7*A2 + 11*A3 + 8*B1 + 5*B2 + 10*B3 + 5*C1 + 5*C2 + 8*C3 + 9*D1 + 3*D2 + 7*D3), Cost).

Page 9: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

11

Eplex Constraints

ConstraintsVars :: Min..Max

Expr1 $= Epxr2 eplex:(Expr1 =:= Epxr2)

Expr1 $>= Epxr2 eplex:(Expr1 >= Epxr2)

Expr1 $=< Expr2 eplex:(Expr1 =< Epxr2)

integers(Vars)

Linear expressionsX123 3.4+Expr -ExprE1+E2 E1-E2 E1*E2sum( ListOfExpr )ListOfExpr1 * ListOfExpr2

Page 10: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

12

Be Aware of Conventions

In mathematical programming, non-negative variables are usually assumed as default

In ECLiPSe, the default is-1.0Inf .. 1.0Inf

Non-negative variables need to be declared, e.g.X :: 0.0 .. 1.0Inf

Page 11: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

13

Common Arithmetic Solver Interface

$::/2

$=/2, =:=/2

$>=/2, >=/2

$=</2, =</2

$>/2, >/2

$</2, </2

$\=/2

=\=/2

::/2

#::/2

#= /2

#>=/2, #>/2

#=</2, #</2

#\=/2

integers/1

reals/1

suspend

ic

eplex

std arith

Page 12: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

14

Black-box Optimizer

optimize(+Objective, -Cost) Objective is min(Expr) or max(Expr) Runs the external solver as a black box Considers all $=, $>=, $=< constraints, their variables, the variable

bounds and the integers/1 constraints Computes an optimal solution for Objective Returns the cost Cost Instantiates the variables to the computed solution values Selects LP/QP/MIP depending on integers and Objective

Page 13: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

15

Black-box use of LP and MIP solver

A linear problem?- X+Y $>= 3, X-Y $= 0, optimize(min(X),Cost).

Y=1.5

X=1.5

Cost=1.5

A mixed integer problem?- X+Y $>=3, X-Y $= 0, integers([X]), optimize(min(X),Cost).

Y=2.0

X=2

Cost=2.0

A linear problem?- X+Y $>= 3, X-Y $= 0, optimize(min(X),Cost).

Y=1.5

X=1.5

Cost=1.5

A mixed integer problem?- X+Y $>=3, X-Y $= 0, integers([X]), optimize(min(X),Cost).

Y=2.0

X=2

Cost=2.0

X

Y

Cost

Page 14: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

16

Summary: Eplex as black-box solver

CLP modelling / LP or MIP solving1. Do modelling and transformations in ECLiPSe

2. Load model into CPLEX/XPRESS/MP and solve using LP or MIP solver

3. Pass cost and solution values back to ECLiPSe

LP / MIP search

ECLiPSe External Solvervariables & bounds

cost

constraints

solution valuesX1 X2 ... Xm

=

=<

>=

= Cost

c1

c2

cn

Obj

Page 15: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

17

Problems with Black-Box Approach Solvable constraint class severely limited

Pure LP/QP/MIP

Numerical problems The external solvers use floating-point numbers, subject to rounding errors Other ECLiPSe solvers are usually precise (intervals or rational numbers) Instantiating the ECLiPSe variables to inexact values may cause other

implementations of the constraints to fail!

Lack of incrementality Cannot find alternative optima Cannot add constraints or variables

MIP search is a straightjacket A complex search process inside the black box Hard to control (via dozens of parameters) Hard to add problem-specific heuristics

Unsuitable for solver cooperation!

Page 16: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

18

Opening the Black Box

Code for optimize/2:

optimize(Objective, Cost) :-

eplex_solver_setup(Objective),

eplex_solve(Cost),

eplex_get(vars, VarVector),

eplex_get(typed_solution, SolutionVector),

VarVector = SolutionVector.

Code for optimize/2:

optimize(Objective, Cost) :-

eplex_solver_setup(Objective),

eplex_solve(Cost),

eplex_get(vars, VarVector),

eplex_get(typed_solution, SolutionVector),

VarVector = SolutionVector.

Page 17: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

19

Incrementality

Solver setupeplex_solver_setup(+Objective)

Run solver onceeplex_solve(-Cost)

Computes a solution with Cost but does not instantiate variables

Access solution informationStatus, solutions, reduced costs, dual values, slack, statistics, etc.

eplex_get(+What, -Value)

eplex_var_get(+Var, +What, -Value)

New constraints and variables can now be added and the problem re-solved!

Page 18: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

20

Multiple problems (eplex instances)

Declare an eplex instance, e.g.:- eplex_instance(prob).

(an instance called eplex exists by default)

Constraintsprob:(Vars :: Min..Max)prob:(Expr1 $= Epxr2) prob:(Expr1 =:= Epxr2)prob:(Expr1 $>= Epxr2) prob:(Expr1 >= Epxr2)prob:(Expr1 $=< Expr2) prob:(Expr1 =< Epxr2)prob:(integers(Vars))

Solver setupprob:eplex_solver_setup(+Objective)

Run solver onceprob:eplex_solve(-Cost)Computes a solution with Cost but does not instantiate variables

Access solution informationprob:eplex_get(+What, -Value)prob:epelx_var_get(+Var, +What, -Value)

Page 19: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

21

Transportation Problem (with Instance API)

:- lib(eplex).:- eplex_instance(prob).

main(Cost, Vars) :-Vars = [A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2, D3],prob:(Vars :: 0.0..1.0Inf),

prob:(A1 + A2 + A3 $= 200),prob:(B1 + B2 + B3 $= 400),prob:(C1 + C2 + C3 $= 300),prob:(D1 + D2 + D3 $= 100),prob:(A1 + B1 + C1 + D1 $=< 500),prob:(A2 + B2 + C2 + D2 $=< 300),prob:(A3 + B3 + C3 + D3 $=< 400),

prob:eplex_solver_setup(min( 10*A1 + 7*A2 + 11*A3 + 8*B1 + 5*B2 + 10*B3 + 5*C1 + 5*C2 + 8*C3 + 9*D1 + 3*D2 + 7*D3)),

prob:eplex_solve(Cost),

( foreach(Var,Vars) doprob:eplex_var_get(Var, typed_solution, Var)

).

Page 20: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

22

Traditional MIP branching

simplex solution:X = 4.2

X =< 4 X >= 5

At each node: Solve the continuous relaxation (ignoring integrality) with simplex Select an integer variable with fractional simplex solution Try two alternatives, with bounds forced to integers

Eventually, all variables will be narrowed to integers(if a solution exists)

At each node: Solve the continuous relaxation (ignoring integrality) with simplex Select an integer variable with fractional simplex solution Try two alternatives, with bounds forced to integers

Eventually, all variables will be narrowed to integers(if a solution exists)

Page 21: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

24

Doing MIP in ECLiPSe

:- lib(eplex), lib(branch_and_bound).

main :-...<setup constraints>IntVars = <variables that should take integral values>,Objective = <objective function>,...Objective $= CostVar,eplex_solver_setup(min(Objective)),eplex_solve(RelaxedCost), % initial simplex solvingCostVar $>= RelaxedCost) % apply resulting cost bound...minimize(mip_search(IntVars, CostVar), CostVar).

mip_search(IntVars, CostVar) :-...% for each X violating the integrality conditioneplex_var_get(X, solution, RelaxedSol),Split is floor(RelaxedSol),( X $=< Split ; X $>= Split+1 ), % choiceeplex_solve(RelaxedCost), % simplex re-solvingCostVar $>= RelaxedCost, % apply new cost bound...

no integrality constraints!

Page 22: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

25

Ok, but ...

The search procedure became messyextra argument (CostVar)

explicit simplex calls

modularity lost (choices/propagation)

Do we really need to run simplex every time?there might be no significant change

Solution: data-driven triggering, e.g. oninstantiation

instantiation far from relaxed solution

bounds change

bounds exclude relaxed solution

The search procedure became messyextra argument (CostVar)

explicit simplex calls

modularity lost (choices/propagation)

Do we really need to run simplex every time?there might be no significant change

Solution: data-driven triggering, e.g. oninstantiation

instantiation far from relaxed solution

bounds change

bounds exclude relaxed solution

Page 23: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

26

Triggering the solver automatically

eplex_solver_setup(+Objective, ?Cost, +Options,

+TriggerModes)Objective

min(Expr) or max(Expr)

Costvariable - it does not get instantiated, but only bounded by the solution cost.

TriggerModesinst - if a variable was instantiated

deviating_inst - if a variable was instantiated to a value that differs more than a tolerance from its LP-solution

bounds - if a variable bound was changed

deviating_bounds - if a variable bound was changed such that its LP-solution was excluded by more than a tolerance.

new_constraint - when a new constraint appears

<module>:<cond> - waking condition defined by other solver, e.g. ic:min

trigger(Atom) - explicit triggering

eplex_solver_setup(+Objective, ?Cost, +Options,

+TriggerModes)Objective

min(Expr) or max(Expr)

Costvariable - it does not get instantiated, but only bounded by the solution cost.

TriggerModesinst - if a variable was instantiated

deviating_inst - if a variable was instantiated to a value that differs more than a tolerance from its LP-solution

bounds - if a variable bound was changed

deviating_bounds - if a variable bound was changed such that its LP-solution was excluded by more than a tolerance.

new_constraint - when a new constraint appears

<module>:<cond> - waking condition defined by other solver, e.g. ic:min

trigger(Atom) - explicit triggering

Page 24: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

27

Using trigger conditions

Example

?- X+Y $>= 3, X-Y $= 0, eplex_solver_setup(min(X), X, [], [inst]).Y = Y{1.5}X = X{1.5}Delayed goals: lp_demon(…)Yes.

?- X+Y $>= 3, X-Y $= 0, eplex_solver_setup(min(X), X, [], [inst]), X=2.0.Y = Y{2.0}X = 2.0Delayed goals: lp_demon(…)Yes.

Page 25: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

28

Eplex solver as compound constraint

X1 X2 ... Xm

=

=<

>=

= Cost

c1

c2

cn

Obj

solver

setup

ExternalSolver

Solver triggered by events, then:

• new bounds and constraints are sent to the external solver

• external solver is run

• cost bound (or failure) is exported

• solution values are exported and ECLiPSe variables annotated (optional)

Page 26: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

29

More Elegant Implementation of MIP:- lib(eplex), lib(branch_and_bound).

main :-...<setup constraints>IntVars = <variables that should take integral values>,Objective = <objective function>,...Objective $= CostVar,eplex_solver_setup(min(Objective), CostVar, [], [bounds]),...minimize(mip_search(IntVars), CostVar).

mip_search(IntVars) :-...% for each X violating the integrality conditioneplex_var_get(X, solution, RelaxedSol),Split is floor(RelaxedSol),( X $=< Split) ; X $>= Split+1 ), % choice...% simplex re-solving triggered by bounds change% cost bound automatically applied...

No explicit simplex calls, no auxiliary parameters to search procedure Standard CLP search scheme - other choices/constraints can be added now

Page 27: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

30

Control Flow in Classical MIP Solver

Choice

Solve continuousrelaxation

Simplexstep

Choice

Page 28: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

31

Control Flow with Constraint Propagation

Search/Choice

Propagation phase

Search/Choice

constraints

Page 29: 1 The EPLEX Library. 2 Introduction  lib(eplex)  An interface between ECLiPSe and an external LP/MIP solver COIN-OR Open Solver Interface: currently

32

Control Flow with Constraint Propagation

Search/Choice

Propagation phase

Search/Choice

constraints

Eplex instance demon can be integrated similar to a global constraint

eplex instances