lemon/lp_glpk.cc
changeset 2317 6d3ed14efb68
parent 2253 1645f6cc9667
child 2321 e23a610bed51
equal deleted inserted replaced
14:d70e1569818d 15:52d1976b9918
   114   void LpGlpk::_setColName(int col, const std::string & name)
   114   void LpGlpk::_setColName(int col, const std::string & name)
   115   {
   115   {
   116     lpx_set_col_name(lp,col,const_cast<char*>(name.c_str()));
   116     lpx_set_col_name(lp,col,const_cast<char*>(name.c_str()));
   117   }
   117   }
   118   
   118   
   119   void LpGlpk::_setRowCoeffs(int i, 
   119   void LpGlpk::_setRowCoeffs(int i, LpRowIterator b, LpRowIterator e) 
   120 			     int length,
   120   {
   121 			     const int   * indices, 
   121     std::vector<int> indices;
   122 			     const Value   * values )
   122     std::vector<Value> values;
   123   {
   123 
   124     lpx_set_mat_row(lp, i, length,
   124     indices.push_back(0);
   125 		    const_cast<int * >(indices) ,
   125     values.push_back(0);
   126 		    const_cast<Value * >(values));
   126 
   127   }
   127     for(LpRowIterator it=b; it!=e; ++it) {
   128   
   128       indices.push_back(it->first);
   129   void LpGlpk::_setColCoeffs(int i, 
   129       values.push_back(it->second);
   130 			     int length,
   130     }
   131 			     const int   * indices, 
   131 
   132 			     const Value   * values)
   132     lpx_set_mat_row(lp, i, values.size() - 1, &indices[0], &values[0]);
   133   {
   133   }
   134     lpx_set_mat_col(lp, i, length,
   134   
   135 		    const_cast<int * >(indices),
   135   void LpGlpk::_setColCoeffs(int i, LpColIterator b, LpColIterator e) {
   136 		    const_cast<Value * >(values));
   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]);
   137   }
   149   }
   138 
   150 
   139 
   151 
   140   void LpGlpk::_setCoeff(int row, int col, Value value) 
   152   void LpGlpk::_setCoeff(int row, int col, Value value) 
   141   {
   153   {
   142     ///FIXME Of course this is not efficient at all, but GLPK knows not more.
   154 
   143     // First approach: get one row, apply changes and set it again
   155     if (lpx_get_num_cols(lp) < lpx_get_num_rows(lp)) {
   144     //(one idea to improve this: maybe it is better to do this with 1 coloumn)
   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       }
   145     
   179     
   146     int mem_length=2+lpx_get_num_cols(lp);
   180       lpx_set_mat_row(lp, row, length, &indices[0], &values[0]);
   147     int* indices = new int[mem_length];
   181 
   148     Value* values = new Value[mem_length];
   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       }
   149     
   206     
   150 
   207       lpx_set_mat_col(lp, col, length, &indices[0], &values[0]);
   151     int length=lpx_get_mat_row(lp, row, indices, values);
   208     }
   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     
       
   173   }
   209   }
   174 
   210 
   175   void LpGlpk::_setColLowerBound(int i, Value lo)
   211   void LpGlpk::_setColLowerBound(int i, Value lo)
   176   {
   212   {
   177     if (lo==INF) {
   213     if (lo==INF) {