Changeset 2573


Ignore:
Timestamp:
May 8, 2019 6:35:04 AM (5 months ago)
Author:
unxusr
Message:

option to store maps for fast search of column/row names in C API

Location:
trunk/Cbc/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/src/Cbc_C_Interface.cpp

    r2572 r2573  
    77#include <cfloat>
    88#include <cctype>
     9#include <map>
     10#include <string>
    911
    1012#include "CoinPragma.hpp"
     
    4547
    4648const int VERBOSE = 0;
     49
     50typedef std::map< std::string, int > NameIndex;
    4751
    4852// cut generator to accept callbacks in CBC
     
    550554  model->relax_ = 0;
    551555  model->inc_callback = NULL;
     556  model->colNameIndex = NULL;
     557  model->rowNameIndex = NULL;
    552558
    553559  // initialize columns buffer
     
    578584  Cbc_deleteColBuffer(model);
    579585
     586  if (model->colNameIndex)
     587  {
     588    NameIndex *m = (NameIndex *)model->colNameIndex;
     589    delete m;
     590    m = (NameIndex *)model->rowNameIndex;
     591    assert( m != NULL );
     592    delete m;
     593  }
     594
    580595  if (VERBOSE > 1)
    581596    printf("%s delete model->model_\n", prefix);
     
    655670} //  Cbc_loadProblem()
    656671
     672/* should be called after reading a new problem */
     673static void fillAllNameIndexes(Cbc_Model *model)
     674{
     675  if (!model->colNameIndex)
     676    return;
     677
     678  OsiSolverInterface *solver = model->model_->solver();
     679  NameIndex &colNameIndex = *((NameIndex  *)model->colNameIndex);
     680  colNameIndex.clear();
     681  NameIndex &rowNameIndex = *((NameIndex  *)model->rowNameIndex);
     682  rowNameIndex.clear();
     683
     684  for ( int i=0 ; (i<solver->getNumCols()) ; ++i )
     685    colNameIndex[solver->getColName(i)] = i;
     686
     687  for ( int i=0 ; (i<solver->getNumRows()) ; ++i )
     688    rowNameIndex[solver->getRowName(i)] = i;
     689}
     690
    657691/* Read an mps file from the given filename */
    658692COINLIBAPI int COINLINKAGE
     
    667701
    668702  int result = 1;
    669   result = model->model_->solver()->readMps(filename);
     703  OsiSolverInterface *solver = model->model_->solver();
     704  result = solver->readMps(filename);
    670705  assert(result == 0);
     706
     707  fillAllNameIndexes(model);
    671708
    672709  if (VERBOSE > 0)
     
    734771    printf("%s filename = '%s'\n", prefix, filename);
    735772  int result = 1;
    736   result = model->model_->solver()->readLp(filename);
     773  OsiSolverInterface *solver = model->model_->solver();
     774  result = solver->readLp(filename);
    737775  assert(result == 0);
     776
     777  fillAllNameIndexes(model);
    738778
    739779  if (VERBOSE > 0)
     
    945985{
    946986  Cbc_flush(model);
    947   model->model_->solver()->setColName(iColumn, name);
     987  OsiSolverInterface *solver = model->model_->solver();
     988  std::string previousName = solver->getColName(iColumn);
     989  solver->setColName(iColumn, name);
     990
     991  if (!model->colNameIndex)
     992    return;
     993  NameIndex &colNameIndex = *((NameIndex  *)model->colNameIndex);
     994  NameIndex::iterator it = colNameIndex.find(previousName);
     995  if (it!=colNameIndex.end())
     996    colNameIndex.erase(it);
     997  colNameIndex[name] = iColumn;
    948998}
    949999
     
    9511001Cbc_setRowName(Cbc_Model *model, int iRow, const char *name)
    9521002{
    953   model->model_->solver()->setRowName(iRow, name);
     1003  OsiSolverInterface *solver = model->model_->solver();
     1004  std::string previousName = solver->getRowName(iRow);
     1005  solver->setRowName(iRow, name);
     1006
     1007  if (!model->rowNameIndex)
     1008    return;
     1009  NameIndex &rowNameIndex = *((NameIndex  *)model->rowNameIndex);
     1010  NameIndex::iterator it = rowNameIndex.find(std::string(previousName));
     1011  if (it!=rowNameIndex.end())
     1012    rowNameIndex.erase(it);
     1013  rowNameIndex[name] = iRow;
    9541014}
    9551015
     
    14761536    if (isInteger)
    14771537      solver->setInteger(solver->getNumCols() - 1);
     1538  }
     1539
     1540  if (model->colNameIndex)
     1541  {
     1542    NameIndex &colNameIndex = *((NameIndex  *)model->colNameIndex);
     1543    colNameIndex[std::string(name)] = Cbc_getNumCols(model)-1;
    14781544  }
    14791545}
     
    15121578  solver->addRow(nz, cols, coefs, rowLB, rowUB);
    15131579  solver->setRowName(solver->getNumRows() - 1, std::string(name));
    1514 }
    1515 
     1580
     1581  if (model->rowNameIndex)
     1582  {
     1583    NameIndex &rowNameIndex = *((NameIndex  *)model->rowNameIndex);
     1584    rowNameIndex[std::string(name)] = Cbc_getNumRows(model)-1;
     1585  }
     1586}
    15161587
    15171588COINLIBAPI void COINLINKAGE
     
    15201591  Cbc_flush(model);
    15211592  OsiSolverInterface *solver = model->model_->solver();
     1593
     1594  if (model->rowNameIndex)
     1595  {
     1596    NameIndex &rowNameIndex = *((NameIndex  *)model->rowNameIndex);
     1597    for ( int i=0 ; i<numRows; ++i )
     1598      rowNameIndex.erase(solver->getRowName(rows[i]));
     1599  }
     1600
    15221601  solver->deleteRows(numRows, rows);
    15231602}
     
    15281607  Cbc_flush(model);
    15291608  OsiSolverInterface *solver = model->model_->solver();
     1609
     1610  if (model->colNameIndex)
     1611  {
     1612    NameIndex &colNameIndex = *((NameIndex  *)model->colNameIndex);
     1613    for ( int i=0 ; i<numCols; ++i )
     1614      colNameIndex.erase(solver->getColName(cols[i]));
     1615  }
     1616
    15301617  solver->deleteCols(numCols, cols);
    15311618}
     
    20122099}
    20132100
     2101COINLIBAPI void COINLINKAGE
     2102Cbc_storeNameIndexes(Cbc_Model *model, char _store)
     2103{
     2104  if (_store)
     2105  {
     2106    if (model->colNameIndex==NULL)
     2107    {
     2108      assert(model->rowNameIndex==NULL);
     2109      model->colNameIndex = new NameIndex();
     2110      model->rowNameIndex = new NameIndex();
     2111    }
     2112  }
     2113  else
     2114  {
     2115    if (model->colNameIndex!=NULL)
     2116    {
     2117      NameIndex *m = (NameIndex *)model->colNameIndex;
     2118      delete m;
     2119      m = (NameIndex *)model->rowNameIndex;
     2120      assert( m != NULL );
     2121      delete m;
     2122
     2123      model->colNameIndex = model->rowNameIndex = NULL;
     2124    }
     2125  }
     2126}
     2127
     2128COINLIBAPI int COINLINKAGE
     2129Cbc_getColNameIndex(Cbc_Model *model, const char *name)
     2130{
     2131  if (!model->colNameIndex)
     2132  {
     2133    fprintf(stderr, "Call Cbc_storeNameIndex to enable name index search.");
     2134    abort();
     2135  }
     2136
     2137  OsiSolverInterface *solver = model->model_->solver();
     2138  NameIndex &colNameIndex = *((NameIndex  *)model->colNameIndex);
     2139  NameIndex::iterator it = colNameIndex.find(std::string(name));
     2140  if (it == colNameIndex.end())
     2141    return -1;
     2142
     2143  return it->second;
     2144}
     2145
     2146COINLIBAPI int COINLINKAGE
     2147Cbc_getRowNameIndex(Cbc_Model *model, const char *name)
     2148{
     2149  if (!model->rowNameIndex)
     2150  {
     2151    fprintf(stderr, "Call Cbc_storeNameIndex to enable name index search.");
     2152    abort();
     2153  }
     2154
     2155  OsiSolverInterface *solver = model->model_->solver();
     2156  NameIndex &rowNameIndex = *((NameIndex  *)model->rowNameIndex);
     2157  NameIndex::iterator it = rowNameIndex.find(std::string(name));
     2158  if (it == rowNameIndex.end())
     2159    return -1;
     2160
     2161  return it->second;
     2162}
     2163
     2164
    20142165#if defined(__MWERKS__)
    20152166#pragma export off
  • trunk/Cbc/src/Cbc_C_Interface.h

    r2572 r2573  
    4040COINLIBAPI int COINLINKAGE
    4141Cbc_setProblemName(Cbc_Model *model, const char *array);
     42
     43/** @brief activates/deactivates name indexes
     44 *
     45 * @param model problem object
     46 * @param store: 1 maintain indexes of column and constraints names for searching indexes, 0 not
     47 **/
     48COINLIBAPI void COINLINKAGE
     49Cbc_storeNameIndexes(Cbc_Model *model, char _store);
     50
     51/** @brief searches columns by name and returns its index
     52 *
     53 * call Cbc_storeNameIndexes to enable search by name
     54 *
     55 * @param model problem object
     56 * @param name column (variable) name
     57 * @return column index or -1 if not found
     58 **/
     59COINLIBAPI int COINLINKAGE
     60Cbc_getColNameIndex(Cbc_Model *model, const char *name);
     61
     62/** @brief searches rows by name and returns its index
     63 *
     64 * call Cbc_storeNameIndexes to enable search by name
     65 *
     66 * @param model problem object
     67 * @param name row (constraint) name
     68 * @return row index or -1 if not found
     69 **/
     70COINLIBAPI int COINLINKAGE
     71Cbc_getRowNameIndex(Cbc_Model *model, const char *name);
    4272
    4373/** @brief Creates a new column
Note: See TracChangeset for help on using the changeset viewer.