lemon/lp_glpk.cc
changeset 2389 df6a32249b46
parent 2368 6b2e8b734ae7
child 2391 14a343be7a5a
equal deleted inserted replaced
25:0624b9ad5c4a 26:8eca0fa12f52
   101     return i;
   101     return i;
   102   }
   102   }
   103 
   103 
   104   
   104   
   105   void LpGlpk::_eraseCol(int i) {
   105   void LpGlpk::_eraseCol(int i) {
   106     int cols[2];
   106     int ca[2];
   107     cols[1]=i;
   107     ca[1]=i;
   108     lpx_del_cols(lp, 1, cols);
   108     lpx_del_cols(lp, 1, ca);
   109   }
   109   }
   110   
   110   
   111   void LpGlpk::_eraseRow(int i) {
   111   void LpGlpk::_eraseRow(int i) {
   112     int rows[2];
   112     int ra[2];
   113     rows[1]=i;
   113     ra[1]=i;
   114     lpx_del_rows(lp, 1, rows);
   114     lpx_del_rows(lp, 1, ra);
   115   }
   115   }
   116 
   116 
   117   void LpGlpk::_getColName(int col, std::string & name) const
   117   void LpGlpk::_getColName(int c, std::string & name) const
   118   {
   118   {
   119     
   119     
   120     char *n = lpx_get_col_name(lp,col);
   120     char *n = lpx_get_col_name(lp,c);
   121     name = n?n:"";
   121     name = n?n:"";
   122   }
   122   }
   123   
   123   
   124   
   124   
   125   void LpGlpk::_setColName(int col, const std::string & name)
   125   void LpGlpk::_setColName(int c, const std::string & name)
   126   {
   126   {
   127     lpx_set_col_name(lp,col,const_cast<char*>(name.c_str()));
   127     lpx_set_col_name(lp,c,const_cast<char*>(name.c_str()));
   128 
   128 
   129   }
   129   }
   130 
   130 
   131   int LpGlpk::_colByName(const std::string& name) const
   131   int LpGlpk::_colByName(const std::string& name) const
   132   {
   132   {
   149     }
   149     }
   150 
   150 
   151     lpx_set_mat_row(lp, i, values.size() - 1, &indices[0], &values[0]);
   151     lpx_set_mat_row(lp, i, values.size() - 1, &indices[0], &values[0]);
   152   }
   152   }
   153 
   153 
   154   void LpGlpk::_getRowCoeffs(int i, RowIterator b) const
   154   void LpGlpk::_getRowCoeffs(int ix, RowIterator b) const
   155   {
   155   {
   156     int length = lpx_get_mat_row(lp, i, 0, 0);
   156     int length = lpx_get_mat_row(lp, ix, 0, 0);
   157     
   157     
   158     std::vector<int> indices(length + 1);
   158     std::vector<int> indices(length + 1);
   159     std::vector<Value> values(length + 1);
   159     std::vector<Value> values(length + 1);
   160     
   160     
   161     lpx_get_mat_row(lp, i, &indices[0], &values[0]);
   161     lpx_get_mat_row(lp, ix, &indices[0], &values[0]);
   162     
   162     
   163     for (int i = 1; i <= length; ++i) {
   163     for (int i = 1; i <= length; ++i) {
   164       *b = std::make_pair(indices[i], values[i]);
   164       *b = std::make_pair(indices[i], values[i]);
   165       ++b;
   165       ++b;
   166     }
   166     }
   167   }
   167   }
   168   
   168   
   169   void LpGlpk::_setColCoeffs(int i, ConstColIterator b, ConstColIterator e) {
   169   void LpGlpk::_setColCoeffs(int ix, ConstColIterator b, ConstColIterator e) {
   170 
   170 
   171     std::vector<int> indices;
   171     std::vector<int> indices;
   172     std::vector<Value> values;
   172     std::vector<Value> values;
   173 
   173 
   174     indices.push_back(0);
   174     indices.push_back(0);
   177     for(ConstColIterator it=b; it!=e; ++it) {
   177     for(ConstColIterator it=b; it!=e; ++it) {
   178       indices.push_back(it->first);
   178       indices.push_back(it->first);
   179       values.push_back(it->second);
   179       values.push_back(it->second);
   180     }
   180     }
   181     
   181     
   182     lpx_set_mat_col(lp, i, values.size() - 1, &indices[0], &values[0]);
   182     lpx_set_mat_col(lp, ix, values.size() - 1, &indices[0], &values[0]);
   183   }
   183   }
   184 
   184 
   185   void LpGlpk::_getColCoeffs(int i, ColIterator b) const
   185   void LpGlpk::_getColCoeffs(int ix, ColIterator b) const
   186   {
   186   {
   187     int length = lpx_get_mat_col(lp, i, 0, 0);
   187     int length = lpx_get_mat_col(lp, ix, 0, 0);
   188     
   188     
   189     std::vector<int> indices(length + 1);
   189     std::vector<int> indices(length + 1);
   190     std::vector<Value> values(length + 1);
   190     std::vector<Value> values(length + 1);
   191     
   191     
   192     lpx_get_mat_col(lp, i, &indices[0], &values[0]);
   192     lpx_get_mat_col(lp, ix, &indices[0], &values[0]);
   193     
   193     
   194     for (int i = 1; i <= length; ++i) {
   194     for (int i = 1; i <= length; ++i) {
   195       *b = std::make_pair(indices[i], values[i]);
   195       *b = std::make_pair(indices[i], values[i]);
   196       ++b;
   196       ++b;
   197     }
   197     }
   198   }
   198   }
   199 
   199 
   200   void LpGlpk::_setCoeff(int row, int col, Value value) 
   200   void LpGlpk::_setCoeff(int ix, int jx, Value value) 
   201   {
   201   {
   202 
   202 
   203     if (lpx_get_num_cols(lp) < lpx_get_num_rows(lp)) {
   203     if (lpx_get_num_cols(lp) < lpx_get_num_rows(lp)) {
   204 
   204 
   205       int length=lpx_get_mat_row(lp, row, 0, 0);
   205       int length=lpx_get_mat_row(lp, ix, 0, 0);
   206       
   206       
   207       std::vector<int> indices(length + 2);
   207       std::vector<int> indices(length + 2);
   208       std::vector<Value> values(length + 2);
   208       std::vector<Value> values(length + 2);
   209       
   209       
   210       lpx_get_mat_row(lp, row, &indices[0], &values[0]);
   210       lpx_get_mat_row(lp, ix, &indices[0], &values[0]);
   211       
   211       
   212       //The following code does not suppose that the elements of the
   212       //The following code does not suppose that the elements of the
   213       //array indices are sorted
   213       //array indices are sorted
   214       bool found=false;
   214       bool found=false;
   215       for (int i = 1; i <= length; ++i) {
   215       for (int i = 1; i <= length; ++i) {
   216         if (indices[i]==col){
   216         if (indices[i]==jx){
   217           found=true;
   217           found=true;
   218           values[i]=value;
   218           values[i]=value;
   219           break;
   219           break;
   220         }
   220         }
   221       }
   221       }
   222       if (!found){
   222       if (!found){
   223         ++length;
   223         ++length;
   224         indices[length]=col;
   224         indices[length]=jx;
   225         values[length]=value;
   225         values[length]=value;
   226       }
   226       }
   227     
   227     
   228       lpx_set_mat_row(lp, row, length, &indices[0], &values[0]);
   228       lpx_set_mat_row(lp, ix, length, &indices[0], &values[0]);
   229 
   229 
   230     } else {
   230     } else {
   231 
   231 
   232       int length=lpx_get_mat_col(lp, col, 0, 0);
   232       int length=lpx_get_mat_col(lp, jx, 0, 0);
   233       
   233       
   234       std::vector<int> indices(length + 2);
   234       std::vector<int> indices(length + 2);
   235       std::vector<Value> values(length + 2);
   235       std::vector<Value> values(length + 2);
   236       
   236       
   237       lpx_get_mat_col(lp, col, &indices[0], &values[0]);
   237       lpx_get_mat_col(lp, jx, &indices[0], &values[0]);
   238       
   238       
   239       //The following code does not suppose that the elements of the
   239       //The following code does not suppose that the elements of the
   240       //array indices are sorted
   240       //array indices are sorted
   241       bool found=false;
   241       bool found=false;
   242       for (int i = 1; i <= length; ++i) {
   242       for (int i = 1; i <= length; ++i) {
   243         if (indices[i]==col){
   243         if (indices[i]==jx){
   244           found=true;
   244           found=true;
   245           values[i]=value;
   245           values[i]=value;
   246           break;
   246           break;
   247         }
   247         }
   248       }
   248       }
   249       if (!found){
   249       if (!found){
   250         ++length;
   250         ++length;
   251         indices[length]=row;
   251         indices[length]=ix;
   252         values[length]=value;
   252         values[length]=value;
   253       }
   253       }
   254     
   254     
   255       lpx_set_mat_col(lp, col, length, &indices[0], &values[0]);
   255       lpx_set_mat_col(lp, jx, length, &indices[0], &values[0]);
   256     }
   256     }
   257   }
   257   }
   258 
   258 
   259   LpGlpk::Value LpGlpk::_getCoeff(int row, int col) const
   259   LpGlpk::Value LpGlpk::_getCoeff(int ix, int jx) const
   260   {
   260   {
   261 
   261 
   262     int length=lpx_get_mat_row(lp, row, 0, 0);
   262     int length=lpx_get_mat_row(lp, ix, 0, 0);
   263     
   263     
   264     std::vector<int> indices(length + 1);
   264     std::vector<int> indices(length + 1);
   265     std::vector<Value> values(length + 1);
   265     std::vector<Value> values(length + 1);
   266     
   266     
   267     lpx_get_mat_row(lp, row, &indices[0], &values[0]);
   267     lpx_get_mat_row(lp, ix, &indices[0], &values[0]);
   268     
   268     
   269     //The following code does not suppose that the elements of the
   269     //The following code does not suppose that the elements of the
   270     //array indices are sorted
   270     //array indices are sorted
   271     for (int i = 1; i <= length; ++i) {
   271     for (int i = 1; i <= length; ++i) {
   272       if (indices[i]==col){
   272       if (indices[i]==jx){
   273 	return values[i];
   273 	return values[i];
   274       }
   274       }
   275     }
   275     }
   276     return 0;
   276     return 0;
   277 
   277