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 reduced cost exists, then pass into CPLEX
2092 if (redcost_ != NULL) {
2093 int err = CPXcopystart(env_, getMutableLpPtr(), NULL, NULL, NULL, NULL,
2094 const_cast< double * >(redcost_),
2095 const_cast< double * >(rowsol_));
2096 checkCPXerror(err, "CPXcopystart", "setRowPrice");
2097 }
2098 }
2099 }
2100
2101 //#############################################################################
2102 // Problem modifying methods (matrix)
2103 //#############################################################################
2104 void OsiCpxSolverInterface::addCol(const CoinPackedVectorBase &vec,
2105 const double collb, const double colub,
2106 const double obj)
2107 {
2108 debugMessage("OsiCpxSolverInterface::addCol(%p, %g, %g, %g)\n", (void *)&vec, collb, colub, obj);
2109
2110 int nc = getNumCols();
2111 assert(coltypesize_ >= nc);
2112
2113 resizeColType(nc + 1);
2114 coltype_[nc] = 'C';
2115
2116 int err;
2117 int cmatbeg[2] = { 0, vec.getNumElements() };
2118
2119 err = CPXaddcols(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ROW),
2120 1, vec.getNumElements(), const_cast< double * >(&obj),
2121 cmatbeg,
2122 const_cast< int * >(vec.getIndices()),
2123 const_cast< double * >(vec.getElements()),
2124 const_cast< double * >(&collb),
2125 const_cast< double * >(&colub), NULL);
2126 checkCPXerror(err, "CPXaddcols", "addCol");
2127 }
2128 //-----------------------------------------------------------------------------
2129 void OsiCpxSolverInterface::addCols(const int numcols,
2130 const CoinPackedVectorBase *const *cols,
2131 const double *collb, const double *colub,
2132 const double *obj)
2133 {
2134 debugMessage("OsiCpxSolverInterface::addCols(%d, %p, %p, %p, %p)\n", numcols, (void *)cols, (void *)collb, (void *)colub, (void *)obj);
2135
2136 int nc = getNumCols();
2137 assert(coltypesize_ >= nc);
2138
2139 resizeColType(nc + numcols);
2140 CoinFillN(&coltype_[nc], numcols, 'C');
2141
2142 int i;
2143 int nz = 0;
2144 for (i = 0; i < numcols; ++i)
2145 nz += cols[i]->getNumElements();
2146
2147 int *index = new int[nz];
2148 double *elem = new double[nz];
2149 int *start = new int[numcols + 1];
2150
2151 nz = 0;
2152 start[0] = 0;
2153 for (i = 0; i < numcols; ++i) {
2154 const CoinPackedVectorBase *col = cols[i];
2155 const int len = col->getNumElements();
2156 CoinDisjointCopyN(col->getIndices(), len, index + nz);
2157 CoinDisjointCopyN(col->getElements(), len, elem + nz);
2158 nz += len;
2159 start[i + 1] = nz;
2160 }
2161 int err = CPXaddcols(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ROW),
2162 numcols, nz, const_cast< double * >(obj),
2163 start, index, elem,
2164 const_cast< double * >(collb),
2165 const_cast< double * >(colub), NULL);
2166 checkCPXerror(err, "CPXaddcols", "addCols");
2167
2168 delete[] start;
2169 delete[] elem;
2170 delete[] index;
2171
2172 // int i;
2173 // for( i = 0; i < numcols; ++i )
2174 // addCol( *(cols[i]), collb[i], colub[i], obj[i] );
2175 }
2176 //-----------------------------------------------------------------------------
2177 void OsiCpxSolverInterface::deleteCols(const int num, const int *columnIndices)
2178 {
2179 debugMessage("OsiCpxSolverInterface::deleteCols(%d, %p)\n", num, (void *)columnIndices);
2180
2181 int ncols = getNumCols();
2182 int *delstat = new int[ncols];
2183 int i, err;
2184
2185 CoinFillN(delstat, ncols, 0);
2186 for (i = 0; i < num; ++i)
2187 delstat[columnIndices[i]] = 1;
2188 err = CPXdelsetcols(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_ROW), delstat);
2189 checkCPXerror(err, "CPXdelsetcols", "deleteCols");
2190
2191 for (i = 0; i < ncols; ++i) {
2192 assert(delstat[i] <= i);
2193 if (delstat[i] != -1)
2194 coltype_[delstat[i]] = coltype_[i];
2195 }
2196
2197 delete[] delstat;
2198
2199 //---
2200 //--- MVG: took from OsiClp for updating names
2201 //---
2202 int nameDiscipline;
2203 getIntParam(OsiNameDiscipline, nameDiscipline);
2204 if (num && nameDiscipline) {
2205 // Very clumsy (and inefficient) - need to sort and then go backwards in ? chunks
2206 int *indices = CoinCopyOfArray(columnIndices, num);
2207 std::sort(indices, indices + num);
2208 int num2 = num;
2209 while (num2) {
2210 int next = indices[num2 - 1];
2211 int firstDelete = num2 - 1;
2212 int i;
2213 for (i = num2 - 2; i >= 0; --i) {
2214 if (indices[i] + 1 == next) {
2215 --next;
2216 firstDelete = i;
2217 } else {
2218 break;
2219 }
2220 }
2221 OsiSolverInterface::deleteColNames(indices[firstDelete], num2 - firstDelete);
2222 num2 = firstDelete;
2223 assert(num2 >= 0);
2224 }
2225 delete[] indices;
2226 }
2227 }
2228
2229 //-----------------------------------------------------------------------------
2230 void OsiCpxSolverInterface::addRow(const CoinPackedVectorBase &vec,
2231 const double rowlb, const double rowub)
2232 {
2233 debugMessage("OsiCpxSolverInterface::addRow(%p, %g, %g)\n", (void *)&vec, rowlb, rowub);
2234
2235 char sense;
2236 double rhs, range;
2237
2238 convertBoundToSense(rowlb, rowub, sense, rhs, range);
2239 addRow(vec, sense, rhs, range);
2240 }
2241 //-----------------------------------------------------------------------------
2242 void OsiCpxSolverInterface::addRow(const CoinPackedVectorBase &vec,
2243 const char rowsen, const double rowrhs,
2244 const double rowrng)
2245 {
2246 debugMessage("OsiCpxSolverInterface::addRow(%p, %c, %g, %g)\n", (void *)&vec, rowsen, rowrhs, rowrng);
2247
2248 int err;
2249 int rmatbeg = 0;
2250 double rhs;
2251 double range;
2252 char sense = rowsen;
2253
2254 switch (rowsen) {
2255 case 'R':
2256 //assert(rowrng >= 0.0);
2257 rhs = rowrhs - rowrng;
2258 range = rowrng;
2259 break;
2260 case 'N':
2261 sense = 'R';
2262 rhs = -getInfinity();
2263 range = 2 * getInfinity();
2264 break;
2265 default:
2266 rhs = rowrhs;
2267 range = 0.0;
2268 }
2269
2270 err = CPXaddrows(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN), 0, 1, vec.getNumElements(),
2271 &rhs,
2272 &sense,
2273 &rmatbeg,
2274 const_cast< int * >(vec.getIndices()),
2275 const_cast< double * >(vec.getElements()),
2276 NULL, NULL);
2277 checkCPXerror(err, "CPXaddrows", "addRow");
2278 if (sense == 'R') {
2279 int row = getNumRows() - 1;
2280 err = CPXchgrngval(env_, getLpPtr(OsiCpxSolverInterface::FREECACHED_ROW), 1, &row, &range);
2281 checkCPXerror(err, "CPXchgrngval", "addRow");
2282 }
2283 }
2284 //-----------------------------------------------------------------------------
2285 void OsiCpxSolverInterface::addRows(const int numrows,
2286 const CoinPackedVectorBase *const *rows,
2287 const double *rowlb, const double *rowub)
2288 {
2289 debugMessage("OsiCpxSolverInterface::addRows(%d, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowlb, (void *)rowub);
2290
2291 int i;
2292
2293 for (i = 0; i < numrows; ++i)
2294 addRow(*(rows[i]), rowlb[i], rowub[i]);
2295 }
2296 //-----------------------------------------------------------------------------
2297 void OsiCpxSolverInterface::addRows(const int numrows,
2298 const CoinPackedVectorBase *const *rows,
2299 const char *rowsen, const double *rowrhs,
2300 const double *rowrng)
2301 {
2302 debugMessage("OsiCpxSolverInterface::addRows(%d, %p, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
2303
2304 int i;
2305
2306 for (i = 0; i < numrows; ++i)
2307 addRow(*(rows[i]), rowsen[i], rowrhs[i], rowrng[i]);
2308 }
2309 //-----------------------------------------------------------------------------
2310 void OsiCpxSolverInterface::deleteRows(const int num, const int *rowIndices)
2311 {
2312 debugMessage("OsiCpxSolverInterface::deleteRows(%d, %p)\n", num, (void *)rowIndices);
2313
2314 int nrows = getNumRows();
2315 int *delstat = new int[nrows];
2316 int i, err;
2317
2318 CoinFillN(delstat, nrows, 0);
2319 for (i = 0; i < num; ++i)
2320 delstat[rowIndices[i]] = 1;
2321 err = CPXdelsetrows(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN), delstat);
2322 checkCPXerror(err, "CPXdelsetrows", "deleteRows");
2323 delete[] delstat;
2324
2325 //---
2326 //--- SV: took from OsiClp for updating names
2327 //---
2328 int nameDiscipline;
2329 getIntParam(OsiNameDiscipline, nameDiscipline);
2330 if (num && nameDiscipline) {
2331 // Very clumsy (and inefficient) - need to sort and then go backwards in ? chunks
2332 int *indices = CoinCopyOfArray(rowIndices, num);
2333 std::sort(indices, indices + num);
2334 int num2 = num;
2335 while (num2) {
2336 int next = indices[num2 - 1];
2337 int firstDelete = num2 - 1;
2338 int i;
2339 for (i = num2 - 2; i >= 0; --i) {
2340 if (indices[i] + 1 == next) {
2341 --next;
2342 firstDelete = i;
2343 } else {
2344 break;
2345 }
2346 }
2347 OsiSolverInterface::deleteRowNames(indices[firstDelete], num2 - firstDelete);
2348 num2 = firstDelete;
2349 assert(num2 >= 0);
2350 }
2351 delete[] indices;
2352 }
2353 }
2354
2355 //#############################################################################
2356 // Methods to input a problem
2357 //#############################################################################
2358
2359 void OsiCpxSolverInterface::loadProblem(const CoinPackedMatrix &matrix,
2360 const double *collb, const double *colub,
2361 const double *obj,
2362 const double *rowlb, const double *rowub)
2363 {
2364 debugMessage("OsiCpxSolverInterface::loadProblem(1)(%p, %p, %p, %p, %p, %p)\n", (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowlb, (void *)rowub);
2365
2366 const double inf = getInfinity();
2367
2368 int nrows = matrix.getNumRows();
2369 char *rowSense = new char[nrows];
2370 double *rowRhs = new double[nrows];
2371 double *rowRange = new double[nrows];
2372
2373 int i;
2374 for (i = nrows - 1; i >= 0; --i) {
2375 const double lower = rowlb ? rowlb[i] : -inf;
2376 const double upper = rowub ? rowub[i] : inf;
2377 convertBoundToSense(lower, upper, rowSense[i], rowRhs[i], rowRange[i]);
2378 }
2379
2380 loadProblem(matrix, collb, colub, obj, rowSense, rowRhs, rowRange);
2381 delete[] rowSense;
2382 delete[] rowRhs;
2383 delete[] rowRange;
2384 }
2385
2386 //-----------------------------------------------------------------------------
2387
2388 void OsiCpxSolverInterface::assignProblem(CoinPackedMatrix *&matrix,
2389 double *&collb, double *&colub,
2390 double *&obj,
2391 double *&rowlb, double *&rowub)
2392 {
2393 debugMessage("OsiCpxSolverInterface::assignProblem()\n");
2394
2395 loadProblem(*matrix, collb, colub, obj, rowlb, rowub);
2396 delete matrix;
2397 matrix = 0;
2398 delete[] collb;
2399 collb = 0;
2400 delete[] colub;
2401 colub = 0;
2402 delete[] obj;
2403 obj = 0;
2404 delete[] rowlb;
2405 rowlb = 0;
2406 delete[] rowub;
2407 rowub = 0;
2408 }
2409
2410 //-----------------------------------------------------------------------------
2411
2412 void OsiCpxSolverInterface::loadProblem(const CoinPackedMatrix &matrix,
2413 const double *collb, const double *colub,
2414 const double *obj,
2415 const char *rowsen, const double *rowrhs,
2416 const double *rowrng)
2417 {
2418 debugMessage("OsiCpxSolverInterface::loadProblem(2)(%p, %p, %p, %p, %p, %p, %p)\n",
2419 (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
2420
2421 char *lclRowsen = NULL;
2422 double *lclRowrhs = NULL;
2423
2424 int nc = matrix.getNumCols();
2425 int nr = matrix.getNumRows();
2426
2427 if (nr == 0 && nc == 0) { // empty LP
2428 if (lp_ != NULL) { // kill old LP
2429 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2430 int err = CPXfreeprob(env_, &lp_);
2431 checkCPXerror(err, "CPXfreeprob", "loadProblem");
2432 lp_ = NULL;
2433 freeAllMemory();
2434 CPXchgobjsen(env_, getLpPtr(), objDirection);
2435 }
2436 return;
2437 }
2438
2439 if (nr == 0) {
2440 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2441
2442 if (lp_ != NULL) { // kill old LP
2443 int err = CPXfreeprob(env_, &lp_);
2444 checkCPXerror(err, "CPXfreeprob", "loadProblem");
2445 lp_ = NULL;
2446 freeAllMemory();
2447 }
2448
2449 // getLpPtr() call will create new LP
2450 int err = CPXnewcols(env_, getLpPtr(), nc, obj, collb, colub, NULL, NULL);
2451 checkCPXerror(err, "CPXcopylp", "loadProblem");
2452
2453 CPXchgobjsen(env_, getLpPtr(), objDirection);
2454
2455 return;
2456 }
2457
2458 if (rowsen == NULL) {
2459 lclRowsen = new char[nr];
2460 CoinFillN(lclRowsen, nr, 'G');
2461 rowsen = lclRowsen;
2462 }
2463 if (rowrhs == NULL) {
2464 lclRowrhs = new double[nr];
2465 CoinFillN(lclRowrhs, nr, 0.0);
2466 rowrhs = lclRowrhs;
2467 }
2468
2469 int i;
2470
2471 // Set column values to defaults if NULL pointer passed
2472 double *clb;
2473 double *cub;
2474 double *ob;
2475 double *rr = NULL;
2476 double *rhs;
2477 if (collb != NULL)
2478 clb = const_cast< double * >(collb);
2479 else {
2480 clb = new double[nc];
2481 CoinFillN(clb, nc, 0.0);
2482 }
2483 if (colub != NULL)
2484 cub = const_cast< double * >(colub);
2485 else {
2486 cub = new double[nc];
2487 CoinFillN(cub, nc, getInfinity());
2488 }
2489 if (obj != NULL)
2490 ob = const_cast< double * >(obj);
2491 else {
2492 ob = new double[nc];
2493 CoinFillN(ob, nc, 0.0);
2494 }
2495 if (rowrng != NULL) {
2496 rhs = new double[nr];
2497 rr = new double[nr];
2498 for (i = 0; i < nr; i++) {
2499 if (rowsen[i] == 'R') {
2500 if (rowrng[i] >= 0) {
2501 rhs[i] = rowrhs[i] - rowrng[i];
2502 rr[i] = rowrng[i];
2503 } else {
2504 rhs[i] = rowrhs[i];
2505 rr[i] = -rowrng[i];
2506 }
2507 } else {
2508 rhs[i] = rowrhs[i];
2509 rr[i] = 0.0;
2510 }
2511 }
2512 } else
2513 rhs = const_cast< double * >(rowrhs);
2514
2515 bool freeMatrixRequired = false;
2516 CoinPackedMatrix *m = NULL;
2517 if (!matrix.isColOrdered()) {
2518 m = new CoinPackedMatrix();
2519 m->reverseOrderedCopyOf(matrix);
2520 freeMatrixRequired = true;
2521 } else
2522 m = const_cast< CoinPackedMatrix * >(&matrix);
2523
2524 assert(nc == m->getNumCols());
2525 assert(nr == m->getNumRows());
2526 assert(m->isColOrdered());
2527
2528 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2529 int err = CPXcopylp(env_, getLpPtr(),
2530 nc, nr,
2531 // Leave ObjSense alone(set to current value).
2532 objDirection,
2533 ob,
2534 rhs,
2535 const_cast< char * >(rowsen),
2536 const_cast< int * >(m->getVectorStarts()),
2537 const_cast< int * >(m->getVectorLengths()),
2538 const_cast< int * >(m->getIndices()),
2539 const_cast< double * >(m->getElements()),
2540 const_cast< double * >(clb),
2541 const_cast< double * >(cub),
2542 rr);
2543 checkCPXerror(err, "CPXcopylp", "loadProblem");
2544
2545 if (collb == NULL)
2546 delete[] clb;
2547 if (colub == NULL)
2548 delete[] cub;
2549 if (obj == NULL)
2550 delete[] ob;
2551 if (rowrng != NULL) {
2552 delete[] rr;
2553 delete[] rhs;
2554 }
2555
2556 if (freeMatrixRequired)
2557 delete m;
2558
2559 resizeColType(nc);
2560 CoinFillN(coltype_, nc, 'C');
2561
2562 if (lclRowsen != NULL)
2563 delete[] lclRowsen;
2564 if (lclRowrhs != NULL)
2565 delete[] lclRowrhs;
2566 }
2567
2568 //-----------------------------------------------------------------------------
2569
2570 void OsiCpxSolverInterface::assignProblem(CoinPackedMatrix *&matrix,
2571 double *&collb, double *&colub,
2572 double *&obj,
2573 char *&rowsen, double *&rowrhs,
2574 double *&rowrng)
2575 {
2576 debugMessage("OsiCpxSolverInterface::assignProblem()\n");
2577
2578 loadProblem(*matrix, collb, colub, obj, rowsen, rowrhs, rowrng);
2579 delete matrix;
2580 matrix = 0;
2581 delete[] collb;
2582 collb = 0;
2583 delete[] colub;
2584 colub = 0;
2585 delete[] obj;
2586 obj = 0;
2587 delete[] rowsen;
2588 rowsen = 0;
2589 delete[] rowrhs;
2590 rowrhs = 0;
2591 delete[] rowrng;
2592 rowrng = 0;
2593 }
2594
2595 //-----------------------------------------------------------------------------
2596
2597 void OsiCpxSolverInterface::loadProblem(const int numcols, const int numrows,
2598 const int *start, const int *index,
2599 const double *value,
2600 const double *collb, const double *colub,
2601 const double *obj,
2602 const double *rowlb, const double *rowub)
2603 {
2604 debugMessage("OsiCpxSolverInterface::loadProblem(3)()\n");
2605
2606 const double inf = getInfinity();
2607
2608 char *rowSense = new char[numrows];
2609 double *rowRhs = new double[numrows];
2610 double *rowRange = new double[numrows];
2611
2612 for (int i = numrows - 1; i >= 0; --i) {
2613 const double lower = rowlb ? rowlb[i] : -inf;
2614 const double upper = rowub ? rowub[i] : inf;
2615 convertBoundToSense(lower, upper, rowSense[i], rowRhs[i], rowRange[i]);
2616 }
2617
2618 loadProblem(numcols, numrows, start, index, value, collb, colub, obj,
2619 rowSense, rowRhs, rowRange);
2620 delete[] rowSense;
2621 delete[] rowRhs;
2622 delete[] rowRange;
2623 }
2624
2625 //-----------------------------------------------------------------------------
2626
2627 void OsiCpxSolverInterface::loadProblem(const int numcols, const int numrows,
2628 const int *start, const int *index,
2629 const double *value,
2630 const double *collb, const double *colub,
2631 const double *obj,
2632 const char *rowsen, const double *rowrhs,
2633 const double *rowrng)
2634 {
2635 debugMessage("OsiCpxSolverInterface::loadProblem(4)(%d, %d, %p, %p, %p, %p, %p, %p, %p, %p, %p)\n",
2636 numcols, numrows, (void *)start, (void *)index, (void *)value, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
2637
2638 const int nc = numcols;
2639 const int nr = numrows;
2640
2641 if (nr == 0 && nc == 0) {
2642 // empty LP
2643 if (lp_ != NULL) { // kill old LP
2644 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2645 int err = CPXfreeprob(env_, &lp_);
2646 checkCPXerror(err, "CPXfreeprob", "loadProblem");
2647 lp_ = NULL;
2648 freeAllMemory();
2649 CPXchgobjsen(env_, getLpPtr(), objDirection);
2650 }
2651 return;
2652 }
2653
2654 if (nr == 0) {
2655 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2656
2657 if (lp_ != NULL) { // kill old LP
2658 int err = CPXfreeprob(env_, &lp_);
2659 checkCPXerror(err, "CPXfreeprob", "loadProblem");
2660 lp_ = NULL;
2661 freeAllMemory();
2662 }
2663
2664 // getLpPtr() call will create new LP
2665 int err = CPXnewcols(env_, getLpPtr(), nc, obj, collb, colub, NULL, NULL);
2666 checkCPXerror(err, "CPXcopylp", "loadProblem");
2667
2668 CPXchgobjsen(env_, getLpPtr(), objDirection);
2669
2670 return;
2671 }
2672
2673 char *lclRowsen = NULL;
2674 double *lclRowrhs = NULL;
2675
2676 if (rowsen == NULL) {
2677 lclRowsen = new char[nr];
2678 CoinFillN(lclRowsen, nr, 'G');
2679 rowsen = lclRowsen;
2680 }
2681 if (rowrhs == NULL) {
2682 lclRowrhs = new double[nr];
2683 CoinFillN(lclRowrhs, nr, 0.0);
2684 }
2685
2686 int i;
2687
2688 // Set column values to defaults if NULL pointer passed
2689 int *len = new int[nc];
2690 double *clb = new double[nc];
2691 double *cub = new double[nc];
2692 double *ob = new double[nc];
2693 double *rr = new double[nr];
2694 double *rhs = new double[nr];
2695 char *sen = new char[nr];
2696
2697 for (i = 0; i < nc; ++i) {
2698 len[i] = start[i + 1] - start[i];
2699 }
2700
2701 if (collb != NULL)
2702 CoinDisjointCopyN(collb, nc, clb);
2703 else
2704 CoinFillN(clb, nc, 0.0);
2705
2706 if (colub != NULL)
2707 CoinDisjointCopyN(colub, nc, cub);
2708 else
2709 CoinFillN(cub, nc, getInfinity());
2710
2711 if (obj != NULL)
2712 CoinDisjointCopyN(obj, nc, ob);
2713 else
2714 CoinFillN(ob, nc, 0.0);
2715
2716 if (rowrng != NULL) {
2717 for (i = 0; i < nr; i++) {
2718 if (rowsen[i] == 'R') {
2719 if (rowrng[i] >= 0) {
2720 rhs[i] = rowrhs[i] - rowrng[i];
2721 rr[i] = rowrng[i];
2722 } else {
2723 rhs[i] = rowrhs[i];
2724 rr[i] = -rowrng[i];
2725 }
2726 } else {
2727 rhs[i] = rowrhs[i];
2728 rr[i] = 0.0;
2729 }
2730 }
2731 } else {
2732 CoinDisjointCopyN(rowrhs, nr, rhs);
2733 }
2734
2735 CoinDisjointCopyN(rowsen, nr, sen);
2736
2737 int objDirection = CPXgetobjsen(env_, getMutableLpPtr());
2738
2739 int err = CPXcopylp(env_, getLpPtr(),
2740 nc, nr,
2741 // Leave ObjSense alone(set to current value).
2742 objDirection, ob, rhs, sen,
2743 const_cast< int * >(start),
2744 len, const_cast< int * >(index),
2745 const_cast< double * >(value),
2746 clb, cub, rr);
2747
2748 checkCPXerror(err, "CPXcopylp", "loadProblem");
2749
2750 delete[] len;
2751 delete[] clb;
2752 delete[] cub;
2753 delete[] ob;
2754 delete[] rr;
2755 delete[] rhs;
2756 delete[] sen;
2757
2758 if (lclRowsen != NULL)
2759 delete[] lclRowsen;
2760 if (lclRowrhs != NULL)
2761 delete[] lclRowrhs;
2762
2763 resizeColType(nc);
2764 CoinFillN(coltype_, nc, 'C');
2765 }
2766
2767 //-----------------------------------------------------------------------------
2768 // Read mps files
2769 //-----------------------------------------------------------------------------
2770 int OsiCpxSolverInterface::readMps(const char *filename,
2771 const char *extension)
2772 {
2773 debugMessage("OsiCpxSolverInterface::readMps(%s, %s)\n", filename, extension);
2774
2775 #if 0
2776 std::string f(filename);
2777 std::string e(extension);
2778 std::string fullname = f + "." + e;
2779 int err = CPXreadcopyprob( env_, getLpPtr(), const_cast<char*>( fullname.c_str() ), NULL );
2780 checkCPXerror( err, "CPXreadcopyprob", "readMps" );
2781 #endif
2782 // just call base class method
2783 return OsiSolverInterface::readMps(filename, extension);
2784 }
2785
2786 //-----------------------------------------------------------------------------
2787 // Write mps files
2788 //-----------------------------------------------------------------------------
2789 void OsiCpxSolverInterface::writeMps(const char *filename,
2790 const char *extension,
2791 double objSense) const
2792 {
2793 debugMessage("OsiCpxSolverInterface::writeMps(%s, %s, %g)\n", filename, extension, objSense);
2794
2795 // *FIXME* : this will not output ctype information to the MPS file
2796 char filetype[4] = "MPS";
2797 std::string f(filename);
2798 std::string e(extension);
2799 std::string fullname = f + "." + e;
2800 int err = CPXwriteprob(env_, getMutableLpPtr(), const_cast< char * >(fullname.c_str()), filetype);
2801 checkCPXerror(err, "CPXwriteprob", "writeMps");
2802 }
2803
2804 void OsiCpxSolverInterface::passInMessageHandler(CoinMessageHandler *handler)
2805 {
2806 int err;
2807 CPXCHANNELptr cpxresults;
2808 CPXCHANNELptr cpxwarning;
2809 CPXCHANNELptr cpxerror;
2810 CPXCHANNELptr cpxlog;
2811 err = CPXgetchannels(env_, &cpxresults, &cpxwarning, &cpxerror, &cpxlog);
2812 checkCPXerror(err, "CPXgetchannels", "gutsOfConstructor");
2813
2814 err = CPXdelfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
2815 checkCPXerror(err, "CPXdelfuncdest", "gutsOfConstructor");
2816 err = CPXdelfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
2817 checkCPXerror(err, "CPXdelfuncdest", "gutsOfConstructor");
2818 err = CPXdelfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
2819 checkCPXerror(err, "CPXdelfuncdest", "gutsOfConstructor");
2820 err = CPXdelfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
2821 checkCPXerror(err, "CPXdelfuncdest", "gutsOfConstructor");
2822
2823 OsiSolverInterface::passInMessageHandler(handler);
2824
2825 err = CPXaddfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
2826 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
2827 err = CPXaddfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
2828 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
2829 err = CPXaddfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
2830 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
2831 err = CPXaddfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
2832 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
2833 }
2834
2835 //#############################################################################
2836 // CPX specific public interfaces
2837 //#############################################################################
2838
2839 CPXENVptr OsiCpxSolverInterface::getEnvironmentPtr()
2840 {
2841 assert(env_ != NULL);
2842 return env_;
2843 }
2844
2845 CPXLPptr OsiCpxSolverInterface::getLpPtr(int keepCached)
2846 {
2847 freeCachedData(keepCached);
2848 return getMutableLpPtr();
2849 }
2850
2851 //-----------------------------------------------------------------------------
2852
2853 const char *OsiCpxSolverInterface::getCtype() const
2854 {
2855 debugMessage("OsiCpxSolverInterface::getCtype()\n");
2856
2857 return coltype_;
2858 }
2859
2860 //#############################################################################
2861 // Constructors, destructors clone and assignment
2862 //#############################################################################
2863
2864 //-------------------------------------------------------------------
2865 // Default Constructor
2866 //-------------------------------------------------------------------
2867 OsiCpxSolverInterface::OsiCpxSolverInterface()
2868 : OsiSolverInterface()
2869 , env_(NULL)
2870 , lp_(NULL)
2871 , hotStartCStat_(NULL)
2872 , hotStartCStatSize_(0)
2873 , hotStartRStat_(NULL)
2874 , hotStartRStatSize_(0)
2875 , hotStartMaxIteration_(1000000)
2876 , // ??? default iteration limit for strong branching is large
2877 obj_(NULL)
2878 , collower_(NULL)
2879 , colupper_(NULL)
2880 , rowsense_(NULL)
2881 , rhs_(NULL)
2882 , rowrange_(NULL)
2883 , rowlower_(NULL)
2884 , rowupper_(NULL)
2885 , colsol_(NULL)
2886 , rowsol_(NULL)
2887 , redcost_(NULL)
2888 , rowact_(NULL)
2889 , matrixByRow_(NULL)
2890 , matrixByCol_(NULL)
2891 , coltype_(NULL)
2892 , coltypesize_(0)
2893 , probtypemip_(false)
2894 , domipstart(false)
2895 , disableadvbasis(false)
2896 {
2897 debugMessage("OsiCpxSolverInterface::OsiCpxSolverInterface()\n");
2898
2899 gutsOfConstructor();
2900 }
2901
2902 //----------------------------------------------------------------
2903 // Clone
2904 //----------------------------------------------------------------
2905 OsiSolverInterface *OsiCpxSolverInterface::clone(bool copyData) const
2906 {
2907 debugMessage("OsiCpxSolverInterface::clone(%d)\n", copyData);
2908
2909 return (new OsiCpxSolverInterface(*this));
2910 }
2911
2912 //-------------------------------------------------------------------
2913 // Copy constructor
2914 //-------------------------------------------------------------------
2915 OsiCpxSolverInterface::OsiCpxSolverInterface(const OsiCpxSolverInterface &source)
2916 : OsiSolverInterface(source)
2917 , env_(NULL)
2918 , lp_(NULL)
2919 , hotStartCStat_(NULL)
2920 , hotStartCStatSize_(0)
2921 , hotStartRStat_(NULL)
2922 , hotStartRStatSize_(0)
2923 , hotStartMaxIteration_(source.hotStartMaxIteration_)
2924 , obj_(NULL)
2925 , collower_(NULL)
2926 , colupper_(NULL)
2927 , rowsense_(NULL)
2928 , rhs_(NULL)
2929 , rowrange_(NULL)
2930 , rowlower_(NULL)
2931 , rowupper_(NULL)
2932 , colsol_(NULL)
2933 , rowsol_(NULL)
2934 , redcost_(NULL)
2935 , rowact_(NULL)
2936 , matrixByRow_(NULL)
2937 , matrixByCol_(NULL)
2938 , coltype_(NULL)
2939 , coltypesize_(0)
2940 , probtypemip_(false)
2941 , domipstart(false)
2942 , disableadvbasis(false)
2943 {
2944 debugMessage("OsiCpxSolverInterface::OsiCpxSolverInterface(%p)\n", (void *)&source);
2945
2946 gutsOfConstructor();
2947 gutsOfCopy(source);
2948 }
2949
2950 //-------------------------------------------------------------------
2951 // Destructor
2952 //-------------------------------------------------------------------
2953 OsiCpxSolverInterface::~OsiCpxSolverInterface()
2954 {
2955 debugMessage("OsiCpxSolverInterface::~OsiCpxSolverInterface()\n");
2956
2957 gutsOfDestructor();
2958 }
2959
2960 //----------------------------------------------------------------
2961 // Assignment operator
2962 //-------------------------------------------------------------------
2963 OsiCpxSolverInterface &OsiCpxSolverInterface::operator=(const OsiCpxSolverInterface &rhs)
2964 {
2965 debugMessage("OsiCpxSolverInterface::operator=(%p)\n", (void *)&rhs);
2966
2967 if (this != &rhs) {
2968 OsiSolverInterface::operator=(rhs);
2969 gutsOfDestructor();
2970 gutsOfConstructor();
2971 if (rhs.lp_ != NULL)
2972 gutsOfCopy(rhs);
2973 }
2974 return *this;
2975 }
2976
2977 //#############################################################################
2978 // Applying cuts
2979 //#############################################################################
2980
2981 void OsiCpxSolverInterface::applyColCut(const OsiColCut &cc)
2982 {
2983 debugMessage("OsiCpxSolverInterface::applyColCut(%p)\n", (void *)&cc);
2984
2985 const double *cplexColLB = getColLower();
2986 const double *cplexColUB = getColUpper();
2987 const CoinPackedVector &lbs = cc.lbs();
2988 const CoinPackedVector &ubs = cc.ubs();
2989 int i;
2990
2991 for (i = 0; i < lbs.getNumElements(); ++i)
2992 if (lbs.getElements()[i] > cplexColLB[lbs.getIndices()[i]])
2993 setColLower(lbs.getIndices()[i], lbs.getElements()[i]);
2994 for (i = 0; i < ubs.getNumElements(); ++i)
2995 if (ubs.getElements()[i] < cplexColUB[ubs.getIndices()[i]])
2996 setColUpper(ubs.getIndices()[i], ubs.getElements()[i]);
2997 }
2998
2999 //-----------------------------------------------------------------------------
3000
3001 void OsiCpxSolverInterface::applyRowCut(const OsiRowCut &rowCut)
3002 {
3003 debugMessage("OsiCpxSolverInterface::applyRowCut(%p)\n", (void *)&rowCut);
3004
3005 int err = 0;
3006 double rhs = 0.0;
3007 double rng = 0.0;
3008 char sns;
3009 double lb = rowCut.lb();
3010 double ub = rowCut.ub();
3011 if (lb <= -getInfinity() && ub >= getInfinity()) // free constraint
3012 {
3013 rhs = -getInfinity();
3014 rng = 2 * getInfinity(); // CPLEX doesn't support free constraints
3015 sns = 'R'; // -> implement them as ranged rows with infinite bounds
3016 } else if (lb <= -getInfinity()) // <= constraint
3017 {
3018 rhs = ub;
3019 sns = 'L';
3020 } else if (ub >= getInfinity()) // >= constraint
3021 {
3022 rhs = lb;
3023 sns = 'G';
3024 } else if (ub == lb) // = constraint
3025 {
3026 rhs = ub;
3027 sns = 'E';
3028 } else // range constraint
3029 {
3030 rhs = lb;
3031 rng = ub - lb;
3032 sns = 'R';
3033 }
3034 int rmatbeg = 0;
3035 err = CPXaddrows(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN), 0, 1, rowCut.row().getNumElements(),
3036 &rhs, &sns, &rmatbeg,
3037 const_cast< int * >(rowCut.row().getIndices()),
3038 const_cast< double * >(rowCut.row().getElements()),
3039 NULL, NULL);
3040 checkCPXerror(err, "CPXaddrows", "applyRowCut");
3041 if (sns == 'R') {
3042 err = CPXchgcoef(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN),
3043 CPXgetnumrows(env_, getLpPtr(OsiCpxSolverInterface::KEEPCACHED_COLUMN)) - 1,
3044 -2, rng);
3045 checkCPXerror(err, "CPXchgcoef", "applyRowCut");
3046 }
3047 }
3048
3049 //#############################################################################
3050 // Private methods (non-static and static) and static data
3051 //#############################################################################
3052
3053 //-------------------------------------------------------------------
3054 // Get pointer to CPXLPptr.
3055 // const methods should use getMutableLpPtr().
3056 // non-const methods should use getLpPtr().
3057 //-------------------------------------------------------------------
3058 CPXLPptr OsiCpxSolverInterface::getMutableLpPtr() const
3059 {
3060 if (lp_ == NULL) {
3061 int err;
3062 assert(env_ != NULL);
3063 #if 0
3064 //char pn[] = "OSI_CPLEX";
3065 lp_ = CPXcreateprob( env_, &err, pn );
3066 #else
3067 std::string pn;
3068 getStrParam(OsiProbName, pn);
3069 lp_ = CPXcreateprob(env_, &err, const_cast< char * >(pn.c_str()));
3070 #endif
3071 checkCPXerror(err, "CPXcreateprob", "getMutableLpPtr");
3072 // err = CPXchgprobtype(env_,lp_,CPXPROB_LP);
3073 // checkCPXerror( err, "CPXchgprobtype", "getMutableLpPtr" );
3074 assert(lp_ != NULL);
3075 }
3076 return lp_;
3077 }
3078
3079 //-------------------------------------------------------------------
3080 CPXENVptr OsiCpxSolverInterface::getMutableEnvironmentPtr() const
3081 {
3082 assert(env_ != NULL);
3083 return env_;
3084 }
3085
3086 //-------------------------------------------------------------------
3087
3088 void OsiCpxSolverInterface::gutsOfCopy(const OsiCpxSolverInterface &source)
3089 {
3090 // Set Objective Sense
3091 setObjSense(source.getObjSense());
3092
3093 // Set Rim and constraints
3094 const double *obj = source.getObjCoefficients();
3095 const double *rhs = source.getRightHandSide();
3096 const char *sense = source.getRowSense();
3097 const CoinPackedMatrix *cols = source.getMatrixByCol();
3098 const double *lb = source.getColLower();
3099 const double *ub = source.getColUpper();
3100 loadProblem(*cols, lb, ub, obj, sense, rhs, source.getRowRange());
3101
3102 // Set MIP information
3103 resizeColType(source.coltypesize_);
3104 CoinDisjointCopyN(source.coltype_, source.coltypesize_, coltype_);
3105
3106 // Set Solution
3107 setColSolution(source.getColSolution());
3108 setRowPrice(source.getRowPrice());
3109
3110 // Should also copy row and col names.
3111 #if 0
3112 char** cname = new char*[numcols];
3113 char* cnamestore = NULL;
3114 int surplus;
3115 err = CPXgetcolname( env_, source.lp_, cname, NULL, 0, &surplus, 0, numcols-1 );
3116 if( err != CPXERR_NO_NAMES )
3117 {
3118 cnamestore = new char[-surplus];
3119 err = CPXgetcolname( env_, source.lp_, cname, cnamestore, -surplus, &surplus, 0, numcols-1 );
3120 checkCPXerror( err, "CPXgetcolname", "gutsOfCopy" );
3121 assert( surplus == 0 );
3122 }
3123 else
3124 {
3125 delete [] cname;
3126 cname = NULL;
3127 }
3128
3129 char** rname = new char*[numrows];
3130 char* rnamestore = NULL;
3131 err = CPXgetrowname( env_, source.lp_, rname, NULL, 0, &surplus, 0, numrows-1 );
3132 if( err != CPXERR_NO_NAMES )
3133 {
3134 rnamestore = new char[-surplus];
3135 err = CPXgetrowname( env_, source.lp_, rname, rnamestore, -surplus, &surplus, 0, numrows-1 );
3136 checkCPXerror( err, "CPXgetrowname", "gutsOfCopy" );
3137 assert( surplus == 0 );
3138 }
3139 else
3140 {
3141 delete [] rname;
3142 rname = NULL;
3143 }
3144
3145 err = CPXcopylpwnames( env_, getLpPtr(),
3146 numcols, numrows, objsen,
3147 const_cast<double *>(obj),
3148 const_cast<double *>(rhs),
3149 const_cast<char *>(sense),
3150 const_cast<int *>(cols->vectorStarts()),
3151 const_cast<int *>(cols->vectorLengths()),
3152 const_cast<int *>(cols->indices()),
3153 const_cast<double *>(cols->elements()),
3154 const_cast<double *>(lb),
3155 const_cast<double *>(ub),
3156 rng,
3157 cname, rname);
3158 checkCPXerror( err, "CPXcopylpwnames", "gutsOfCopy" );
3159
3160 if( rname != NULL )
3161 {
3162 delete [] rnamestore;
3163 delete [] rname;
3164 }
3165 if( cname != NULL )
3166 {
3167 delete [] cnamestore;
3168 delete [] cname;
3169 }
3170 delete [] rng;
3171 #endif
3172 }
3173
3174 //-------------------------------------------------------------------
3175 void OsiCpxSolverInterface::gutsOfConstructor()
3176 {
3177 int err;
3178 #if CPX_VERSION >= 800
3179 env_ = CPXopenCPLEX(&err);
3180 #else
3181 env_ = CPXopenCPLEXdevelop(&err);
3182 #endif
3183
3184 checkCPXerror(err, "CPXopenCPLEXdevelop", "gutsOfConstructor");
3185 assert(env_ != NULL);
3186
3187 CPXCHANNELptr cpxresults;
3188 CPXCHANNELptr cpxwarning;
3189 CPXCHANNELptr cpxerror;
3190 CPXCHANNELptr cpxlog;
3191 err = CPXgetchannels(env_, &cpxresults, &cpxwarning, &cpxerror, &cpxlog);
3192 checkCPXerror(err, "CPXgetchannels", "gutsOfConstructor");
3193
3194 err = CPXaddfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
3195 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
3196 err = CPXaddfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
3197 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
3198 err = CPXaddfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
3199 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
3200 err = CPXaddfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
3201 checkCPXerror(err, "CPXaddfuncdest", "gutsOfConstructor");
3202
3203 /* turn off all output to screen */
3204 err = CPXsetintparam(env_, CPX_PARAM_SCRIND, CPX_OFF);
3205 checkCPXerror(err, "CPXsetintparam", "gutsOfConstructor");
3206
3207 #if 0
3208 // CPXcreateprob was moved to getLpPtr() method.
3209 lp_ = CPXcreateprob( env_, &err, "OSI_CPLEX" );
3210 checkCPXerror( err, "CPXcreateprob", "gutsOfConstructor" );
3211 // err = CPXchgprobtype(env_,lp_,CPXPROB_LP);
3212 // checkCPXerror( err, "CPXchgprobtype", "getMutableLpPtr" );
3213 assert( lp_ != NULL );
3214 #endif
3215 }
3216
3217 //-------------------------------------------------------------------
3218 void OsiCpxSolverInterface::gutsOfDestructor()
3219 {
3220 if (lp_ != NULL) {
3221 int err = CPXfreeprob(env_, &lp_);
3222 checkCPXerror(err, "CPXfreeprob", "gutsOfDestructor");
3223 lp_ = NULL;
3224 freeAllMemory();
3225 }
3226
3227 if (env_ != NULL) {
3228 int err = CPXcloseCPLEX(&env_);
3229 checkCPXerror(err, "CPXcloseCPLEX", "gutsOfDestructor");
3230 env_ = NULL;
3231 }
3232
3233 assert(lp_ == NULL);
3234 assert(env_ == NULL);
3235 assert(obj_ == NULL);
3236 assert(collower_ == NULL);
3237 assert(colupper_ == NULL);
3238 assert(rowsense_ == NULL);
3239 assert(rhs_ == NULL);
3240 assert(rowrange_ == NULL);
3241 assert(rowlower_ == NULL);
3242 assert(rowupper_ == NULL);
3243 assert(colsol_ == NULL);
3244 assert(rowsol_ == NULL);
3245 assert(redcost_ == NULL);
3246 assert(rowact_ == NULL);
3247 assert(matrixByRow_ == NULL);
3248 assert(matrixByCol_ == NULL);
3249 assert(coltype_ == NULL);
3250 assert(coltypesize_ == 0);
3251 }
3252
3253 //-------------------------------------------------------------------
3254 /// free cached vectors
3255
3256 void OsiCpxSolverInterface::freeCachedColRim()
3257 {
3258 freeCacheDouble(obj_);
3259 freeCacheDouble(collower_);
3260 freeCacheDouble(colupper_);
3261 assert(obj_ == NULL);
3262 assert(collower_ == NULL);
3263 assert(colupper_ == NULL);
3264 }
3265
3266 void OsiCpxSolverInterface::freeCachedRowRim()
3267 {
3268 freeCacheChar(rowsense_);
3269 freeCacheDouble(rhs_);
3270 freeCacheDouble(rowrange_);
3271 freeCacheDouble(rowlower_);
3272 freeCacheDouble(rowupper_);
3273 assert(rowsense_ == NULL);
3274 assert(rhs_ == NULL);
3275 assert(rowrange_ == NULL);
3276 assert(rowlower_ == NULL);
3277 assert(rowupper_ == NULL);
3278 }
3279
3280 void OsiCpxSolverInterface::freeCachedMatrix()
3281 {
3282 freeCacheMatrix(matrixByRow_);
3283 freeCacheMatrix(matrixByCol_);
3284 assert(matrixByRow_ == NULL);
3285 assert(matrixByCol_ == NULL);
3286 }
3287
3288 void OsiCpxSolverInterface::freeCachedResults()
3289 {
3290 freeCacheDouble(colsol_);
3291 freeCacheDouble(rowsol_);
3292 freeCacheDouble(redcost_);
3293 freeCacheDouble(rowact_);
3294 assert(colsol_ == NULL);
3295 assert(rowsol_ == NULL);
3296 assert(redcost_ == NULL);
3297 assert(rowact_ == NULL);
3298 }
3299
3300 void OsiCpxSolverInterface::freeCachedData(int keepCached)
3301 {
3302 if (!(keepCached & OsiCpxSolverInterface::KEEPCACHED_COLUMN))
3303 freeCachedColRim();
3304 if (!(keepCached & OsiCpxSolverInterface::KEEPCACHED_ROW))
3305 freeCachedRowRim();
3306 if (!(keepCached & OsiCpxSolverInterface::KEEPCACHED_MATRIX))
3307 freeCachedMatrix();
3308 if (!(keepCached & OsiCpxSolverInterface::KEEPCACHED_RESULTS))
3309 freeCachedResults();
3310 }
3311
3312 void OsiCpxSolverInterface::freeAllMemory()
3313 {
3314 freeCachedData();
3315 if (hotStartCStat_ != NULL)
3316 delete[] hotStartCStat_;
3317 if (hotStartRStat_ != NULL)
3318 delete[] hotStartRStat_;
3319 hotStartCStat_ = NULL;
3320 hotStartCStatSize_ = 0;
3321 hotStartRStat_ = NULL;
3322 hotStartRStatSize_ = 0;
3323 freeColType();
3324 }
3325
3326 //#############################################################################
3327 // Resets as if default constructor
3328 void OsiCpxSolverInterface::reset()
3329 {
3330 int err;
3331 CPXCHANNELptr cpxresults;
3332 CPXCHANNELptr cpxwarning;
3333 CPXCHANNELptr cpxerror;
3334 CPXCHANNELptr cpxlog;
3335
3336 err = CPXgetchannels(env_, &cpxresults, &cpxwarning, &cpxerror, &cpxlog);
3337
3338 err = CPXdelfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
3339 checkCPXerror(err, "CPXdelfuncdest", "reset");
3340 err = CPXdelfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
3341 checkCPXerror(err, "CPXdelfuncdest", "reset");
3342 err = CPXdelfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
3343 checkCPXerror(err, "CPXdelfuncdest", "reset");
3344 err = CPXdelfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
3345 checkCPXerror(err, "CPXdelfuncdest", "reset");
3346
3347 setInitialData(); // clear base class (this may reset the message handler, too)
3348
3349 err = CPXaddfuncdest(env_, cpxresults, messageHandler(), OsiCpxMessageCallbackResultLog);
3350 checkCPXerror(err, "CPXaddfuncdest", "reset");
3351 err = CPXaddfuncdest(env_, cpxlog, messageHandler(), OsiCpxMessageCallbackResultLog);
3352 checkCPXerror(err, "CPXaddfuncdest", "reset");
3353 err = CPXaddfuncdest(env_, cpxwarning, messageHandler(), OsiCpxMessageCallbackWarning);
3354 checkCPXerror(err, "CPXaddfuncdest", "reset");
3355 err = CPXaddfuncdest(env_, cpxerror, messageHandler(), OsiCpxMessageCallbackError);
3356 checkCPXerror(err, "CPXaddfuncdest", "reset");
3357
3358 if (lp_ != NULL) { // kill old LP
3359 int err = CPXfreeprob(env_, &lp_);
3360 checkCPXerror(err, "CPXfreeprob", "loadProblem");
3361 lp_ = NULL;
3362 freeAllMemory();
3363 }
3364 }
3365 #if CPX_VERSION >= 900
3366 /**********************************************************************/
3367 /* Returns 1 if can just do getBInv etc
3368 2 if has all OsiSimplex methods
3369 and 0 if it has none */
3370 int OsiCpxSolverInterface::canDoSimplexInterface() const
3371 {
3372 return 1;
3373 }
3374
3375 /**********************************************************************/
3376 bool OsiCpxSolverInterface::basisIsAvailable() const
3377 {
3378 CPXLPptr lp = getMutableLpPtr();
3379
3380 int solnmethod, solntype, pfeasind, dfeasind;
3381
3382 int status = CPXsolninfo(env_, lp, &solnmethod, &solntype,
3383 &pfeasind, &dfeasind);
3384 if (status) {
3385 return false;
3386 }
3387 if (solntype == CPX_BASIC_SOLN) {
3388 return true;
3389 }
3390 return false;
3391 }
3392
3393 /**********************************************************************/
3394 /* CPLEX return codes:
3395 For cstat:
3396 CPX_AT_LOWER 0 : variable at lower bound
3397 CPX_BASIC 1 : variable is basic
3398 CPX_AT_UPPER 2 : variable at upper bound
3399 CPX_FREE_SUPER 3 : variable free and non-basic
3400
3401 For rstat:
3402
3403 Non ranged rows:
3404 CPX_AT_LOWER 0 : associated slack/surplus/artificial variable non-basic
3405 at value 0.0
3406 CPX_BASIC 1 : associated slack/surplus/artificial variable basic
3407
3408 Ranged rows:
3409 CPX_AT_LOWER 0 : associated slack/surplus/artificial variable non-basic
3410 at its lower bound
3411 CPX_BASIC 1 : associated slack/surplus/artificial variable basic
3412 CPX_AT_UPPER 2 : associated slack/surplus/artificial variable non-basic
3413 at upper bound
3414
3415 Cplex adds a slack with coeff +1 in <= and =, with coeff -1 in >=, slack being
3416 non negative. We switch in order to get a "Clp tableau" where all the
3417 slacks have coeff +1.
3418
3419 If a slack for >= is non basic, invB is not changed; column of the slack in
3420 opt tableau is flipped.
3421
3422 If slack for >= is basic, corresp. row of invB is flipped; whole row of opt
3423 tableau is flipped; then whole column for the slack in opt tableau is flipped.
3424 */
3425
3426 /* Osi return codes:
3427 0: free
3428 1: basic
3429 2: upper
3430 3: lower
3431 */
3432 void OsiCpxSolverInterface::getBasisStatus(int *cstat, int *rstat) const
3433 {
3434 CPXLPptr lp = getMutableLpPtr();
3435 int status = CPXgetbase(env_, lp, cstat, rstat);
3436 if (status) {
3437 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): Unable to get base\n");
3438 exit(1);
3439 }
3440
3441 int ncol = getNumCols();
3442 int nrow = getNumRows();
3443 const int objsense = (int)getObjSense();
3444 const double *dual = getRowPrice();
3445 const double *row_act = getRowActivity();
3446 const double *rowLower = getRowLower();
3447 const double *rowUpper = getRowUpper();
3448
3449 char *sense = new char[nrow];
3450 status = CPXgetsense(env_, lp, sense, 0, nrow - 1);
3451
3452 if (status) {
3453 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): Unable to get sense for the rows\n");
3454 exit(1);
3455 }
3456
3457 for (int i = 0; i < ncol; i++) {
3458 switch (cstat[i]) {
3459 case 0:
3460 cstat[i] = 3;
3461 break;
3462 case 1:
3463 break;
3464 case 2:
3465 break;
3466 case 3:
3467 cstat[i] = 0;
3468 break;
3469 default:
3470 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): unknown column status: %d\n", cstat[i]);
3471 break;
3472 }
3473 }
3474
3475 if (objsense == 1) {
3476
3477 for (int i = 0; i < nrow; i++) {
3478 switch (rstat[i]) {
3479 case 0:
3480 rstat[i] = 3;
3481
3482 if (sense[i] == 'E') {
3483 if (dual[i] > 0) {
3484 rstat[i] = 2;
3485 }
3486 }
3487
3488 if (sense[i] == 'R') {
3489 if (rowUpper[i] > rowLower[i] + 1e-6) {
3490 if (row_act[i] < rowUpper[i] - 1e-6) {
3491 rstat[i] = 2;
3492 }
3493 } else {
3494 if (dual[i] > 0) {
3495 rstat[i] = 2;
3496 }
3497 }
3498 }
3499
3500 if (sense[i] == 'G') {
3501 rstat[i] = 2;
3502 }
3503
3504 break;
3505
3506 case 1:
3507 break;
3508 case 2:
3509 if (sense[i] == 'E') {
3510 if (dual[i] < 0) {
3511 rstat[i] = 3;
3512 }
3513 }
3514
3515 if (sense[i] == 'R') {
3516 if (rowUpper[i] > rowLower[i] + 1e-6) {
3517 if (row_act[i] > rowLower[i] + 1e-6) {
3518 rstat[i] = 3;
3519 }
3520 } else {
3521 if (dual[i] < 0) {
3522 rstat[i] = 3;
3523 }
3524 }
3525 }
3526
3527 break;
3528 default:
3529 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): unknown row status: %d\n", rstat[i]);
3530 break;
3531 }
3532 }
3533 } else { // objsense == -1
3534 for (int i = 0; i < nrow; i++) {
3535 switch (rstat[i]) {
3536 case 0:
3537 rstat[i] = 3;
3538
3539 if (sense[i] == 'E') {
3540 if (dual[i] < 0) {
3541 rstat[i] = 2;
3542 }
3543 }
3544
3545 if (sense[i] == 'R') {
3546 if (rowUpper[i] > rowLower[i] + 1e-6) {
3547 if (row_act[i] < rowUpper[i] - 1e-6) {
3548 rstat[i] = 2;
3549 }
3550 } else {
3551 if (dual[i] < 0) {
3552 rstat[i] = 2;
3553 }
3554 }
3555 }
3556
3557 if (sense[i] == 'G') {
3558 rstat[i] = 2;
3559 }
3560
3561 break;
3562
3563 case 1:
3564 break;
3565 case 2:
3566 if (sense[i] == 'E') {
3567 if (dual[i] > 0) {
3568 rstat[i] = 3;
3569 }
3570 }
3571
3572 if (sense[i] == 'R') {
3573 if (rowUpper[i] > rowLower[i] + 1e-6) {
3574 if (row_act[i] > rowLower[i] + 1e-6) {
3575 rstat[i] = 3;
3576 }
3577 } else {
3578 if (dual[i] > 0) {
3579 rstat[i] = 3;
3580 }
3581 }
3582 }
3583
3584 break;
3585 default:
3586 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): unknown row status: %d\n", rstat[i]);
3587 break;
3588 }
3589 }
3590 }
3591 delete[] sense;
3592 }
3593
3594 /**********************************************************************/
3595 void OsiCpxSolverInterface::getBInvARow(int row, double *z, double *slack)
3596 const
3597 {
3598
3599 CPXLPptr lp = getMutableLpPtr();
3600
3601 int nrow = getNumRows();
3602 int ncol = getNumCols();
3603 char *sense = new char[nrow];
3604 int status = CPXgetsense(env_, lp, sense, 0, nrow - 1);
3605 if (status) {
3606 printf("### ERROR: OsiCpxSolverInterface::getBInvARow(): Unable to get senses for the rows\n");
3607 exit(1);
3608 }
3609
3610 status = CPXbinvarow(env_, lp, row, z);
3611 if (status) {
3612 printf("### ERROR: OsiCpxSolverInterface::getBInvARow(): Unable to get row %d of the tableau\n", row);
3613 exit(1);
3614 }
3615
3616 if (slack != NULL) {
3617 status = CPXbinvrow(env_, lp, row, slack);
3618 if (status) {
3619 printf("### ERROR: OsiCpxSolverInterface::getBInvARow(): Unable to get row %d of B inverse\n", row);
3620 exit(1);
3621 }
3622
3623 // slack contains now the row of BInv(cplex);
3624 // slack(cplex) is obtained by flipping in slack all entries for >= constr
3625 // slack(clp) is obtained by flipping the same entries in slack(cplex)
3626 // i.e. slack(clp) is the current slack.
3627 }
3628
3629 if (sense[row] == 'G') {
3630 int *ind_bas = new int[nrow];
3631 int ind_slack = ncol + row;
3632 getBasics(ind_bas);
3633 for (int i = 0; i < nrow; i++) {
3634 if (ind_bas[i] == ind_slack) { // slack for row is basic; whole row
3635 // must be flipped
3636 for (int j = 0; j < nrow; j++) {
3637 z[j] = -z[j];
3638 }
3639 if (slack != NULL) {
3640 for (int j = 0; j < nrow; j++) {
3641 slack[j] = -slack[j];
3642 }
3643 }
3644 }
3645 break;
3646 }
3647 delete[] ind_bas;
3648 }
3649 delete[] sense;
3650 } /* getBInvARow */
3651
3652 /**********************************************************************/
3653 void OsiCpxSolverInterface::getBInvRow(int row, double *z) const
3654 {
3655
3656 CPXLPptr lp = getMutableLpPtr();
3657
3658 int nrow = getNumRows();
3659 int ncol = getNumCols();
3660
3661 int status = CPXbinvrow(env_, lp, row, z);
3662 if (status) {
3663 printf("### ERROR: OsiCpxSolverInterface::getBInvRow(): Unable to get row %d of the basis inverse\n", row);
3664 exit(1);
3665 }
3666 int *ind_bas = new int[nrow];
3667 getBasics(ind_bas);
3668 if (ind_bas[row] >= ncol) { // binv row corresponds to a slack variable
3669 int Arow = ind_bas[row] - ncol; // Arow is the number of the row in the problem matrix which this slack belongs to
3670 char sense;
3671 status = CPXgetsense(env_, lp, &sense, Arow, Arow);
3672 if (status) {
3673 printf("### ERROR: OsiCpxSolverInterface::getBInvRow(): Unable to get senses for row %d\n", Arow);
3674 exit(1);
3675 }
3676 if (sense == 'G') { // slack has coeff -1 in Cplex; thus row in binv must be flipped
3677 for (int j = 0; j < nrow; j++) {
3678 z[j] = -z[j];
3679 }
3680 }
3681 }
3682 delete[] ind_bas;
3683 /*
3684 char sense;
3685 status = CPXgetsense(env_, lp, &sense, row, row);
3686 if(status) {
3687 printf("### ERROR: OsiCpxSolverInterface::getBInvRow(): Unable to get senses for row %d\n", row);
3688 exit(1);
3689 }
3690
3691 if(sense == 'G') {
3692 int *ind_bas = new int[nrow];
3693 getBasics(ind_bas);
3694
3695 for(int i=0; i<nrow; i++) {
3696 int ind_piv = ind_bas[i] - ncol;
3697 if(ind_piv == row) {
3698 // slack has coeff -1 in Cplex and is basic;
3699 // row of invB must be flipped
3700
3701 for(int j=0; j<nrow; j++) {
3702 z[j] = -z[j];
3703 }
3704 break;
3705 }
3706 }
3707 delete[] ind_bas;
3708 }
3709 */
3710 } /* getBInvRow */
3711
3712 /**********************************************************************/
3713 void OsiCpxSolverInterface::getBInvACol(int col, double *vec) const
3714 {
3715
3716 CPXLPptr lp = getMutableLpPtr();
3717
3718 int status = CPXbinvacol(env_, lp, col, vec);
3719 if (status) {
3720 printf("### ERROR: OsiCpxSolverInterface::getBInvACol(): Unable to get column %d of the tableau\n", col);
3721 exit(1);
3722 }
3723
3724 int nrow = getNumRows();
3725 int ncol = getNumCols();
3726 char *sense = new char[nrow];
3727 status = CPXgetsense(env_, lp, sense, 0, nrow - 1);
3728 if (status) {
3729 printf("### ERROR: OsiCpxSolverInterface::getBInvACol(): Unable to get senses for the rows\n");
3730 exit(1);
3731 }
3732 int *ind_bas = new int[nrow];
3733 getBasics(ind_bas);
3734
3735 for (int i = 0; i < nrow; i++)
3736 if (ind_bas[i] > ncol && sense[ind_bas[i] - ncol] == 'G')
3737 vec[i] = -vec[i]; // slack for row is basic; whole row must be flipped
3738
3739 delete[] sense;
3740 delete[] ind_bas;
3741 } /* getBInvACol */
3742
3743 /**********************************************************************/
3744 void OsiCpxSolverInterface::getBInvCol(int col, double *vec) const
3745 {
3746
3747 CPXLPptr lp = getMutableLpPtr();
3748
3749 int status = CPXbinvcol(env_, lp, col, vec);
3750 if (status) {
3751 printf("### ERROR: OsiCpxSolverInterface::getBInvCol(): Unable to get column %d of the basis inverse\n", col);
3752 exit(1);
3753 }
3754
3755 int nrow = getNumRows();
3756 int ncol = getNumCols();
3757 char *sense = new char[nrow];
3758 status = CPXgetsense(env_, lp, sense, 0, nrow - 1);
3759 if (status) {
3760 printf("### ERROR: OsiCpxSolverInterface::getBInvCol(): Unable to get senses for the rows\n");
3761 exit(1);
3762 }
3763 int *ind_bas = new int[nrow];
3764 getBasics(ind_bas);
3765
3766 for (int i = 0; i < nrow; i++)
3767 if (ind_bas[i] > ncol && sense[ind_bas[i] - ncol] == 'G')
3768 vec[i] = -vec[i]; // slack for row i is basic; whole row must be flipped
3769
3770 delete[] sense;
3771 delete[] ind_bas;
3772 } /* getBInvCol */
3773
3774 /**********************************************************************/
3775 void OsiCpxSolverInterface::getBasics(int *index) const
3776 {
3777 int ncol = getNumCols();
3778 int nrow = getNumRows();
3779
3780 CPXLPptr lp = getMutableLpPtr();
3781
3782 double *cplex_trash = new double[nrow];
3783 int status = CPXgetbhead(env_, lp, index, cplex_trash);
3784 if (status) {
3785 printf("### ERROR: OsiCpxSolverInterface::getBasics(): Unable to get basis head\n");
3786 exit(1);
3787 }
3788
3789 for (int i = 0; i < nrow; i++) {
3790 if (index[i] < 0) {
3791 index[i] = ncol - 1 - index[i];
3792 }
3793 }
3794 delete[] cplex_trash;
3795 } /* getBasics */
3796
3797 #else /* not CPX_VERSION >= 900 */
3798
3799 /**********************************************************************/
3800 /* Returns 1 if can just do getBInv etc
3801 2 if has all OsiSimplex methods
3802 and 0 if it has none */
3803 int OsiCpxSolverInterface::canDoSimplexInterface() const
3804 {
3805 return 0;
3806 }
3807
3808 /**********************************************************************/
3809 bool OsiCpxSolverInterface::basisIsAvailable() const
3810 {
3811 printf("### ERROR: OsiCpxSolverInterface::basisIsAvailable(): Cplex version lower than 9.0\n");
3812 exit(1);
3813 return false;
3814 }
3815
3816 /**********************************************************************/
3817 void OsiCpxSolverInterface::getBasisStatus(int *cstat, int *rstat) const
3818 {
3819 printf("### ERROR: OsiCpxSolverInterface::getBasisStatus(): Cplex version lower than 9.0\n");
3820 exit(1);
3821 }
3822
3823 /**********************************************************************/
3824 void OsiCpxSolverInterface::getBInvARow(int row, double *z, double *slack)
3825 const
3826 {
3827 printf("### ERROR: OsiCpxSolverInterface::getBInvARow(): Cplex version lower than 9.0\n");
3828 exit(1);
3829 } /* getBInvARow */
3830
3831 /**********************************************************************/
3832 void OsiCpxSolverInterface::getBInvRow(int row, double *z) const
3833 {
3834 printf("### ERROR: OsiCpxSolverInterface::getBInvRow(): Cplex version lower than 9.0\n");
3835 exit(1);
3836 } /* getBInvRow */
3837
3838 /**********************************************************************/
3839 void OsiCpxSolverInterface::getBInvACol(int col, double *vec) const
3840 {
3841 printf("### ERROR: OsiCpxSolverInterface::getBInvACol(): Cplex version lower than 9.0\n");
3842 exit(1);
3843 } /* getBInvACol */
3844
3845 /**********************************************************************/
3846 void OsiCpxSolverInterface::getBInvCol(int col, double *vec) const
3847 {
3848 printf("### ERROR: OsiCpxSolverInterface::getBInvCol(): Cplex version lower than 9.0\n");
3849 exit(1);
3850 } /* getBInvCol */
3851
3852 /**********************************************************************/
3853 void OsiCpxSolverInterface::getBasics(int *index) const
3854 {
3855 printf("### ERROR: OsiCpxSolverInterface::getBasics(): Cplex version lower than 9.0\n");
3856 exit(1);
3857 } /* getBasics */
3858 #endif /* not CPX_VERSION >= 900 */
3859
3860 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
3861 */