Debian Codegraph @

Osi/src/OsiCpx/OsiCpxSolverInterface.cpp

raw symbols SCIP
1 //-----------------------------------------------------------------------------
2 // name: OSI Interface for CPLEX
3 // author: Tobias Pfender
4 // Konrad-Zuse-Zentrum Berlin (Germany)
5 // email: [email protected]
6 // date: 09/25/2000
7 // license: this file may be freely distributed under the terms of EPL
8 // comments: please scan this file for '???' and read the comments
9 //-----------------------------------------------------------------------------
10 // Copyright (C) 2000, Tobias Pfender, International Business Machines
11 // Corporation and others. All Rights Reserved.
12
13 #include <iostream>
14 #include <cassert>
15 #include <string>
16 #include <numeric>
17
18 #include "CoinPragma.hpp"
19
20 #include "OsiCpxSolverInterface.hpp"
21
22 #include "cplex.h"
23
24 // CPLEX 10.0 removed CPXERR_NO_INT_SOLN
25 #if !defined(CPXERR_NO_INT_SOLN)
26 #define CPXERR_NO_INT_SOLN CPXERR_NO_SOLN
27 #endif
28
29 // #define DEBUG 1
30
31 #ifdef DEBUG
32 #define debugMessage printf
33 #else
34 #define debugMessage \
35 if (false) \
36 printf
37 #endif
38
39 //#############################################################################
40 // A couple of helper functions
41 //#############################################################################
42
43 inline void freeCacheDouble(double *&ptr)
44 {
45 if (ptr != NULL) {
46 delete[] ptr;
47 ptr = NULL;
48 }
49 }
50
51 inline void freeCacheChar(char *&ptr)
52 {
53 if (ptr != NULL) {
54 delete[] ptr;
55 ptr = NULL;
56 }
57 }
58
59 inline void freeCacheMatrix(CoinPackedMatrix *&ptr)
60 {
61 if (ptr != NULL) {
62 delete ptr;
63 ptr = NULL;
64 }
65 }
66
67 static inline void
68 checkCPXerror(int err, std::string cpxfuncname, std::string osimethod)
69 {
70 if (err != 0) {
71 char s[100];
72 sprintf(s, "%s returned error %d", cpxfuncname.c_str(), err);
73 #ifdef DEBUG
74 std::cerr << "ERROR: " << s << " (" << osimethod << " in OsiCpxSolverInterface)" << std::endl;
75 #endif
76 throw CoinError(s, osimethod.c_str(), "OsiCpxSolverInterface");
77 }
78 }
79
80 static bool incompletemessage = false;
81
82 static void CPXPUBLIC OsiCpxMessageCallbackPrint(CoinMessageHandler *handler, const char *msg)
83 {
84 /* cplex adds the newlines into their message, while the coin message handler like to add its own newlines
85 * we treat the cases where there is a newline in the beginning or no newline at the end separately
86 * TODO a better way is to scan msg for newlines and to send each line to the handler separately */
87 if (msg[0] == '\n') {
88 if (incompletemessage) {
89 *handler << CoinMessageEol;
90 incompletemessage = false;
91 } else
92 handler->message(0, "CPX", " ", ' ') << CoinMessageEol;
93
94 ++msg;
95
96 if (!*msg)
97 return;
98 }
99
100 size_t len = strlen(msg);
101 assert(len > 0);
102
103 if (msg[len - 1] == '\n') {
104 (const_cast< char * >(msg))[len - 1] = '\0';
105 if (incompletemessage) {
106 *handler << msg << CoinMessageEol;
107 incompletemessage = false;
108 } else
109 handler->message(0, "CPX", msg, ' ') << CoinMessageEol;
110 } else {
111 handler->message(0, "CPX", msg, ' ');
112 incompletemessage = true;
113 }
114 }
115
116 static void CPXPUBLIC OsiCpxMessageCallbackResultLog(void *handle, const char *msg)
117 {
118 if (!*msg)
119 return;
120
121 if (handle) {
122 if (((CoinMessageHandler *)handle)->logLevel() >= 1)
123 OsiCpxMessageCallbackPrint((CoinMessageHandler *)handle, msg);
124 } else {
125 printf(msg);
126 }
127 }
128
129 static void CPXPUBLIC OsiCpxMessageCallbackWarning(void *handle, const char *msg)
130 {
131 if (handle) {
132 if (((CoinMessageHandler *)handle)->logLevel() >= 0)
133 OsiCpxMessageCallbackPrint((CoinMessageHandler *)handle, msg);
134 } else {
135 printf(msg);
136 }
137 }
138
139 static void CPXPUBLIC OsiCpxMessageCallbackError(void *handle, const char *msg)
140 {
141 if (handle) {
142 OsiCpxMessageCallbackPrint((CoinMessageHandler *)handle, msg);
143 } else {
144 fprintf(stderr, msg);
145 }
146 }
147
148 void OsiCpxSolverInterface::switchToLP(void)
149 {
150 debugMessage("OsiCpxSolverInterface::switchToLP()\n");
151
152 if (probtypemip_) {
153 CPXLPptr lp = getMutableLpPtr();
154
155 #if CPX_VERSION >= 800
156 assert(CPXgetprobtype(env_, lp) == CPXPROB_MILP);
157 #else
158 assert(CPXgetprobtype(env_, lp) == CPXPROB_MIP);
159 #endif
160
161 int err = CPXchgprobtype(env_, lp, CPXPROB_LP);
162 checkCPXerror(err, "CPXchgprobtype", "switchToLP");
163 probtypemip_ = false;
164 }
165 }
166
167 void OsiCpxSolverInterface::switchToMIP(void)
168 {
169 debugMessage("OsiCpxSolverInterface::switchToMIP()\n");
170
171 if (!probtypemip_) {
172 CPXLPptr lp = getMutableLpPtr();
173 int nc = getNumCols();
174 int *cindarray = new int[nc];
175
176 assert(CPXgetprobtype(env_, lp) == CPXPROB_LP);
177 assert(coltype_ != NULL);
178
179 for (int i = 0; i < nc; ++i)
180 cindarray[i] = i;
181
182 #if CPX_VERSION >= 800
183 int err = CPXchgprobtype(env_, lp, CPXPROB_MILP);
184 #else
185 int err = CPXchgprobtype(env_, lp, CPXPROB_MIP);
186 #endif
187 checkCPXerror(err, "CPXchgprobtype", "switchToMIP");
188
189 err = CPXchgctype(env_, lp, nc, cindarray, coltype_);
190 checkCPXerror(err, "CPXchgctype", "switchToMIP");
191
192 delete[] cindarray;
193 probtypemip_ = true;
194 }
195 }
196
197 void OsiCpxSolverInterface::resizeColType(int minsize)
198 {
199 debugMessage("OsiCpxSolverInterface::resizeColType()\n");
200
201 if (minsize > coltypesize_) {
202 int newcoltypesize = 2 * coltypesize_;
203 if (minsize > newcoltypesize)
204 newcoltypesize = minsize;
205 char *newcoltype = new char[newcoltypesize];
206
207 if (coltype_ != NULL) {
208 CoinDisjointCopyN(coltype_, coltypesize_, newcoltype);
209 delete[] coltype_;
210 }
211 coltype_ = newcoltype;
212 coltypesize_ = newcoltypesize;
213 }
214 assert(minsize == 0 || coltype_ != NULL);
215 assert(coltypesize_ >= minsize);
216 }
217
218 void OsiCpxSolverInterface::freeColType()
219 {
220 debugMessage("OsiCpxSolverInterface::freeColType()\n");
221
222 if (coltypesize_ > 0) {
223 delete[] coltype_;
224 coltype_ = NULL;
225 coltypesize_ = 0;
226 }
227 assert(coltype_ == NULL);
228 }
229
230 //#############################################################################
231 // Solve methods
232 //#############################################################################
233
234 void OsiCpxSolverInterface::initialSolve()
235 {
236 debugMessage("OsiCpxSolverInterface::initialSolve()\n");
237
238 switchToLP();
239
240 bool takeHint;
241 OsiHintStrength strength;
242
243 int algorithm = 0;
244 getHintParam(OsiDoDualInInitial, takeHint, strength);
245 if (strength != OsiHintIgnore)
246 algorithm = takeHint ? -1 : 1;
247
248 int presolve = 1;
249 getHintParam(OsiDoPresolveInInitial, takeHint, strength);
250 if (strength != OsiHintIgnore)
251 presolve = takeHint ? 1 : 0;
252
253 CPXLPptr lp = getLpPtr(OsiCpxSolverInterface::FREECACHED_RESULTS);
254
255 if (presolve)
256 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_ON);
257 else
258 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_OFF);
259
260 // if (messageHandler()->logLevel() == 0)
261 // CPXsetintparam( env_, CPX_PARAM_SCRIND, CPX_OFF );
262 // else
263 // CPXsetintparam( env_, CPX_PARAM_SCRIND, CPX_ON );
264
265 if (messageHandler()->logLevel() == 0)
266 CPXsetintparam(env_, CPX_PARAM_SIMDISPLAY, 0);
267 else if (messageHandler()->logLevel() == 1)
268 CPXsetintparam(env_, CPX_PARAM_SIMDISPLAY, 1);
269 else if (messageHandler()->logLevel() > 1)
270 CPXsetintparam(env_, CPX_PARAM_SIMDISPLAY, 2);
271
272 CPXsetintparam(env_, CPX_PARAM_ADVIND, !disableadvbasis);
273
274 double objoffset;
275 double primalobjlimit;
276 double dualobjlimit;
277 getDblParam(OsiObjOffset, objoffset);
278 getDblParam(OsiPrimalObjectiveLimit, primalobjlimit);
279 getDblParam(OsiDualObjectiveLimit, dualobjlimit);
280
281 if (getObjSense() == +1) {
282 if (primalobjlimit < COIN_DBL_MAX)
283 CPXsetdblparam(env_, CPX_PARAM_OBJLLIM, primalobjlimit + objoffset);
284 if (dualobjlimit > -COIN_DBL_MAX)
285 CPXsetdblparam(env_, CPX_PARAM_OBJULIM, dualobjlimit + objoffset);
286 } else {
287 if (primalobjlimit > -COIN_DBL_MAX)
288 CPXsetdblparam(env_, CPX_PARAM_OBJULIM, primalobjlimit + objoffset);
289 if (dualobjlimit < COIN_DBL_MAX)
290 CPXsetdblparam(env_, CPX_PARAM_OBJLLIM, dualobjlimit + objoffset);
291 }
292
293 int term;
294 switch (algorithm) {
295 default:
296 case 0:
297 term = CPXlpopt(env_, lp);
298 #if CPX_VERSION >= 800
299 checkCPXerror(term, "CPXlpopt", "initialSolve");
300 #endif
301 break;
302 case 1:
303 term = CPXprimopt(env_, lp);
304 #if CPX_VERSION >= 800
305 checkCPXerror(term, "CPXprimopt", "initialSolve");
306 #endif
307 break;
308 case -1:
309 term = CPXdualopt(env_, lp);
310 #if CPX_VERSION >= 800
311 checkCPXerror(term, "CPXdualopt", "initialSolve");
312 #endif
313 break;
314 }
315
316 /* If the problem is found infeasible during presolve, resolve it to get a
317 proper term code */
318 #if CPX_VERSION >= 800
319 int stat = CPXgetstat(env_, getMutableLpPtr());
320 if (stat == CPX_STAT_INForUNBD && presolve) {
321 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_OFF);
322 switch (algorithm) {
323 default:
324 case 0:
325 term = CPXlpopt(env_, lp);
326 checkCPXerror(term, "CPXlpopt", "initialSolve");
327 break;
328 case 1:
329 term = CPXprimopt(env_, lp);
330 checkCPXerror(term, "CPXprimopt", "initialSolve");
331 break;
332 case -1:
333 term = CPXdualopt(env_, lp);
334 checkCPXerror(term, "CPXdualopt", "initialSolve");
335 break;
336 }
337 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_ON);
338 }
339 #else
340 if (term == CPXERR_PRESLV_INForUNBD && presolve) {
341 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_OFF);
342 switch (algorithm) {
343 default:
344 case 0:
345 term = CPXlpopt(env_, lp);
346 break;
347 case 1:
348 term = CPXprimopt(env_, lp);
349 break;
350 case -1:
351 term = CPXdualopt(env_, lp);
352 break;
353 }
354 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_ON);
355 }
356 #endif
357
358 disableadvbasis = false;
359 }
360 //-----------------------------------------------------------------------------
361 void OsiCpxSolverInterface::resolve()
362 {
363 debugMessage("OsiCpxSolverInterface::resolve()\n");
364
365 switchToLP();
366
367 bool takeHint;
368 OsiHintStrength strength;
369
370 int algorithm = 0;
371 getHintParam(OsiDoDualInResolve, takeHint, strength);
372 if (strength != OsiHintIgnore)
373 algorithm = takeHint ? -1 : 1;
374
375 int presolve = 0;
376 getHintParam(OsiDoPresolveInResolve, takeHint, strength);
377 if (strength != OsiHintIgnore)
378 presolve = takeHint ? 1 : 0;
379
380 CPXLPptr lp = getLpPtr(OsiCpxSolverInterface::FREECACHED_RESULTS);
381
382 if (presolve)
383 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_ON);
384 else
385 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_OFF);
386
387 // if (messageHandler()->logLevel() == 0)
388 // CPXsetintparam( env_, CPX_PARAM_SCRIND, CPX_OFF );
389 // else
390 // CPXsetintparam( env_, CPX_PARAM_SCRIND, CPX_ON );
391
392 if (messageHandler()->logLevel() == 0)
393 CPXsetintparam(env_, CPX_PARAM_SIMDISPLAY, 0);
394 else if (messageHandler()->logLevel() == 1)
395 CPXsetintparam(env_, CPX_PARAM_SIMDISPLAY, 1);
396 else if (messageHandler()->logLevel() > 1)
397 CPXsetintparam(env_, CPX_PARAM_SIMDISPLAY, 2);
398
399 CPXsetintparam(env_, CPX_PARAM_ADVIND, !disableadvbasis);
400
401 int term;
402 switch (algorithm) {
403 default:
404 case 0:
405 term = CPXlpopt(env_, lp);
406 #if CPX_VERSION >= 800
407 checkCPXerror(term, "CPXlpopt", "resolve");
408 #endif
409 break;
410 case 1:
411 term = CPXprimopt(env_, lp);
412 #if CPX_VERSION >= 800
413 checkCPXerror(term, "CPXprimopt", "resolve");
414 #endif
415 break;
416 case -1:
417 term = CPXdualopt(env_, lp);
418 #if CPX_VERSION >= 800
419 checkCPXerror(term, "CPXdualopt", "resolve");
420 #endif
421 break;
422 }
423
424 /* If the problem is found infeasible during presolve, resolve it to get a
425 proper term code */
426 #if CPX_VERSION >= 800
427 int stat = CPXgetstat(env_, getMutableLpPtr());
428 if (stat == CPX_STAT_INForUNBD && presolve) {
429 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_OFF);
430 switch (algorithm) {
431 default:
432 case 0:
433 term = CPXlpopt(env_, lp);
434 checkCPXerror(term, "CPXlpopt", "resolve");
435 break;
436 case 1:
437 term = CPXprimopt(env_, lp);
438 checkCPXerror(term, "CPXprimopt", "resolve");
439 break;
440 case -1:
441 term = CPXdualopt(env_, lp);
442 checkCPXerror(term, "CPXdualopt", "resolve");
443 break;
444 }
445 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_ON);
446 }
447 #else
448 if (term == CPXERR_PRESLV_INForUNBD && presolve) {
449 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_OFF);
450 switch (algorithm) {
451 default:
452 case 0:
453 term = CPXlpopt(env_, lp);
454 checkCPXerror(term, "CPXlpopt", "resolve");
455 break;
456 case 1:
457 term = CPXprimopt(env_, lp);
458 checkCPXerror(term, "CPXprimopt", "resolve");
459 break;
460 case -1:
461 term = CPXdualopt(env_, lp);
462 checkCPXerror(term, "CPXdualopt", "resolve");
463 break;
464 }
465 CPXsetintparam(env_, CPX_PARAM_PREIND, CPX_ON);
466 }
467 #endif
468
469 disableadvbasis = false;
470 }
471 //-----------------------------------------------------------------------------
472 void OsiCpxSolverInterface::branchAndBound()
473 {
474 int term;
475
476 debugMessage("OsiCpxSolverInterface::branchAndBound()\n");
477
478 switchToMIP();
479
480 if (colsol_ != NULL && domipstart) {
481 int ncols = getNumCols();
482 int *ind = new int[ncols];
483
484 CoinIotaN(ind, ncols, 0);
485 #if 1
486 int zero = 0;
487 term = CPXaddmipstarts(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ALL), 1, ncols, &zero, ind, colsol_, NULL, NULL);
488 checkCPXerror(term, "CPXaddmipstarts", "branchAndBound");
489 #else
490 /* deprecated */
491 term = CPXcopymipstart(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ALL), ncols, ind, colsol_);
492 checkCPXerror(term, "CPXcopymipstart", "branchAndBound");
493 #endif
494
495 delete[] ind;
496
497 CPXsetintparam(env_, CPX_PARAM_ADVIND, CPX_ON);
498 } else
499 CPXsetintparam(env_, CPX_PARAM_ADVIND, CPX_OFF);
500
501 CPXLPptr lp = getLpPtr(OsiCpxSolverInterface::FREECACHED_RESULTS);
502
503 // if (messageHandler()->logLevel() == 0)
504 // CPXsetintparam( env_, CPX_PARAM_SCRIND, CPX_OFF );
505 // else
506 // CPXsetintparam( env_, CPX_PARAM_SCRIND, CPX_ON );
507
508 if (messageHandler()->logLevel() == 0)
509 CPXsetintparam(env_, CPX_PARAM_SIMDISPLAY, 0);
510 else if (messageHandler()->logLevel() == 1)
511 CPXsetintparam(env_, CPX_PARAM_SIMDISPLAY, 1);
512 else if (messageHandler()->logLevel() > 1)
513 CPXsetintparam(env_, CPX_PARAM_SIMDISPLAY, 2);
514
515 term = CPXmipopt(env_, lp);
516 checkCPXerror(term, "CPXmipopt", "branchAndBound");
517 }
518
519 //#############################################################################
520 // Parameter related methods
521 //#############################################################################
522
523 bool OsiCpxSolverInterface::setIntParam(OsiIntParam key, int value)
524 {
525 debugMessage("OsiCpxSolverInterface::setIntParam(%d, %d)\n", key, value);
526
527 bool retval = false;
528 switch (key) {
529 case OsiMaxNumIteration:
530 retval = (CPXsetintparam(env_, CPX_PARAM_ITLIM, value) == 0); // ??? OsiMaxNumIteration == #Simplex-iterations ???
531 break;
532 case OsiMaxNumIterationHotStart:
533 if (value >= 0) {
534 hotStartMaxIteration_ = value;
535 retval = true;
536 } else
537 retval = false;
538 break;
539 case OsiNameDiscipline:
540 retval = OsiSolverInterface::setIntParam(key, value);
541 break;
542 case OsiLastIntParam:
543 retval = false;
544 break;
545 default:
546 retval = false;
547 break;
548 }
549 return retval;
550 }
551
552 //-----------------------------------------------------------------------------
553
554 bool OsiCpxSolverInterface::setDblParam(OsiDblParam key, double value)
555 {
556 debugMessage("OsiCpxSolverInterface::setDblParam(%d, %g)\n", key, value);
557
558 bool retval = false;
559 switch (key) {
560 case OsiDualTolerance:
561 retval = (CPXsetdblparam(env_, CPX_PARAM_EPOPT, value) == 0); // ??? OsiDualTolerance == CPLEX Optimality tolerance ???
562 break;
563 case OsiPrimalTolerance:
564 retval = (CPXsetdblparam(env_, CPX_PARAM_EPRHS, value) == 0); // ??? OsiPrimalTolerance == CPLEX Feasibility tolerance ???
565 break;
566 case OsiDualObjectiveLimit:
567 case OsiPrimalObjectiveLimit:
568 case OsiObjOffset:
569 retval = OsiSolverInterface::setDblParam(key, value);
570 break;
571 case OsiLastDblParam:
572 retval = false;
573 break;
574 default:
575 retval = false;
576 break;
577 }
578 return retval;
579 }
580
581 //-----------------------------------------------------------------------------
582
583 bool OsiCpxSolverInterface::setStrParam(OsiStrParam key, const std::string &value)
584 {
585 debugMessage("OsiCpxSolverInterface::setStrParam(%d, %s)\n", key, value.c_str());
586
587 bool retval = false;
588 switch (key) {
589 case OsiProbName:
590 OsiSolverInterface::setStrParam(key, value);
591 return retval = true;
592 case OsiSolverName:
593 return false;
594 case OsiLastStrParam:
595 return false;
596 default:
597 return false;
598 }
599 return false;
600 }
601
602 //-----------------------------------------------------------------------------
603
604 bool OsiCpxSolverInterface::getIntParam(OsiIntParam key, int &value) const
605 {
606 debugMessage("OsiCpxSolverInterface::getIntParam(%d)\n", key);
607
608 bool retval = false;
609 switch (key) {
610 case OsiMaxNumIteration:
611 retval = (CPXgetintparam(env_, CPX_PARAM_ITLIM, &value) == 0); // ??? OsiMaxNumIteration == #Simplex-iterations ???
612 break;
613 case OsiMaxNumIterationHotStart:
614 value = hotStartMaxIteration_;
615 retval = true;
616 break;
617 case OsiNameDiscipline:
618 retval = OsiSolverInterface::getIntParam(key, value);
619 break;
620 case OsiLastIntParam:
621 retval = false;
622 break;
623 default:
624 retval = false;
625 break;
626 }
627 return retval;
628 }
629
630 //-----------------------------------------------------------------------------
631
632 bool OsiCpxSolverInterface::getDblParam(OsiDblParam key, double &value) const
633 {
634 debugMessage("OsiCpxSolverInterface::getDblParam(%d)\n", key);
635
636 bool retval = false;
637 switch (key) {
638 case OsiDualTolerance:
639 retval = (CPXgetdblparam(env_, CPX_PARAM_EPOPT, &value) == 0); // ??? OsiDualTolerance == CPLEX Optimality tolerance ???
640 break;
641 case OsiPrimalTolerance:
642 retval = (CPXgetdblparam(env_, CPX_PARAM_EPRHS, &value) == 0); // ??? OsiPrimalTolerance == CPLEX Feasibility tolerance ???
643 break;
644 case OsiDualObjectiveLimit:
645 case OsiPrimalObjectiveLimit:
646 case OsiObjOffset:
647 retval = OsiSolverInterface::getDblParam(key, value);
648 break;
649 case OsiLastDblParam:
650 retval = false;
651 break;
652 default:
653 retval = false;
654 break;
655 }
656 return retval;
657 }
658
659 //-----------------------------------------------------------------------------
660
661 bool OsiCpxSolverInterface::getStrParam(OsiStrParam key, std::string &value) const
662 {
663 debugMessage("OsiCpxSolverInterface::getStrParam(%d)\n", key);
664
665 switch (key) {
666 case OsiProbName:
667 OsiSolverInterface::getStrParam(key, value);
668 break;
669 case OsiSolverName:
670 value = "cplex";
671 break;
672 case OsiLastStrParam:
673 return false;
674 default:
675 return false;
676 }
677
678 return true;
679 }
680
681 //#############################################################################
682 // Methods returning info on how the solution process terminated
683 //#############################################################################
684
685 bool OsiCpxSolverInterface::isAbandoned() const
686 {
687 debugMessage("OsiCpxSolverInterface::isAbandoned()\n");
688
689 int stat = CPXgetstat(env_, getMutableLpPtr());
690
691 #if CPX_VERSION >= 800
692 return (stat == 0 || stat == CPX_STAT_NUM_BEST || stat == CPX_STAT_ABORT_USER);
693 #else
694 return (stat == 0 || stat == CPX_NUM_BEST_FEAS || stat == CPX_NUM_BEST_INFEAS || stat == CPX_ABORT_FEAS || stat == CPX_ABORT_INFEAS || stat == CPX_ABORT_CROSSOVER);
695 #endif
696 }
697
698 bool OsiCpxSolverInterface::isProvenOptimal() const
699 {
700 debugMessage("OsiCpxSolverInterface::isProvenOptimal()\n");
701
702 int stat = CPXgetstat(env_, getMutableLpPtr());
703
704 #if CPX_VERSION >= 800
705 return ((probtypemip_ == false && (stat == CPX_STAT_OPTIMAL || stat == CPX_STAT_OPTIMAL_INFEAS))
706 || (probtypemip_ == true && (stat == CPXMIP_OPTIMAL || stat == CPXMIP_OPTIMAL_TOL)));
707 #else
708 return ((probtypemip_ == false && (stat == CPX_OPTIMAL || stat == CPX_OPTIMAL_INFEAS)) || (probtypemip_ == true && stat == CPXMIP_OPTIMAL));
709 #endif
710 }
711
712 bool OsiCpxSolverInterface::isProvenPrimalInfeasible() const
713 {
714 debugMessage("OsiCpxSolverInterface::isProvenPrimalInfeasible()\n");
715
716 int stat = CPXgetstat(env_, getMutableLpPtr());
717
718 #if CPX_VERSION >= 800
719 // In CPLEX 8, the return code is with respect
720 // to the original problem, regardless of the algorithm used to solve it
721 // --tkr 7/31/03
722 return (stat == CPX_STAT_INFEASIBLE);
723 // return (method == CPX_ALG_PRIMAL && stat == CPX_STAT_INFEASIBLE ||
724 // method == CPX_ALG_DUAL && stat == CPX_STAT_UNBOUNDED);
725 #else
726
727 int method = CPXgetmethod(env_, getMutableLpPtr());
728
729 return ((method == CPX_ALG_PRIMAL && stat == CPX_INFEASIBLE) || (method == CPX_ALG_DUAL && stat == CPX_UNBOUNDED) || stat == CPX_ABORT_PRIM_INFEAS || stat == CPX_ABORT_PRIM_DUAL_INFEAS);
730 #endif
731 }
732
733 bool OsiCpxSolverInterface::isProvenDualInfeasible() const
734 {
735 debugMessage("OsiCpxSolverInterface::isProvenDualInfeasible()\n");
736
737 int stat = CPXgetstat(env_, getMutableLpPtr());
738
739 #if CPX_VERSION >= 800
740 // In CPLEX 8, the return code is with respect
741 // to the original problem, regardless of the algorithm used to solve it
742 // --tkr 7/31/03
743 return (stat == CPX_STAT_UNBOUNDED);
744 //return (method == CPX_ALG_PRIMAL && stat == CPX_STAT_UNBOUNDED ||
745 // method == CPX_ALG_DUAL && stat == CPX_STAT_INFEASIBLE);
746 #else
747
748 int method = CPXgetmethod(env_, getMutableLpPtr());
749
750 return ((method == CPX_ALG_PRIMAL && stat == CPX_UNBOUNDED) || (method == CPX_ALG_DUAL && stat == CPX_INFEASIBLE) || stat == CPX_ABORT_DUAL_INFEAS || stat == CPX_ABORT_PRIM_DUAL_INFEAS);
751 #endif
752 }
753
754 bool OsiCpxSolverInterface::isPrimalObjectiveLimitReached() const
755 {
756 debugMessage("OsiCpxSolverInterface::isPrimalObjectiveLimitReached()\n");
757
758 int stat = CPXgetstat(env_, getMutableLpPtr());
759 int method = CPXgetmethod(env_, getMutableLpPtr());
760
761 #if CPX_VERSION >= 800
762 return method == CPX_ALG_PRIMAL && stat == CPX_STAT_ABORT_OBJ_LIM;
763 #else
764 return method == CPX_ALG_PRIMAL && stat == CPX_OBJ_LIM;
765 #endif
766 }
767
768 bool OsiCpxSolverInterface::isDualObjectiveLimitReached() const
769 {
770 debugMessage("OsiCpxSolverInterface::isDualObjectiveLimitReached()\n");
771
772 int stat = CPXgetstat(env_, getMutableLpPtr());
773 int method = CPXgetmethod(env_, getMutableLpPtr());
774
775 #if CPX_VERSION >= 800
776 return method == CPX_ALG_DUAL && stat == CPX_STAT_ABORT_OBJ_LIM;
777 #else
778 return method == CPX_ALG_DUAL && stat == CPX_OBJ_LIM;
779 #endif
780 }
781
782 bool OsiCpxSolverInterface::isIterationLimitReached() const
783 {
784 debugMessage("OsiCpxSolverInterface::isIterationLimitReached()\n");
785
786 int stat = CPXgetstat(env_, getMutableLpPtr());
787
788 #if CPX_VERSION >= 800
789 return stat == CPX_STAT_ABORT_IT_LIM;
790 #else
791 return stat == CPX_IT_LIM_FEAS || stat == CPX_IT_LIM_INFEAS;
792 #endif
793 }
794
795 //#############################################################################
796 // WarmStart related methods
797 //#############################################################################
798
799 CoinWarmStart *OsiCpxSolverInterface::getEmptyWarmStart() const
800 {
801 return (dynamic_cast< CoinWarmStart * >(new CoinWarmStartBasis()));
802 }
803
804 CoinWarmStart *OsiCpxSolverInterface::getWarmStart() const
805 {
806 debugMessage("OsiCpxSolverInterface::getWarmStart()\n");
807
808 if (probtypemip_)
809 return getEmptyWarmStart();
810
811 CoinWarmStartBasis *ws = NULL;
812 int numcols = getNumCols();
813 int numrows = getNumRows();
814 int *cstat = new int[numcols];
815 int *rstat = new int[numrows];
816 char *sense = new char[numrows];
817 int restat, i;
818
819 restat = CPXgetsense(env_, getMutableLpPtr(), sense, 0, numrows - 1);
820 if (restat == 0)
821 restat = CPXgetbase(env_, getMutableLpPtr(), cstat, rstat);
822 if (restat == 0) {
823 ws = new CoinWarmStartBasis;
824 ws->setSize(numcols, numrows);
825
826 for (i = 0; i < numrows; ++i) {
827 switch (rstat[i]) {
828 case CPX_BASIC:
829 ws->setArtifStatus(i, CoinWarmStartBasis::basic);
830 break;
831 case CPX_AT_LOWER:
832 if (sense[i] == 'G')
833 ws->setArtifStatus(i, CoinWarmStartBasis::atUpperBound);
834 else
835 ws->setArtifStatus(i, CoinWarmStartBasis::atLowerBound);
836 break;
837 case CPX_AT_UPPER:
838 if (sense[i] == 'L')
839 ws->setArtifStatus(i, CoinWarmStartBasis::atLowerBound);
840 else
841 ws->setArtifStatus(i, CoinWarmStartBasis::atUpperBound);
842 break;
843 default: // unknown row status
844 delete ws;
845 ws = NULL;
846 goto TERMINATE;
847 }
848 }
849
850 for (i = 0; i < numcols; ++i) {
851 switch (cstat[i]) {
852 case CPX_BASIC:
853 ws->setStructStatus(i, CoinWarmStartBasis::basic);
854 break;
855 case CPX_AT_LOWER:
856 ws->setStructStatus(i, CoinWarmStartBasis::atLowerBound);
857 break;
858 case CPX_AT_UPPER:
859 ws->setStructStatus(i, CoinWarmStartBasis::atUpperBound);
860 break;
861 case CPX_FREE_SUPER:
862 ws->setStructStatus(i, CoinWarmStartBasis::isFree);
863 break;
864 default: // unknown column status
865 delete ws;
866 ws = NULL;
867 goto TERMINATE;
868 }
869 }
870 }
871 TERMINATE:
872 delete[] cstat;
873 delete[] rstat;
874 delete[] sense;
875
876 return ws;
877 }
878
879 //-----------------------------------------------------------------------------
880
881 bool OsiCpxSolverInterface::setWarmStart(const CoinWarmStart *warmstart)
882 {
883 debugMessage("OsiCpxSolverInterface::setWarmStart(%p)\n", (void *)warmstart);
884
885 const CoinWarmStartBasis *ws = dynamic_cast< const CoinWarmStartBasis * >(warmstart);
886 int numcols, numrows, i, restat;
887 int *cstat, *rstat;
888 bool retval = false;
889
890 if (!ws)
891 return false;
892
893 numcols = getNumCols();
894 numrows = getNumRows();
895
896 /* if too small warm start information is given, we take this as a sign to disable a warm start in the next LP solve */
897 if (ws->getNumStructural() < numcols || ws->getNumArtificial() < numrows) {
898 disableadvbasis = true;
899 return false;
900 }
901
902 switchToLP();
903
904 cstat = new int[numcols];
905 rstat = new int[numrows];
906 for (i = 0; i < numrows; ++i) {
907 switch (ws->getArtifStatus(i)) {
908 case CoinWarmStartBasis::basic:
909 rstat[i] = CPX_BASIC;
910 break;
911 case CoinWarmStartBasis::atLowerBound:
912 rstat[i] = CPX_AT_LOWER;
913 break;
914 case CoinWarmStartBasis::atUpperBound:
915 rstat[i] = CPX_AT_UPPER;
916 break;
917 default: // unknown row status
918 retval = false;
919 goto TERMINATE;
920 }
921 }
922 for (i = 0; i < numcols; ++i) {
923 switch (ws->getStructStatus(i)) {
924 case CoinWarmStartBasis::basic:
925 cstat[i] = CPX_BASIC;
926 break;
927 case CoinWarmStartBasis::atLowerBound:
928 cstat[i] = CPX_AT_LOWER;
929 break;
930 case CoinWarmStartBasis::atUpperBound:
931 cstat[i] = CPX_AT_UPPER;
932 break;
933 case CoinWarmStartBasis::isFree:
934 cstat[i] = CPX_FREE_SUPER;
935 break;
936 default: // unknown row status
937 retval = false;
938 goto TERMINATE;
939 }
940 }
941
942 // *FIXME* : can this be getMutableLpPtr() ? Does any cached data change by
943 // *FIXME* : setting warmstart? Or at least wouldn't it be sufficient to
944 // *FIXME* : clear the cached results but not the problem data?
945 // -> is fixed by using FREECACHED_RESULTS; only cached solution will be discarded
946 restat = CPXcopybase(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_RESULTS), cstat, rstat);
947 retval = (restat == 0);
948 TERMINATE:
949 delete[] cstat;
950 delete[] rstat;
951 return retval;
952 }
953
954 //#############################################################################
955 // Hotstart related methods (primarily used in strong branching)
956 //#############################################################################
957
958 void OsiCpxSolverInterface::markHotStart()
959 {
960 debugMessage("OsiCpxSolverInterface::markHotStart()\n");
961
962 int err;
963 int numcols, numrows;
964
965 assert(!probtypemip_);
966
967 numcols = getNumCols();
968 numrows = getNumRows();
969 if (numcols > hotStartCStatSize_) {
970 delete[] hotStartCStat_;
971 hotStartCStatSize_ = static_cast< int >(1.2 * static_cast< double >(numcols)); // get some extra space for future hot starts
972 hotStartCStat_ = new int[hotStartCStatSize_];
973 }
974 if (numrows > hotStartRStatSize_) {
975 delete[] hotStartRStat_;
976 hotStartRStatSize_ = static_cast< int >(1.2 * static_cast< double >(numrows)); // get some extra space for future hot starts
977 hotStartRStat_ = new int[hotStartRStatSize_];
978 }
979 err = CPXgetbase(env_, getMutableLpPtr(), hotStartCStat_, hotStartRStat_);
980 checkCPXerror(err, "CPXgetbase", "markHotStart");
981 }
982
983 void OsiCpxSolverInterface::solveFromHotStart()
984 {
985 debugMessage("OsiCpxSolverInterface::solveFromHotStart()\n");
986
987 int err;
988 int maxiter;
989
990 switchToLP();
991
992 assert(getNumCols() <= hotStartCStatSize_);
993 assert(getNumRows() <= hotStartRStatSize_);
994 err = CPXcopybase(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_RESULTS), hotStartCStat_, hotStartRStat_);
995 checkCPXerror(err, "CPXcopybase", "solveFromHotStart");
996
997 err = CPXgetintparam(env_, CPX_PARAM_ITLIM, &maxiter);
998 checkCPXerror(err, "CPXgetintparam", "solveFromHotStart");
999 err = CPXsetintparam(env_, CPX_PARAM_ITLIM, hotStartMaxIteration_);
1000 checkCPXerror(err, "CPXsetintparam", "solveFromHotStart");
1001
1002 resolve();
1003
1004 err = CPXsetintparam(env_, CPX_PARAM_ITLIM, maxiter);
1005 checkCPXerror(err, "CPXsetintparam", "solveFromHotStart");
1006 }
1007
1008 void OsiCpxSolverInterface::unmarkHotStart()
1009 {
1010 debugMessage("OsiCpxSolverInterface::unmarkHotStart()\n");
1011
1012 // ??? be lazy with deallocating memory and do nothing here, deallocate memory in the destructor
1013 }
1014
1015 //#############################################################################
1016 // Problem information methods (original data)
1017 //#############################################################################
1018
1019 //------------------------------------------------------------------
1020 // Get number of rows, columns, elements, ...
1021 //------------------------------------------------------------------
1022 int OsiCpxSolverInterface::getNumCols() const
1023 {
1024 debugMessage("OsiCpxSolverInterface::getNumCols()\n");
1025
1026 return CPXgetnumcols(env_, getMutableLpPtr());
1027 }
1028 int OsiCpxSolverInterface::getNumRows() const
1029 {
1030 debugMessage("OsiCpxSolverInterface::getNumRows()\n");
1031
1032 return CPXgetnumrows(env_, getMutableLpPtr());
1033 }
1034 int OsiCpxSolverInterface::getNumElements() const
1035 {
1036 debugMessage("OsiCpxSolverInterface::getNumElements()\n");
1037
1038 return CPXgetnumnz(env_, getMutableLpPtr());
1039 }
1040
1041 //------------------------------------------------------------------
1042 // Get pointer to rim vectors
1043 //------------------------------------------------------------------
1044
1045 const double *OsiCpxSolverInterface::getColLower() const
1046 {
1047 debugMessage("OsiCpxSolverInterface::getColLower()\n");
1048
1049 if (collower_ == NULL) {
1050 int ncols = CPXgetnumcols(env_, getMutableLpPtr());
1051 if (ncols > 0) {
1052 collower_ = new double[ncols];
1053 CPXgetlb(env_, getMutableLpPtr(), collower_, 0, ncols - 1);
1054 }
1055 }
1056 return collower_;
1057 }
1058 //------------------------------------------------------------------
1059 const double *OsiCpxSolverInterface::getColUpper() const
1060 {
1061 debugMessage("OsiCpxSolverInterface::getColUpper()\n");
1062
1063 if (colupper_ == NULL) {
1064 int ncols = CPXgetnumcols(env_, getMutableLpPtr());
1065 if (ncols > 0) {
1066 colupper_ = new double[ncols];
1067 CPXgetub(env_, getMutableLpPtr(), colupper_, 0, ncols - 1);
1068 }
1069 }
1070 return colupper_;
1071 }
1072 //------------------------------------------------------------------
1073 const char *OsiCpxSolverInterface::getRowSense() const
1074 {
1075 debugMessage("OsiCpxSolverInterface::getRowSense()\n");
1076
1077 if (rowsense_ == NULL) {
1078 // rowsense is determined with rhs, so invoke rhs
1079 getRightHandSide();
1080 assert(rowsense_ != NULL || getNumRows() == 0);
1081 }
1082 return rowsense_;
1083 }
1084 //------------------------------------------------------------------
1085 const double *OsiCpxSolverInterface::getRightHandSide() const
1086 {
1087 debugMessage("OsiCpxSolverInterface::getRightHandSide()\n");
1088
1089 if (rhs_ == NULL) {
1090 CPXLPptr lp = getMutableLpPtr();
1091 int nrows = getNumRows();
1092 if (nrows > 0) {
1093 rhs_ = new double[nrows];
1094 CPXgetrhs(env_, lp, rhs_, 0, nrows - 1);
1095
1096 assert(rowrange_ == NULL);
1097 rowrange_ = new double[nrows];
1098 CPXgetrngval(env_, lp, rowrange_, 0, nrows - 1);
1099
1100 assert(rowsense_ == NULL);
1101 rowsense_ = new char[nrows];
1102 CPXgetsense(env_, lp, rowsense_, 0, nrows - 1);
1103
1104 double inf = getInfinity();
1105 int i;
1106 for (i = 0; i < nrows; ++i) {
1107 if (rowsense_[i] != 'R')
1108 rowrange_[i] = 0.0;
1109 else {
1110 if (rhs_[i] <= -inf) {
1111 rowsense_[i] = 'N';
1112 rowrange_[i] = 0.0;
1113 rhs_[i] = 0.0;
1114 } else {
1115 if (rowrange_[i] >= 0.0)
1116 rhs_[i] = rhs_[i] + rowrange_[i];
1117 else
1118 rowrange_[i] = -rowrange_[i];
1119 }
1120 }
1121 }
1122 }
1123 }
1124 return rhs_;
1125 }
1126 //------------------------------------------------------------------
1127 const double *OsiCpxSolverInterface::getRowRange() const
1128 {
1129 debugMessage("OsiCpxSolverInterface::getRowRange()\n");
1130
1131 if (rowrange_ == NULL) {
1132 // rowrange is determined with rhs, so invoke rhs
1133 getRightHandSide();
1134 assert(rowrange_ != NULL || getNumRows() == 0);
1135 }
1136 return rowrange_;
1137 }
1138 //------------------------------------------------------------------
1139 const double *OsiCpxSolverInterface::getRowLower() const
1140 {
1141 debugMessage("OsiCpxSolverInterface::getRowLower()\n");
1142
1143 if (rowlower_ == NULL) {
1144 int nrows = getNumRows();
1145 const char *rowsense = getRowSense();
1146 const double *rhs = getRightHandSide();
1147 const double *rowrange = getRowRange();
1148
1149 if (nrows > 0) {
1150 rowlower_ = new double[nrows];
1151
1152 double dum1;
1153 for (int i = 0; i < nrows; i++)
1154 convertSenseToBound(rowsense[i], rhs[i], rowrange[i],
1155 rowlower_[i], dum1);
1156 }
1157 }
1158 return rowlower_;
1159 }
1160 //------------------------------------------------------------------
1161 const double *OsiCpxSolverInterface::getRowUpper() const
1162 {
1163 debugMessage("OsiCpxSolverInterface::getRowUpper()\n");
1164
1165 if (rowupper_ == NULL) {
1166 int nrows = getNumRows();
1167 const char *rowsense = getRowSense();
1168 const double *rhs = getRightHandSide();
1169 const double *rowrange = getRowRange();
1170
1171 if (nrows > 0) {
1172 rowupper_ = new double[nrows];
1173
1174 double dum1;
1175 for (int i = 0; i < nrows; i++)
1176 convertSenseToBound(rowsense[i], rhs[i], rowrange[i],
1177 dum1, rowupper_[i]);
1178 }
1179 }
1180
1181 return rowupper_;
1182 }
1183 //------------------------------------------------------------------
1184 const double *OsiCpxSolverInterface::getObjCoefficients() const
1185 {
1186 debugMessage("OsiCpxSolverInterface::getObjCoefficients()\n");
1187
1188 if (obj_ == NULL) {
1189 int ncols = CPXgetnumcols(env_, getMutableLpPtr());
1190 if (ncols > 0) {
1191 obj_ = new double[ncols];
1192 int err = CPXgetobj(env_, getMutableLpPtr(), obj_, 0, ncols - 1);
1193 checkCPXerror(err, "CPXgetobj", "getObjCoefficients");
1194 }
1195 }
1196 return obj_;
1197 }
1198 //------------------------------------------------------------------
1199 double OsiCpxSolverInterface::getObjSense() const
1200 {
1201 debugMessage("OsiCpxSolverInterface::getObjSense()\n");
1202
1203 if (CPXgetobjsen(env_, getMutableLpPtr()) == CPX_MIN)
1204 return +1.0;
1205 else
1206 return -1.0;
1207 }
1208
1209 //------------------------------------------------------------------
1210 // Return information on integrality
1211 //------------------------------------------------------------------
1212
1213 bool OsiCpxSolverInterface::isContinuous(int colNumber) const
1214 {
1215 debugMessage("OsiCpxSolverInterface::isContinuous(%d)\n", colNumber);
1216
1217 return getCtype()[colNumber] == 'C';
1218 }
1219
1220 //------------------------------------------------------------------
1221 // Row and column copies of the matrix ...
1222 //------------------------------------------------------------------
1223
1224 const CoinPackedMatrix *OsiCpxSolverInterface::getMatrixByRow() const
1225 {
1226 debugMessage("OsiCpxSolverInterface::getMatrixByRow()\n");
1227
1228 if (matrixByRow_ == NULL) {
1229 int nrows = getNumRows();
1230 int ncols = getNumCols();
1231 int nelems;
1232 int *starts = new int[nrows + 1];
1233 int *len = new int[nrows];
1234
1235 int requiredSpace;
1236 CPXgetrows(env_, getMutableLpPtr(),
1237 &nelems, starts, NULL, NULL, 0, &requiredSpace,
1238 0, nrows - 1);
1239
1240 assert(-requiredSpace == getNumElements());
1241 int *indices = new int[-requiredSpace];
1242 double *elements = new double[-requiredSpace];
1243
1244 CPXgetrows(env_, getMutableLpPtr(),
1245 &nelems, starts, indices, elements, -requiredSpace,
1246 &requiredSpace, 0, nrows - 1);
1247 assert(requiredSpace == 0);
1248
1249 matrixByRow_ = new CoinPackedMatrix();
1250
1251 // Should be able to pass null for length of packed matrix,
1252 // assignMatrix does not seem to allow (even though documentation
1253 // say it is possible to do this).
1254 // For now compute the length.
1255 starts[nrows] = nelems;
1256 for (int i = 0; i < nrows; ++i)
1257 len[i] = starts[i + 1] - starts[i];
1258
1259 matrixByRow_->assignMatrix(false /* not column ordered */,
1260 ncols, nrows, nelems,
1261 elements, indices, starts, len /*NULL*/);
1262 }
1263 return matrixByRow_;
1264 }
1265
1266 //------------------------------------------------------------------
1267
1268 const CoinPackedMatrix *OsiCpxSolverInterface::getMatrixByCol() const
1269 {
1270 debugMessage("OsiCpxSolverInterface::getMatrixByCol()\n");
1271
1272 if (matrixByCol_ == NULL) {
1273 int nrows = getNumRows();
1274 int ncols = getNumCols();
1275 int nelems;
1276 int *starts = new int[ncols + 1];
1277 int *len = new int[ncols];
1278
1279 int requiredSpace;
1280 CPXgetcols(env_, getMutableLpPtr(),
1281 &nelems, starts, NULL, NULL, 0, &requiredSpace,
1282 0, ncols - 1);
1283 assert(-requiredSpace == getNumElements());
1284
1285 int *indices = new int[-requiredSpace];
1286 double *elements = new double[-requiredSpace];
1287
1288 CPXgetcols(env_, getMutableLpPtr(),
1289 &nelems, starts, indices, elements, -requiredSpace,
1290 &requiredSpace, 0, ncols - 1);
1291 assert(requiredSpace == 0);
1292
1293 matrixByCol_ = new CoinPackedMatrix();
1294
1295 // Should be able to pass null for length of packed matrix,
1296 // assignMatrix does not seem to allow (even though documentation
1297 // say it is possible to do this).
1298 // For now compute the length.
1299 starts[ncols] = nelems;
1300 for (int i = 0; i < ncols; i++)
1301 len[i] = starts[i + 1] - starts[i];
1302
1303 matrixByCol_->assignMatrix(true /* column ordered */,
1304 nrows, ncols, nelems,
1305 elements, indices, starts, len /*NULL*/);
1306 assert(matrixByCol_->getNumCols() == ncols);
1307 assert(matrixByCol_->getNumRows() == nrows);
1308 }
1309 return matrixByCol_;
1310 }
1311
1312 //------------------------------------------------------------------
1313 // Get solver's value for infinity
1314 //------------------------------------------------------------------
1315 double OsiCpxSolverInterface::getInfinity() const
1316 {
1317 debugMessage("OsiCpxSolverInterface::getInfinity()\n");
1318
1319 return CPX_INFBOUND;
1320 }
1321
1322 //#############################################################################
1323 // Problem information methods (results)
1324 //#############################################################################
1325
1326 // *FIXME*: what should be done if a certain vector doesn't exist???
1327
1328 const double *OsiCpxSolverInterface::getColSolution() const
1329 {
1330 debugMessage("OsiCpxSolverInterface::getColSolution()\n");
1331
1332 if (colsol_ == NULL) {
1333 CPXLPptr lp = getMutableLpPtr();
1334 int ncols = CPXgetnumcols(env_, lp);
1335 if (ncols > 0) {
1336 colsol_ = new double[ncols];
1337
1338 if (probtypemip_) {
1339 #if CPX_VERSION >= 1100
1340 int solntype;
1341 CPXsolninfo(env_, lp, NULL, &solntype, NULL, NULL);
1342 if (solntype != CPX_NO_SOLN) {
1343 int err = CPXgetmipx(env_, lp, colsol_, 0, ncols - 1);
1344 checkCPXerror(err, "CPXgetmipx", "getColSolution");
1345 } else {
1346 CoinFillN(colsol_, ncols, 0.0);
1347 }
1348 #else
1349 int err = CPXgetmipx(env_, lp, colsol_, 0, ncols - 1);
1350 if (err == CPXERR_NO_INT_SOLN)
1351 CoinFillN(colsol_, ncols, 0.0);
1352 else
1353 checkCPXerror(err, "CPXgetmipx", "getColSolution");
1354 #endif
1355 } else {
1356 int solntype;
1357 CPXsolninfo(env_, lp, NULL, &solntype, NULL, NULL);
1358 if (solntype != CPX_NO_SOLN) {
1359 int err = CPXgetx(env_, lp, colsol_, 0, ncols - 1);
1360 checkCPXerror(err, "CPXgetx", "getColSolution");
1361 } else {
1362 CoinFillN(colsol_, ncols, 0.0);
1363 }
1364 }
1365 }
1366 }
1367
1368 return colsol_;
1369 }
1370 //------------------------------------------------------------------
1371 const double *OsiCpxSolverInterface::getRowPrice() const
1372 {
1373 debugMessage("OsiCpxSolverInterface::getRowPrice()\n");
1374
1375 if (rowsol_ == NULL) {
1376 int nrows = getNumRows();
1377 if (nrows > 0) {
1378 rowsol_ = new double[nrows];
1379 int solntype;
1380
1381 /* check if a solution exists, if Cplex >= 11.0 */
1382 #if CPX_VERSION >= 1100
1383 CPXsolninfo(env_, getMutableLpPtr(), NULL, &solntype, NULL, NULL);
1384 #else
1385 solntype = CPX_BASIC_SOLN;
1386 #endif
1387
1388 if (solntype != CPX_NO_SOLN) {
1389 int err = CPXgetpi(env_, getMutableLpPtr(), rowsol_, 0, nrows - 1);
1390 checkCPXerror(err, "CPXgetpi", "getRowPrice");
1391 } else {
1392 CoinFillN(rowsol_, nrows, 0.0);
1393 }
1394 }
1395 }
1396 return rowsol_;
1397 }
1398 //------------------------------------------------------------------
1399 const double *OsiCpxSolverInterface::getReducedCost() const
1400 {
1401 debugMessage("OsiCpxSolverInterface::getReducedCost()\n");
1402
1403 if (redcost_ == NULL) {
1404 int ncols = CPXgetnumcols(env_, getMutableLpPtr());
1405 if (ncols > 0) {
1406 redcost_ = new double[ncols];
1407 int solntype;
1408
1409 /* check if a solution exists, if Cplex >= 11.0 */
1410 #if CPX_VERSION >= 1100
1411 CPXsolninfo(env_, getMutableLpPtr(), NULL, &solntype, NULL, NULL);
1412 #else
1413 solntype = CPX_BASIC_SOLN;
1414 #endif
1415
1416 if (solntype != CPX_NO_SOLN) {
1417 int err = CPXgetdj(env_, getMutableLpPtr(), redcost_, 0, ncols - 1);
1418 checkCPXerror(err, "CPXgetdj", "getReducedCost");
1419 } else {
1420 CoinFillN(redcost_, ncols, 0.0);
1421 }
1422 }
1423 }
1424 return redcost_;
1425 }
1426 //------------------------------------------------------------------
1427 const double *OsiCpxSolverInterface::getRowActivity() const
1428 {
1429 debugMessage("OsiCpxSolverInterface::getRowActivity()\n");
1430
1431 if (rowact_ == NULL) {
1432 int nrows = getNumRows();
1433 if (nrows > 0) {
1434 rowact_ = new double[nrows];
1435 int solntype;
1436
1437 if (probtypemip_) {
1438 /* check if a solution exists, if Cplex >= 11.0 */
1439 #if CPX_VERSION >= 1100
1440 CPXsolninfo(env_, getMutableLpPtr(), NULL, &solntype, NULL, NULL);
1441 if (solntype != CPX_NO_SOLN) {
1442 double *rowslack = new double[nrows];
1443 int err = CPXgetmipslack(env_, getMutableLpPtr(), rowslack, 0, nrows - 1);
1444 checkCPXerror(err, "CPXgetmipslack", "getRowActivity");
1445 for (int r = 0; r < nrows; ++r)
1446 rowact_[r] = getRightHandSide()[r] - rowslack[r];
1447 delete[] rowslack;
1448 } else {
1449 CoinFillN(rowact_, nrows, 0.0);
1450 }
1451 #else
1452 double *rowslack = new double[nrows];
1453 int err = CPXgetmipslack(env_, getMutableLpPtr(), rowslack, 0, nrows - 1);
1454 if (err == CPXERR_NO_SOLN || err == CPXERR_NO_INT_SOLN) {
1455 CoinFillN(rowact_, nrows, 0.0);
1456 } else {
1457 checkCPXerror(err, "CPXgetmipslack", "getRowActivity");
1458 for (int r = 0; r < nrows; ++r)
1459 rowact_[r] = getRightHandSide()[r] - rowslack[r];
1460 }
1461 delete[] rowslack;
1462 #endif
1463 } else {
1464 CPXsolninfo(env_, getMutableLpPtr(), NULL, &solntype, NULL, NULL);
1465 if (solntype != CPX_NO_SOLN) {
1466 int err = CPXgetax(env_, getMutableLpPtr(), rowact_, 0, nrows - 1);
1467 checkCPXerror(err, "CPXgetax", "getRowActivity");
1468 } else {
1469 CoinFillN(rowact_, nrows, 0.0);
1470 }
1471 }
1472 }
1473 }
1474
1475 return rowact_;
1476 }
1477 //------------------------------------------------------------------
1478 double OsiCpxSolverInterface::getObjValue() const
1479 {
1480 debugMessage("OsiCpxSolverInterface::getObjValue()\n");
1481
1482 double objval = 0.0;
1483 int err;
1484 int solntype;
1485
1486 CPXLPptr lp = getMutableLpPtr();
1487
1488 if (probtypemip_) {
1489 #if CPX_VERSION >= 1100
1490 CPXsolninfo(env_, lp, NULL, &solntype, NULL, NULL);
1491 if (solntype != CPX_NO_SOLN) {
1492 err = CPXgetmipobjval(env_, lp, &objval);
1493 checkCPXerror(err, "CPXgetmipobjval", "getObjValue");
1494 } else {
1495 // => return 0.0 as objective value (?? is this the correct behaviour ??)
1496 objval = 0.0;
1497 }
1498 #else
1499 err = CPXgetmipobjval(env_, lp, &objval);
1500 if (err == CPXERR_NO_INT_SOLN)
1501 // => return 0.0 as objective value (?? is this the correct behaviour ??)
1502 objval = 0.0;
1503 else
1504 checkCPXerror(err, "CPXgetmipobjval", "getObjValue");
1505 #endif
1506 } else {
1507 CPXsolninfo(env_, lp, NULL, &solntype, NULL, NULL);
1508 if (solntype != CPX_NO_SOLN) {
1509 err = CPXgetobjval(env_, lp, &objval);
1510 checkCPXerror(err, "CPXgetobjval", "getObjValue");
1511 } else {
1512 // => return 0.0 as objective value (?? is this the correct behaviour ??)
1513 objval = 0.0;
1514 }
1515 }
1516
1517 // Adjust objective function value by constant term in objective function
1518 double objOffset;
1519 getDblParam(OsiObjOffset, objOffset);
1520 objval -= objOffset;
1521
1522 return objval;
1523 }
1524 //------------------------------------------------------------------
1525 int OsiCpxSolverInterface::getIterationCount() const
1526 {
1527 debugMessage("OsiCpxSolverInterface::getIterationCount()\n");
1528
1529 // CPXgetitcnt prints an error if no solution exists, so check before, if Cplex >= 11.0
1530 #if CPX_VERSION >= 1100
1531 int solntype;
1532
1533 CPXsolninfo(env_, getMutableLpPtr(), NULL, &solntype, NULL, NULL);
1534
1535 if (solntype == CPX_NO_SOLN)
1536 return 0;
1537 #endif
1538
1539 if (probtypemip_)
1540 return CPXgetmipitcnt(env_, getMutableLpPtr());
1541 else
1542 return CPXgetitcnt(env_, getMutableLpPtr());
1543 }
1544 //------------------------------------------------------------------
1545 std::vector< double * > OsiCpxSolverInterface::getDualRays(int maxNumRays,
1546 bool fullRay) const
1547 {
1548 debugMessage("OsiCpxSolverInterface::getDualRays(%d,%s)\n", maxNumRays,
1549 fullRay ? "true" : "false");
1550
1551 if (fullRay == true) {
1552 throw CoinError("Full dual rays not yet implemented.", "getDualRays",
1553 "OsiCpxSolverInterface");
1554 }
1555
1556 OsiCpxSolverInterface solver(*this);
1557
1558 const int numcols = getNumCols();
1559 const int numrows = getNumRows();
1560 int *index = new int[CoinMax(numcols, numrows)];
1561 int i;
1562 for (i = CoinMax(numcols, numrows) - 1; i >= 0; --i) {
1563 index[i] = i;
1564 }
1565 double *obj = new double[CoinMax(numcols, 2 * numrows)];
1566 CoinFillN(obj, numcols, 0.0);
1567 solver.setObjCoeffSet(index, index + numcols, obj);
1568
1569 double *clb = new double[2 * numrows];
1570 double *cub = new double[2 * numrows];
1571
1572 const double plusone = 1.0;
1573 const double minusone = -1.0;
1574 const char *sense = getRowSense();
1575
1576 const CoinPackedVectorBase **cols = new const CoinPackedVectorBase *[2 * numrows];
1577 int newcols = 0;
1578 for (i = 0; i < numrows; ++i) {
1579 switch (sense[i]) {
1580 case 'L':
1581 cols[newcols++] = new CoinShallowPackedVector(1, &index[i], &minusone, false);
1582 break;
1583 case 'G':
1584 cols[newcols++] = new CoinShallowPackedVector(1, &index[i], &plusone, false);
1585 break;
1586 case 'R':
1587 cols[newcols++] = new CoinShallowPackedVector(1, &index[i], &minusone, false);
1588 cols[newcols++] = new CoinShallowPackedVector(1, &index[i], &plusone, false);
1589 break;
1590 case 'N':
1591 break;
1592 }
1593 }
1594
1595 CoinFillN(obj, newcols, 1.0);
1596 CoinFillN(clb, newcols, 0.0);
1597 CoinFillN(cub, newcols, getInfinity());
1598
1599 solver.addCols(newcols, cols, clb, cub, obj);
1600 delete[] index;
1601 delete[] cols;
1602 delete[] clb;
1603 delete[] cub;
1604 delete[] obj;
1605
1606 solver.setObjSense(1.0); // minimize
1607 solver.initialSolve();
1608
1609 const double *solverpi = solver.getRowPrice();
1610 double *pi = new double[numrows];
1611 for (i = numrows - 1; i >= 0; --i) {
1612 pi[i] = -solverpi[i];
1613 }
1614 return std::vector< double * >(1, pi);
1615 }
1616
1617 //------------------------------------------------------------------
1618 std::vector< double * > OsiCpxSolverInterface::getPrimalRays(int maxNumRays) const
1619 {
1620 debugMessage("OsiCpxSolverInterface::getPrimalRays(%d)\n", maxNumRays);
1621
1622 double *ray = new double[getNumCols()];
1623 int err = CPXgetray(env_, getMutableLpPtr(), ray);
1624 checkCPXerror(err, "CPXgetray", "getPrimalRays");
1625 return std::vector< double * >(1, ray);
1626 }
1627
1628 //#############################################################################
1629 // Problem modifying methods (rim vectors)
1630 //#############################################################################
1631
1632 void OsiCpxSolverInterface::setObjCoeff(int elementIndex, double elementValue)
1633 {
1634 debugMessage("OsiCpxSolverInterface::setObjCoeff(%d, %g)\n", elementIndex, elementValue);
1635
1636 // int err = CPXchgobj(env_, getLpPtr( OsiCpxSolverInterface::FREECACHED_COLUMN ), 1, &elementIndex, &elementValue);
1637 int err = CPXchgobj(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_PROBLEM), 1, &elementIndex, &elementValue);
1638 checkCPXerror(err, "CPXchgobj", "setObjCoeff");
1639 if (obj_ != NULL) {
1640 obj_[elementIndex] = elementValue;
1641 }
1642 }
1643 //-----------------------------------------------------------------------------
1644 void OsiCpxSolverInterface::setObjCoeffSet(const int *indexFirst,
1645 const int *indexLast,
1646 const double *coeffList)
1647 {
1648 debugMessage("OsiCpxSolverInterface::setObjCoeffSet(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)coeffList);
1649
1650 const long int cnt = indexLast - indexFirst;
1651 // int err = CPXchgobj(env_,
1652 // getLpPtr(OsiCpxSolverInterface::FREECACHED_COLUMN), cnt,
1653 // const_cast<int*>(indexFirst),
1654 // const_cast<double*>(coeffList));
1655 int err = CPXchgobj(env_,
1656 getLpPtr(OsiCpxSolverInterface::KEEPCACHED_PROBLEM),
1657 static_cast< int >(cnt),
1658 const_cast< int * >(indexFirst),
1659 const_cast< double * >(coeffList));
1660 checkCPXerror(err, "CPXchgobj", "setObjCoeffSet");
1661 if (obj_ != NULL) {
1662 for (int i = 0; i < cnt; ++i) {
1663 obj_[indexFirst[i]] = coeffList[i];
1664 }
1665 }
1666 }
1667 //-----------------------------------------------------------------------------
1668 void OsiCpxSolverInterface::setColLower(int elementIndex, double elementValue)
1669 {
1670 debugMessage("OsiCpxSolverInterface::setColLower(%d, %g)\n", elementIndex, elementValue);
1671
1672 char c = 'L';
1673 // int err = CPXchgbds( env_, getLpPtr( OsiCpxSolverInterface::FREECACHED_COLUMN ), 1, &elementIndex, &c, &elementValue );
1674 int err = CPXchgbds(env_,
1675 getLpPtr(OsiCpxSolverInterface::KEEPCACHED_PROBLEM),
1676 1, &elementIndex, &c, &elementValue);
1677 checkCPXerror(err, "CPXchgbds", "setColLower");
1678 if (collower_ != NULL) {
1679 collower_[elementIndex] = elementValue;
1680 }
1681 }
1682 //-----------------------------------------------------------------------------
1683 void OsiCpxSolverInterface::setColUpper(int elementIndex, double elementValue)
1684 {
1685 debugMessage("OsiCpxSolverInterface::setColUpper(%d, %g)\n", elementIndex, elementValue);
1686
1687 char c = 'U';
1688 // int err = CPXchgbds( env_, getLpPtr( OsiCpxSolverInterface::FREECACHED_COLUMN ), 1, &elementIndex, &c, &elementValue );
1689 int err = CPXchgbds(env_,
1690 getLpPtr(OsiCpxSolverInterface::KEEPCACHED_PROBLEM),
1691 1, &elementIndex, &c, &elementValue);
1692 checkCPXerror(err, "CPXchgbds", "setColUpper");
1693 if (colupper_ != NULL) {
1694 colupper_[elementIndex] = elementValue;
1695 }
1696 }
1697 //-----------------------------------------------------------------------------
1698 void OsiCpxSolverInterface::setColBounds(int elementIndex, double lower, double upper)
1699 {
1700 debugMessage("OsiCpxSolverInterface::setColBounds(%d, %g, %g)\n", elementIndex, lower, upper);
1701
1702 char c[2] = { 'L', 'U' };
1703 int ind[2];
1704 double bd[2];
1705 int err;
1706
1707 ind[0] = elementIndex;
1708 ind[1] = elementIndex;
1709 bd[0] = lower;
1710 bd[1] = upper;
1711 // err = CPXchgbds( env_, getLpPtr( OsiCpxSolverInterface::FREECACHED_COLUMN ), 2, ind, c, bd );
1712 err = CPXchgbds(env_,
1713 getLpPtr(OsiCpxSolverInterface::KEEPCACHED_PROBLEM),
1714 2, ind, c, bd);
1715 checkCPXerror(err, "CPXchgbds", "setColBounds");
1716 if (collower_ != NULL) {
1717 collower_[elementIndex] = lower;
1718 }
1719 if (colupper_ != NULL) {
1720 colupper_[elementIndex] = upper;
1721 }
1722 }
1723 //-----------------------------------------------------------------------------
1724 void OsiCpxSolverInterface::setColSetBounds(const int *indexFirst,
1725 const int *indexLast,
1726 const double *boundList)
1727 {
1728 debugMessage("OsiCpxSolverInterface::setColSetBounds(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)boundList);
1729
1730 const long int cnt = indexLast - indexFirst;
1731 if (cnt <= 0)
1732 return;
1733
1734 char *c = new char[2 * cnt];
1735 int *ind = new int[2 * cnt];
1736 for (int i = 0; i < cnt; ++i) {
1737 register const int j = 2 * i;
1738 c[j] = 'L';
1739 c[j + 1] = 'U';
1740 const int colind = indexFirst[i];
1741 ind[j] = colind;
1742 ind[j + 1] = colind;
1743
1744 if (collower_ != NULL) {
1745 collower_[colind] = boundList[2 * i];
1746 }
1747 if (colupper_ != NULL) {
1748 colupper_[colind] = boundList[2 * i + 1];
1749 }
1750 }
1751 // int err = CPXchgbds( env_,
1752 // getLpPtr(OsiCpxSolverInterface::FREECACHED_ROW ),
1753 // 2*cnt, ind, c,
1754 // const_cast<double*>(boundList) );
1755 int err = CPXchgbds(env_,
1756 getLpPtr(OsiCpxSolverInterface::KEEPCACHED_PROBLEM),
1757 2 * static_cast< int >(cnt), ind, c, const_cast< double * >(boundList));
1758 checkCPXerror(err, "CPXchgbds", "setColSetBounds");
1759 delete[] ind;
1760 delete[] c;
1761 // OsiSolverInterface::setColSetBounds( indexFirst, indexLast, boundList );
1762 }
1763 //-----------------------------------------------------------------------------
1764 void OsiCpxSolverInterface::setRowLower(int i, double elementValue)
1765 {
1766 debugMessage("OsiCpxSolverInterface::setRowLower(%d, %g)\n", i, elementValue);
1767
1768 double rhs = getRightHandSide()[i];
1769 double range = getRowRange()[i];
1770 char sense = getRowSense()[i];
1771 double lower = 0, upper = 0;
1772
1773 convertSenseToBound(sense, rhs, range, lower, upper);
1774 if (lower != elementValue) {
1775 convertBoundToSense(elementValue, upper, sense, rhs, range);
1776 setRowType(i, sense, rhs, range);
1777 }
1778 }
1779 //-----------------------------------------------------------------------------
1780 void OsiCpxSolverInterface::setRowUpper(int i, double elementValue)
1781 {
1782 debugMessage("OsiCpxSolverInterface::setRowUpper(%d, %g)\n", i, elementValue);
1783
1784 double rhs = getRightHandSide()[i];
1785 double range = getRowRange()[i];
1786 char sense = getRowSense()[i];
1787 double lower = 0, upper = 0;
1788
1789 convertSenseToBound(sense, rhs, range, lower, upper);
1790 if (upper != elementValue) {
1791 convertBoundToSense(lower, elementValue, sense, rhs, range);
1792 setRowType(i, sense, rhs, range);
1793 }
1794 }
1795 //-----------------------------------------------------------------------------
1796 void OsiCpxSolverInterface::setRowBounds(int elementIndex, double lower, double upper)
1797 {
1798 debugMessage("OsiCpxSolverInterface::setRowBounds(%d, %g, %g)\n", elementIndex, lower, upper);
1799
1800 double rhs, range;
1801 char sense;
1802
1803 convertBoundToSense(lower, upper, sense, rhs, range);
1804 setRowType(elementIndex, sense, rhs, range);
1805 }
1806 //-----------------------------------------------------------------------------
1807 void OsiCpxSolverInterface::setRowType(int i, char sense, double rightHandSide,
1808 double range)
1809 {
1810 debugMessage("OsiCpxSolverInterface::setRowType(%d, %c, %g, %g)\n", i, sense, rightHandSide, range);
1811
1812 int err;
1813
1814 // in CPLEX, ranged constraints are interpreted as rhs <= coeff*x <= rhs+range, which is different from Osi
1815 double cpxrhs = rightHandSide;
1816 if (sense == 'R') {
1817 assert(range >= 0.0);
1818 cpxrhs -= range;
1819 }
1820 if (sense == 'N') {
1821 sense = 'R';
1822 cpxrhs = -getInfinity();
1823 range = 2 * getInfinity();
1824 }
1825
1826 /**************
1827 err = CPXchgsense( env_, getLpPtr( OsiCpxSolverInterface::FREECACHED_ROW ),
1828 1, &i, &sense );
1829 checkCPXerror( err, "CPXchgsense", "setRowType" );
1830 err = CPXchgrhs( env_, getLpPtr( OsiCpxSolverInterface::FREECACHED_ROW ),
1831 1, &i, &rightHandSide );
1832 checkCPXerror( err, "CPXchgrhs", "setRowType" );
1833 err = CPXchgrngval( env_, getLpPtr( OsiCpxSolverInterface::FREECACHED_ROW ),
1834 1, &i, &range );
1835 checkCPXerror( err, "CPXchgrngval", "setRowType" );
1836 ***************/
1837
1838 err = CPXchgsense(env_,
1839 getLpPtr(OsiCpxSolverInterface::KEEPCACHED_PROBLEM),
1840 1, &i, &sense);
1841 checkCPXerror(err, "CPXchgsense", "setRowType");
1842 if (rowsense_ != NULL) {
1843 rowsense_[i] = sense;
1844 }
1845
1846 err = CPXchgrhs(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_PROBLEM),
1847 1, &i, &cpxrhs);
1848 checkCPXerror(err, "CPXchgrhs", "setRowType");
1849 if (rhs_ != NULL) {
1850 rhs_[i] = rightHandSide;
1851 }
1852 err = CPXchgrngval(env_,
1853 getLpPtr(OsiCpxSolverInterface::KEEPCACHED_PROBLEM),
1854 1, &i, &range);
1855 checkCPXerror(err, "CPXchgrngval", "setRowType");
1856 if (rowrange_ != NULL) {
1857 rowrange_[i] = range;
1858 }
1859
1860 if (rowlower_ != NULL || rowupper_ != NULL) {
1861 double dummy;
1862 convertSenseToBound(sense, rightHandSide, range,
1863 rowlower_ ? rowlower_[i] : dummy,
1864 rowupper_ ? rowupper_[i] : dummy);
1865 }
1866 }
1867 //-----------------------------------------------------------------------------
1868 void OsiCpxSolverInterface::setRowSetBounds(const int *indexFirst,
1869 const int *indexLast,
1870 const double *boundList)
1871 {
1872 debugMessage("OsiCpxSolverInterface::setRowSetBounds(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)boundList);
1873
1874 const long int cnt = indexLast - indexFirst;
1875 if (cnt <= 0)
1876 return;
1877
1878 char *sense = new char[cnt];
1879 double *rhs = new double[cnt];
1880 double *range = new double[cnt];
1881 for (int i = 0; i < cnt; ++i) {
1882 convertBoundToSense(boundList[2 * i], boundList[2 * i + 1],
1883 sense[i], rhs[i], range[i]);
1884 }
1885 setRowSetTypes(indexFirst, indexLast, sense, rhs, range);
1886 delete[] range;
1887 delete[] rhs;
1888 delete[] sense;
1889
1890 // OsiSolverInterface::setRowSetBounds( indexFirst, indexLast, boundList );
1891 }
1892 //-----------------------------------------------------------------------------
1893 void OsiCpxSolverInterface::setRowSetTypes(const int *indexFirst,
1894 const int *indexLast,
1895 const char *senseList,
1896 const double *rhsList,
1897 const double *rangeList)
1898 {
1899 debugMessage("OsiCpxSolverInterface::setRowSetTypes(%p, %p, %p, %p, %p)\n",
1900 (void *)indexFirst, (void *)indexLast, (void *)senseList, (void *)rhsList, (void *)rangeList);
1901
1902 const long int cnt = indexLast - indexFirst;
1903 if (cnt <= 0)
1904 return;
1905
1906 char *sense = new char[cnt];
1907 double *rhs = new double[cnt];
1908 double *range = new double[cnt];
1909 int *rangeind = new int[cnt];
1910 int rangecnt = 0;
1911 for (int i = 0; i < cnt; ++i) {
1912 sense[i] = senseList[i];
1913 rhs[i] = rhsList[i];
1914 if (sense[i] == 'R') {
1915 assert(rangeList[i] >= 0.0);
1916 rhs[i] -= rangeList[i];
1917 rangeind[rangecnt] = indexFirst[i];
1918 range[rangecnt] = rangeList[i];
1919 ++rangecnt;
1920 }
1921 if (sense[i] == 'N') {
1922 sense[i] = 'R';
1923 rhs[i] = -getInfinity();
1924 rangeind[rangecnt] = indexFirst[i];
1925 range[rangecnt] = 2 * getInfinity();
1926 ++rangecnt;
1927 }
1928 }
1929 int err;
1930 /******************
1931 err = CPXchgsense(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_ROW),
1932 cnt, const_cast<int*>(indexFirst), sense);
1933 checkCPXerror( err, "CPXchgsense", "setRowSetTypes" );
1934 err = CPXchgrhs(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_ROW),
1935 cnt, const_cast<int*>(indexFirst), rhs);
1936 checkCPXerror( err, "CPXchgrhs", "setRowSetTypes" );
1937 err = CPXchgrngval(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_ROW),
1938 rangecnt, rangeind, range);
1939 checkCPXerror( err, "CPXchgrngval", "setRowSetTypes" );
1940 ********************/
1941
1942 err = CPXchgsense(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ROW),
1943 static_cast< int >(cnt), const_cast< int * >(indexFirst), sense);
1944 checkCPXerror(err, "CPXchgsense", "setRowSetTypes");
1945 err = CPXchgrhs(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_ROW),
1946 static_cast< int >(cnt), const_cast< int * >(indexFirst), rhs);
1947 checkCPXerror(err, "CPXchgrhs", "setRowSetTypes");
1948 err = CPXchgrngval(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_ROW),
1949 rangecnt, rangeind, range);
1950 checkCPXerror(err, "CPXchgrngval", "setRowSetTypes");
1951
1952 int j;
1953 if (rowsense_ != NULL) {
1954 for (j = 0; j < cnt; j++) {
1955 rowsense_[indexFirst[j]] = sense[j];
1956 }
1957 }
1958 if (rhs_ != NULL) {
1959 for (j = 0; j < cnt; j++) {
1960 rhs_[indexFirst[j]] = rhs[j];
1961 }
1962 }
1963 if (rowrange_ != NULL) {
1964 for (j = 0; j < rangecnt; j++) {
1965 rowrange_[rangeind[j]] = range[j];
1966 }
1967 }
1968 delete[] rangeind;
1969 delete[] range;
1970 delete[] rhs;
1971 delete[] sense;
1972 // OsiSolverInterface::setRowSetTypes( indexFirst, indexLast, senseList,
1973 // rhsList, rangeList );
1974 }
1975 //#############################################################################
1976 void OsiCpxSolverInterface::setContinuous(int index)
1977 {
1978 debugMessage("OsiCpxSolverInterface::setContinuous(%d)\n", index);
1979
1980 assert(coltype_ != NULL);
1981 assert(coltypesize_ >= getNumCols());
1982
1983 coltype_[index] = 'C';
1984
1985 if (probtypemip_) {
1986 CPXLPptr lp = getMutableLpPtr();
1987 int err;
1988 err = CPXchgctype(env_, lp, 1, &index, &coltype_[index]);
1989 checkCPXerror(err, "CPXchgctype", "setContinuous");
1990 }
1991 }
1992 //-----------------------------------------------------------------------------
1993 void OsiCpxSolverInterface::setInteger(int index)
1994 {
1995 debugMessage("OsiCpxSolverInterface::setInteger(%d)\n", index);
1996
1997 assert(coltype_ != NULL);
1998 assert(coltypesize_ >= getNumCols());
1999
2000 if (getColLower()[index] == 0.0 && getColUpper()[index] == 1.0)
2001 coltype_[index] = 'B';
2002 else
2003 coltype_[index] = 'I';
2004
2005 if (probtypemip_) {
2006 CPXLPptr lp = getMutableLpPtr();
2007 int err;
2008 err = CPXchgctype(env_, lp, 1, &index, &coltype_[index]);
2009 checkCPXerror(err, "CPXchgctype", "setInteger");
2010 }
2011 }
2012 //-----------------------------------------------------------------------------
2013 void OsiCpxSolverInterface::setContinuous(const int *indices, int len)
2014 {
2015 debugMessage("OsiCpxSolverInterface::setContinuous(%p, %d)\n", (void *)indices, len);
2016
2017 for (int i = 0; i < len; ++i)
2018 setContinuous(indices[i]);
2019 }
2020 //-----------------------------------------------------------------------------
2021 void OsiCpxSolverInterface::setInteger(const int *indices, int len)
2022 {
2023 debugMessage("OsiCpxSolverInterface::setInteger(%p, %d)\n", (void *)indices, len);
2024
2025 for (int i = 0; i < len; ++i)
2026 setInteger(indices[i]);
2027 }
2028 //#############################################################################
2029
2030 void OsiCpxSolverInterface::setObjSense(double s)
2031 {
2032 debugMessage("OsiCpxSolverInterface::setObjSense(%g)\n", s);
2033
2034 if (s == +1.0)
2035 CPXchgobjsen(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_RESULTS), CPX_MIN);
2036 else
2037 CPXchgobjsen(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_RESULTS), CPX_MAX);
2038 }
2039
2040 //-----------------------------------------------------------------------------
2041
2042 void OsiCpxSolverInterface::setColSolution(const double *cs)
2043 {
2044 debugMessage("OsiCpxSolverInterface::setColSolution(%p)\n", (void *)cs);
2045
2046 int nc = getNumCols();
2047
2048 if (cs == NULL)
2049 freeCachedResults();
2050 else if (nc > 0) {
2051 // If colsol isn't allocated, then allocate it
2052 if (colsol_ == NULL)
2053 colsol_ = new double[nc];
2054
2055 // Copy in new col solution.
2056 CoinDisjointCopyN(cs, nc, colsol_);
2057
2058 // CPLEX < 7.0 doesn't support setting a col solution without a row solution
2059 // -> if a row solution exists or CPLEX version >= 7, then pass into CPLEX
2060 #if CPX_VERSION < 700
2061 if (rowsol_ != NULL)
2062 #endif
2063 {
2064 int err = CPXcopystart(env_, getMutableLpPtr(), NULL, NULL,
2065 const_cast< double * >(colsol_),
2066 const_cast< double * >(rowsol_),
2067 NULL, NULL);
2068 checkCPXerror(err, "CPXcopystart", "setColSolution");
2069 }
2070 }
2071 }
2072
2073 //-----------------------------------------------------------------------------
2074
2075 void OsiCpxSolverInterface::setRowPrice(const double *rs)
2076 {
2077 debugMessage("OsiCpxSolverInterface::setRowPrice(%p)\n", (void *)rs);
2078
2079 int nr = getNumRows();
2080
2081 if (rs == NULL)
2082 freeCachedResults();
2083 else if (nr > 0) {
2084 // If rowsol isn't allocated, then allocate it
2085 if (rowsol_ == NULL)
2086 rowsol_ = new double[nr];
2087
2088 // Copy in new row solution.
2089 CoinDisjointCopyN(rs, nr, rowsol_);
2090
2091 // if a col solution exists, then pass into CPLEX
2092 if (colsol_ != NULL) {
2093 int err = CPXcopystart(env_, getMutableLpPtr(), NULL, NULL,
2094 const_cast< double * >(colsol_),
2095 const_cast< double * >(rowsol_),
2096 NULL, NULL);
2097 checkCPXerror(err, "CPXcopystart", "setRowPrice");
2098 }
2099 }
2100 }
2101
2102 //#############################################################################
2103 // Problem modifying methods (matrix)
2104 //#############################################################################
2105 void OsiCpxSolverInterface::addCol(const CoinPackedVectorBase &vec,
2106 const double collb, const double colub,
2107 const double obj)
2108 {
2109 debugMessage("OsiCpxSolverInterface::addCol(%p, %g, %g, %g)\n", (void *)&vec, collb, colub, obj);
2110
2111 int nc = getNumCols();
2112 assert(coltypesize_ >= nc);
2113
2114 resizeColType(nc + 1);
2115 coltype_[nc] = 'C';
2116
2117 int err;
2118 int cmatbeg[2] = { 0, vec.getNumElements() };
2119
2120 err = CPXaddcols(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ROW),
2121 1, vec.getNumElements(), const_cast< double * >(&obj),
2122 cmatbeg,
2123 const_cast< int * >(vec.getIndices()),
2124 const_cast< double * >(vec.getElements()),
2125 const_cast< double * >(&collb),
2126 const_cast< double * >(&colub), NULL);
2127 checkCPXerror(err, "CPXaddcols", "addCol");
2128 }
2129 //-----------------------------------------------------------------------------
2130 void OsiCpxSolverInterface::addCols(const int numcols,
2131 const CoinPackedVectorBase *const *cols,
2132 const double *collb, const double *colub,
2133 const double *obj)
2134 {
2135 debugMessage("OsiCpxSolverInterface::addCols(%d, %p, %p, %p, %p)\n", numcols, (void *)cols, (void *)collb, (void *)colub, (void *)obj);
2136
2137 int nc = getNumCols();
2138 assert(coltypesize_ >= nc);
2139
2140 resizeColType(nc + numcols);
2141 CoinFillN(&coltype_[nc], numcols, 'C');
2142
2143 int i;
2144 int nz = 0;
2145 for (i = 0; i < numcols; ++i)
2146 nz += cols[i]->getNumElements();
2147
2148 int *index = new int[nz];
2149 double *elem = new double[nz];
2150 int *start = new int[numcols + 1];
2151
2152 nz = 0;
2153 start[0] = 0;
2154 for (i = 0; i < numcols; ++i) {
2155 const CoinPackedVectorBase *col = cols[i];
2156 const int len = col->getNumElements();
2157 CoinDisjointCopyN(col->getIndices(), len, index + nz);
2158 CoinDisjointCopyN(col->getElements(), len, elem + nz);
2159 nz += len;
2160 start[i + 1] = nz;
2161 }
2162 int err = CPXaddcols(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ROW),
2163 numcols, nz, const_cast< double * >(obj),
2164 start, index, elem,
2165 const_cast< double * >(collb),
2166 const_cast< double * >(colub), NULL);
2167 checkCPXerror(err, "CPXaddcols", "addCols");
2168
2169 delete[] start;
2170 delete[] elem;
2171 delete[] index;
2172
2173 // int i;
2174 // for( i = 0; i < numcols; ++i )
2175 // addCol( *(cols[i]), collb[i], colub[i], obj[i] );
2176 }
2177 //-----------------------------------------------------------------------------
2178 void OsiCpxSolverInterface::deleteCols(const int num, const int *columnIndices)
2179 {
2180 debugMessage("OsiCpxSolverInterface::deleteCols(%d, %p)\n", num, (void *)columnIndices);
2181
2182 int ncols = getNumCols();
2183 int *delstat = new int[ncols];
2184 int i, err;
2185
2186 CoinFillN(delstat, ncols, 0);
2187 for (i = 0; i < num; ++i)
2188 delstat[columnIndices[i]] = 1;
2189 err = CPXdelsetcols(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ROW), delstat);
2190 checkCPXerror(err, "CPXdelsetcols", "deleteCols");
2191
2192 for (i = 0; i < ncols; ++i) {
2193 assert(delstat[i] <= i);
2194 if (delstat[i] != -1)
2195 coltype_[delstat[i]] = coltype_[i];
2196 }
2197
2198 delete[] delstat;
2199
2200 //---
2201 //--- MVG: took from OsiClp for updating names
2202 //---
2203 int nameDiscipline;
2204 getIntParam(OsiNameDiscipline, nameDiscipline);
2205 if (num && nameDiscipline) {
2206 // Very clumsy (and inefficient) - need to sort and then go backwards in ? chunks
2207 int *indices = CoinCopyOfArray(columnIndices, num);
2208 std::sort(indices, indices + num);
2209 int num2 = num;
2210 while (num2) {
2211 int next = indices[num2 - 1];
2212 int firstDelete = num2 - 1;
2213 int i;
2214 for (i = num2 - 2; i >= 0; --i) {
2215 if (indices[i] + 1 == next) {
2216 --next;
2217 firstDelete = i;
2218 } else {
2219 break;
2220 }
2221 }
2222 OsiSolverInterface::deleteColNames(indices[firstDelete], num2 - firstDelete);
2223 num2 = firstDelete;
2224 assert(num2 >= 0);
2225 }
2226 delete[] indices;
2227 }
2228 }
2229
2230 //-----------------------------------------------------------------------------
2231 void OsiCpxSolverInterface::addRow(const CoinPackedVectorBase &vec,
2232 const double rowlb, const double rowub)
2233 {
2234 debugMessage("OsiCpxSolverInterface::addRow(%p, %g, %g)\n", (void *)&vec, rowlb, rowub);
2235
2236 char sense;
2237 double rhs, range;
2238
2239 convertBoundToSense(rowlb, rowub, sense, rhs, range);
2240 addRow(vec, sense, rhs, range);
2241 }
2242 //-----------------------------------------------------------------------------
2243 void OsiCpxSolverInterface::addRow(const CoinPackedVectorBase &vec,
2244 const char rowsen, const double rowrhs,
2245 const double rowrng)
2246 {
2247 debugMessage("OsiCpxSolverInterface::addRow(%p, %c, %g, %g)\n", (void *)&vec, rowsen, rowrhs, rowrng);
2248
2249 int err;
2250 int rmatbeg = 0;
2251 double rhs;
2252 double range;
2253 char sense = rowsen;
2254
2255 switch (rowsen) {
2256 case 'R':
2257 //assert(rowrng >= 0.0);
2258 rhs = rowrhs - rowrng;
2259 range = rowrng;
2260 break;
2261 case 'N':
2262 sense = 'R';
2263 rhs = -getInfinity();
2264 range = 2 * getInfinity();
2265 break;
2266 default:
2267 rhs = rowrhs;
2268 range = 0.0;
2269 }
2270
2271 err = CPXaddrows(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN), 0, 1, vec.getNumElements(),
2272 &rhs,
2273 &sense,
2274 &rmatbeg,
2275 const_cast< int * >(vec.getIndices()),
2276 const_cast< double * >(vec.getElements()),
2277 NULL, NULL);
2278 checkCPXerror(err, "CPXaddrows", "addRow");
2279 if (sense == 'R') {
2280 int row = getNumRows() - 1;
2281 err = CPXchgrngval(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_ROW), 1, &row, &range);
2282 checkCPXerror(err, "CPXchgrngval", "addRow");
2283 }
2284 }
2285 //-----------------------------------------------------------------------------
2286 void OsiCpxSolverInterface::addRows(const int numrows,
2287 const CoinPackedVectorBase *const *rows,
2288 const double *rowlb, const double *rowub)
2289 {
2290 debugMessage("OsiCpxSolverInterface::addRows(%d, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowlb, (void *)rowub);
2291
2292 int i;
2293
2294 for (i = 0; i < numrows; ++i)
2295 addRow(*(rows[i]), rowlb[i], rowub[i]);
2296 }
2297 //-----------------------------------------------------------------------------
2298 void OsiCpxSolverInterface::addRows(const int numrows,
2299 const CoinPackedVectorBase *const *rows,
2300 const char *rowsen, const double *rowrhs,
2301 const double *rowrng)
2302 {
2303 debugMessage("OsiCpxSolverInterface::addRows(%d, %p, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
2304
2305 int i;
2306
2307 for (i = 0; i < numrows; ++i)
2308 addRow(*(rows[i]), rowsen[i], rowrhs[i], rowrng[i]);
2309 }
2310 //-----------------------------------------------------------------------------
2311 void OsiCpxSolverInterface::deleteRows(const int num, const int *rowIndices)
2312 {
2313 debugMessage("OsiCpxSolverInterface::deleteRows(%d, %p)\n", num, (void *)rowIndices);
2314
2315 int nrows = getNumRows();
2316 int *delstat = new int[nrows];
2317 int i, err;
2318
2319 CoinFillN(delstat, nrows, 0);
2320 for (i = 0; i < num; ++i)
2321 delstat[rowIndices[i]] = 1;
2322 err = CPXdelsetrows(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN), delstat);
2323 checkCPXerror(err, "CPXdelsetrows", "deleteRows");
2324 delete[] delstat;
2325
2326 //---
2327 //--- SV: took from OsiClp for updating names
2328 //---
2329 int nameDiscipline;
2330 getIntParam(OsiNameDiscipline, nameDiscipline);
2331 if (num && nameDiscipline) {
2332 // Very clumsy (and inefficient) - need to sort and then go backwards in ? chunks
2333 int *indices = CoinCopyOfArray(rowIndices, num);
2334 std::sort(indices, indices + num);
2335 int num2 = num;
2336 while (num2) {
2337 int next = indices[num2 - 1];
2338 int firstDelete = num2 - 1;
2339 int i;
2340 for (i = num2 - 2; i >= 0; --i) {
2341 if (indices[i] + 1 == next) {
2342 --next;
2343 firstDelete = i;
2344 } else {
2345 break;
2346 }
2347 }
2348 OsiSolverInterface::deleteRowNames(indices[firstDelete], num2 - firstDelete);
2349 num2 = firstDelete;
2350 assert(num2 >= 0);
2351 }
2352 delete[] indices;
2353 }
2354 }
2355
2356 //#############################################################################
2357 // Methods to input a problem
2358 //#############################################################################
2359
2360 void OsiCpxSolverInterface::loadProblem(const CoinPackedMatrix &matrix,
2361 const double *collb, const double *colub,
2362 const double *obj,
2363 const double *rowlb, const double *rowub)
2364 {
2365 debugMessage("OsiCpxSolverInterface::loadProblem(1)(%p, %p, %p, %p, %p, %p)\n", (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowlb, (void *)rowub);
2366
2367 const double inf = getInfinity();
2368
2369 int nrows = matrix.getNumRows();
2370 char *rowSense = new char[nrows];
2371 double *rowRhs = new double[nrows];
2372 double *rowRange = new double[nrows];
2373
2374 int i;
2375 for (i = nrows - 1; i >= 0; --i) {
2376 const double lower = rowlb ? rowlb[i] : -inf;
2377 const double upper = rowub ? rowub[i] : inf;
2378 convertBoundToSense(lower, upper, rowSense[i], rowRhs[i], rowRange[i]);
2379 }
2380
2381 loadProblem(matrix, collb, colub, obj, rowSense, rowRhs, rowRange);
2382 delete[] rowSense;
2383 delete[] rowRhs;
2384 delete[] rowRange;
2385 }
2386
2387 //-----------------------------------------------------------------------------
2388
2389 void OsiCpxSolverInterface::assignProblem(CoinPackedMatrix *&matrix,
2390 double *&collb, double *&colub,
2391 double *&obj,
2392 double *&rowlb, double *&rowub)
2393 {
2394 debugMessage("OsiCpxSolverInterface::assignProblem()\n");
2395
2396 loadProblem(*matrix, collb, colub, obj, rowlb, rowub);
2397 delete matrix;
2398 matrix = 0;
2399 delete[] collb;
2400 collb = 0;
2401 delete[] colub;
2402 colub = 0;
2403 delete[] obj;
2404 obj = 0;
2405 delete[] rowlb;
2406 rowlb = 0;
2407 delete[] rowub;
2408 rowub = 0;
2409 }
2410
2411 //-----------------------------------------------------------------------------
2412
2413 void OsiCpxSolverInterface::loadProblem(const CoinPackedMatrix &matrix,
2414 const double *collb, const double *colub,
2415 const double *obj,
2416 const char *rowsen, const double *rowrhs,
2417 const double *rowrng)
2418 {
2419 debugMessage("OsiCpxSolverInterface::loadProblem(2)(%p, %p, %p, %p, %p, %p, %p)\n",
2420 (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
2421
2422 char *lclRowsen = NULL;
2423 double *lclRowrhs = NULL;
2424
2425 int nc = matrix.getNumCols();
2426 int nr = matrix.getNumRows();
2427
2428 if (nr == 0 && nc == 0) { // empty LP
2429 if (lp_ != NULL) { // kill old LP
2430 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2431 int err = CPXfreeprob(env_, &lp_);
2432 checkCPXerror(err, "CPXfreeprob", "loadProblem");
2433 lp_ = NULL;
2434 freeAllMemory();
2435 CPXchgobjsen(env_, getLpPtr(), objDirection);
2436 }
2437 return;
2438 }
2439
2440 if (nr == 0) {
2441 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2442
2443 if (lp_ != NULL) { // kill old LP
2444 int err = CPXfreeprob(env_, &lp_);
2445 checkCPXerror(err, "CPXfreeprob", "loadProblem");
2446 lp_ = NULL;
2447 freeAllMemory();
2448 }
2449
2450 // getLpPtr() call will create new LP
2451 int err = CPXnewcols(env_, getLpPtr(), nc, obj, collb, colub, NULL, NULL);
2452 checkCPXerror(err, "CPXcopylp", "loadProblem");
2453
2454 CPXchgobjsen(env_, getLpPtr(), objDirection);
2455
2456 return;
2457 }
2458
2459 if (rowsen == NULL) {
2460 lclRowsen = new char[nr];
2461 CoinFillN(lclRowsen, nr, 'G');
2462 rowsen = lclRowsen;
2463 }
2464 if (rowrhs == NULL) {
2465 lclRowrhs = new double[nr];
2466 CoinFillN(lclRowrhs, nr, 0.0);
2467 rowrhs = lclRowrhs;
2468 }
2469
2470 int i;
2471
2472 // Set column values to defaults if NULL pointer passed
2473 double *clb;
2474 double *cub;
2475 double *ob;
2476 double *rr = NULL;
2477 double *rhs;
2478 if (collb != NULL)
2479 clb = const_cast< double * >(collb);
2480 else {
2481 clb = new double[nc];
2482 CoinFillN(clb, nc, 0.0);
2483 }
2484 if (colub != NULL)
2485 cub = const_cast< double * >(colub);
2486 else {
2487 cub = new double[nc];
2488 CoinFillN(cub, nc, getInfinity());
2489 }
2490 if (obj != NULL)
2491 ob = const_cast< double * >(obj);
2492 else {
2493 ob = new double[nc];
2494 CoinFillN(ob, nc, 0.0);
2495 }
2496 if (rowrng != NULL) {
2497 rhs = new double[nr];
2498 rr = new double[nr];
2499 for (i = 0; i < nr; i++) {
2500 if (rowsen[i] == 'R') {
2501 if (rowrng[i] >= 0) {
2502 rhs[i] = rowrhs[i] - rowrng[i];
2503 rr[i] = rowrng[i];
2504 } else {
2505 rhs[i] = rowrhs[i];
2506 rr[i] = -rowrng[i];
2507 }
2508 } else {
2509 rhs[i] = rowrhs[i];
2510 rr[i] = 0.0;
2511 }
2512 }
2513 } else
2514 rhs = const_cast< double * >(rowrhs);
2515
2516 bool freeMatrixRequired = false;
2517 CoinPackedMatrix *m = NULL;
2518 if (!matrix.isColOrdered()) {
2519 m = new CoinPackedMatrix();
2520 m->reverseOrderedCopyOf(matrix);
2521 freeMatrixRequired = true;
2522 } else
2523 m = const_cast< CoinPackedMatrix * >(&matrix);
2524
2525 assert(nc == m->getNumCols());
2526 assert(nr == m->getNumRows());
2527 assert(m->isColOrdered());
2528
2529 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2530 int err = CPXcopylp(env_, getLpPtr(),
2531 nc, nr,
2532 // Leave ObjSense alone(set to current value).
2533 objDirection,
2534 ob,
2535 rhs,
2536 const_cast< char * >(rowsen),
2537 const_cast< int * >(m->getVectorStarts()),
2538 const_cast< int * >(m->getVectorLengths()),
2539 const_cast< int * >(m->getIndices()),
2540 const_cast< double * >(m->getElements()),
2541 const_cast< double * >(clb),
2542 const_cast< double * >(cub),
2543 rr);
2544 checkCPXerror(err, "CPXcopylp", "loadProblem");
2545
2546 if (collb == NULL)
2547 delete[] clb;
2548 if (colub == NULL)
2549 delete[] cub;
2550 if (obj == NULL)
2551 delete[] ob;
2552 if (rowrng != NULL) {
2553 delete[] rr;
2554 delete[] rhs;
2555 }
2556
2557 if (freeMatrixRequired)
2558 delete m;
2559
2560 resizeColType(nc);
2561 CoinFillN(coltype_, nc, 'C');
2562
2563 if (lclRowsen != NULL)
2564 delete[] lclRowsen;
2565 if (lclRowrhs != NULL)
2566 delete[] lclRowrhs;
2567 }
2568
2569 //-----------------------------------------------------------------------------
2570
2571 void OsiCpxSolverInterface::assignProblem(CoinPackedMatrix *&matrix,
2572 double *&collb, double *&colub,
2573 double *&obj,
2574 char *&rowsen, double *&rowrhs,
2575 double *&rowrng)
2576 {
2577 debugMessage("OsiCpxSolverInterface::assignProblem()\n");
2578
2579 loadProblem(*matrix, collb, colub, obj, rowsen, rowrhs, rowrng);
2580 delete matrix;
2581 matrix = 0;
2582 delete[] collb;
2583 collb = 0;
2584 delete[] colub;
2585 colub = 0;
2586 delete[] obj;
2587 obj = 0;
2588 delete[] rowsen;
2589 rowsen = 0;
2590 delete[] rowrhs;
2591 rowrhs = 0;
2592 delete[] rowrng;
2593 rowrng = 0;
2594 }
2595
2596 //-----------------------------------------------------------------------------
2597
2598 void OsiCpxSolverInterface::loadProblem(const int numcols, const int numrows,
2599 const int *start, const int *index,
2600 const double *value,
2601 const double *collb, const double *colub,
2602 const double *obj,
2603 const double *rowlb, const double *rowub)
2604 {
2605 debugMessage("OsiCpxSolverInterface::loadProblem(3)()\n");
2606
2607 const double inf = getInfinity();
2608
2609 char *rowSense = new char[numrows];
2610 double *rowRhs = new double[numrows];
2611 double *rowRange = new double[numrows];
2612
2613 for (int i = numrows - 1; i >= 0; --i) {
2614 const double lower = rowlb ? rowlb[i] : -inf;
2615 const double upper = rowub ? rowub[i] : inf;
2616 convertBoundToSense(lower, upper, rowSense[i], rowRhs[i], rowRange[i]);
2617 }
2618
2619 loadProblem(numcols, numrows, start, index, value, collb, colub, obj,
2620 rowSense, rowRhs, rowRange);
2621 delete[] rowSense;
2622 delete[] rowRhs;
2623 delete[] rowRange;
2624 }
2625
2626 //-----------------------------------------------------------------------------
2627
2628 void OsiCpxSolverInterface::loadProblem(const int numcols, const int numrows,
2629 const int *start, const int *index,
2630 const double *value,
2631 const double *collb, const double *colub,
2632 const double *obj,
2633 const char *rowsen, const double *rowrhs,
2634 const double *rowrng)
2635 {
2636 debugMessage("OsiCpxSolverInterface::loadProblem(4)(%d, %d, %p, %p, %p, %p, %p, %p, %p, %p, %p)\n",
2637 numcols, numrows, (void *)start, (void *)index, (void *)value, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
2638
2639 const int nc = numcols;
2640 const int nr = numrows;
2641
2642 if (nr == 0 && nc == 0) {
2643 // empty LP
2644 if (lp_ != NULL) { // kill old LP
2645 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2646 int err = CPXfreeprob(env_, &lp_);
2647 checkCPXerror(err, "CPXfreeprob", "loadProblem");
2648 lp_ = NULL;
2649 freeAllMemory();
2650 CPXchgobjsen(env_, getLpPtr(), objDirection);
2651 }
2652 return;
2653 }
2654
2655 if (nr == 0) {
2656 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2657
2658 if (lp_ != NULL) { // kill old LP
2659 int err = CPXfreeprob(env_, &lp_);
2660 checkCPXerror(err, "CPXfreeprob", "loadProblem");
2661 lp_ = NULL;
2662 freeAllMemory();
2663 }
2664
2665 // getLpPtr() call will create new LP
2666 int err = CPXnewcols(env_, getLpPtr(), nc, obj, collb, colub, NULL, NULL);
2667 checkCPXerror(err, "CPXcopylp", "loadProblem");
2668
2669 CPXchgobjsen(env_, getLpPtr(), objDirection);
2670
2671 return;
2672 }
2673
2674 char *lclRowsen = NULL;
2675 double *lclRowrhs = NULL;
2676
2677 if (rowsen == NULL) {
2678 lclRowsen = new char[nr];
2679 CoinFillN(lclRowsen, nr, 'G');
2680 rowsen = lclRowsen;
2681 }
2682 if (rowrhs == NULL) {
2683 lclRowrhs = new double[nr];
2684 CoinFillN(lclRowrhs, nr, 0.0);
2685 }
2686
2687 int i;
2688
2689 // Set column values to defaults if NULL pointer passed
2690 int *len = new int[nc];
2691 double *clb = new double[nc];
2692 double *cub = new double[nc];
2693 double *ob = new double[nc];
2694 double *rr = new double[nr];
2695 double *rhs = new double[nr];
2696 char *sen = new char[nr];
2697
2698 for (i = 0; i < nc; ++i) {
2699 len[i] = start[i + 1] - start[i];
2700 }
2701
2702 if (collb != NULL)
2703 CoinDisjointCopyN(collb, nc, clb);
2704 else
2705 CoinFillN(clb, nc, 0.0);
2706
2707 if (colub != NULL)
2708 CoinDisjointCopyN(colub, nc, cub);
2709 else
2710 CoinFillN(cub, nc, getInfinity());
2711
2712 if (obj != NULL)
2713 CoinDisjointCopyN(obj, nc, ob);
2714 else
2715 CoinFillN(ob, nc, 0.0);
2716
2717 if (rowrng != NULL) {
2718 for (i = 0; i < nr; i++) {
2719 if (rowsen[i] == 'R') {
2720 if (rowrng[i] >= 0) {
2721 rhs[i] = rowrhs[i] - rowrng[i];
2722 rr[i] = rowrng[i];
2723 } else {
2724 rhs[i] = rowrhs[i];
2725 rr[i] = -rowrng[i];
2726 }
2727 } else {
2728 rhs[i] = rowrhs[i];
2729 rr[i] = 0.0;
2730 }
2731 }
2732 } else {
2733 CoinDisjointCopyN(rowrhs, nr, rhs);
2734 }
2735
2736 CoinDisjointCopyN(rowsen, nr, sen);
2737
2738 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2739
2740 int err = CPXcopylp(env_, getLpPtr(),
2741 nc, nr,
2742 // Leave ObjSense alone(set to current value).
2743 objDirection, ob, rhs, sen,
2744 const_cast< int * >(start),
2745 len, const_cast< int * >(index),
2746 const_cast< double * >(value),
2747 clb, cub, rr);
2748
2749 checkCPXerror(err, "CPXcopylp", "loadProblem");
2750
2751 delete[] len;
2752 delete[] clb;
2753 delete[] cub;
2754 delete[] ob;
2755 delete[] rr;
2756 delete[] rhs;
2757 delete[] sen;
2758
2759 if (lclRowsen != NULL)
2760 delete[] lclRowsen;
2761 if (lclRowrhs != NULL)
2762 delete[] lclRowrhs;
2763
2764 resizeColType(nc);
2765 CoinFillN(coltype_, nc, 'C');
2766 }
2767
2768 //-----------------------------------------------------------------------------
2769 // Read mps files
2770 //-----------------------------------------------------------------------------
2771 int OsiCpxSolverInterface::readMps(const char *filename,
2772 const char *extension)
2773 {
2774 debugMessage("OsiCpxSolverInterface::readMps(%s, %s)\n", filename, extension);
2775
2776 #if 0
2777 std::string f(filename);
2778 std::string e(extension);
2779 std::string fullname = f + "." + e;
2780 int err = CPXreadcopyprob( env_, getLpPtr(), const_cast<char*>( fullname.c_str() ), NULL );
2781 checkCPXerror( err, "CPXreadcopyprob", "readMps" );
2782 #endif
2783 // just call base class method
2784 return OsiSolverInterface::readMps(filename, extension);
2785 }
2786
2787 //-----------------------------------------------------------------------------
2788 // Write mps files
2789 //-----------------------------------------------------------------------------
2790 void OsiCpxSolverInterface::writeMps(const char *filename,
2791 const char *extension,
2792 double objSense) const
2793 {
2794 debugMessage("OsiCpxSolverInterface::writeMps(%s, %s, %g)\n", filename, extension, objSense);
2795
2796 // *FIXME* : this will not output ctype information to the MPS file
2797 char filetype[4] = "MPS";
2798 std::string f(filename);
2799 std::string e(extension);
2800 std::string fullname = f + "." + e;
2801 int err = CPXwriteprob(env_, getMutableLpPtr(), const_cast< char * >(fullname.c_str()), filetype);
2802 checkCPXerror(err, "CPXwriteprob", "writeMps");
2803 }
2804
2805 void OsiCpxSolverInterface::passInMessageHandler(CoinMessageHandler *handler)
2806 {
2807 int err;
2808 CPXCHANNELptr cpxresults;
2809 CPXCHANNELptr cpxwarning;
2810 CPXCHANNELptr cpxerror;
2811 CPXCHANNELptr cpxlog;
2812 err = CPXgetchannels(env_, &cpxresults, &cpxwarning, &cpxerror, &cpxlog);
2813 checkCPXerror(err, "CPXgetchannels", "gutsOfConstructor");
2814
2815 err = CPXdelfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
2816 checkCPXerror(err, "CPXdelfuncdest", "gutsOfConstructor");
2817 err = CPXdelfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
2818 checkCPXerror(err, "CPXdelfuncdest", "gutsOfConstructor");
2819 err = CPXdelfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
2820 checkCPXerror(err, "CPXdelfuncdest", "gutsOfConstructor");
2821 err = CPXdelfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
2822 checkCPXerror(err, "CPXdelfuncdest", "gutsOfConstructor");
2823
2824 OsiSolverInterface::passInMessageHandler(handler);
2825
2826 err = CPXaddfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
2827 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
2828 err = CPXaddfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
2829 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
2830 err = CPXaddfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
2831 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
2832 err = CPXaddfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
2833 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
2834 }
2835
2836 //#############################################################################
2837 // CPX specific public interfaces
2838 //#############################################################################
2839
2840 CPXENVptr OsiCpxSolverInterface::getEnvironmentPtr()
2841 {
2842 assert(env_ != NULL);
2843 return env_;
2844 }
2845
2846 CPXLPptr OsiCpxSolverInterface::getLpPtr(int keepCached)
2847 {
2848 freeCachedData(keepCached);
2849 return getMutableLpPtr();
2850 }
2851
2852 //-----------------------------------------------------------------------------
2853
2854 const char *OsiCpxSolverInterface::getCtype() const
2855 {
2856 debugMessage("OsiCpxSolverInterface::getCtype()\n");
2857
2858 return coltype_;
2859 }
2860
2861 //#############################################################################
2862 // Constructors, destructors clone and assignment
2863 //#############################################################################
2864
2865 //-------------------------------------------------------------------
2866 // Default Constructor
2867 //-------------------------------------------------------------------
2868 OsiCpxSolverInterface::OsiCpxSolverInterface()
2869 : OsiSolverInterface()
2870 , env_(NULL)
2871 , lp_(NULL)
2872 , hotStartCStat_(NULL)
2873 , hotStartCStatSize_(0)
2874 , hotStartRStat_(NULL)
2875 , hotStartRStatSize_(0)
2876 , hotStartMaxIteration_(1000000)
2877 , // ??? default iteration limit for strong branching is large
2878 obj_(NULL)
2879 , collower_(NULL)
2880 , colupper_(NULL)
2881 , rowsense_(NULL)
2882 , rhs_(NULL)
2883 , rowrange_(NULL)
2884 , rowlower_(NULL)
2885 , rowupper_(NULL)
2886 , colsol_(NULL)
2887 , rowsol_(NULL)
2888 , redcost_(NULL)
2889 , rowact_(NULL)
2890 , matrixByRow_(NULL)
2891 , matrixByCol_(NULL)
2892 , coltype_(NULL)
2893 , coltypesize_(0)
2894 , probtypemip_(false)
2895 , domipstart(false)
2896 , disableadvbasis(false)
2897 {
2898 debugMessage("OsiCpxSolverInterface::OsiCpxSolverInterface()\n");
2899
2900 gutsOfConstructor();
2901 }
2902
2903 //----------------------------------------------------------------
2904 // Clone
2905 //----------------------------------------------------------------
2906 OsiSolverInterface *OsiCpxSolverInterface::clone(bool copyData) const
2907 {
2908 debugMessage("OsiCpxSolverInterface::clone(%d)\n", copyData);
2909
2910 return (new OsiCpxSolverInterface(*this));
2911 }
2912
2913 //-------------------------------------------------------------------
2914 // Copy constructor
2915 //-------------------------------------------------------------------
2916 OsiCpxSolverInterface::OsiCpxSolverInterface(const OsiCpxSolverInterface &source)
2917 : OsiSolverInterface(source)
2918 , env_(NULL)
2919 , lp_(NULL)
2920 , hotStartCStat_(NULL)
2921 , hotStartCStatSize_(0)
2922 , hotStartRStat_(NULL)
2923 , hotStartRStatSize_(0)
2924 , hotStartMaxIteration_(source.hotStartMaxIteration_)
2925 , obj_(NULL)
2926 , collower_(NULL)
2927 , colupper_(NULL)
2928 , rowsense_(NULL)
2929 , rhs_(NULL)
2930 , rowrange_(NULL)
2931 , rowlower_(NULL)
2932 , rowupper_(NULL)
2933 , colsol_(NULL)
2934 , rowsol_(NULL)
2935 , redcost_(NULL)
2936 , rowact_(NULL)
2937 , matrixByRow_(NULL)
2938 , matrixByCol_(NULL)
2939 , coltype_(NULL)
2940 , coltypesize_(0)
2941 , probtypemip_(false)
2942 , domipstart(false)
2943 , disableadvbasis(false)
2944 {
2945 debugMessage("OsiCpxSolverInterface::OsiCpxSolverInterface(%p)\n", (void *)&source);
2946
2947 gutsOfConstructor();
2948 gutsOfCopy(source);
2949 }
2950
2951 //-------------------------------------------------------------------
2952 // Destructor
2953 //-------------------------------------------------------------------
2954 OsiCpxSolverInterface::~OsiCpxSolverInterface()
2955 {
2956 debugMessage("OsiCpxSolverInterface::~OsiCpxSolverInterface()\n");
2957
2958 gutsOfDestructor();
2959 }
2960
2961 //----------------------------------------------------------------
2962 // Assignment operator
2963 //-------------------------------------------------------------------
2964 OsiCpxSolverInterface &OsiCpxSolverInterface::operator=(const OsiCpxSolverInterface &rhs)
2965 {
2966 debugMessage("OsiCpxSolverInterface::operator=(%p)\n", (void *)&rhs);
2967
2968 if (this != &rhs) {
2969 OsiSolverInterface::operator=(rhs);
2970 gutsOfDestructor();
2971 gutsOfConstructor();
2972 if (rhs.lp_ != NULL)
2973 gutsOfCopy(rhs);
2974 }
2975 return *this;
2976 }
2977
2978 //#############################################################################
2979 // Applying cuts
2980 //#############################################################################
2981
2982 void OsiCpxSolverInterface::applyColCut(const OsiColCut &cc)
2983 {
2984 debugMessage("OsiCpxSolverInterface::applyColCut(%p)\n", (void *)&cc);
2985
2986 const double *cplexColLB = getColLower();
2987 const double *cplexColUB = getColUpper();
2988 const CoinPackedVector &lbs = cc.lbs();
2989 const CoinPackedVector &ubs = cc.ubs();
2990 int i;
2991
2992 for (i = 0; i < lbs.getNumElements(); ++i)
2993 if (lbs.getElements()[i] > cplexColLB[lbs.getIndices()[i]])
2994 setColLower(lbs.getIndices()[i], lbs.getElements()[i]);
2995 for (i = 0; i < ubs.getNumElements(); ++i)
2996 if (ubs.getElements()[i] < cplexColUB[ubs.getIndices()[i]])
2997 setColUpper(ubs.getIndices()[i], ubs.getElements()[i]);
2998 }
2999
3000 //-----------------------------------------------------------------------------
3001
3002 void OsiCpxSolverInterface::applyRowCut(const OsiRowCut &rowCut)
3003 {
3004 debugMessage("OsiCpxSolverInterface::applyRowCut(%p)\n", (void *)&rowCut);
3005
3006 int err = 0;
3007 double rhs = 0.0;
3008 double rng = 0.0;
3009 char sns;
3010 double lb = rowCut.lb();
3011 double ub = rowCut.ub();
3012 if (lb <= -getInfinity() && ub >= getInfinity()) // free constraint
3013 {
3014 rhs = -getInfinity();
3015 rng = 2 * getInfinity(); // CPLEX doesn't support free constraints
3016 sns = 'R'; // -> implement them as ranged rows with infinite bounds
3017 } else if (lb <= -getInfinity()) // <= constraint
3018 {
3019 rhs = ub;
3020 sns = 'L';
3021 } else if (ub >= getInfinity()) // >= constraint
3022 {
3023 rhs = lb;
3024 sns = 'G';
3025 } else if (ub == lb) // = constraint
3026 {
3027 rhs = ub;
3028 sns = 'E';
3029 } else // range constraint
3030 {
3031 rhs = lb;
3032 rng = ub - lb;
3033 sns = 'R';
3034 }
3035 int rmatbeg = 0;
3036 err = CPXaddrows(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN), 0, 1, rowCut.row().getNumElements(),
3037 &rhs, &sns, &rmatbeg,
3038 const_cast< int * >(rowCut.row().getIndices()),
3039 const_cast< double * >(rowCut.row().getElements()),
3040 NULL, NULL);
3041 checkCPXerror(err, "CPXaddrows", "applyRowCut");
3042 if (sns == 'R') {
3043 err = CPXchgcoef(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN),
3044 CPXgetnumrows(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN)) - 1,
3045 -2, rng);
3046 checkCPXerror(err, "CPXchgcoef", "applyRowCut");
3047 }
3048 }
3049
3050 //#############################################################################
3051 // Private methods (non-static and static) and static data
3052 //#############################################################################
3053
3054 //-------------------------------------------------------------------
3055 // Get pointer to CPXLPptr.
3056 // const methods should use getMutableLpPtr().
3057 // non-const methods should use getLpPtr().
3058 //-------------------------------------------------------------------
3059 CPXLPptr OsiCpxSolverInterface::getMutableLpPtr() const
3060 {
3061 if (lp_ == NULL) {
3062 int err;
3063 assert(env_ != NULL);
3064 #if 0
3065 //char pn[] = "OSI_CPLEX";
3066 lp_ = CPXcreateprob( env_, &err, pn );
3067 #else
3068 std::string pn;
3069 getStrParam(OsiProbName, pn);
3070 lp_ = CPXcreateprob(env_, &err, const_cast< char * >(pn.c_str()));
3071 #endif
3072 checkCPXerror(err, "CPXcreateprob", "getMutableLpPtr");
3073 // err = CPXchgprobtype(env_,lp_,CPXPROB_LP);
3074 // checkCPXerror( err, "CPXchgprobtype", "getMutableLpPtr" );
3075 assert(lp_ != NULL);
3076 }
3077 return lp_;
3078 }
3079
3080 //-------------------------------------------------------------------
3081 CPXENVptr OsiCpxSolverInterface::getMutableEnvironmentPtr() const
3082 {
3083 assert(env_ != NULL);
3084 return env_;
3085 }
3086
3087 //-------------------------------------------------------------------
3088
3089 void OsiCpxSolverInterface::gutsOfCopy(const OsiCpxSolverInterface &source)
3090 {
3091 // Set Objective Sense
3092 setObjSense(source.getObjSense());
3093
3094 // Set Rim and constraints
3095 const double *obj = source.getObjCoefficients();
3096 const double *rhs = source.getRightHandSide();
3097 const char *sense = source.getRowSense();
3098 const CoinPackedMatrix *cols = source.getMatrixByCol();
3099 const double *lb = source.getColLower();
3100 const double *ub = source.getColUpper();
3101 loadProblem(*cols, lb, ub, obj, sense, rhs, source.getRowRange());
3102
3103 // Set MIP information
3104 resizeColType(source.coltypesize_);
3105 CoinDisjointCopyN(source.coltype_, source.coltypesize_, coltype_);
3106
3107 // Set Solution
3108 setColSolution(source.getColSolution());
3109 setRowPrice(source.getRowPrice());
3110
3111 // Should also copy row and col names.
3112 #if 0
3113 char** cname = new char*[numcols];
3114 char* cnamestore = NULL;
3115 int surplus;
3116 err = CPXgetcolname( env_, source.lp_, cname, NULL, 0, &surplus, 0, numcols-1 );
3117 if( err != CPXERR_NO_NAMES )
3118 {
3119 cnamestore = new char[-surplus];
3120 err = CPXgetcolname( env_, source.lp_, cname, cnamestore, -surplus, &surplus, 0, numcols-1 );
3121 checkCPXerror( err, "CPXgetcolname", "gutsOfCopy" );
3122 assert( surplus == 0 );
3123 }
3124 else
3125 {
3126 delete [] cname;
3127 cname = NULL;
3128 }
3129
3130 char** rname = new char*[numrows];
3131 char* rnamestore = NULL;
3132 err = CPXgetrowname( env_, source.lp_, rname, NULL, 0, &surplus, 0, numrows-1 );
3133 if( err != CPXERR_NO_NAMES )
3134 {
3135 rnamestore = new char[-surplus];
3136 err = CPXgetrowname( env_, source.lp_, rname, rnamestore, -surplus, &surplus, 0, numrows-1 );
3137 checkCPXerror( err, "CPXgetrowname", "gutsOfCopy" );
3138 assert( surplus == 0 );
3139 }
3140 else
3141 {
3142 delete [] rname;
3143 rname = NULL;
3144 }
3145
3146 err = CPXcopylpwnames( env_, getLpPtr(),
3147 numcols, numrows, objsen,
3148 const_cast<double *>(obj),
3149 const_cast<double *>(rhs),
3150 const_cast<char *>(sense),
3151 const_cast<int *>(cols->vectorStarts()),
3152 const_cast<int *>(cols->vectorLengths()),
3153 const_cast<int *>(cols->indices()),
3154 const_cast<double *>(cols->elements()),
3155 const_cast<double *>(lb),
3156 const_cast<double *>(ub),
3157 rng,
3158 cname, rname);
3159 checkCPXerror( err, "CPXcopylpwnames", "gutsOfCopy" );
3160
3161 if( rname != NULL )
3162 {
3163 delete [] rnamestore;
3164 delete [] rname;
3165 }
3166 if( cname != NULL )
3167 {
3168 delete [] cnamestore;
3169 delete [] cname;
3170 }
3171 delete [] rng;
3172 #endif
3173 }
3174
3175 //-------------------------------------------------------------------
3176 void OsiCpxSolverInterface::gutsOfConstructor()
3177 {
3178 int err;
3179 #if CPX_VERSION >= 800
3180 env_ = CPXopenCPLEX(&err);
3181 #else
3182 env_ = CPXopenCPLEXdevelop(&err);
3183 #endif
3184
3185 checkCPXerror(err, "CPXopenCPLEXdevelop", "gutsOfConstructor");
3186 assert(env_ != NULL);
3187
3188 CPXCHANNELptr cpxresults;
3189 CPXCHANNELptr cpxwarning;
3190 CPXCHANNELptr cpxerror;
3191 CPXCHANNELptr cpxlog;
3192 err = CPXgetchannels(env_, &cpxresults, &cpxwarning, &cpxerror, &cpxlog);
3193 checkCPXerror(err, "CPXgetchannels", "gutsOfConstructor");
3194
3195 err = CPXaddfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
3196 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
3197 err = CPXaddfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
3198 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
3199 err = CPXaddfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
3200 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
3201 err = CPXaddfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
3202 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
3203
3204 /* turn off all output to screen */
3205 err = CPXsetintparam(env_, CPX_PARAM_SCRIND, CPX_OFF);
3206 checkCPXerror(err, "CPXsetintparam", "gutsOfConstructor");
3207
3208 #if 0
3209 // CPXcreateprob was moved to getLpPtr() method.
3210 lp_ = CPXcreateprob( env_, &err, "OSI_CPLEX" );
3211 checkCPXerror( err, "CPXcreateprob", "gutsOfConstructor" );
3212 // err = CPXchgprobtype(env_,lp_,CPXPROB_LP);
3213 // checkCPXerror( err, "CPXchgprobtype", "getMutableLpPtr" );
3214 assert( lp_ != NULL );
3215 #endif
3216 }
3217
3218 //-------------------------------------------------------------------
3219 void OsiCpxSolverInterface::gutsOfDestructor()
3220 {
3221 if (lp_ != NULL) {
3222 int err = CPXfreeprob(env_, &lp_);
3223 checkCPXerror(err, "CPXfreeprob", "gutsOfDestructor");
3224 lp_ = NULL;
3225 freeAllMemory();
3226 }
3227
3228 if (env_ != NULL) {
3229 int err = CPXcloseCPLEX(&env_);
3230 checkCPXerror(err, "CPXcloseCPLEX", "gutsOfDestructor");
3231 env_ = NULL;
3232 }
3233
3234 assert(lp_ == NULL);
3235 assert(env_ == NULL);
3236 assert(obj_ == NULL);
3237 assert(collower_ == NULL);
3238 assert(colupper_ == NULL);
3239 assert(rowsense_ == NULL);
3240 assert(rhs_ == NULL);
3241 assert(rowrange_ == NULL);
3242 assert(rowlower_ == NULL);
3243 assert(rowupper_ == NULL);
3244 assert(colsol_ == NULL);
3245 assert(rowsol_ == NULL);
3246 assert(redcost_ == NULL);
3247 assert(rowact_ == NULL);
3248 assert(matrixByRow_ == NULL);
3249 assert(matrixByCol_ == NULL);
3250 assert(coltype_ == NULL);
3251 assert(coltypesize_ == 0);
3252 }
3253
3254 //-------------------------------------------------------------------
3255 /// free cached vectors
3256
3257 void OsiCpxSolverInterface::freeCachedColRim()
3258 {
3259 freeCacheDouble(obj_);
3260 freeCacheDouble(collower_);
3261 freeCacheDouble(colupper_);
3262 assert(obj_ == NULL);
3263 assert(collower_ == NULL);
3264 assert(colupper_ == NULL);
3265 }
3266
3267 void OsiCpxSolverInterface::freeCachedRowRim()
3268 {
3269 freeCacheChar(rowsense_);
3270 freeCacheDouble(rhs_);
3271 freeCacheDouble(rowrange_);
3272 freeCacheDouble(rowlower_);
3273 freeCacheDouble(rowupper_);
3274 assert(rowsense_ == NULL);
3275 assert(rhs_ == NULL);
3276 assert(rowrange_ == NULL);
3277 assert(rowlower_ == NULL);
3278 assert(rowupper_ == NULL);
3279 }
3280
3281 void OsiCpxSolverInterface::freeCachedMatrix()
3282 {
3283 freeCacheMatrix(matrixByRow_);
3284 freeCacheMatrix(matrixByCol_);
3285 assert(matrixByRow_ == NULL);
3286 assert(matrixByCol_ == NULL);
3287 }
3288
3289 void OsiCpxSolverInterface::freeCachedResults()
3290 {
3291 freeCacheDouble(colsol_);
3292 freeCacheDouble(rowsol_);
3293 freeCacheDouble(redcost_);
3294 freeCacheDouble(rowact_);
3295 assert(colsol_ == NULL);
3296 assert(rowsol_ == NULL);
3297 assert(redcost_ == NULL);
3298 assert(rowact_ == NULL);
3299 }
3300
3301 void OsiCpxSolverInterface::freeCachedData(int keepCached)
3302 {
3303 if (!(keepCached & OsiCpxSolverInterface::KEEPCACHED_COLUMN))
3304 freeCachedColRim();
3305 if (!(keepCached & OsiCpxSolverInterface::KEEPCACHED_ROW))
3306 freeCachedRowRim();
3307 if (!(keepCached & OsiCpxSolverInterface::KEEPCACHED_MATRIX))
3308 freeCachedMatrix();
3309 if (!(keepCached & OsiCpxSolverInterface::KEEPCACHED_RESULTS))
3310 freeCachedResults();
3311 }
3312
3313 void OsiCpxSolverInterface::freeAllMemory()
3314 {
3315 freeCachedData();
3316 if (hotStartCStat_ != NULL)
3317 delete[] hotStartCStat_;
3318 if (hotStartRStat_ != NULL)
3319 delete[] hotStartRStat_;
3320 hotStartCStat_ = NULL;
3321 hotStartCStatSize_ = 0;
3322 hotStartRStat_ = NULL;
3323 hotStartRStatSize_ = 0;
3324 freeColType();
3325 }
3326
3327 //#############################################################################
3328 // Resets as if default constructor
3329 void OsiCpxSolverInterface::reset()
3330 {
3331 int err;
3332 CPXCHANNELptr cpxresults;
3333 CPXCHANNELptr cpxwarning;
3334 CPXCHANNELptr cpxerror;
3335 CPXCHANNELptr cpxlog;
3336
3337 err = CPXgetchannels(env_, &cpxresults, &cpxwarning, &cpxerror, &cpxlog);
3338
3339 err = CPXdelfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
3340 checkCPXerror(err, "CPXdelfuncdest", "reset");
3341 err = CPXdelfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
3342 checkCPXerror(err, "CPXdelfuncdest", "reset");
3343 err = CPXdelfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
3344 checkCPXerror(err, "CPXdelfuncdest", "reset");
3345 err = CPXdelfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
3346 checkCPXerror(err, "CPXdelfuncdest", "reset");
3347
3348 setInitialData(); // clear base class (this may reset the message handler, too)
3349
3350 err = CPXaddfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
3351 checkCPXerror(err, "CPXaddfuncdest", "reset");
3352 err = CPXaddfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
3353 checkCPXerror(err, "CPXaddfuncdest", "reset");
3354 err = CPXaddfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
3355 checkCPXerror(err, "CPXaddfuncdest", "reset");
3356 err = CPXaddfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
3357 checkCPXerror(err, "CPXaddfuncdest", "reset");
3358
3359 if (lp_ != NULL) { // kill old LP
3360 int err = CPXfreeprob(env_, &lp_);
3361 checkCPXerror(err, "CPXfreeprob", "loadProblem");
3362 lp_ = NULL;
3363 freeAllMemory();
3364 }
3365 }
3366 #if CPX_VERSION >= 900
3367 /**********************************************************************/
3368 /* Returns 1 if can just do getBInv etc
3369 2 if has all OsiSimplex methods
3370 and 0 if it has none */
3371 int OsiCpxSolverInterface::canDoSimplexInterface() const
3372 {
3373 return 1;
3374 }
3375
3376 /**********************************************************************/
3377 bool OsiCpxSolverInterface::basisIsAvailable() const
3378 {
3379 CPXLPptr lp = getMutableLpPtr();
3380
3381 int solnmethod, solntype, pfeasind, dfeasind;
3382
3383 int status = CPXsolninfo(env_, lp, &solnmethod, &solntype,
3384 &pfeasind, &dfeasind);
3385 if (status) {
3386 return false;
3387 }
3388 if (solntype == CPX_BASIC_SOLN) {
3389 return true;
3390 }
3391 return false;
3392 }
3393
3394 /**********************************************************************/
3395 /* CPLEX return codes:
3396 For cstat:
3397 CPX_AT_LOWER 0 : variable at lower bound
3398 CPX_BASIC 1 : variable is basic
3399 CPX_AT_UPPER 2 : variable at upper bound
3400 CPX_FREE_SUPER 3 : variable free and non-basic
3401
3402 For rstat:
3403
3404 Non ranged rows:
3405 CPX_AT_LOWER 0 : associated slack/surplus/artificial variable non-basic
3406 at value 0.0
3407 CPX_BASIC 1 : associated slack/surplus/artificial variable basic
3408
3409 Ranged rows:
3410 CPX_AT_LOWER 0 : associated slack/surplus/artificial variable non-basic
3411 at its lower bound
3412 CPX_BASIC 1 : associated slack/surplus/artificial variable basic
3413 CPX_AT_UPPER 2 : associated slack/surplus/artificial variable non-basic
3414 at upper bound
3415
3416 Cplex adds a slack with coeff +1 in <= and =, with coeff -1 in >=, slack being
3417 non negative. We switch in order to get a "Clp tableau" where all the
3418 slacks have coeff +1.
3419
3420 If a slack for >= is non basic, invB is not changed; column of the slack in
3421 opt tableau is flipped.
3422
3423 If slack for >= is basic, corresp. row of invB is flipped; whole row of opt
3424 tableau is flipped; then whole column for the slack in opt tableau is flipped.
3425 */
3426
3427 /* Osi return codes:
3428 0: free
3429 1: basic
3430 2: upper
3431 3: lower
3432 */
3433 void OsiCpxSolverInterface::getBasisStatus(int *cstat, int *rstat) const
3434 {
3435 CPXLPptr lp = getMutableLpPtr();
3436 int status = CPXgetbase(env_, lp, cstat, rstat);
3437 if (status) {
3438 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): Unable to get base\n");
3439 exit(1);
3440 }
3441
3442 int ncol = getNumCols();
3443 int nrow = getNumRows();
3444 const int objsense = (int)getObjSense();
3445 const double *dual = getRowPrice();
3446 const double *row_act = getRowActivity();
3447 const double *rowLower = getRowLower();
3448 const double *rowUpper = getRowUpper();
3449
3450 char *sense = new char[nrow];
3451 status = CPXgetsense(env_, lp, sense, 0, nrow - 1);
3452
3453 if (status) {
3454 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): Unable to get sense for the rows\n");
3455 exit(1);
3456 }
3457
3458 for (int i = 0; i < ncol; i++) {
3459 switch (cstat[i]) {
3460 case 0:
3461 cstat[i] = 3;
3462 break;
3463 case 1:
3464 break;
3465 case 2:
3466 break;
3467 case 3:
3468 cstat[i] = 0;
3469 break;
3470 default:
3471 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): unknown column status: %d\n", cstat[i]);
3472 break;
3473 }
3474 }
3475
3476 if (objsense == 1) {
3477
3478 for (int i = 0; i < nrow; i++) {
3479 switch (rstat[i]) {
3480 case 0:
3481 rstat[i] = 3;
3482
3483 if (sense[i] == 'E') {
3484 if (dual[i] > 0) {
3485 rstat[i] = 2;
3486 }
3487 }
3488
3489 if (sense[i] == 'R') {
3490 if (rowUpper[i] > rowLower[i] + 1e-6) {
3491 if (row_act[i] < rowUpper[i] - 1e-6) {
3492 rstat[i] = 2;
3493 }
3494 } else {
3495 if (dual[i] > 0) {
3496 rstat[i] = 2;
3497 }
3498 }
3499 }
3500
3501 if (sense[i] == 'G') {
3502 rstat[i] = 2;
3503 }
3504
3505 break;
3506
3507 case 1:
3508 break;
3509 case 2:
3510 if (sense[i] == 'E') {
3511 if (dual[i] < 0) {
3512 rstat[i] = 3;
3513 }
3514 }
3515
3516 if (sense[i] == 'R') {
3517 if (rowUpper[i] > rowLower[i] + 1e-6) {
3518 if (row_act[i] > rowLower[i] + 1e-6) {
3519 rstat[i] = 3;
3520 }
3521 } else {
3522 if (dual[i] < 0) {
3523 rstat[i] = 3;
3524 }
3525 }
3526 }
3527
3528 break;
3529 default:
3530 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): unknown row status: %d\n", rstat[i]);
3531 break;
3532 }
3533 }
3534 } else { // objsense == -1
3535 for (int i = 0; i < nrow; i++) {
3536 switch (rstat[i]) {
3537 case 0:
3538 rstat[i] = 3;
3539
3540 if (sense[i] == 'E') {
3541 if (dual[i] < 0) {
3542 rstat[i] = 2;
3543 }
3544 }
3545
3546 if (sense[i] == 'R') {
3547 if (rowUpper[i] > rowLower[i] + 1e-6) {
3548 if (row_act[i] < rowUpper[i] - 1e-6) {
3549 rstat[i] = 2;
3550 }
3551 } else {
3552 if (dual[i] < 0) {
3553 rstat[i] = 2;
3554 }
3555 }
3556 }
3557
3558 if (sense[i] == 'G') {
3559 rstat[i] = 2;
3560 }
3561
3562 break;
3563
3564 case 1:
3565 break;
3566 case 2:
3567 if (sense[i] == 'E') {
3568 if (dual[i] > 0) {
3569 rstat[i] = 3;
3570 }
3571 }
3572
3573 if (sense[i] == 'R') {
3574 if (rowUpper[i] > rowLower[i] + 1e-6) {
3575 if (row_act[i] > rowLower[i] + 1e-6) {
3576 rstat[i] = 3;
3577 }
3578 } else {
3579 if (dual[i] > 0) {
3580 rstat[i] = 3;
3581 }
3582 }
3583 }
3584
3585 break;
3586 default:
3587 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): unknown row status: %d\n", rstat[i]);
3588 break;
3589 }
3590 }
3591 }
3592 delete[] sense;
3593 }
3594
3595 /**********************************************************************/
3596 void OsiCpxSolverInterface::getBInvARow(int row, double *z, double *slack)
3597 const
3598 {
3599
3600 CPXLPptr lp = getMutableLpPtr();
3601
3602 int nrow = getNumRows();
3603 int ncol = getNumCols();
3604 char *sense = new char[nrow];
3605 int status = CPXgetsense(env_, lp, sense, 0, nrow - 1);
3606 if (status) {
3607 printf("### ERROR: OsiCpxSolverInterface::getBInvARow(): Unable to get senses for the rows\n");
3608 exit(1);
3609 }
3610
3611 status = CPXbinvarow(env_, lp, row, z);
3612 if (status) {
3613 printf("### ERROR: OsiCpxSolverInterface::getBInvARow(): Unable to get row %d of the tableau\n", row);
3614 exit(1);
3615 }
3616
3617 if (slack != NULL) {
3618 status = CPXbinvrow(env_, lp, row, slack);
3619 if (status) {
3620 printf("### ERROR: OsiCpxSolverInterface::getBInvARow(): Unable to get row %d of B inverse\n", row);
3621 exit(1);
3622 }
3623
3624 // slack contains now the row of BInv(cplex);
3625 // slack(cplex) is obtained by flipping in slack all entries for >= constr
3626 // slack(clp) is obtained by flipping the same entries in slack(cplex)
3627 // i.e. slack(clp) is the current slack.
3628 }
3629
3630 if (sense[row] == 'G') {
3631 int *ind_bas = new int[nrow];
3632 int ind_slack = ncol + row;
3633 getBasics(ind_bas);
3634 for (int i = 0; i < nrow; i++) {
3635 if (ind_bas[i] == ind_slack) { // slack for row is basic; whole row
3636 // must be flipped
3637 for (int j = 0; j < nrow; j++) {
3638 z[j] = -z[j];
3639 }
3640 if (slack != NULL) {
3641 for (int j = 0; j < nrow; j++) {
3642 slack[j] = -slack[j];
3643 }
3644 }
3645 }
3646 break;
3647 }
3648 delete[] ind_bas;
3649 }
3650 delete[] sense;
3651 } /* getBInvARow */
3652
3653 /**********************************************************************/
3654 void OsiCpxSolverInterface::getBInvRow(int row, double *z) const
3655 {
3656
3657 CPXLPptr lp = getMutableLpPtr();
3658
3659 int nrow = getNumRows();
3660 int ncol = getNumCols();
3661
3662 int status = CPXbinvrow(env_, lp, row, z);
3663 if (status) {
3664 printf("### ERROR: OsiCpxSolverInterface::getBInvRow(): Unable to get row %d of the basis inverse\n", row);
3665 exit(1);
3666 }
3667 int *ind_bas = new int[nrow];
3668 getBasics(ind_bas);
3669 if (ind_bas[row] >= ncol) { // binv row corresponds to a slack variable
3670 int Arow = ind_bas[row] - ncol; // Arow is the number of the row in the problem matrix which this slack belongs to
3671 char sense;
3672 status = CPXgetsense(env_, lp, &sense, Arow, Arow);
3673 if (status) {
3674 printf("### ERROR: OsiCpxSolverInterface::getBInvRow(): Unable to get senses for row %d\n", Arow);
3675 exit(1);
3676 }
3677 if (sense == 'G') { // slack has coeff -1 in Cplex; thus row in binv must be flipped
3678 for (int j = 0; j < nrow; j++) {
3679 z[j] = -z[j];
3680 }
3681 }
3682 }
3683 delete[] ind_bas;
3684 /*
3685 char sense;
3686 status = CPXgetsense(env_, lp, &sense, row, row);
3687 if(status) {
3688 printf("### ERROR: OsiCpxSolverInterface::getBInvRow(): Unable to get senses for row %d\n", row);
3689 exit(1);
3690 }
3691
3692 if(sense == 'G') {
3693 int *ind_bas = new int[nrow];
3694 getBasics(ind_bas);
3695
3696 for(int i=0; i<nrow; i++) {
3697 int ind_piv = ind_bas[i] - ncol;
3698 if(ind_piv == row) {
3699 // slack has coeff -1 in Cplex and is basic;
3700 // row of invB must be flipped
3701
3702 for(int j=0; j<nrow; j++) {
3703 z[j] = -z[j];
3704 }
3705 break;
3706 }
3707 }
3708 delete[] ind_bas;
3709 }
3710 */
3711 } /* getBInvRow */
3712
3713 /**********************************************************************/
3714 void OsiCpxSolverInterface::getBInvACol(int col, double *vec) const
3715 {
3716
3717 CPXLPptr lp = getMutableLpPtr();
3718
3719 int status = CPXbinvacol(env_, lp, col, vec);
3720 if (status) {
3721 printf("### ERROR: OsiCpxSolverInterface::getBInvACol(): Unable to get column %d of the tableau\n", col);
3722 exit(1);
3723 }
3724
3725 int nrow = getNumRows();
3726 int ncol = getNumCols();
3727 char *sense = new char[nrow];
3728 status = CPXgetsense(env_, lp, sense, 0, nrow - 1);
3729 if (status) {
3730 printf("### ERROR: OsiCpxSolverInterface::getBInvACol(): Unable to get senses for the rows\n");
3731 exit(1);
3732 }
3733 int *ind_bas = new int[nrow];
3734 getBasics(ind_bas);
3735
3736 for (int i = 0; i < nrow; i++)
3737 if (ind_bas[i] > ncol && sense[ind_bas[i] - ncol] == 'G')
3738 vec[i] = -vec[i]; // slack for row is basic; whole row must be flipped
3739
3740 delete[] sense;
3741 delete[] ind_bas;
3742 } /* getBInvACol */
3743
3744 /**********************************************************************/
3745 void OsiCpxSolverInterface::getBInvCol(int col, double *vec) const
3746 {
3747
3748 CPXLPptr lp = getMutableLpPtr();
3749
3750 int status = CPXbinvcol(env_, lp, col, vec);
3751 if (status) {
3752 printf("### ERROR: OsiCpxSolverInterface::getBInvCol(): Unable to get column %d of the basis inverse\n", col);
3753 exit(1);
3754 }
3755
3756 int nrow = getNumRows();
3757 int ncol = getNumCols();
3758 char *sense = new char[nrow];
3759 status = CPXgetsense(env_, lp, sense, 0, nrow - 1);
3760 if (status) {
3761 printf("### ERROR: OsiCpxSolverInterface::getBInvCol(): Unable to get senses for the rows\n");
3762 exit(1);
3763 }
3764 int *ind_bas = new int[nrow];
3765 getBasics(ind_bas);
3766
3767 for (int i = 0; i < nrow; i++)
3768 if (ind_bas[i] > ncol && sense[ind_bas[i] - ncol] == 'G')
3769 vec[i] = -vec[i]; // slack for row i is basic; whole row must be flipped
3770
3771 delete[] sense;
3772 delete[] ind_bas;
3773 } /* getBInvCol */
3774
3775 /**********************************************************************/
3776 void OsiCpxSolverInterface::getBasics(int *index) const
3777 {
3778 int ncol = getNumCols();
3779 int nrow = getNumRows();
3780
3781 CPXLPptr lp = getMutableLpPtr();
3782
3783 double *cplex_trash = new double[nrow];
3784 int status = CPXgetbhead(env_, lp, index, cplex_trash);
3785 if (status) {
3786 printf("### ERROR: OsiCpxSolverInterface::getBasics(): Unable to get basis head\n");
3787 exit(1);
3788 }
3789
3790 for (int i = 0; i < nrow; i++) {
3791 if (index[i] < 0) {
3792 index[i] = ncol - 1 - index[i];
3793 }
3794 }
3795 delete[] cplex_trash;
3796 } /* getBasics */
3797
3798 #else /* not CPX_VERSION >= 900 */
3799
3800 /**********************************************************************/
3801 /* Returns 1 if can just do getBInv etc
3802 2 if has all OsiSimplex methods
3803 and 0 if it has none */
3804 int OsiCpxSolverInterface::canDoSimplexInterface() const
3805 {
3806 return 0;
3807 }
3808
3809 /**********************************************************************/
3810 bool OsiCpxSolverInterface::basisIsAvailable() const
3811 {
3812 printf("### ERROR: OsiCpxSolverInterface::basisIsAvailable(): Cplex version lower than 9.0\n");
3813 exit(1);
3814 return false;
3815 }
3816
3817 /**********************************************************************/
3818 void OsiCpxSolverInterface::getBasisStatus(int *cstat, int *rstat) const
3819 {
3820 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): Cplex version lower than 9.0\n");
3821 exit(1);
3822 }
3823
3824 /**********************************************************************/
3825 void OsiCpxSolverInterface::getBInvARow(int row, double *z, double *slack)
3826 const
3827 {
3828 printf("### ERROR: OsiCpxSolverInterface::getBInvARow(): Cplex version lower than 9.0\n");
3829 exit(1);
3830 } /* getBInvARow */
3831
3832 /**********************************************************************/
3833 void OsiCpxSolverInterface::getBInvRow(int row, double *z) const
3834 {
3835 printf("### ERROR: OsiCpxSolverInterface::getBInvRow(): Cplex version lower than 9.0\n");
3836 exit(1);
3837 } /* getBInvRow */
3838
3839 /**********************************************************************/
3840 void OsiCpxSolverInterface::getBInvACol(int col, double *vec) const
3841 {
3842 printf("### ERROR: OsiCpxSolverInterface::getBInvACol(): Cplex version lower than 9.0\n");
3843 exit(1);
3844 } /* getBInvACol */
3845
3846 /**********************************************************************/
3847 void OsiCpxSolverInterface::getBInvCol(int col, double *vec) const
3848 {
3849 printf("### ERROR: OsiCpxSolverInterface::getBInvCol(): Cplex version lower than 9.0\n");
3850 exit(1);
3851 } /* getBInvCol */
3852
3853 /**********************************************************************/
3854 void OsiCpxSolverInterface::getBasics(int *index) const
3855 {
3856 printf("### ERROR: OsiCpxSolverInterface::getBasics(): Cplex version lower than 9.0\n");
3857 exit(1);
3858 } /* getBasics */
3859 #endif /* not CPX_VERSION >= 900 */
3860
3861 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
3862 */