Search code examples
linear-programminginteger-programmingcoin-orcoin-or-clp

Limit Coin-OR CLP variables to integers


I've created a small program using the java clp implementation https://github.com/quantego/clp-java. Sometimes the solution returns decimals, which I don't want as the variables that I'm using are actually supposed to be booleans with a lower bound of 0 and an upper bound of 1. I was wondering if these variables could be limited to being an integer or if a variable can be defined as a boolean.

Currently I'm doing something like this:

CLP model = new CLP();
var expr = model.createExpression();
var goal = model.createExpression();
var var = model.addVariable().lb(0).ub(1);
expr.add(1, var);
var var2 = model.addVariable().lb(0).ub(1);
expr.add(1, var2);
var var3 = model.addVariable().lb(0).ub(1);
expr.add(1, var3);
expr.eq(1);
goal.add(4, var);
goal.add(2, var2);
goal.add(1, var3);
goal.asObjective();
model.minimize();
model.solve();

This is not the exact same code, because that code is too difficult to explain here. Is there anything that I can do to limit the variables to 0 and 1? Of so I have to use some other implementation?


Solution

  • No you can't.

    Clp is a linear programming solver which has no support for enforcing integrality.

    Cbc is the (mixed-)integer programming solver (by the same authors) based on Clp supporting this.

    Sadly, your third-party library is only interfacing Clp, not Cbc. See for example:

    You need to do that with some other tooling!

    (Btw: Building a MILP-solver on top of some LP-solver is extremely complex -> there is no workaround for you here)

    Alternatives

    (Not much of a java user)

    Consider using Googles or-tools' wrappers (the project itself is a huge umbrella optimization project based on C++ with many language-wrappers).

    They might have removed Clp/Cbc in the latest version (or upcoming ones), but if so, it has been replaced by something better (SCIP or Highs). In terms of the modelling-api it does not matter (model once, choose solver-backend).