COIN-OR::LEMON - Graph Library

Changeset 2312:07e46cbb7d85 in lemon-0.x for lemon/lp_glpk.cc


Ignore:
Timestamp:
11/29/06 16:01:13 (17 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3088
Message:

modified _setColCoeff and _setRowCoeff parameters
const simplify() for expressions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/lp_glpk.cc

    r2253 r2312  
    117117  }
    118118 
    119   void LpGlpk::_setRowCoeffs(int i,
    120                              int length,
    121                              const int   * indices,
    122                              const Value   * values )
    123   {
    124     lpx_set_mat_row(lp, i, length,
    125                     const_cast<int * >(indices) ,
    126                     const_cast<Value * >(values));
    127   }
    128  
    129   void LpGlpk::_setColCoeffs(int i,
    130                              int length,
    131                              const int   * indices,
    132                              const Value   * values)
    133   {
    134     lpx_set_mat_col(lp, i, length,
    135                     const_cast<int * >(indices),
    136                     const_cast<Value * >(values));
     119  void LpGlpk::_setRowCoeffs(int i, LpRowIterator b, LpRowIterator e)
     120  {
     121    std::vector<int> indices;
     122    std::vector<Value> values;
     123
     124    indices.push_back(0);
     125    values.push_back(0);
     126
     127    for(LpRowIterator it=b; it!=e; ++it) {
     128      indices.push_back(it->first);
     129      values.push_back(it->second);
     130    }
     131
     132    lpx_set_mat_row(lp, i, values.size() - 1, &indices[0], &values[0]);
     133  }
     134 
     135  void LpGlpk::_setColCoeffs(int i, LpColIterator b, LpColIterator e) {
     136
     137    std::vector<int> indices;
     138    std::vector<Value> values;
     139
     140    indices.push_back(0);
     141    values.push_back(0);
     142
     143    for(LpColIterator it=b; it!=e; ++it) {
     144      indices.push_back(it->first);
     145      values.push_back(it->second);
     146    }
     147   
     148    lpx_set_mat_col(lp, i, values.size() - 1, &indices[0], &values[0]);
    137149  }
    138150
     
    140152  void LpGlpk::_setCoeff(int row, int col, Value value)
    141153  {
    142     ///FIXME Of course this is not efficient at all, but GLPK knows not more.
    143     // First approach: get one row, apply changes and set it again
    144     //(one idea to improve this: maybe it is better to do this with 1 coloumn)
     154
     155    if (lpx_get_num_cols(lp) < lpx_get_num_rows(lp)) {
     156
     157      int length=lpx_get_mat_row(lp, row, 0, 0);
     158     
     159      std::vector<int> indices(length + 2);
     160      std::vector<Value> values(length + 2);
     161     
     162      lpx_get_mat_row(lp, row, &indices[0], &values[0]);
     163     
     164      //The following code does not suppose that the elements of the
     165      //array indices are sorted
     166      bool found=false;
     167      for (int i = 1; i <= length; ++i) {
     168        if (indices[i]==col){
     169          found=true;
     170          values[i]=value;
     171          break;
     172        }
     173      }
     174      if (!found){
     175        ++length;
     176        indices[length]=col;
     177        values[length]=value;
     178      }
    145179   
    146     int mem_length=2+lpx_get_num_cols(lp);
    147     int* indices = new int[mem_length];
    148     Value* values = new Value[mem_length];
     180      lpx_set_mat_row(lp, row, length, &indices[0], &values[0]);
     181
     182    } else {
     183
     184      int length=lpx_get_mat_col(lp, col, 0, 0);
     185     
     186      std::vector<int> indices(length + 2);
     187      std::vector<Value> values(length + 2);
     188     
     189      lpx_get_mat_col(lp, col, &indices[0], &values[0]);
     190     
     191      //The following code does not suppose that the elements of the
     192      //array indices are sorted
     193      bool found=false;
     194      for (int i = 1; i <= length; ++i) {
     195        if (indices[i]==col){
     196          found=true;
     197          values[i]=value;
     198          break;
     199        }
     200      }
     201      if (!found){
     202        ++length;
     203        indices[length]=row;
     204        values[length]=value;
     205      }
    149206   
    150 
    151     int length=lpx_get_mat_row(lp, row, indices, values);
    152 
    153     //The following code does not suppose that the elements of the array indices are sorted
    154     int i=1;
    155     bool found=false;
    156     while (i <= length && !found){
    157       if (indices[i]==col){
    158         found = true;
    159         values[i]=value;
    160       }
    161       ++i;
    162     }
    163     if (!found){
    164       ++length;
    165       indices[length]=col;
    166       values[length]=value;
    167     }
    168    
    169     lpx_set_mat_row(lp, row, length, indices, values);
    170     delete [] indices;
    171     delete [] values;
    172    
     207      lpx_set_mat_col(lp, col, length, &indices[0], &values[0]);
     208    }
    173209  }
    174210
Note: See TracChangeset for help on using the changeset viewer.