ukrishnas / LPSharp

LP solvers for optimization problems in C#, .Net, Windows platforms
MIT License
0 stars 0 forks source link

Loading models into CLP takes more time than solving them #3

Closed ukrishnas closed 3 months ago

ukrishnas commented 3 months ago

Load time of the model into CLP is of the same order a solving the model in CLP. Loading the model in CLP is 3 times slower than in GLOP for the model shown below. Comments state that code uses CoinModel to build the model and CoinBuild is 5x faster. Also, each call to set a coefficient involves a hash lookup in CoinHash. These should be optimized.

ClpInterface::SetCoefficient

/*
 * Clp model can be built using CoinBuild, CoinModel, CoinPackedMatrix and then
 * uploaded efficiently into ClpModel. CoinBuild is extremely lightweight and
 * best suited for adding entire column or row vectors. CoinModel has more
 * flexibility, like adding an element at a time, for the cost of being five
 * times slower than CoinBuild but four times faster than ClpModel. This
 * interface uses CoinModel. Add methods require row and column indices, and
 * CoinModelHash is used to convert names to indices.
 * /
bool ClpInterface::SetCoefficient(const char *row_name, const char *column_name, double value) {
    int row_index = row_hash_.hash(row_name);
    int column_index = column_hash_.hash(column_name);
    if (row_index == -1 || column_index == -1) {
        return false;
    }
    coin_model_.setElement(row_index, column_index, value);
    return true;
}

Usage in LPSharp:

foreach (var rowName in model.A.Indices) {
  var row = model.A[rowName];
  foreach (var colName in row.Indices) {
     this.clp.SetCoefficient(rowName, colName, row[colName]);
  }
}
ukrishnas commented 3 months ago

The new update will fix this. The new implementation uses CoinBuild for rows and columns. Variables (columns) are added to a column build object. Constraints (rows) are added into a build object.

The other implementations I tried without success were:

ukrishnas commented 3 months ago

This is fixed.