Wiki

Clone wiki

OscaR / LP

Linear Programming (need to be updated)

  • Find all the linear programming examples here.
  • OscaR does not implement its own LP solver but a high level scala modeling layer. Your model can be solved with high quality commercial or open source solvers namely gurobi, glpk, lp_solve (the solver is just one parameter of OscaR model).
  • We refer to the install section to set-up Eclipse and OscaR to use external linear solvers.

Hands-On

Whatever you\'ll do, you just need to import three packages :

You can now create a solver and create some variables in this solver:

Alternatively, you can mix continuous variables with integer variables using a MIP solver (rather than an LP solver).

Even if all the constraints are added, it is important to use the subjectTo{} function that executes the block of constraints.

Writing large linear expressions and linear constraints

OscaR has some facilities for writing large linear expressions. Here is an example of making a summation over an array of variables:

This function also works for a larger number of dimensions:

Note that the summation indexes can be any Iterable object. Have a look at the doc/api here to find all the different ways to create linear expressions with sum.

Integer vs Continuous Variables

OscaR recognizes that a MIPVar must be continuous or integer automatically.

  • Continuous Variable: If you specify the two bounds with lb,ub then the allowed values are in the continuous interval [lb,ub]. For instance

MIPVar(mip,\"x0\",0,40).

  • Integer Variable: If you give it a Scala range (using **to** or **until**) you create an integer variable that can take values in that range. For instance MIPVar(mip,\"x2\",0 until 18).

Retrieve the solution and get status of the solver

To retrieve the values of the variables:

To retrieve the value of the objective you can do Alternatively you can store the objective as a symbolic linear expression type (**LinearExpression**) and get its value later:

Get the outcome of the Solver

A MIP/LP solver can finish well or not so well. You can check its status with

It gives you a value from this enumeration (names are quite explicit):

Change the Solver

OscaR supports lp_solve, glpk, and gurobi (the latter two are only available on dev branch and you must add the jar files yourself since we cannot distribute them with OscaR).

You can specify which solver you want to use in the constructor of the solver. For instance:

The default solver is lp_solve for MIP and LP.

Releasing the memory of the Solver

Because OscaR relies on C/C++ libraries for the linear programming solving the memory cannot be released automatically. So it is always wise to release the memory of the internal MIP/LP solver when you don\'t need it anymore:

Updated