66
Germán Ferrari Instituto de Computación, Universidad de la República, Uruguay An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala Germán Ferrari Instituto de Computación, Universidad de la República, Uruguay

An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Embed Size (px)

Citation preview

Page 1: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Germán FerrariInstituto de Computación, Universidad de la República, Uruguay

An embedded DSL to manipulate MathProg

Mixed Integer Programming

models within Scala

An embedded DSL to manipulate MathProg

Mixed Integer Programming

models within ScalaGermán Ferrari

Instituto de Computación, Universidad de la República, Uruguay

Page 2: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

GNU MathProg

MathProg = Mathematical Programming

Has nothing to do with computer programming

It’s a synonym of “mathematical optimization”

A “program” here refers to a plan or schedule

Page 3: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

GNU MathProg

It’s a modeling language

Optimization problem Mathematical modeling

GNU MathProg Computational experiments

Supported by the GNU Linear Programming Kit

Page 4: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

GNU MathProg

Supports linear models, with either real or integer decision variables

This is called “Mixed Integer Programming” (MIP) because it mixes integer and real variables

Page 5: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Generic MIP optimization problemminimize f:

sum{j in J} c[j] * x[j] +

sum{k in K} d[k] * y[k];

subject to g{i in I}:

sum{j in J} a[i,j] * x[j] +

sum{k in K} e[i,k] * y[k] <= b[i];

Page 6: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Generic MIP optimization problem

Find x[j] and y[k], that minimize a function f, satisfying constraints g[i]

minimize f:

sum{j in J} c[j] * x[j] +

sum{k in K} d[k] * y[k];

subject to g{i in I}:

sum{j in J} a[i,j] * x[j] +

sum{k in K} e[i,k] * y[k] <= b[i];

Page 7: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Generic MIP optimization problem

Find x[j] and y[k], that minimize a function f, satisfying constraints g[i]

minimize f:

sum{j in J} c[j] * x[j] +

sum{k in K} d[k] * y[k];

subject to g{i in I}:

sum{j in J} a[i,j] * x[j] +

sum{k in K} e[i,k] * y[k] <= b[i];

Decision variables(real or integer)

Page 8: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Generic MIP optimization problem

Find x[j] and y[k], that minimize a function f, satisfying constraints g[i]

minimize f:

sum{j in J} c[j] * x[j] +

sum{k in K} d[k] * y[k];

subject to g{i in I}:

sum{j in J} a[i,j] * x[j] +

sum{k in K} e[i,k] * y[k] <= b[i];

Decision variables(real or integer)Objective

function

Page 9: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Generic MIP optimization problem

Find x[j] and y[k], that minimize a function f, satisfying constraints g[i]

minimize f:

sum{j in J} c[j] * x[j] +

sum{k in K} d[k] * y[k];

subject to g{i in I}:

sum{j in J} a[i,j] * x[j] +

sum{k in K} e[i,k] * y[k] <= b[i];

Decision variables(real or integer)Objective

function

Constraints

Page 10: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Generic MIP optimization problem

Find x[j] and y[k], that minimize a function f, satisfying constraints g[i]

minimize f:

sum{j in J} c[j] * x[j] +

sum{k in K} d[k] * y[k];

subject to g{i in I}:

sum{j in J} a[i,j] * x[j] +

sum{k in K} e[i,k] * y[k] <= b[i];

Decision variables(real or integer)Objective

function

ConstraintsLinear

Page 11: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Generic MIP optimization problem

Full modelparam m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; param e{I, K}; param b{I};

var x{J} integer, >= 0;var y{K} >= 0;

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];subject to g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

Page 12: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

param m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; param e{I, K}; param b{I};

var x{J} integer, >= 0;var y{K} >= 0;

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];subject to g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

Generic MIP optimization problem

Declaration of variables

Page 13: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

param m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; param e{I, K}; param b{I};

var x{J} integer, >= 0;var y{K} >= 0;

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];subject to g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

Generic MIP optimization problem

Parameters

Page 14: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Generic MIP optimization problemparam m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; param e{I, K}; param b{I};

var x{J} integer, >= 0;var y{K} >= 0;

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];subject to g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

Indexing sets

Page 15: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Why MathProg in Scala

Page 16: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Why MathProg in Scala

Generate constraints

Create derived models

Develop custom resolution methods at high level

...

Page 17: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

MathProg in Scala (deep embedding)

SyntaxRepresent MathProg models with Scala data typesMimic MathProg syntax with Scala functions

SemanticsGenerate MathProg code, others ...

Page 18: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

MathProg in Scala (deep embedding)

SyntaxRepresent MathProg models with Scala data typesMimic MathProg syntax with Scala functions

SemanticsGenerate MathProg code, others ...

Abstract syntax tree

Page 19: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

MathProg in Scala (deep embedding)

SyntaxRepresent MathProg models with Scala data typesMimic MathProg syntax with Scala functions

SemanticsGenerate MathProg code, others ...

Smart constructors and combinators

Abstract syntax tree

Page 20: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

MathProg EDSLparam m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; // ...

var x{J} integer, >= 0;var y{K} >= 0;

val m = param("m")val n = param("n")val l = param("l")

val I = set("I") := 1 to mval J = set("J") := 1 to n val K = set("K") := 1 to l

val c = param("c", J)val d = param("d", K)val a = param("a", I, J)// ...

val x = xvar("x", J).integer >= 0val y = xvar("y", K) >= 0

Page 21: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

MathProg EDSLparam m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; // ...

var x{J} integer, >= 0;var y{K} >= 0;

val m = param("m")val n = param("n")val l = param("l")

val I = set("I") := 1 to mval J = set("J") := 1 to n val K = set("K") := 1 to l

val c = param("c", J)val d = param("d", K)val a = param("a", I, J)// ...

val x = xvar("x", J).integer >= 0val y = xvar("y", K) >= 0

set

param

xvar ...

Page 22: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

MathProg EDSLparam m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; // ...

var x{J} integer, >= 0;var y{K} >= 0;

val m = param("m")val n = param("n")val l = param("l")

val I = set("I") := 1 to mval J = set("J") := 1 to n val K = set("K") := 1 to l

val c = param("c", J)val d = param("d", K)val a = param("a", I, J)// ...

val x = xvar("x", J).integer >= 0val y = xvar("y", K) >= 0

set

param

xvar ...

ParamStat( "c", domain = Some( IndExpr( List( IndEntry( Nil, SetRef(J) ) ))))

Page 23: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

val m = param("m")val n = param("n")val l = param("l")

val I = set("I") := 1 to mval J = set("J") := 1 to n val K = set("K") := 1 to l

val c = param("c", J)val d = param("d", K)val a = param("a", I, J)// ...

val x = xvar("x", J).integer >= 0val y = xvar("y", K) >= 0

MathProg EDSL

Scala range notation for arithmetic sets

param m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; // ...

var x{J} integer, >= 0;var y{K} >= 0;

Page 24: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

val m = param("m")val n = param("n")val l = param("l")

val I = set("I") := 1 to mval J = set("J") := 1 to n val K = set("K") := 1 to l

val c = param("c", J)val d = param("d", K)val a = param("a", I, J)// ...

val x = xvar("x", J).integer >= 0val y = xvar("y", K) >= 0

param m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; // ...

var x{J} integer, >= 0;var y{K} >= 0;

MathProg EDSL

Varargs for simple indexing expressions

Page 25: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

val m = param("m")val n = param("n")val l = param("l")

val I = set("I") := 1 to mval J = set("J") := 1 to n val K = set("K") := 1 to l

val c = param("c", J)val d = param("d", K)val a = param("a", I, J)// ...

val x = xvar("x", J).integer >= 0val y = xvar("y", K) >= 0

param m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; // ...

var x{J} integer, >= 0;var y{K} >= 0;

MathProg EDSL

Returns a new variable with the `integer’ attribute

Page 26: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

val m = param("m")val n = param("n")val l = param("l")

val I = set("I") := 1 to mval J = set("J") := 1 to n val K = set("K") := 1 to l

val c = param("c", J)val d = param("d", K)val a = param("a", I, J)// ...

val x = xvar("x", J).integer >= 0val y = xvar("y", K) >= 0

MathProg EDSL

Another variable, now adding the lower bound attribute

param m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; // ...

var x{J} integer, >= 0;var y{K} >= 0;

Page 27: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

val m = param("m")val n = param("n")val l = param("l")

val I = set("I") := 1 to mval J = set("J") := 1 to n val K = set("K") := 1 to l

val c = param("c", J)val d = param("d", K)val a = param("a", I, J)// ...

val x = xvar("x", J).integer >= 0val y = xvar("y", K) >= 0

param m; param n; param l;

set I := 1 .. m;set J := 1 .. n;set K := 1 .. l;

param c{J}; param d{K};param a{I, J}; // ...

var x{J} integer, >= 0;var y{K} >= 0;

MathProg EDSL

Everything is immutable

Page 28: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

MathProg EDSL

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];

s.t. g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

val f = minimize("f") { sum(j in J)(c(j) * x(j)) + sum(k in K)(d(k) * y(k)) } val g = st("g", i in I) { sum(j in J)(a(i, j) * x(j)) + sum(k in K)(e(i, k) * y(k)) <= b(i) }

Page 29: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

MathProg EDSL minimize

st ...minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];

s.t. g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

val f = minimize("f") { sum(j in J)(c(j) * x(j)) + sum(k in K)(d(k) * y(k)) } val g = st("g", i in I) { sum(j in J)(a(i, j) * x(j)) + sum(k in K)(e(i, k) * y(k)) <= b(i) }

Page 30: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];

s.t. g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

val f = minimize("f") { sum(j in J)(c(j) * x(j)) + sum(k in K)(d(k) * y(k)) } val g = st("g", i in I) { sum(j in J)(a(i, j) * x(j)) + sum(k in K)(e(i, k) * y(k)) <= b(i) }

MathProg EDSLMultiple parameters lists

Page 31: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];

s.t. g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

val f = minimize("f") { sum(j in J)(c(j) * x(j)) + sum(k in K)(d(k) * y(k)) } val g = st("g", i in I) { sum(j in J)(a(i, j) * x(j)) + sum(k in K)(e(i, k) * y(k)) <= b(i) }

MathProg EDSLMultiple parameters lists

Page 32: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];

s.t. g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

val f = minimize("f") { sum(j in J)(c(j) * x(j)) + sum(k in K)(d(k) * y(k)) } val g = st("g", i in I) { sum(j in J)(a(i, j) * x(j)) + sum(k in K)(e(i, k) * y(k)) <= b(i) }

MathProg EDSLaddition,multiplication, summation ...

Page 33: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];

s.t. g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

val f = minimize("f") { sum(j in J)(c(j) * x(j)) + sum(k in K)(d(k) * y(k)) } val g = st("g", i in I) { sum(j in J)(a(i, j) * x(j)) + sum(k in K)(e(i, k) * y(k)) <= b(i) }

MathProg EDSL References to individual parameters and variables by application

Page 34: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];

s.t. g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

val f = minimize("f") { sum(j in J)(c(j) * x(j)) + sum(k in K)(d(k) * y(k)) } val g = st("g", i in I) { sum(j in J)(a(i, j) * x(j)) + sum(k in K)(e(i, k) * y(k)) <= b(i) }

MathProg EDSL

Constraint

Page 35: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

minimize f: sum{j in J} c[j] * x[j] + sum{k in K} d[k] * y[k];

s.t. g{i in I}: sum{j in J} a[i,j] * x[j] + sum{k in K} e[i,k] * y[k] <= b[i];

val f = minimize("f") { sum(j in J)(c(j) * x(j)) + sum(k in K)(d(k) * y(k)) } val g = st("g", i in I) { sum(j in J)(a(i, j) * x(j)) + sum(k in K)(e(i, k) * y(k)) <= b(i) }

MathProg EDSL

Creates a new constraint with the specified name and indexing

Page 36: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax

Page 37: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax

Many of the syntactic constructs require:

A broad (open?) number of overloadings

Be used with infix notation

Page 38: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Many of the syntactic constructs require:

A broad (open?) number of overloadings

Be used with infix notation

Implementing the syntax

type classes and object-oriented forwarders

Page 39: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Many of the syntactic constructs require:

A broad (open?) number of overloadings

Be used with infix notation

Implementing the syntax

type classes and object-oriented forwarders

implicits prioritization

Page 40: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

param("p", J) >= 0

Page 41: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

param("p", J) >= 0

ParamStat

Page 42: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

param("p", J) >= 0

ParamStat

ParamStat doesn’t have a `>=’ method

Page 43: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

param("p", J) >= 0implicit conversion to something that provides the method

ParamStat doesn’t have a `>=’ method

ParamStat

Page 44: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

param("p", J) >= 0 // ParamStatparam("p", J) >= p2 // ParamStat xvar("x", I) >= 0 // VarStat i >= j // LogicExpr p(j1) >= p(j2) // LogicExpr x(i) >= 0 // ConstraintStat 10 >= x(i) // ConstraintStat

Many overloadings

… more

Page 45: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

implicit class GTESyntax[A](lhe: A) { def >=[B, C](rhe: B)(implicit GTEOp: GTEOp[A,B,C]): C = GTEOp.gte(lhe, rhe)}

Page 46: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

implicit class GTESyntax[A](lhe: A) { def >=[B, C](rhe: B)(implicit GTEOp: GTEOp[A,B,C]): C = GTEOp.gte(lhe, rhe)}

Fully generic

Page 47: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

implicit class GTESyntax[A](lhe: A) { def >=[B, C](rhe: B)(implicit GTEOp: GTEOp[A,B,C]): C = GTEOp.gte(lhe, rhe)}

trait GTEOp[A,B,C] { def gte(lhe: A, rhe: B): C}

Type class

Page 48: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

implicit class GTESyntax[A](lhe: A) { def >=[B, C](rhe: B)(implicit GTEOp: GTEOp[A,B,C]): C = GTEOp.gte(lhe, rhe)}

trait GTEOp[A,B,C] { def gte(lhe: A, rhe: B): C}

Type classThe implementation is forwarded to the implicit instance

Page 49: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

implicit class GTESyntax[A](lhe: A) { def >=[B, C](rhe: B)(implicit GTEOp: GTEOp[A,B,C]): C = GTEOp.gte(lhe, rhe)}

The available implicit instances encode the rules by which the operators can be used

Page 50: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”

implicit class GTESyntax[A](lhe: A) { def >=[B, C](rhe: B)(implicit GTEOp: GTEOp[A,B,C]): C = GTEOp.gte(lhe, rhe)}

The instance of the type class is required at the method level, so both A and B can be used in the implicits search

Page 51: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”instances

implicit def ParamStatGTEOp[A]( implicit conv: A => NumExpr): GTEOp[ParamStat, A, ParamStat] = /* ... */

>= for parameters and “numbers”param(“p”) >= m

Page 52: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”instances

implicit def ParamStatGTEOp[A]( implicit conv: A => NumExpr): GTEOp[ParamStat, A, ParamStat] = /* ... */

>= for parameters and “numbers”param(“p”) >= m

Page 53: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”instances

implicit def ParamStatGTEOp[A]( implicit conv: A => NumExpr): GTEOp[ParamStat, A, ParamStat] = /* ... */

>= for parameters and “numbers”param(“p”) >= m

View bound

Page 54: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”instances

implicit def VarStatGTEOp[A]( implicit conv: A => NumExpr): GTEOp[VarStat, A, VarStat] = /* ... */

>= for variables and “numbers”xvar(“x”) >= m

Analogous to the parameter case

Page 55: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”instances

implicit def NumExprGTEOp[A, B]( implicit convA: A => NumExpr, convB: B => NumExpr): GTEOp[A, B, LogicExpr] = /* ... */

>= for two “numbers”i >= j

Page 56: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”instances

implicit def NumExprGTEOp[A, B]( implicit convA: A => NumExpr, convB: B => NumExpr): GTEOp[A, B, LogicExpr] = /* ... */

>= for two “numbers”i >= j

Page 57: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”instances

implicit def NumExprGTEOp[A, B]( implicit convA: A => NumExpr, convB: B => NumExpr): GTEOp[A, B, LogicExpr] = /* ... */

>= for two “numbers”i >= j

Page 58: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implementing the syntax: “>=”instances

implicit def LinExprGTEOp[A, B]( implicit convA: A => LinExpr, convB: B => LinExpr): GTEOp[A, B, ConstraintStat] = /* ... */

>= for two “linear expressions”x(i) >= 0 and 10 >= x(i)

Analogous to the numerical expression case

Page 59: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

implicit def NumExprGTEOp[A, B]( implicit convA: A => NumExpr, convB: B => NumExpr)

conflicts with: implicit def ParamStatGTEOp[A]( implicit conv: A => NumExpr)

implicit def LinExprGTEOp[A, B]( implicit convA: A => LinExpr, convB: B => LinExpr)

Instances are ambiguous

Page 60: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

implicit def NumExprGTEOp[A, B]( implicit convA: A => NumExpr, convB: B => NumExpr)

conflicts with: implicit def ParamStatGTEOp[A]( implicit conv: A => NumExpr)

implicit def LinExprGTEOp[A, B]( implicit convA: A => LinExpr, convB: B => LinExpr)

Instances are ambiguous

`ParamStat <% NumExpr‘to allow `param(“p2”) >= p1’

Page 61: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Instances are ambiguousimplicit def NumExprGTEOp[A, B]( implicit convA: A => NumExpr, convB: B => NumExpr)

conflicts with: implicit def ParamStatGTEOp[A]( implicit conv: A => NumExpr)

implicit def LinExprGTEOp[A, B]( implicit convA: A => LinExpr, convB: B => LinExpr)

NumExpr <: LinExpr => NumExpr <% LinExpr

Page 62: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implicits prioritizationtrait GTEInstances extends GTEInstancesLowPriority1 { implicit def ParamStatGTEOp[A](implicit conv: A => NumExpr) /* */ implicit def VarStatGTEOp[A](implicit conv: A => NumExpr) /* */} trait GTEInstancesLowPriority1 extends GTEInstancesLowPriority2 { implicit def NumExprGTEOp[A, B]( implicit convA: A => NumExpr, convB: B => NumExpr) /* */} trait GTEInstancesLowPriority2 { implicit def LinExprGTEOp[A, B]( implicit convA: A => LinExpr, convB: B => LinExpr) /* */}

Page 63: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Implicits prioritizationtrait GTEInstances extends GTEInstancesLowPriority1 { implicit def ParamStatGTEOp[A](implicit conv: A => NumExpr) /* */ implicit def VarStatGTEOp[A](implicit conv: A => NumExpr) /* */} trait GTEInstancesLowPriority1 extends GTEInstancesLowPriority2 { implicit def NumExprGTEOp[A, B]( implicit convA: A => NumExpr, convB: B => NumExpr) /* */} trait GTEInstancesLowPriority2 { implicit def LinExprGTEOp[A, B]( implicit convA: A => LinExpr, convB: B => LinExpr) /* */}

> priority

> priority

Page 64: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

Next steps

New semantics

More static controls

Support other kind of problems beyond MIP

Talk directly to solvers

Arity, scope, ...

Multi-stage stochastic programming

Page 65: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

import amphip.dsl._import amphip.dsl._

amphip is a collection of experiments around manipulating MathProg models within Scala

github.com/gerferra/amphip

amphip is a collection of experiments around manipulating MathProg models within Scala

github.com/gerferra/amphip

Page 66: An embedded DSL to manipulate MathProg Mixed Integer Programming models within Scala

FINgithub.com/gerferra/amphip