166 typedef _Value Value; |
166 typedef _Value Value; |
167 /// \e |
167 /// \e |
168 typedef IterablePartition<int>::ClassIt RowIt; |
168 typedef IterablePartition<int>::ClassIt RowIt; |
169 /// \e |
169 /// \e |
170 typedef IterablePartition<int>::ClassIt ColIt; |
170 typedef IterablePartition<int>::ClassIt ColIt; |
171 protected: |
171 public: |
172 /// \e |
172 /// \e |
173 IterablePartition<int> row_iter_map; |
173 IterablePartition<int> row_iter_map; |
174 /// \e |
174 /// \e |
175 IterablePartition<int> col_iter_map; |
175 IterablePartition<int> col_iter_map; |
176 /// \e |
176 /// \e |
177 const int VALID_ID; |
177 const int VALID_CLASS; |
178 /// \e |
178 /// \e |
179 const int INVALID_ID; |
179 const int INVALID_CLASS; |
180 public: |
180 public: |
181 /// \e |
181 /// \e |
182 LPSolverBase() : row_iter_map(2), |
182 LPSolverBase() : row_iter_map(2), |
183 col_iter_map(2), |
183 col_iter_map(2), |
184 VALID_ID(0), INVALID_ID(1) { } |
184 VALID_CLASS(0), INVALID_CLASS(1) { } |
185 /// \e |
185 /// \e |
186 virtual ~LPSolverBase() { } |
186 virtual ~LPSolverBase() { } |
187 /// \e |
187 /// \e |
188 virtual void setMinimize() = 0; |
188 virtual void setMinimize() = 0; |
189 /// \e |
189 /// \e |
190 virtual void setMaximize() = 0; |
190 virtual void setMaximize() = 0; |
191 /// \e |
191 protected: |
192 virtual RowIt addRow() = 0; |
192 /// \e |
193 /// \e |
193 virtual int _addRow() = 0; |
194 virtual ColIt addCol() = 0; |
194 /// \e |
195 /// temporally, glpk style indexing |
195 virtual int _addCol() = 0; |
196 virtual void setRowCoeffs(RowIt row_it, int num, |
196 public: |
197 int* indices, _Value* doubles) = 0; |
197 /// \e |
198 //pair<RowIt, _Value>-bol kell megadni egy std range-et |
198 RowIt addRow() { |
|
199 int i=_addRow(); |
|
200 RowIt row_it; |
|
201 row_iter_map.first(row_it, INVALID_CLASS); |
|
202 if (row_iter_map.valid(row_it)) { //van hasznalhato hely |
|
203 row_iter_map.set(row_it, INVALID_CLASS, VALID_CLASS); |
|
204 row_iter_map[row_it]=i; |
|
205 } else { //a cucc vegere kell inzertalni mert nincs szabad hely |
|
206 row_it=row_iter_map.push_back(i, VALID_CLASS); |
|
207 } |
|
208 return row_it; |
|
209 } |
|
210 /// \e |
|
211 ColIt addCol() { |
|
212 int i=_addCol(); |
|
213 ColIt col_it; |
|
214 col_iter_map.first(col_it, INVALID_CLASS); |
|
215 if (col_iter_map.valid(col_it)) { //van hasznalhato hely |
|
216 col_iter_map.set(col_it, INVALID_CLASS, VALID_CLASS); |
|
217 col_iter_map[col_it]=i; |
|
218 } else { //a cucc vegere kell inzertalni mert nincs szabad hely |
|
219 col_it=col_iter_map.push_back(i, VALID_CLASS); |
|
220 } |
|
221 return col_it; |
|
222 } |
|
223 /// \e |
|
224 virtual void setRowCoeffs(int i, |
|
225 std::vector<std::pair<int, double> > coeffs) = 0; |
|
226 /// \e |
|
227 virtual void setColCoeffs(int i, |
|
228 std::vector<std::pair<int, double> > coeffs) = 0; |
199 /// \e |
229 /// \e |
200 template <typename Begin, typename End> |
230 template <typename Begin, typename End> |
201 void setRowCoeffs(RowIt row_it, Begin begin, End end) { |
231 void setRowCoeffs(RowIt row_it, Begin begin, End end) { |
202 int mem_length=1+colNum(); |
232 std::vector<std::pair<int, double> > coeffs; |
203 int* indices = new int[mem_length]; |
|
204 _Value* doubles = new _Value[mem_length]; |
|
205 int length=0; |
|
206 for ( ; begin!=end; ++begin) { |
233 for ( ; begin!=end; ++begin) { |
207 ++length; |
234 coeffs.push_back(std:: |
208 indices[length]=col_iter_map[begin->first]; |
235 make_pair(col_iter_map[begin->first], begin->second)); |
209 doubles[length]=begin->second; |
236 } |
210 } |
237 setRowCoeffs(row_iter_map[row_it], coeffs); |
211 setRowCoeffs(row_it, length, indices, doubles); |
238 } |
212 delete [] indices; |
|
213 delete [] doubles; |
|
214 } |
|
215 /// temporally, glpk style indexing |
|
216 virtual void setColCoeffs(ColIt col_it, int num, |
|
217 int* indices, _Value* doubles) = 0; |
|
218 //pair<ColIt, _Value>-bol kell megadni egy std range-et |
|
219 /// \e |
239 /// \e |
220 template <typename Begin, typename End> |
240 template <typename Begin, typename End> |
221 void setColCoeffs(ColIt col_it, Begin begin, End end) { |
241 void setColCoeffs(ColIt col_it, Begin begin, End end) { |
222 int mem_length=1+rowNum(); |
242 std::vector<std::pair<int, double> > coeffs; |
223 int* indices = new int[mem_length]; |
|
224 _Value* doubles = new _Value[mem_length]; |
|
225 int length=0; |
|
226 for ( ; begin!=end; ++begin) { |
243 for ( ; begin!=end; ++begin) { |
227 ++length; |
244 coeffs.push_back(std:: |
228 indices[length]=row_iter_map[begin->first]; |
245 make_pair(row_iter_map[begin->first], begin->second)); |
229 doubles[length]=begin->second; |
246 } |
230 } |
247 setColCoeffs(col_iter_map[col_it], coeffs); |
231 setColCoeffs(col_it, length, indices, doubles); |
248 } |
232 delete [] indices; |
249 /// temporally, glpk style indexing |
233 delete [] doubles; |
250 //virtual void setRowCoeffs(RowIt row_it, int num, |
234 } |
251 // int* indices, _Value* doubles) = 0; |
235 /// \e |
252 //pair<RowIt, _Value>-bol kell megadni egy std range-et |
236 virtual void eraseCol(const ColIt& col_it) = 0; |
253 /// \e |
237 /// \e |
254 // virtual void seColCoeffs(int i, |
238 virtual void eraseRow(const RowIt& row_it) = 0; |
255 // std::vector<std::pair<int, double> > coeffs) = 0; |
|
256 /// \e |
|
257 // template <typename Begin, typename End> |
|
258 // void setRowCoeffs(RowIt row_it, Begin begin, End end) { |
|
259 // int mem_length=1+colNum(); |
|
260 // int* indices = new int[mem_length]; |
|
261 // _Value* doubles = new _Value[mem_length]; |
|
262 // int length=0; |
|
263 // for ( ; begin!=end; ++begin) { |
|
264 // ++length; |
|
265 // indices[length]=col_iter_map[begin->first]; |
|
266 // doubles[length]=begin->second; |
|
267 // } |
|
268 // setRowCoeffs(row_it, length, indices, doubles); |
|
269 // delete [] indices; |
|
270 // delete [] doubles; |
|
271 // } |
|
272 /// temporally, glpk style indexing |
|
273 //virtual void setColCoeffs(ColIt col_it, int num, |
|
274 // int* indices, _Value* doubles) = 0; |
|
275 //pair<ColIt, _Value>-bol kell megadni egy std range-et |
|
276 /// \e |
|
277 // template <typename Begin, typename End> |
|
278 // void setColCoeffs(ColIt col_it, Begin begin, End end) { |
|
279 // int mem_length=1+rowNum(); |
|
280 // int* indices = new int[mem_length]; |
|
281 // _Value* doubles = new _Value[mem_length]; |
|
282 // int length=0; |
|
283 // for ( ; begin!=end; ++begin) { |
|
284 // ++length; |
|
285 // indices[length]=row_iter_map[begin->first]; |
|
286 // doubles[length]=begin->second; |
|
287 // } |
|
288 // setColCoeffs(col_it, length, indices, doubles); |
|
289 // delete [] indices; |
|
290 // delete [] doubles; |
|
291 // } |
|
292 protected: |
|
293 /// \e |
|
294 virtual void _eraseCol(int i) = 0; |
|
295 /// \e |
|
296 virtual void _eraseRow(int i) = 0; |
|
297 public: |
|
298 /// \e |
|
299 void eraseCol(const ColIt& col_it) { |
|
300 col_iter_map.set(col_it, VALID_CLASS, INVALID_CLASS); |
|
301 int cols[2]; |
|
302 cols[1]=col_iter_map[col_it]; |
|
303 _eraseCol(cols[1]); |
|
304 col_iter_map[col_it]=0; //glpk specifikus, de kell ez?? |
|
305 ColIt it; |
|
306 for (col_iter_map.first(it, VALID_CLASS); |
|
307 col_iter_map.valid(it); col_iter_map.next(it)) { |
|
308 if (col_iter_map[it]>cols[1]) --col_iter_map[it]; |
|
309 } |
|
310 } |
|
311 /// \e |
|
312 void eraseRow(const RowIt& row_it) { |
|
313 row_iter_map.set(row_it, VALID_CLASS, INVALID_CLASS); |
|
314 int rows[2]; |
|
315 rows[1]=row_iter_map[row_it]; |
|
316 _eraseRow(rows[1]); |
|
317 row_iter_map[row_it]=0; //glpk specifikus, de kell ez?? |
|
318 RowIt it; |
|
319 for (row_iter_map.first(it, VALID_CLASS); |
|
320 row_iter_map.valid(it); row_iter_map.next(it)) { |
|
321 if (row_iter_map[it]>rows[1]) --row_iter_map[it]; |
|
322 } |
|
323 } |
239 /// \e |
324 /// \e |
240 virtual void setColBounds(const ColIt& col_it, int bound_type, |
325 virtual void setColBounds(const ColIt& col_it, int bound_type, |
241 _Value lo, _Value up) =0; |
326 _Value lo, _Value up) =0; |
242 /// \e |
327 /// \e |
243 virtual _Value getObjCoef(const ColIt& col_it) = 0; |
328 virtual _Value getObjCoef(const ColIt& col_it) = 0; |
341 } |
399 } |
342 /// \e |
400 /// \e |
343 void setMaximize() { |
401 void setMaximize() { |
344 lpx_set_obj_dir(lp, LPX_MAX); |
402 lpx_set_obj_dir(lp, LPX_MAX); |
345 } |
403 } |
346 /// \e |
404 protected: |
347 ColIt addCol() { |
405 /// \e |
348 int i=lpx_add_cols(lp, 1); |
406 int _addCol() { |
349 ColIt col_it; |
407 return lpx_add_cols(lp, 1); |
350 col_iter_map.first(col_it, INVALID_ID); |
408 } |
351 if (col_iter_map.valid(col_it)) { //van hasznalhato hely |
409 /// \e |
352 col_iter_map.set(col_it, INVALID_ID, VALID_ID); |
410 int _addRow() { |
353 col_iter_map[col_it]=i; |
411 return lpx_add_rows(lp, 1); |
354 //col_id_to_lp_col_id[col_iter_map[col_it]]=i; |
412 } |
355 } else { //a cucc vegere kell inzertalni mert nincs szabad hely |
413 public: |
356 //col_id_to_lp_col_id.push_back(i); |
|
357 //int j=col_id_to_lp_col_id.size()-1; |
|
358 col_it=col_iter_map.push_back(i, VALID_ID); |
|
359 } |
|
360 // edge_index_map.set(e, i); |
|
361 // lpx_set_col_bnds(lp, i, LPX_DB, 0.0, 1.0); |
|
362 // lpx_set_obj_coef(lp, i, cost[e]); |
|
363 return col_it; |
|
364 } |
|
365 /// \e |
|
366 RowIt addRow() { |
|
367 int i=lpx_add_rows(lp, 1); |
|
368 RowIt row_it; |
|
369 row_iter_map.first(row_it, INVALID_ID); |
|
370 if (row_iter_map.valid(row_it)) { //van hasznalhato hely |
|
371 row_iter_map.set(row_it, INVALID_ID, VALID_ID); |
|
372 row_iter_map[row_it]=i; |
|
373 } else { //a cucc vegere kell inzertalni mert nincs szabad hely |
|
374 row_it=row_iter_map.push_back(i, VALID_ID); |
|
375 } |
|
376 return row_it; |
|
377 } |
|
378 using Parent::setRowCoeffs; |
414 using Parent::setRowCoeffs; |
379 void setRowCoeffs(RowIt row_it, int length, |
415 /// \e |
380 int* indices, double* doubles) { |
416 virtual void setRowCoeffs(int i, |
381 lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles); |
417 std::vector<std::pair<int, double> > coeffs) { |
382 } |
418 int mem_length=1+colNum(); |
383 using Parent::setColCoeffs; |
419 int* indices = new int[mem_length]; |
384 void setColCoeffs(ColIt col_it, int length, |
420 double* doubles = new double[mem_length]; |
385 int* indices, double* doubles) { |
421 int length=0; |
386 lpx_set_mat_col(lp, col_iter_map[col_it], length, indices, doubles); |
422 for (std::vector<std::pair<int, double> >:: |
387 } |
423 const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) { |
|
424 ++length; |
|
425 indices[length]=it->first; |
|
426 doubles[length]=it->second; |
|
427 std::cout << " " << indices[length] << " " |
|
428 << doubles[length] << std::endl; |
|
429 } |
|
430 std::cout << i << " " << length << std::endl; |
|
431 lpx_set_mat_row(lp, i, length, indices, doubles); |
|
432 delete [] indices; |
|
433 delete [] doubles; |
|
434 } |
|
435 /// \e |
|
436 virtual void setColCoeffs(int i, |
|
437 std::vector<std::pair<int, double> > coeffs) { |
|
438 int mem_length=1+rowNum(); |
|
439 int* indices = new int[mem_length]; |
|
440 double* doubles = new double[mem_length]; |
|
441 int length=0; |
|
442 for (std::vector<std::pair<int, double> >:: |
|
443 const_iterator it=coeffs.begin(); it!=coeffs.end(); ++it) { |
|
444 ++length; |
|
445 indices[length]=it->first; |
|
446 doubles[length]=it->second; |
|
447 } |
|
448 lpx_set_mat_col(lp, i, length, indices, doubles); |
|
449 delete [] indices; |
|
450 delete [] doubles; |
|
451 } |
|
452 // /// \e |
|
453 // /// temporally, glpk style indexing |
|
454 // virtual void setRowCoeffs(RowIt row_it, int num, |
|
455 // int* indices, _Value* doubles) = 0; |
|
456 // //pair<RowIt, _Value>-bol kell megadni egy std range-et |
|
457 // /// \e |
|
458 // template <typename Begin, typename End> |
|
459 // void setRowCoeffs(RowIt row_it, Begin begin, End end) { |
|
460 // int mem_length=1+colNum(); |
|
461 // int* indices = new int[mem_length]; |
|
462 // _Value* doubles = new _Value[mem_length]; |
|
463 // int length=0; |
|
464 // for ( ; begin!=end; ++begin) { |
|
465 // ++length; |
|
466 // indices[length]=col_iter_map[begin->first]; |
|
467 // doubles[length]=begin->second; |
|
468 // } |
|
469 // setRowCoeffs(row_it, length, indices, doubles); |
|
470 // delete [] indices; |
|
471 // delete [] doubles; |
|
472 // } |
|
473 // void setRowCoeffs(RowIt row_it, int length, |
|
474 // int* indices, double* doubles) { |
|
475 // lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles); |
|
476 // } |
|
477 // using Parent::setColCoeffs; |
|
478 // void setColCoeffs(ColIt col_it, int length, |
|
479 // int* indices, double* doubles) { |
|
480 // lpx_set_mat_col(lp, col_iter_map[col_it], length, indices, doubles); |
|
481 // } |
388 // //pair<RowIt, double>-bol kell megadni egy std range-et |
482 // //pair<RowIt, double>-bol kell megadni egy std range-et |
389 // /// \e |
483 // /// \e |
390 // template <typename Begin, typename End> |
484 // template <typename Begin, typename End> |
391 // void setColCoeffs(const ColIt& col_it, |
485 // void setColCoeffs(const ColIt& col_it, |
392 // Begin begin, End end) { |
486 // Begin begin, End end) { |
420 // lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles); |
514 // lpx_set_mat_row(lp, row_iter_map[row_it], length, indices, doubles); |
421 // delete [] indices; |
515 // delete [] indices; |
422 // delete [] doubles; |
516 // delete [] doubles; |
423 // } |
517 // } |
424 /// \e |
518 /// \e |
425 void eraseCol(const ColIt& col_it) { |
519 protected: |
426 col_iter_map.set(col_it, VALID_ID, INVALID_ID); |
520 virtual void _eraseCol(int i) { |
427 int cols[2]; |
521 int cols[2]; |
428 cols[1]=col_iter_map[col_it]; |
522 cols[1]=i; |
429 lpx_del_cols(lp, 1, cols); |
523 lpx_del_cols(lp, 1, cols); |
430 col_iter_map[col_it]=0; //glpk specifikus |
524 } |
431 ColIt it; |
525 virtual void _eraseRow(int i) { |
432 for (col_iter_map.first(it, VALID_ID); |
|
433 col_iter_map.valid(it); col_iter_map.next(it)) { |
|
434 if (col_iter_map[it]>cols[1]) --col_iter_map[it]; |
|
435 } |
|
436 } |
|
437 /// \e |
|
438 void eraseRow(const RowIt& row_it) { |
|
439 row_iter_map.set(row_it, VALID_ID, INVALID_ID); |
|
440 int rows[2]; |
526 int rows[2]; |
441 rows[1]=row_iter_map[row_it]; |
527 rows[1]=i; |
442 lpx_del_rows(lp, 1, rows); |
528 lpx_del_rows(lp, 1, rows); |
443 row_iter_map[row_it]=0; //glpk specifikus |
529 } |
444 RowIt it; |
530 public: |
445 for (row_iter_map.first(it, VALID_ID); |
|
446 row_iter_map.valid(it); row_iter_map.next(it)) { |
|
447 if (row_iter_map[it]>rows[1]) --row_iter_map[it]; |
|
448 } |
|
449 } |
|
450 /// \e |
531 /// \e |
451 void setColBounds(const ColIt& col_it, int bound_type, |
532 void setColBounds(const ColIt& col_it, int bound_type, |
452 double lo, double up) { |
533 double lo, double up) { |
453 lpx_set_col_bnds(lp, col_iter_map[col_it], bound_type, lo, up); |
534 lpx_set_col_bnds(lp, col_iter_map[col_it], bound_type, lo, up); |
454 } |
535 } |