Debian Codegraph @

Osi/src/OsiMsk/OsiMskSolverInterface.cpp

raw symbols SCIP
1 /* Osi interface for Mosek ver. 7.0
2 Lower versions are not supported
3 -----------------------------------------------------------------------------
4 name: OSI Interface for MOSEK
5 -----------------------------------------------------------------------------
6
7 This code is licensed under the terms of the Eclipse Public License (EPL).
8
9 Originally developed by Bo Jensen from MOSEK ApS. No longer a maintainer.
10 */
11
12 #include <iostream>
13 #include <cassert>
14 #include <string>
15 #include <numeric>
16 #include "OsiConfig.h"
17 #include "CoinPragma.hpp"
18 #include "CoinError.hpp"
19 #include "CoinFinite.hpp"
20 #include "OsiMskSolverInterface.hpp"
21 #include "OsiRowCut.hpp"
22 #include "OsiColCut.hpp"
23 #include "CoinPackedMatrix.hpp"
24 #include "CoinWarmStartBasis.hpp"
25
26 #include "mosek.h"
27
28 #define MSK_OSI_DEBUG_LEVEL 0
29 #ifndef NDEBUG
30 #define MSK_OSI_ASSERT_LEVEL 0
31 #else
32 #define MSK_OSI_ASSERT_LEVEL 4
33 #endif
34 #define MSK_DO_MOSEK_LOG 0
35
36 #define debugMessage printf
37
38 //Choose algorithm to be default in initial solve
39 #define INITIAL_SOLVE MSK_OPTIMIZER_FREE
40
41 //Choose algorithm to be default in resolve
42 #define RESOLVE_SOLVE MSK_OPTIMIZER_FREE_SIMPLEX
43
44 // Unset this flag to disable the warnings from interface.
45
46 #define MSK_WARNING_ON
47
48 #if MSK_VERSION_MAJOR >= 9
49 #define MSKCONST const
50 #endif
51
52 #undef getc
53
54 //#############################################################################
55 // A couple of helper functions
56 //#############################################################################
57
58 // Free memory pointet to by a double pointer
59
60 inline void freeCacheDouble( double*& ptr )
61 {
62 if( ptr != NULL )
63 {
64 delete [] ptr;
65 ptr = NULL;
66 }
67 }
68
69 // Free memory pointet to by a char pointer
70
71 inline void freeCacheChar( char*& ptr )
72 {
73 if( ptr != NULL )
74 {
75 delete [] ptr;
76 ptr = NULL;
77 }
78 }
79
80 // Free memory pointet to by a CoinPackedMatrix pointer
81
82 inline void freeCacheMatrix( CoinPackedMatrix*& ptr )
83 {
84 if( ptr != NULL )
85 {
86 delete ptr;
87 ptr = NULL;
88 }
89 }
90
91 // Function used to connect MOSEK to stream
92
93 #if MSK_VERSION_MAJOR >= 7
94 static void MSKAPI printlog(void *ptr, const char* s)
95 #else
96 static void MSKAPI printlog(void *ptr, char* s)
97 #endif
98 {
99 printf("%s",s);
100 }
101
102 static
103 void MSKAPI OsiMskStreamFuncLog(MSKuserhandle_t handle, MSKCONST char* str) {
104 if (handle) {
105 if (((CoinMessageHandler*)handle)->logLevel() >= 1)
106 ((CoinMessageHandler*)handle)->message(0, "MSK", str, ' ') << CoinMessageEol;
107 } else {
108 printf(str);
109 printf("\n");
110 }
111 }
112
113 static
114 void MSKAPI OsiMskStreamFuncWarning(MSKuserhandle_t handle, MSKCONST char* str) {
115 if (handle) {
116 if (((CoinMessageHandler*)handle)->logLevel() >= 0)
117 ((CoinMessageHandler*)handle)->message(0, "MSK", str, ' ') << CoinMessageEol;
118 } else {
119 printf(str);
120 printf("\n");
121 }
122 }
123
124 static
125 void MSKAPI OsiMskStreamFuncError(MSKuserhandle_t handle, MSKCONST char* str) {
126 if (handle) {
127 ((CoinMessageHandler*)handle)->message(0, "MSK", str, ' ') << CoinMessageEol;
128 } else {
129 fprintf(stderr, str);
130 fprintf(stderr, "\n");
131 }
132 }
133
134 // Prints a error message and throws a exception
135
136 static inline void
137 checkMSKerror( int err, const char* mskfuncname, const char* osimethod )
138 {
139 if( err != MSK_RES_OK )
140 {
141 char s[100];
142 sprintf( s, "%s returned error %d", mskfuncname, err );
143 std::cout << "ERROR: " << s << " (" << osimethod <<
144 " in OsiMskSolverInterface)" << std::endl;
145 throw CoinError( s, osimethod, "OsiMskSolverInterface" );
146 }
147 }
148
149 // Prints a error message and throws a exception
150
151 static inline void
152 MSKassert(int assertlevel, int test, const char* assertname, const char* osimethod )
153 {
154 if( assertlevel > MSK_OSI_ASSERT_LEVEL && !test )
155 {
156 std::cout << "Assert: " << assertname << " (" << osimethod <<
157 " in OsiMskSolverInterface)" << std::endl;
158 throw CoinError( assertname, osimethod, "OsiMskSolverInterface" );
159 }
160 }
161
162 // Converts Range/Sense/Rhs to MSK bound structure
163
164 static inline void
165 MskConvertSenseToBound(const char rowsen, const double rowrng,
166 const double rowrhs, double &rlb, double &rub, int &rtag)
167 {
168 switch (rowsen)
169 {
170 case 'E':
171 rlb = rowrhs;
172 rub = rowrhs;
173 rtag = MSK_BK_FX;
174 break;
175
176 case 'L':
177 rlb = MSK_INFINITY;
178 rub = rowrhs;
179 rtag = MSK_BK_UP;
180 break;
181 case 'G':
182 rlb = rowrhs;
183 rub = MSK_INFINITY;
184 rtag = MSK_BK_LO;
185 break;
186
187 case 'R':
188 if( rowrng >= 0 )
189 {
190 rlb = rowrhs - rowrng;
191 rub = rowrhs;
192 }
193 else
194 {
195 rlb = rowrhs;
196 rub = rowrhs + rowrng;
197 }
198
199 rtag = MSK_BK_RA;
200 break;
201 case 'N':
202 rlb = -MSK_INFINITY;
203 rub = MSK_INFINITY;
204 rtag = MSK_BK_FR;
205 break;
206
207 default:
208 MSKassert(3,1,"Unknown rowsen","MskConvertSenseToBound");
209 }
210 }
211
212 // Converts a set of bounds to MSK boundkeys
213
214 static inline void
215 MskConvertColBoundToTag(const double collb, const double colub, double &clb, double &cub, int &ctag)
216 {
217 #if MSK_OSI_DEBUG_LEVEL > 3
218 debugMessage("Begin MskConvertColBoundToTag()\n");
219 #endif
220
221 if(collb > -MSK_INFINITY && colub < MSK_INFINITY)
222 {
223 ctag = MSK_BK_RA;
224 clb = collb;
225 cub = colub;
226 }
227 else if(collb <= - MSK_INFINITY && colub < MSK_INFINITY)
228 {
229 ctag = MSK_BK_UP;
230 clb = -MSK_INFINITY;
231 cub = colub;
232 }
233 else if(collb > - MSK_INFINITY && colub >= MSK_INFINITY)
234 {
235 ctag = MSK_BK_LO;
236 clb = collb;
237 cub = MSK_INFINITY;
238 }
239 else if(collb <= -MSK_INFINITY && colub >= MSK_INFINITY)
240 {
241 ctag = MSK_BK_FR;
242 clb = -MSK_INFINITY;
243 cub = MSK_INFINITY;
244 }
245
246 #if MSK_OSI_DEBUG_LEVEL > 3
247 debugMessage("End MskConvertColBoundToTag()\n");
248 #endif
249 }
250
251 // Returns true if "solution" is defined in MOSEK, where solution can be basic, interior or
252 // integer resp. (MSK_SOL_BAS), (MSK_SOL_ITR) or (MSK_SOL_ITG).
253
254 bool OsiMskSolverInterface::definedSolution(int solution) const
255 {
256 #if MSK_OSI_DEBUG_LEVEL > 3
257 debugMessage("Begin OsiMskSolverInterface::definedSolution() %p\n",(void*)this);
258 #endif
259
260
261 int err, res;
262 err = MSK_solutiondef(getMutableLpPtr(), (MSKsoltypee) solution, &res);
263 checkMSKerror(err,"MSK_solutiondef","definedSolution");
264
265 #if MSK_OSI_DEBUG_LEVEL > 3
266 debugMessage("End OsiMskSolverInterface::definedSolution()\n");
267 #endif
268
269
270 return ( res != MSK_RES_OK);
271 }
272
273 // Returns the flag for solver currently switched on in MOSEK resp. (MSK_OPTIMIZER_FREE),
274 // (MSK_OPTIMIZER_INTPNT), (MSK_OPTIMIZER_PRIMAL_SIMPLEX), (MSK_OPTIMIZER_MIXED_INT), or
275 // (MSK_OPTIMIZER_MIXED_INT_CONIC).
276 // MOSEK also has Conic and nonconvex solvers, but these are for obvious reasons not
277 // an option in the Osi interface.
278
279 int OsiMskSolverInterface::solverUsed() const
280 {
281 #if MSK_OSI_DEBUG_LEVEL > 3
282 debugMessage("Begin OsiMskSolverInterface::solverUsed()\n");
283 #endif
284
285 #if MSK_OSI_DEBUG_LEVEL > 3
286 debugMessage("End OsiMskSolverInterface::solverUsed()\n");
287 #endif
288
289 return MSKsolverused_;
290 }
291
292 // Switch the MOSEK solver to LP uses default solver specified by 'InitialSolver'
293
294 void
295 OsiMskSolverInterface::switchToLP( void )
296 {
297 #if MSK_OSI_DEBUG_LEVEL > 3
298 debugMessage("Begin OsiMskSolverInterface::switchToLP()\n");
299 #endif
300
301 int err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_MIO_MODE, MSK_MIO_MODE_IGNORED);
302 checkMSKerror(err,"MSK_putintparam","switchToLp");
303
304 probtypemip_ = false;
305
306 #if MSK_OSI_DEBUG_LEVEL > 3
307 debugMessage("End OsiMskSolverInterface::switchToLP()\n");
308 #endif
309 }
310
311 // Switch the MOSEK solver to MIP.
312
313 void
314 OsiMskSolverInterface::switchToMIP( void )
315 {
316 #if MSK_OSI_DEBUG_LEVEL > 3
317 debugMessage("Begin OsiMskSolverInterface::switchToMIP()\n");
318 #endif
319
320 int err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_MIO_MODE, MSK_MIO_MODE_SATISFIED);
321 checkMSKerror(err,"MSK_putintparam","switchToMIP");
322
323 #if MSK_VERSION_MAJOR == 7
324 err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, MSK_OPTIMIZER_MIXED_INT_CONIC);
325 #else
326 err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, MSK_OPTIMIZER_MIXED_INT);
327 #endif
328 checkMSKerror(err,"MSK_putintparam","switchToMIP");
329 probtypemip_ = true;
330
331 #if MSK_OSI_DEBUG_LEVEL > 3
332 debugMessage("End OsiMskSolverInterface::switchToMIP()\n");
333 #endif
334 }
335
336 // Resize the coltype array.
337
338 void
339 OsiMskSolverInterface::resizeColType( int minsize )
340 {
341 #if MSK_OSI_DEBUG_LEVEL > 3
342 debugMessage("Begin OsiMskSolverInterface::resizeColType()\n");
343 #endif
344
345 if( minsize > coltypesize_ )
346 {
347 int newcoltypesize = 2*coltypesize_;
348 if( minsize > newcoltypesize )
349 newcoltypesize = minsize;
350 char *newcoltype = new char[newcoltypesize];
351
352 if( coltype_ != NULL )
353 {
354 CoinDisjointCopyN( coltype_, coltypesize_, newcoltype );
355 delete[] coltype_;
356 }
357
358 coltype_ = newcoltype;
359 coltypesize_ = newcoltypesize;
360 }
361
362 MSKassert(3,minsize == 0 || coltype_ != NULL,"Unknown rowsen","MskConvertSenseToBound");
363 MSKassert(3,coltypesize_ >= minsize,"coltypesize_ >= minsize","MskConvertSenseToBound");
364
365 #if MSK_OSI_DEBUG_LEVEL > 3
366 debugMessage("End OsiMskSolverInterface::resizeColType()\n");
367 #endif
368 }
369
370 // Free coltype array
371
372 void
373 OsiMskSolverInterface::freeColType()
374 {
375 #if MSK_OSI_DEBUG_LEVEL > 3
376 debugMessage("Begin OsiMskSolverInterface::freeColType()\n");
377 #endif
378
379 if( coltypesize_ > 0 )
380 {
381 delete[] coltype_;
382
383 coltype_ = NULL;
384 coltypesize_ = 0;
385 }
386
387 MSKassert(3,coltype_ == NULL,"coltype_ == NULL","freeColType");
388
389 #if MSK_OSI_DEBUG_LEVEL > 3
390 debugMessage("End OsiMskSolverInterface::freeColType()\n");
391 #endif
392 }
393
394 //#############################################################################
395 // Solve methods
396 //#############################################################################
397
398 //-----------------------------------------------------------------------------
399 // Free cached results and optimize the LP problem in task
400
401 void OsiMskSolverInterface::initialSolve()
402 {
403 #if MSK_OSI_DEBUG_LEVEL > 1
404 debugMessage("Begin OsiMskSolverInterface::initialSolve() %p\n", (void*)this);
405 #endif
406
407 if( definedSolution( MSK_SOL_BAS ) == true )
408 {
409 resolve();
410 }
411 else
412 {
413
414 int err,solver;
415
416 err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_SIM_HOTSTART, MSK_SIM_HOTSTART_STATUS_KEYS);
417 checkMSKerror(err,"MSK_putintparam","initialsolve");
418
419 err = MSK_getintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, &solver);
420 checkMSKerror(err,"MSK_getintparam","initialsolve");
421
422 switchToLP();
423
424 err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, INITIAL_SOLVE);
425 checkMSKerror(err,"MSK_putintparam","initialSolve");
426
427 err = MSK_getintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, &MSKsolverused_);
428 checkMSKerror(err,"MSK_getintparam","initialsolve");
429
430 #if MSK_DO_MOSEK_LOG > 0
431 err = MSK_putintparam( getMutableLpPtr(),MSK_IPAR_LOG_SIM,MSK_DO_MOSEK_LOG);
432 checkMSKerror(err,"MSK_putintparam","initialsolve");
433 #endif
434
435 Mskerr = MSK_optimize(getLpPtr( OsiMskSolverInterface::FREECACHED_RESULTS ));
436
437 #if MSK_DO_MOSEK_LOG > 0
438 err = MSK_solutionsummary( getMutableLpPtr(),MSK_STREAM_LOG);
439 checkMSKerror(err,"MSK_solutionsummary","initialsolve");
440 #endif
441
442 err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, solver);
443 checkMSKerror(err,"MSK_putintparam","initialsolve");
444
445 }
446
447 #if MSK_OSI_DEBUG_LEVEL > 1
448 debugMessage("End OsiMskSolverInterface::initialSolve()\n");
449 #endif
450 }
451
452 //-----------------------------------------------------------------------------
453 // Resolves an LP problem.
454
455 void OsiMskSolverInterface::resolve()
456 {
457 #if MSK_OSI_DEBUG_LEVEL > 1
458 debugMessage("Begin OsiMskSolverInterface::resolve %p\n", (void*)this);
459 #endif
460
461 if( definedSolution( MSK_SOL_BAS ) == true )
462 {
463 int err,solver;
464
465 err = MSK_getintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, &solver);
466 checkMSKerror(err,"MSK_getintparam","resolve");
467
468 switchToLP();
469
470 err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_SIM_HOTSTART, MSK_SIM_HOTSTART_STATUS_KEYS);
471 checkMSKerror(err,"MSK_putintparam","resolve");
472
473 err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, RESOLVE_SOLVE);
474 checkMSKerror(err,"MSK_putintparam","resolve");
475
476 #if 0
477 err = MSK_putintparam( getMutableLpPtr(), MSK_IPAR_SIM_MAX_ITERATIONS, 20000000 );
478 #endif
479
480 err = MSK_getintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, &MSKsolverused_);
481 checkMSKerror(err,"MSK_getintparam","resolve");
482
483 #if MSK_DO_MOSEK_LOG > 0
484 err = MSK_putintparam( getMutableLpPtr(),MSK_IPAR_LOG,MSK_DO_MOSEK_LOG);
485 checkMSKerror(err,"MSK_putintparam","resolve");
486
487 err = MSK_putintparam( getMutableLpPtr(),MSK_IPAR_LOG_SIM,MSK_DO_MOSEK_LOG);
488 checkMSKerror(err,"MSK_putintparam","resolve");
489 #endif
490
491 Mskerr = MSK_optimize(getLpPtr( OsiMskSolverInterface::FREECACHED_RESULTS ));
492
493 #if MSK_DO_MOSEK_LOG > 0
494 err = MSK_solutionsummary( getMutableLpPtr(),MSK_STREAM_LOG);
495 printf("Mskerr : %d\n",Mskerr);
496 #endif
497
498 err = MSK_putintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, solver);
499 checkMSKerror(err,"MSK_putintparam","resolve");
500 }
501 else
502 {
503 initialSolve();
504 }
505
506 #if MSK_OSI_DEBUG_LEVEL > 1
507 debugMessage("End OsiMskSolverInterface::resolve... press any key to continue\n");
508 getchar();
509 #endif
510 }
511
512 //-----------------------------------------------------------------------------
513 // Resolves an MIP problem with MOSEK MIP solver.
514
515 void OsiMskSolverInterface::branchAndBound()
516 {
517 #if MSK_OSI_DEBUG_LEVEL > 1
518 debugMessage("Begin OsiMskSolverInterface::branchAndBound()\n");
519 #endif
520
521 switchToMIP();
522
523 int err = MSK_getintparam(getMutableLpPtr(), MSK_IPAR_OPTIMIZER, &MSKsolverused_);
524 checkMSKerror(err,"MSK_getintparam","branchAndBound");
525
526 Mskerr = MSK_optimize(getLpPtr( OsiMskSolverInterface::FREECACHED_RESULTS ));
527
528 #if MSK_OSI_DEBUG_LEVEL > 1
529 debugMessage("End OsiMskSolverInterface::branchAndBound()\n");
530 #endif
531 }
532
533 //#############################################################################
534 // Parameter related methods
535 //#############################################################################
536
537 //-----------------------------------------------------------------------------
538 // Sets a int parameter in MOSEK
539
540 bool
541 OsiMskSolverInterface::setIntParam(OsiIntParam key, int value)
542 {
543 #if MSK_OSI_DEBUG_LEVEL > 3
544 debugMessage("Begin OsiMskSolverInterface::setIntParam(%d, %d)\n", key, value);
545 #endif
546
547 bool retval = false;
548
549 switch (key)
550 {
551 case OsiMaxNumIteration:
552 retval = (MSK_putintparam(getMutableLpPtr(),
553 MSK_IPAR_SIM_MAX_ITERATIONS,
554 value ) == MSK_RES_OK);
555 break;
556
557 case OsiMaxNumIterationHotStart:
558 if (value < 0) {
559 retval = false;
560 } else {
561 hotStartMaxIteration_ = value;
562 retval = true;
563 }
564 break;
565 case OsiNameDiscipline:
566 retval = false;
567 break;
568 case OsiLastIntParam:
569 retval = false;
570 break;
571 }
572
573 #if MSK_OSI_DEBUG_LEVEL > 3
574 debugMessage("End OsiMskSolverInterface::setIntParam(%d, %d)\n", key, value);
575 #endif
576
577 return retval;
578 }
579
580 //-----------------------------------------------------------------------------
581 // Sets a double parameter in MOSEK.
582
583 bool
584 OsiMskSolverInterface::setDblParam(OsiDblParam key, double value)
585 {
586 #if MSK_OSI_DEBUG_LEVEL > 3
587 debugMessage("Begin OsiMskSolverInterface::setDblParam(%d, %g)\n", key, value);
588 #endif
589
590 bool retval = false;
591 switch (key)
592 {
593 case OsiDualObjectiveLimit:
594 if( getObjSense() == +1 ) // Minimize
595 retval = ( MSK_putdouparam( getMutableLpPtr(),
596 MSK_DPAR_UPPER_OBJ_CUT,
597 value ) == MSK_RES_OK ); // min
598 else
599 retval = ( MSK_putdouparam( getMutableLpPtr(),
600 MSK_DPAR_LOWER_OBJ_CUT,
601 value ) == MSK_RES_OK ); // max
602 break;
603 case OsiPrimalObjectiveLimit:
604 if( getObjSense() == +1 ) // Minimize
605 retval = ( MSK_putdouparam( getMutableLpPtr(),
606 MSK_DPAR_LOWER_OBJ_CUT,
607 value ) == MSK_RES_OK ); // min
608 else
609 retval = ( MSK_putdouparam( getMutableLpPtr(),
610 MSK_DPAR_UPPER_OBJ_CUT,
611 value ) == MSK_RES_OK ); // max
612 break;
613 case OsiDualTolerance:
614 retval = ( MSK_putdouparam( getMutableLpPtr(),
615 MSK_DPAR_BASIS_TOL_S,
616 value ) == MSK_RES_OK );
617 break;
618 case OsiPrimalTolerance:
619 retval = ( MSK_putdouparam( getMutableLpPtr(),
620 MSK_DPAR_BASIS_TOL_X,
621 value ) == MSK_RES_OK );
622 break;
623 case OsiObjOffset:
624 ObjOffset_ = value;
625 retval = true;
626 break;
627 case OsiLastDblParam:
628 retval = false;
629 break;
630 }
631
632 #if MSK_OSI_DEBUG_LEVEL > 3
633 debugMessage("End OsiMskSolverInterface::setDblParam(%d, %g)\n", key, value);
634 #endif
635
636 return retval;
637 }
638
639
640 //-----------------------------------------------------------------------------
641 // Sets a string parameter in MOSEK.
642
643 bool
644 OsiMskSolverInterface::setStrParam(OsiStrParam key, const std::string & value)
645 {
646 #if MSK_OSI_DEBUG_LEVEL > 3
647 debugMessage("Begin OsiMskSolverInterface::setStrParam(%d, %s)\n", key, value.c_str());
648 #endif
649
650 bool retval=false;
651 switch (key)
652 {
653 case OsiProbName:
654 OsiSolverInterface::setStrParam(key,value);
655 return retval = true;
656 case OsiSolverName:
657 return false;
658 case OsiLastStrParam:
659 return false;
660 }
661
662 #if MSK_OSI_DEBUG_LEVEL > 3
663 debugMessage("End OsiMskSolverInterface::setStrParam(%d, %s)\n", key, value.c_str());
664 #endif
665
666 return retval;
667 }
668
669 //-----------------------------------------------------------------------------
670 // Gets a int parameter in MOSEK.
671
672 bool
673 OsiMskSolverInterface::getIntParam(OsiIntParam key, int& value) const
674 {
675 #if MSK_OSI_DEBUG_LEVEL > 3
676 debugMessage("Begin OsiMskSolverInterface::getIntParam(%d)\n", key);
677 #endif
678
679 bool retval = false;
680 switch (key)
681 {
682 case OsiMaxNumIteration:
683 retval = (MSK_getintparam(getMutableLpPtr(),
684 MSK_IPAR_SIM_MAX_ITERATIONS,
685 &value ) == MSK_RES_OK);
686 break;
687 case OsiMaxNumIterationHotStart:
688 value = hotStartMaxIteration_;
689 retval = true;
690 break;
691 case OsiNameDiscipline:
692 value = 0;
693 retval = false;
694 break;
695 case OsiLastIntParam:
696 retval = false;
697 break;
698 }
699
700 #if MSK_OSI_DEBUG_LEVEL > 3
701 debugMessage("End OsiMskSolverInterface::getIntParam(%d)\n", key);
702 #endif
703
704 return retval;
705 }
706
707 //-----------------------------------------------------------------------------
708 // Get a double parameter in MOSEK.
709
710 bool
711 OsiMskSolverInterface::getDblParam(OsiDblParam key, double& value) const
712 {
713 #if MSK_OSI_DEBUG_LEVEL > 3
714 debugMessage("Begin OsiMskSolverInterface::getDblParam(%d)\n", key);
715 #endif
716
717 bool retval = false;
718
719 switch (key)
720 {
721 case OsiDualObjectiveLimit:
722 if( getObjSense() == +1 )
723 retval = ( MSK_getdouparam( getMutableLpPtr(),
724 MSK_DPAR_UPPER_OBJ_CUT,
725 &value ) == MSK_RES_OK ); // max
726 else
727 retval = ( MSK_getdouparam( getMutableLpPtr(),
728 MSK_DPAR_LOWER_OBJ_CUT,
729 &value ) == MSK_RES_OK ); // min
730 break;
731 case OsiPrimalObjectiveLimit:
732 if( getObjSense() == +1 )
733 retval = ( MSK_getdouparam( getMutableLpPtr(),
734 MSK_DPAR_LOWER_OBJ_CUT,
735 &value ) == MSK_RES_OK ); // min
736 else
737 retval = ( MSK_getdouparam( getMutableLpPtr(),
738 MSK_DPAR_UPPER_OBJ_CUT,
739 &value ) == MSK_RES_OK ); // max
740 break;
741 case OsiDualTolerance:
742 retval = ( MSK_getdouparam( getMutableLpPtr(),
743 MSK_DPAR_BASIS_TOL_S,
744 &value ) == MSK_RES_OK );
745 break;
746 case OsiPrimalTolerance:
747 retval = ( MSK_getdouparam( getMutableLpPtr(),
748 MSK_DPAR_BASIS_TOL_X,
749 &value ) == MSK_RES_OK );
750 break;
751 case OsiObjOffset:
752 value = ObjOffset_;
753 retval = true;
754 break;
755 case OsiLastDblParam:
756 retval = false;
757 break;
758 }
759
760 #if MSK_OSI_DEBUG_LEVEL > 3
761 debugMessage("End OsiMskSolverInterface::getDblParam(%d)\n", key);
762 #endif
763
764 return retval;
765 }
766
767 //-----------------------------------------------------------------------------
768 // Gets a string parameter from MOSEK
769
770 bool
771 OsiMskSolverInterface::getStrParam(OsiStrParam key, std::string & value) const
772 {
773 #if MSK_OSI_DEBUG_LEVEL > 3
774 debugMessage("Begin OsiMskSolverInterface::getStrParam(%d)\n", key);
775 #endif
776
777 switch (key)
778 {
779 case OsiProbName:
780 OsiSolverInterface::getStrParam(key, value);
781 break;
782 case OsiSolverName:
783 value = "MOSEK";
784 break;
785 case OsiLastStrParam:
786 return false;
787 }
788
789 #if MSK_OSI_DEBUG_LEVEL > 3
790 debugMessage("End OsiMskSolverInterface::getStrParam(%d)\n", key);
791 #endif
792
793 return true;
794 }
795
796 //#############################################################################
797 // Methods returning info on how the solution process terminated
798 //#############################################################################
799
800 //-----------------------------------------------------------------------------
801 // Returns true if solver abandoned in last call to solver.
802 // Mosek does not use this functionality
803
804 bool OsiMskSolverInterface::isAbandoned() const
805 {
806 #if MSK_OSI_DEBUG_LEVEL > 3
807 debugMessage("Begin OsiMskSolverInterface::isAbandoned()\n");
808 debugMessage("isAbandoned() %d\n",(Mskerr != MSK_RES_OK));
809 debugMessage("End OsiMskSolverInterface::isAbandoned()\n");
810 #endif
811
812 return (Mskerr != MSK_RES_OK);
813 }
814
815 //-----------------------------------------------------------------------------
816 // Returns true if "solution" available is proved to be optimal, where "solution" in LP
817 // could be both interior and basic, checks for both.
818
819 bool OsiMskSolverInterface::isProvenOptimal() const
820 {
821 #if MSK_OSI_DEBUG_LEVEL > 3
822 debugMessage("Begin OsiMskSolverInterface::isProvenOptimal()\n");
823 #endif
824
825 int err;
826 MSKsolstae status;
827 MSKsoltypee solution;
828
829 if( probtypemip_ == false)
830 {
831 if( definedSolution( MSK_SOL_BAS ) == true )
832 solution = MSK_SOL_BAS;
833 else
834 {
835 if( definedSolution( MSK_SOL_ITR ) == true )
836 solution = MSK_SOL_ITR;
837 else
838 return false;
839 }
840 }
841 else
842 {
843 if( definedSolution( MSK_SOL_ITG ) == true )
844 solution = MSK_SOL_ITG;
845 else
846 return false;
847 }
848
849 err = MSK_getsolution(getMutableLpPtr(),
850 solution,
851 NULL,
852 &status,
853 NULL,
854 NULL,
855 NULL,
856 NULL,
857 NULL,
858 NULL,
859 NULL,
860 NULL,
861 NULL,
862 NULL,
863 NULL);
864
865 checkMSKerror(err,"MSK_getsolution","isProvenOptimal");
866
867 #if MSK_OSI_DEBUG_LEVEL > 3
868 debugMessage("Solution type , %d status %d \n ",(status == MSK_SOL_STA_OPTIMAL), status) ;
869 debugMessage("End OsiMskSolverInterface::isProvenOptimal()\n");
870 #endif
871
872 return ( status == MSK_SOL_STA_OPTIMAL || status == MSK_SOL_STA_INTEGER_OPTIMAL);
873 }
874
875 //-----------------------------------------------------------------------------
876 // Returns true if a certificate of primal inf. exits
877
878 bool OsiMskSolverInterface::isProvenPrimalInfeasible() const
879 {
880 #if MSK_OSI_DEBUG_LEVEL > 3
881 debugMessage("Begin OsiMskSolverInterface::isProvenPrimalInfeasible()\n");
882 #endif
883
884 int err;
885 MSKsolstae status;
886 MSKsoltypee solution;
887
888 if( probtypemip_ == false)
889 {
890 if( definedSolution( MSK_SOL_BAS ) == true )
891 solution = MSK_SOL_BAS;
892 else
893 {
894 if( definedSolution( MSK_SOL_ITR ) == true )
895 solution = MSK_SOL_ITR;
896 else
897 return false;
898 }
899 }
900 else
901 {
902 if( definedSolution( MSK_SOL_ITG ) == true)
903 solution = MSK_SOL_ITG;
904 else
905 return false;
906 }
907
908 err = MSK_getsolution(getMutableLpPtr(),
909 solution,
910 NULL,
911 &status,
912 NULL,
913 NULL,
914 NULL,
915 NULL,
916 NULL,
917 NULL,
918 NULL,
919 NULL,
920 NULL,
921 NULL,
922 NULL);
923
924 checkMSKerror(err,"MSK_getsolution","isProvenPrimalInfeasible");
925
926 #if MSK_OSI_DEBUG_LEVEL > 3
927 debugMessage("isProvenPrimalInfeasible %d \n",(status == MSK_SOL_STA_PRIM_INFEAS_CER));
928 debugMessage("End OsiMskSolverInterface::isProvenPrimalInfeasible()\n");
929 #endif
930
931 return ( status == MSK_SOL_STA_PRIM_INFEAS_CER );
932 }
933
934 //-----------------------------------------------------------------------------
935 // Should return true if a certificate of dual inf. exits
936 // But COIN does not support this feature thus we return false
937
938 bool OsiMskSolverInterface::isProvenDualInfeasible() const
939 {
940 #if MSK_OSI_DEBUG_LEVEL > 3
941 debugMessage("Begin OsiMskSolverInterface::isProvenDualInfeasible()\n");
942 #endif
943
944 int err;
945 MSKsolstae status;
946 MSKsoltypee solution;
947
948 if( probtypemip_ == false )
949 {
950 if( definedSolution( MSK_SOL_BAS ) == true )
951 solution = MSK_SOL_BAS;
952 else
953 {
954 if( definedSolution( MSK_SOL_ITR ) == true )
955 solution = MSK_SOL_ITR;
956 else
957 return false;
958 }
959 }
960 else
961 {
962 if( definedSolution( MSK_SOL_ITG ) == true)
963 solution = MSK_SOL_ITG;
964 else
965 return false;
966 }
967
968 err = MSK_getsolution(getMutableLpPtr(),
969 solution,
970 NULL,
971 &status,
972 NULL,
973 NULL,
974 NULL,
975 NULL,
976 NULL,
977 NULL,
978 NULL,
979 NULL,
980 NULL,
981 NULL,
982 NULL
983 );
984
985 checkMSKerror(err,"MSK_getsolution","isProvenDualInfeasible");
986
987 #if MSK_OSI_DEBUG_LEVEL > 3
988 debugMessage("isProvenDualInfeasible %d \n",(status == MSK_SOL_STA_DUAL_INFEAS_CER));
989 debugMessage("End OsiMskSolverInterface::isProvenDualInfeasible()\n");
990 #endif
991
992 return ( status == MSK_SOL_STA_DUAL_INFEAS_CER );
993 }
994
995 //-----------------------------------------------------------------------------
996 // Returns true if primal objective limit is reached. Checks the objective sense
997 // first.
998
999 bool OsiMskSolverInterface::isPrimalObjectiveLimitReached() const
1000 {
1001 #if MSK_OSI_DEBUG_LEVEL > 3
1002 debugMessage("Begin OsiMskSolverInterface::isPrimalObjectiveLimitReached()\n");
1003 #endif
1004
1005 if( Mskerr == MSK_RES_TRM_OBJECTIVE_RANGE )
1006 {
1007 int err;
1008 double obj = getObjValue(),value=MSK_INFINITY;
1009 if( getObjSense() == +1 )
1010 {
1011 err = MSK_getdouparam( getMutableLpPtr(),
1012 MSK_DPAR_LOWER_OBJ_CUT,
1013 &value);
1014
1015 checkMSKerror(err,"MSK_getdouparam","isPrimalObjectiveLimitReached");
1016 }
1017 else
1018 {
1019 err = MSK_getdouparam(getMutableLpPtr(),
1020 MSK_DPAR_UPPER_OBJ_CUT,
1021 &value );
1022
1023 checkMSKerror(err,"MSK_getdouparam","isPrimalObjectiveLimitReached");
1024
1025 obj = -obj;
1026 value = -value;
1027 }
1028
1029 #if MSK_OSI_DEBUG_LEVEL > 3
1030 debugMessage("primal objective value %-16.10e , lowerbound %-16.10e , reached %d \n",obj,value,(value <= obj));
1031 debugMessage("End OsiMskSolverInterface::isPrimalObjectiveLimitReached()\n");
1032 #endif
1033
1034 return ( !( value == MSK_INFINITY || value == -MSK_INFINITY ) && value > obj );
1035 }
1036 else
1037 {
1038 if( Mskerr == MSK_RES_OK )
1039 {
1040 if( definedSolution( MSK_SOL_BAS ) == true )
1041 {
1042 int err;
1043 double obj = getObjValue(),value=MSK_INFINITY;
1044
1045 if( getObjSense() == +1 )
1046 {
1047 err = MSK_getdouparam( getMutableLpPtr(),
1048 MSK_DPAR_LOWER_OBJ_CUT,
1049 &value);
1050
1051 checkMSKerror(err,"MSK_getdouparam","isPrimalObjectiveLimitReached");
1052
1053 return (obj < value);
1054 }
1055 else
1056 {
1057 err = MSK_getdouparam(getMutableLpPtr(),
1058 MSK_DPAR_UPPER_OBJ_CUT,
1059 &value );
1060
1061 checkMSKerror(err,"MSK_getdouparam","isPrimalObjectiveLimitReached");
1062
1063 obj = -obj;
1064 value = -value;
1065
1066 return (obj < value);
1067 }
1068 }
1069 }
1070
1071 #if MSK_OSI_DEBUG_LEVEL > 3
1072 debugMessage("End OsiMskSolverInterface::isPrimalObjectiveLimitReached()\n");
1073 #endif
1074
1075 return false;
1076 }
1077 }
1078
1079 //-----------------------------------------------------------------------------
1080 // Returns true if dual objective limit is reached. Checks the objective sense
1081 // first.
1082
1083 bool OsiMskSolverInterface::isDualObjectiveLimitReached() const
1084 {
1085 #if MSK_OSI_DEBUG_LEVEL > 3
1086 debugMessage("Begin OsiMskSolverInterface::isDualObjectiveLimitReached()\n");
1087 #endif
1088
1089 if( Mskerr == MSK_RES_TRM_OBJECTIVE_RANGE )
1090 {
1091 int err;
1092 double obj = getObjValue(),value=MSK_INFINITY;
1093 if( getObjSense() == +1 )
1094 {
1095 err = MSK_getdouparam( getMutableLpPtr(),
1096 MSK_DPAR_UPPER_OBJ_CUT,
1097 &value);
1098
1099 checkMSKerror(err,"MSK_getdouparam","isDualObjectiveLimitReached");
1100 }
1101 else
1102 {
1103 err = MSK_getdouparam( getMutableLpPtr(),
1104 MSK_DPAR_LOWER_OBJ_CUT,
1105 &value );
1106
1107 checkMSKerror(err,"MSK_getdouparam","isDualObjectiveLimitReached");
1108
1109 obj = -obj;
1110 value = -value;
1111 }
1112
1113 checkMSKerror( err, "MSK_getdouparam", "isPrimalObjectiveLimitReached" );
1114
1115 #if MSK_OSI_DEBUG_LEVEL > 3
1116 debugMessage("dual objective value %f , lowerbound %f , reached %i \n",obj,value,(value <= obj));
1117 debugMessage("End OsiMskSolverInterface::isDualObjectiveLimitReached()\n");
1118 #endif
1119
1120 return ( !( value == MSK_INFINITY || value == -MSK_INFINITY ) && value <= obj );
1121 }
1122 else
1123 {
1124 if( Mskerr == MSK_RES_OK )
1125 {
1126 if( definedSolution( MSK_SOL_BAS ) == true )
1127 {
1128 int err;
1129 double obj = getObjValue(),value=MSK_INFINITY;
1130
1131 if( getObjSense() == +1 )
1132 {
1133 err = MSK_getdouparam( getMutableLpPtr(),
1134 MSK_DPAR_UPPER_OBJ_CUT,
1135 &value);
1136
1137 checkMSKerror(err,"MSK_getdouparam","isDualObjectiveLimitReached");
1138
1139 return (obj > value);
1140 }
1141 else
1142 {
1143 err = MSK_getdouparam(getMutableLpPtr(),
1144 MSK_DPAR_LOWER_OBJ_CUT,
1145 &value );
1146
1147 checkMSKerror(err,"MSK_getdouparam","isDualObjectiveLimitReached");
1148
1149 obj = -obj;
1150 value = -value;
1151
1152 return (obj > value);
1153 }
1154 }
1155 }
1156
1157 #if MSK_OSI_DEBUG_LEVEL > 3
1158 debugMessage("End OsiMskSolverInterface::isDualObjectiveLimitReached()\n");
1159 #endif
1160
1161 return false;
1162 }
1163 }
1164
1165 //-----------------------------------------------------------------------------
1166 // Returns true if iteration number used in last call to optimize eq. max number for
1167 // the solver used.
1168
1169 bool OsiMskSolverInterface::isIterationLimitReached() const
1170 {
1171 #if MSK_OSI_DEBUG_LEVEL > 3
1172 debugMessage("Begin OsiMskSolverInterface::isIterationLimitReached()\n");
1173 #endif
1174
1175 #if MSK_OSI_DEBUG_LEVEL > 3
1176 debugMessage("iteration limit reached %d \n",Mskerr);
1177 debugMessage("End OsiMskSolverInterface::isIterationLimitReached()\n");
1178 #endif
1179
1180 return Mskerr == MSK_RES_TRM_MAX_ITERATIONS;
1181 }
1182
1183 //-----------------------------------------------------------------------------
1184 // Returns true if a license problem occured in last call to optimize.
1185
1186 bool OsiMskSolverInterface::isLicenseError() const
1187 {
1188 #if MSK_OSI_DEBUG_LEVEL > 3
1189 debugMessage("Begin OsiMskSolverInterface::isLicenseError()\n");
1190 #endif
1191
1192 #if MSK_OSI_DEBUG_LEVEL > 3
1193 debugMessage("license error %d \n",Mskerr);
1194 debugMessage("End OsiMskSolverInterface::isLicenseError()\n");
1195 #endif
1196
1197 return Mskerr >= MSK_RES_ERR_LICENSE && Mskerr <= MSK_RES_ERR_LICENSE_NO_SERVER_SUPPORT;
1198 }
1199
1200
1201 //#############################################################################
1202 // WarmStart related methods
1203 //#############################################################################
1204
1205 // Get an empty warm start object
1206 CoinWarmStart* OsiMskSolverInterface::getEmptyWarmStart () const
1207 { return (dynamic_cast<CoinWarmStart *>(new CoinWarmStartBasis())) ; }
1208
1209 //-----------------------------------------------------------------------------
1210 // Get warm start, returns NULL pointer if not available
1211
1212 CoinWarmStart* OsiMskSolverInterface::getWarmStart() const
1213 {
1214
1215 CoinWarmStartBasis* ws = NULL;
1216 int numbas = 0,numcols = getNumCols(),numrows = getNumRows(),*skx,*skc,
1217 err, i,*bkc,*bkx;
1218 double *blc,*buc,*blx,*bux,*xc,*xx;
1219
1220 bool skip = false;
1221
1222 #if MSK_OSI_DEBUG_LEVEL > 1
1223 debugMessage("Begin OsiMskSolverInterface::getWarmStart numcols %d numrows %d %p\n",numcols,numrows, (void*)this);
1224 #endif
1225
1226 skx = new int[numcols];
1227 skc = new int[numrows];
1228
1229 bkc = new int[numrows];
1230 blc = new double[numrows];
1231 buc = new double[numrows];
1232 xc = new double[numrows];
1233
1234 bkx = new int[numcols];
1235 blx = new double[numcols];
1236 bux = new double[numcols];
1237 xx = new double[numcols];
1238
1239 #if MSK_VERSION_MAJOR < 9
1240 err = MSK_getboundslice(getMutableLpPtr(),
1241 MSK_ACC_CON,
1242 0,
1243 numrows,
1244 (MSKboundkeye*)bkc,
1245 (MSKrealt*)blc,
1246 (MSKrealt*)buc);
1247
1248 checkMSKerror( err, "MSK_getboundslice", "getWarmStart" );
1249
1250 err = MSK_getboundslice(getMutableLpPtr(),
1251 MSK_ACC_VAR,
1252 0,
1253 numcols,
1254 (MSKboundkeye*)bkx,
1255 (MSKrealt*)blx,
1256 (MSKrealt*)bux);
1257
1258 checkMSKerror( err, "MSK_getboundslice", "getWarmStart" );
1259 #else
1260 err = MSK_getconboundslice(getMutableLpPtr(),
1261 0,
1262 numrows,
1263 (MSKboundkeye*)bkc,
1264 (MSKrealt*)blc,
1265 (MSKrealt*)buc);
1266
1267 checkMSKerror( err, "getconboundslice", "getWarmStart" );
1268
1269 err = MSK_getvarboundslice(getMutableLpPtr(),
1270 0,
1271 numcols,
1272 (MSKboundkeye*)bkx,
1273 (MSKrealt*)blx,
1274 (MSKrealt*)bux);
1275
1276 checkMSKerror( err, "MSK_getvarboundslice", "getWarmStart" );
1277 #endif
1278
1279 MSKassert(3,!probtypemip_,"!probtypemip_","getWarmStart");
1280
1281 if( definedSolution( MSK_SOL_BAS ) == true )
1282 {
1283 err = MSK_getsolution(getMutableLpPtr(),
1284 MSK_SOL_BAS,
1285 NULL,
1286 NULL,
1287 (MSKstakeye*)skc,
1288 (MSKstakeye*)skx,
1289 NULL,
1290 NULL,
1291 NULL,
1292 NULL,
1293 NULL,
1294 NULL,
1295 NULL,
1296 NULL,
1297 NULL);
1298
1299 checkMSKerror( err, "MSK_getsolution", "getWarmStart" );
1300 }
1301 else
1302 {
1303 #if MSK_OSI_DEBUG_LEVEL > 1
1304 debugMessage("No basic solution in OsiMskSolverInterface::getWarmStart()\n");
1305 #endif
1306
1307 /* No basic solution stored choose slack basis */
1308 /* Otherwise the unittest can not be passed */
1309 ws = new CoinWarmStartBasis;
1310 ws->setSize( numcols, numrows );
1311
1312 for( i = 0; i < numrows; ++i )
1313 ws->setArtifStatus( i, CoinWarmStartBasis::basic );
1314
1315 numbas = numrows;
1316
1317 for( i = 0; i < numcols; ++i )
1318 {
1319 switch(bkx[i])
1320 {
1321 case MSK_BK_RA:
1322 case MSK_BK_LO:
1323 ws->setStructStatus( i, CoinWarmStartBasis::atLowerBound );
1324 break;
1325 case MSK_BK_FX:
1326 case MSK_BK_UP:
1327 ws->setStructStatus( i, CoinWarmStartBasis::atUpperBound );
1328 break;
1329 case MSK_BK_FR:
1330 ws->setStructStatus( i, CoinWarmStartBasis::isFree );
1331 break;
1332 default:
1333 checkMSKerror( 1, "Wrong bound key", "getWarmStart" );
1334 }
1335 }
1336
1337 #if MSK_OSI_DEBUG_LEVEL > 1
1338 debugMessage("End OsiMskSolverInterface::getWarmStart()\n");
1339 #endif
1340
1341 delete[] skc;
1342 delete[] skx;
1343
1344 delete[] bkc;
1345 delete[] blc;
1346 delete[] buc;
1347 delete[] xc;
1348
1349 delete[] bkx;
1350 delete[] blx;
1351 delete[] bux;
1352 delete[] xx;
1353
1354 /* Leave function */
1355 return ws;
1356 }
1357
1358 if( err == MSK_RES_OK )
1359 {
1360 /* Status keys should be defined */
1361 ws = new CoinWarmStartBasis;
1362
1363 MSKassert(3,ws != NULL,"1) ws != NULL","getWarmStart");
1364
1365 ws->setSize( numcols, numrows );
1366
1367 for( i = 0; i < numrows && ws != NULL; ++i )
1368 {
1369 switch( skc[i] )
1370 {
1371 case MSK_SK_UNK:
1372 case MSK_SK_BAS:
1373 /* Warning : Slacks in Osi and Mosek er negated */
1374 ws->setArtifStatus( i, CoinWarmStartBasis::basic );
1375 ++numbas;
1376 break;
1377 case MSK_SK_LOW:
1378 /* Warning : Slacks in Osi and Mosek er negated */
1379 ws->setArtifStatus( i, CoinWarmStartBasis::atUpperBound );
1380 break;
1381 case MSK_SK_FIX:
1382 case MSK_SK_UPR:
1383 /* Warning : Slacks in Osi and Mosek er negated */
1384 ws->setArtifStatus( i, CoinWarmStartBasis::atLowerBound );
1385 break;
1386 case MSK_SK_SUPBAS:
1387 ws->setArtifStatus( i, CoinWarmStartBasis::isFree );
1388 break;
1389 default: // unknown row status
1390 delete ws;
1391 ws = NULL;
1392 skip = true;
1393 checkMSKerror( 1, "Wrong slack status key", "getWarmStart" );
1394 break;
1395 }
1396 }
1397
1398 if( skip == false )
1399 {
1400 for( i = 0; i < numcols && ws != NULL; ++i )
1401 {
1402 switch( skx[i] )
1403 {
1404 case MSK_SK_UNK:
1405 case MSK_SK_BAS:
1406 ++numbas;
1407 ws->setStructStatus( i, CoinWarmStartBasis::basic );
1408 break;
1409 case MSK_SK_FIX:
1410 case MSK_SK_LOW:
1411 ws->setStructStatus( i, CoinWarmStartBasis::atLowerBound );
1412 break;
1413 case MSK_SK_UPR:
1414 ws->setStructStatus( i, CoinWarmStartBasis::atUpperBound );
1415 break;
1416 case MSK_SK_SUPBAS:
1417 ws->setStructStatus( i, CoinWarmStartBasis::isFree );
1418 break;
1419 default: // unknown column status
1420 delete ws;
1421 ws = NULL;
1422 checkMSKerror( 1, "Wrong variable status key", "getWarmStart" );
1423 break;
1424 }
1425 }
1426 }
1427 }
1428
1429 delete[] skx;
1430 delete[] skc;
1431
1432 delete[] bkc;
1433 delete[] blc;
1434 delete[] buc;
1435 delete[] xc;
1436
1437 delete[] bkx;
1438 delete[] blx;
1439 delete[] bux;
1440 delete[] xx;
1441
1442 MSKassert(3,ws!=NULL,"2) ws!=NULL","getWarmStart");
1443
1444 #if MSK_OSI_DEBUG_LEVEL > 1
1445 debugMessage("End OsiMskSolverInterface::getWarmStart() (%p) numcols %d numrows %d numbas %d\n",(void*)(ws),numcols,numrows,numbas);
1446 #endif
1447
1448 return ws;
1449 }
1450
1451 //-----------------------------------------------------------------------------
1452 // Set warm start
1453
1454 bool OsiMskSolverInterface::setWarmStart(const CoinWarmStart* warmstart)
1455 {
1456
1457 const CoinWarmStartBasis* ws = dynamic_cast<const CoinWarmStartBasis*>(warmstart);
1458 int numcols, numrows, i, restat,numbas=0;
1459 int *skx, *skc, *bkc, *bkx;
1460 bool retval = false, skip = false;
1461
1462 if( !ws )
1463 return false;
1464
1465 numcols = ws->getNumStructural();
1466 numrows = ws->getNumArtificial();
1467
1468 #if MSK_OSI_DEBUG_LEVEL > 1
1469 debugMessage("Begin OsiMskSolverInterface::setWarmStart(%p) this = %p numcols %d numrows %d\n", (void *)warmstart,(void*)this,numcols,numrows);
1470 #endif
1471
1472 if( numcols != getNumCols() || numrows != getNumRows() )
1473 return false;
1474
1475 switchToLP();
1476
1477 skx = new int[numcols];
1478 skc = new int[numrows];
1479 bkc = new int[numrows];
1480
1481 #if MSK_VERSION_MAJOR < 9
1482 restat = MSK_getboundslice(getMutableLpPtr(),
1483 MSK_ACC_CON,
1484 0,
1485 numrows,
1486 (MSKboundkeye*) (bkc),
1487 NULL,
1488 NULL);
1489
1490 checkMSKerror( restat, "MSK_getboundslice", "setWarmStart" );
1491 #else
1492 restat = MSK_getconboundslice(getMutableLpPtr(),
1493 0,
1494 numrows,
1495 (MSKboundkeye*) (bkc),
1496 NULL,
1497 NULL);
1498
1499 checkMSKerror( restat, "MSK_getconboundslice", "setWarmStart" );
1500 #endif
1501
1502 for( i = 0; i < numrows; ++i )
1503 {
1504 switch( ws->getArtifStatus( i ) )
1505 {
1506 case CoinWarmStartBasis::basic:
1507 skc[i] = MSK_SK_BAS;
1508 ++numbas;
1509 break;
1510 /* Warning : Slacks in Osi and Mosek er negated */
1511 case CoinWarmStartBasis::atUpperBound:
1512 switch(bkc[i])
1513 {
1514 case MSK_BK_LO:
1515 case MSK_BK_RA:
1516 skc[i] = MSK_SK_LOW;
1517 break;
1518 case MSK_BK_FX:
1519 skc[i] = MSK_SK_FIX;
1520 break;
1521 default:
1522 skc[i] = MSK_SK_UNK;
1523 break;
1524 }
1525 break;
1526 /* Warning : Slacks in Osi and Mosek er negated */
1527 case CoinWarmStartBasis::atLowerBound:
1528 switch(bkc[i])
1529 {
1530 case MSK_BK_UP:
1531 case MSK_BK_RA:
1532 skc[i] = MSK_SK_UPR;
1533 break;
1534 case MSK_BK_FX:
1535 skc[i] = MSK_SK_FIX;
1536 break;
1537 default:
1538 skc[i] = MSK_SK_UNK;
1539 break;
1540 }
1541 break;
1542 case CoinWarmStartBasis::isFree:
1543 skc[i] = MSK_SK_SUPBAS;
1544 break;
1545 default: // unknown row status
1546 retval = false;
1547 skip = true;
1548 MSKassert(3,1,"Unkown rowstatus","setWarmStart");
1549 break;
1550 }
1551 }
1552
1553 delete[] bkc;
1554
1555 if( skip == false )
1556 {
1557 bkx = new int[numcols];
1558
1559 #if MSK_VERSION_MAJOR < 9
1560 restat = MSK_getboundslice(getMutableLpPtr(),
1561 MSK_ACC_VAR,
1562 0,
1563 numcols,
1564 (MSKboundkeye*) (bkx),
1565 NULL,
1566 NULL);
1567
1568 checkMSKerror( restat, "MSK_getboundslice", "setWarmStart" );
1569 #else
1570 restat = MSK_getvarboundslice(getMutableLpPtr(),
1571 0,
1572 numcols,
1573 (MSKboundkeye*) (bkx),
1574 NULL,
1575 NULL);
1576
1577 checkMSKerror( restat, "MSK_getvarboundslice", "setWarmStart" );
1578 #endif
1579
1580 for( i = 0; i < numcols; ++i )
1581 {
1582 switch( ws->getStructStatus( i ) )
1583 {
1584 case CoinWarmStartBasis::basic:
1585 skx[i] = MSK_SK_BAS;
1586 ++numbas;
1587 break;
1588 case CoinWarmStartBasis::atLowerBound:
1589 switch(bkx[i])
1590 {
1591 case MSK_BK_LO:
1592 case MSK_BK_RA:
1593 skx[i] = MSK_SK_LOW;
1594 break;
1595 case MSK_BK_FX:
1596 skx[i] = MSK_SK_FIX;
1597 default:
1598 skx[i] = MSK_SK_UNK;
1599 break;
1600 }
1601 break;
1602 case CoinWarmStartBasis::atUpperBound:
1603 switch(bkx[i])
1604 {
1605 case MSK_BK_UP:
1606 case MSK_BK_RA:
1607 skx[i] = MSK_SK_UPR;
1608 break;
1609 case MSK_BK_FX:
1610 skx[i] = MSK_SK_FIX;
1611 default:
1612 skx[i] = MSK_SK_UNK;
1613 break;
1614 }
1615 break;
1616 case CoinWarmStartBasis::isFree:
1617 skx[i] = MSK_SK_SUPBAS;
1618 break;
1619 default: // unknown col status
1620 retval = false;
1621 skip = true;
1622 MSKassert(3,1,"Unkown col status","setWarmStart");
1623 break;
1624 }
1625 }
1626
1627 delete[] bkx;
1628 }
1629
1630 #if MSK_OSI_DEBUG_LEVEL > 1
1631 debugMessage("OsiMskSolverInterface::setWarmStart(%p) numcols %d numrows %d numbas %d\n", (void *)warmstart,numcols,numrows,numbas);
1632 #endif
1633
1634 #if 0
1635 MSKassert(3,numbas == numrows,"Wrong number of basis variables","setWarmStart");
1636 #endif
1637
1638 if( skip == false )
1639 {
1640 restat = MSK_putsolution( getLpPtr( OsiMskSolverInterface::FREECACHED_RESULTS ),
1641 MSK_SOL_BAS,
1642 (MSKstakeye*) (skc),
1643 (MSKstakeye*) (skx),
1644 NULL,
1645 NULL,
1646 NULL,
1647 NULL,
1648 NULL,
1649 NULL,
1650 NULL,
1651 NULL,
1652 NULL);
1653
1654 delete[] skx;
1655 delete[] skc;
1656 }
1657 else
1658 {
1659 #if MSK_OSI_DEBUG_LEVEL > 1
1660 debugMessage("Skipping setting values in OsiMskSolverInterface::setWarmStart()\n");
1661 #endif
1662
1663 #if MSK_OSI_DEBUG_LEVEL > 1
1664 debugMessage("End OsiMskSolverInterface::setWarmStart(%p)\n", (void *)warmstart);
1665 #endif
1666
1667 delete[] skx;
1668 delete[] skc;
1669
1670 return false;
1671 }
1672
1673 retval = (restat == MSK_RES_OK);
1674
1675 #if MSK_OSI_DEBUG_LEVEL > 1
1676 debugMessage("End OsiMskSolverInterface::setWarmStart(%p)\n", (void *)warmstart);
1677 #endif
1678
1679 return retval;
1680 }
1681
1682 //#############################################################################
1683 // Hotstart related methods (primarily used in strong branching)
1684 //#############################################################################
1685
1686 //-----------------------------------------------------------------------------
1687 // Mark hot start
1688
1689 void OsiMskSolverInterface::markHotStart()
1690 {
1691 #if MSK_OSI_DEBUG_LEVEL > 1
1692 debugMessage("Begin OsiMskSolverInterface::markHotStart()\n");
1693 #endif
1694
1695 int err;
1696 int numcols, numrows;
1697
1698 MSKassert(3,!probtypemip_,"probtypemip_","markHotStart");
1699
1700 numcols = getNumCols();
1701 numrows = getNumRows();
1702
1703 if( numcols > hotStartCStatSize_ )
1704 {
1705 delete[] hotStartCStat_;
1706 hotStartCStatSize_ = static_cast<int>( 1.0 * static_cast<double>( numcols ) );
1707 // get some extra space for future hot starts
1708 hotStartCStat_ = new int[hotStartCStatSize_];
1709 }
1710
1711 if( numrows > hotStartRStatSize_ )
1712 {
1713 delete[] hotStartRStat_;
1714 hotStartRStatSize_ = static_cast<int>( 1.0 * static_cast<double>( numrows ) );
1715 // get some extra space for future hot starts
1716 hotStartRStat_ = new int[hotStartRStatSize_];
1717 }
1718
1719 err = MSK_getsolution(getMutableLpPtr(),
1720 MSK_SOL_BAS,
1721 NULL,
1722 NULL,
1723 (MSKstakeye*) (hotStartRStat_),
1724 (MSKstakeye*) (hotStartCStat_),
1725 NULL,
1726 NULL,
1727 NULL,
1728 NULL,
1729 NULL,
1730 NULL,
1731 NULL,
1732 NULL,
1733 NULL);
1734
1735 checkMSKerror( err, "MSK_getsolution", "markHotStart" );
1736
1737 #if MSK_OSI_DEBUG_LEVEL > 1
1738 debugMessage("End OsiMskSolverInterface::markHotStart()\n");
1739 #endif
1740 }
1741
1742 //-----------------------------------------------------------------------------
1743 // Solve from a hot start
1744
1745 void OsiMskSolverInterface::solveFromHotStart()
1746 {
1747 #if MSK_OSI_DEBUG_LEVEL > 1
1748 debugMessage("Begin OsiMskSolverInterface::solveFromHotStart()\n");
1749 #endif
1750
1751 int err;
1752
1753 int maxiter;
1754
1755
1756 switchToLP();
1757
1758 MSKassert(3,getNumCols() <= hotStartCStatSize_,"getNumCols() <= hotStartCStatSize_","solveFromHotStart");
1759 MSKassert(3,getNumRows() <= hotStartRStatSize_,"getNumRows() <= hotStartRStatSize_","solveFromHotStart");
1760
1761 err = MSK_putsolution(getLpPtr( OsiMskSolverInterface::FREECACHED_RESULTS ),
1762 MSK_SOL_BAS,
1763 (MSKstakeye*) (hotStartRStat_),
1764 (MSKstakeye*) (hotStartCStat_),
1765 NULL,
1766 NULL,
1767 NULL,
1768 NULL,
1769 NULL,
1770 NULL,
1771 NULL,
1772 NULL,
1773 NULL);
1774
1775
1776 checkMSKerror( err, "MSK_putsolution", "solveFromHotStart" );
1777
1778 err = MSK_getintparam( getMutableLpPtr(), MSK_IPAR_SIM_MAX_ITERATIONS, &maxiter );
1779 checkMSKerror( err, "MSK_getintparam", "solveFromHotStart" );
1780
1781 err = MSK_putintparam( getMutableLpPtr(), MSK_IPAR_SIM_MAX_ITERATIONS, hotStartMaxIteration_ );
1782 checkMSKerror( err, "MSK_putintparam", "solveFromHotStart" );
1783
1784 resolve();
1785
1786 err = MSK_putintparam( getMutableLpPtr(), MSK_IPAR_SIM_MAX_ITERATIONS, maxiter );
1787
1788 checkMSKerror( err, "MSK_putintparam", "solveFromHotStart" );
1789
1790 #if MSK_OSI_DEBUG_LEVEL > 1
1791 debugMessage("End OsiMskSolverInterface::solveFromHotStart()\n");
1792 #endif
1793 }
1794
1795 //-----------------------------------------------------------------------------
1796 // Unmark a hot start
1797
1798 void OsiMskSolverInterface::unmarkHotStart()
1799 {
1800 #if MSK_OSI_DEBUG_LEVEL > 1
1801 debugMessage("Begin OsiMskSolverInterface::unmarkHotStart()\n");
1802 #endif
1803
1804 freeCachedData();
1805
1806 if( hotStartCStat_ != NULL )
1807 delete[] hotStartCStat_;
1808
1809 if( hotStartRStat_ != NULL )
1810 delete[] hotStartRStat_;
1811
1812 hotStartCStat_ = NULL;
1813 hotStartCStatSize_ = 0;
1814 hotStartRStat_ = NULL;
1815 hotStartRStatSize_ = 0;
1816
1817 #if MSK_OSI_DEBUG_LEVEL > 1
1818 debugMessage("End OsiMskSolverInterface::unmarkHotStart()\n");
1819 #endif
1820 }
1821
1822 //#############################################################################
1823 // Problem information methods (original data)
1824 //#############################################################################
1825
1826
1827 //-----------------------------------------------------------------------------
1828 // Returns number of columns in MOSEK task
1829
1830 int OsiMskSolverInterface::getNumCols() const
1831 {
1832 #if MSK_OSI_DEBUG_LEVEL > 3
1833 debugMessage("Begin OsiMskSolverInterface::getNumCols()\n");
1834 #endif
1835
1836 int numcol, err;
1837 err = MSK_getnumvar(getMutableLpPtr(),&numcol);
1838 checkMSKerror( err, "MSK_getnumvar", "getNumCols" );
1839
1840 #if MSK_OSI_DEBUG_LEVEL > 3
1841 debugMessage("End OsiMskSolverInterface::getNumCols()\n");
1842 #endif
1843
1844 return numcol;
1845 }
1846
1847 //-----------------------------------------------------------------------------
1848 // Returns number of rows in MOSEK task
1849
1850 int OsiMskSolverInterface::getNumRows() const
1851 {
1852 #if MSK_OSI_DEBUG_LEVEL > 3
1853 debugMessage("Begin OsiMskSolverInterface::getNumRows()\n");
1854 #endif
1855
1856 int numrow, err;
1857 err = MSK_getnumcon(getMutableLpPtr(),&numrow);
1858 checkMSKerror( err, "MSK_getnumcon", "getNumRows" );
1859
1860 #if MSK_OSI_DEBUG_LEVEL > 3
1861 debugMessage("End OsiMskSolverInterface::getNumRows()\n");
1862 #endif
1863
1864 return numrow;
1865 }
1866
1867 //-----------------------------------------------------------------------------
1868 // Returns number of non-zeroes (in matrix) in MOSEK task
1869
1870 int OsiMskSolverInterface::getNumElements() const
1871 {
1872 #if MSK_OSI_DEBUG_LEVEL > 3
1873 debugMessage("Begin OsiMskSolverInterface::getNumElements()\n");
1874 #endif
1875
1876 int numnon, err;
1877 err = MSK_getnumanz(getMutableLpPtr(),&numnon);
1878 checkMSKerror( err, "MSK_getnumanz", "getNumElements" );
1879
1880 #if MSK_OSI_DEBUG_LEVEL > 3
1881 debugMessage("End OsiMskSolverInterface::getNumElements()\n");
1882 #endif
1883
1884 return numnon;
1885 }
1886
1887
1888 //-----------------------------------------------------------------------------
1889 // Returns lower bounds on columns in MOSEK task
1890
1891 const double * OsiMskSolverInterface::getColLower() const
1892 {
1893 #if MSK_OSI_DEBUG_LEVEL > 3
1894 debugMessage("Begin OsiMskSolverInterface::getColLower()\n");
1895 #endif
1896
1897 if(collower_ == NULL )
1898 {
1899 int ncols = getNumCols();
1900
1901 MSKassert(3,colupper_ == NULL,"colupper_","getColLower");
1902
1903 if( ncols > 0 )
1904 {
1905 if(colupper_ == NULL)
1906 colupper_ = new double[ncols];
1907
1908 if(collower_ == NULL)
1909 collower_ = new double[ncols];
1910
1911 int *dummy_tags = new int[ncols];
1912
1913 #if MSK_VERSION_MAJOR < 9
1914 int err = MSK_getboundslice(getMutableLpPtr(),
1915 MSK_ACC_VAR,
1916 (MSKidxt)0,
1917 (MSKidxt)ncols,
1918 (MSKboundkeye*) (dummy_tags),
1919 collower_,
1920 colupper_);
1921
1922 checkMSKerror(err, "MSK_getboundslice","getColUpper");
1923 #else
1924 int err = MSK_getvarboundslice(getMutableLpPtr(),
1925 0,
1926 ncols,
1927 (MSKboundkeye*) (dummy_tags),
1928 collower_,
1929 colupper_);
1930
1931 checkMSKerror(err, "MSK_getvarboundslice","getColUpper");
1932 #endif
1933
1934 for( int k = 0; k < ncols; ++k )
1935 {
1936 if( dummy_tags[k] == MSK_BK_UP ||
1937 dummy_tags[k] == MSK_BK_FR )
1938 {
1939 /* No lower */
1940 collower_[k] = -getInfinity();
1941 }
1942
1943 if( dummy_tags[k] == MSK_BK_LO ||
1944 dummy_tags[k] == MSK_BK_FR )
1945 {
1946 /* No upper */
1947 colupper_[k] = getInfinity();
1948 }
1949 }
1950
1951 delete[] dummy_tags;
1952
1953 }
1954 else
1955 {
1956 if(colupper_ != NULL)
1957 delete [] colupper_;
1958
1959 if(collower_ != NULL)
1960 delete [] collower_;
1961
1962 colupper_ = collower_ = NULL;
1963 }
1964 }
1965
1966 #if MSK_OSI_DEBUG_LEVEL > 3
1967 debugMessage("End OsiMskSolverInterface::getColLower()\n");
1968 #endif
1969
1970 return collower_;
1971 }
1972
1973 //-----------------------------------------------------------------------------
1974 // Returns upper bounds on columns in MOSEK task
1975
1976 const double * OsiMskSolverInterface::getColUpper() const
1977 {
1978 #if MSK_OSI_DEBUG_LEVEL > 3
1979 debugMessage("Begin OsiMskSolverInterface::getColUpper()\n");
1980 #endif
1981
1982 if( colupper_ == NULL )
1983 {
1984 int ncols = getNumCols();
1985
1986 MSKassert(3,collower_ == NULL,"collower_ == NULL","getColUpper");
1987
1988 if( ncols > 0 )
1989 {
1990 if(colupper_ == NULL)
1991 colupper_ = new double[ncols];
1992
1993 if(collower_ == NULL)
1994 collower_ = new double[ncols];
1995
1996 int *dummy_tags = new int[ncols];
1997
1998 #if MSK_VERSION_MAJOR < 9
1999 int err = MSK_getboundslice( getMutableLpPtr(),
2000 MSK_ACC_VAR,
2001 (MSKidxt)0,
2002 (MSKidxt)ncols,
2003 (MSKboundkeye*) (dummy_tags),
2004 collower_,
2005 colupper_);
2006
2007 checkMSKerror(err,"MSK_getboundslice","getColUpper");
2008 #else
2009 int err = MSK_getvarboundslice( getMutableLpPtr(),
2010 0,
2011 ncols,
2012 (MSKboundkeye*) (dummy_tags),
2013 collower_,
2014 colupper_);
2015
2016 checkMSKerror(err,"MSK_getvarboundslice","getColUpper");
2017 #endif
2018
2019 delete[] dummy_tags;
2020 }
2021 else
2022 {
2023 if(colupper_ != NULL)
2024 delete [] colupper_;
2025
2026 if(collower_ != NULL)
2027 delete [] collower_;
2028
2029 colupper_ = collower_ = NULL;
2030 }
2031 }
2032
2033 #if MSK_OSI_DEBUG_LEVEL > 3
2034 debugMessage("End OsiMskSolverInterface::getColUpper()\n");
2035 #endif
2036
2037 return colupper_;
2038 }
2039
2040
2041 //-----------------------------------------------------------------------------
2042 // Returns rowsense in MOSEK task, call getRightHandSide to produce triplets.
2043
2044 const char * OsiMskSolverInterface::getRowSense() const
2045 {
2046 #if MSK_OSI_DEBUG_LEVEL > 3
2047 debugMessage("Begin OsiMskSolverInterface::getRowSense()\n");
2048 #endif
2049
2050 if( rowsense_==NULL )
2051 {
2052 getRightHandSide();
2053
2054 if( getNumRows() != 0 )
2055 MSKassert(3,rowsense_!=NULL,"rowsense_!=NULL","getRowSense");
2056 }
2057
2058 #if MSK_OSI_DEBUG_LEVEL > 3
2059 debugMessage("End OsiMskSolverInterface::getRowSense()\n");
2060 #endif
2061
2062 return rowsense_;
2063 }
2064
2065 //-----------------------------------------------------------------------------
2066 // Returns the RHS in triplet form. MOSEK uses always boundkeys instead of the
2067 // triplet, so we have to convert back to triplet.
2068
2069 const double * OsiMskSolverInterface::getRightHandSide() const
2070 {
2071 #if MSK_OSI_DEBUG_LEVEL > 3
2072 debugMessage("Begin OsiMskSolverInterface::getRightHandSide()\n");
2073 #endif
2074
2075 if(rowsense_ == NULL)
2076 {
2077 int nr = getNumRows();
2078 if ( nr != 0 )
2079 {
2080 MSKassert(3,(rhs_ == NULL) && (rowrange_ == NULL),"(rhs_ == NULL) && (rowrange_ == NULL)","getRightHandSide");
2081
2082 rowsense_ = new char[nr];
2083 rhs_ = new double[nr];
2084 rowrange_ = new double[nr];
2085 const double * lb = getRowLower();
2086 const double * ub = getRowUpper();
2087 int i;
2088
2089 for ( i=0; i<nr; i++ )
2090 convertBoundToSense(lb[i], ub[i], rowsense_[i], rhs_[i], rowrange_[i]);
2091 }
2092 }
2093
2094 #if MSK_OSI_DEBUG_LEVEL > 3
2095 debugMessage("End OsiMskSolverInterface::getRightHandSide()\n");
2096 #endif
2097
2098 return rhs_;
2099 }
2100
2101 //-----------------------------------------------------------------------------
2102 // Returns rowrange in MOSEK task, call getRightHandSide to produce triplets.
2103
2104 const double * OsiMskSolverInterface::getRowRange() const
2105 {
2106 #if MSK_OSI_DEBUG_LEVEL > 3
2107 debugMessage("Begin OsiMskSolverInterface::getRowRange()\n");
2108 #endif
2109
2110 if( rowrange_ == NULL )
2111 {
2112 getRightHandSide();
2113 MSKassert(3,rowrange_!=NULL || getNumRows() == 0,"rowrange_!=NULL || getNumRows() == 0","getRowRange");
2114 }
2115
2116 #if MSK_OSI_DEBUG_LEVEL > 3
2117 debugMessage("End OsiMskSolverInterface::getRowRange()\n");
2118 #endif
2119
2120 return rowrange_;
2121 }
2122
2123
2124 //-----------------------------------------------------------------------------
2125 // Returns lower bounds on rows in MOSEK task.
2126
2127 const double * OsiMskSolverInterface::getRowLower() const
2128 {
2129 #if MSK_OSI_DEBUG_LEVEL > 3
2130 debugMessage("Begin OsiMskSolverInterface::getRowLower()\n");
2131 #endif
2132
2133 if( rowlower_ == NULL )
2134 {
2135 MSKassert(3,rowupper_ == NULL,"rowupper_ == NULL","getRowLower");
2136
2137 int nrows = getNumRows();
2138
2139 if( nrows > 0 )
2140 {
2141 rowlower_ = new double[nrows];
2142 rowupper_ = new double[nrows];
2143 int *dummy_tags = new int[nrows];
2144
2145 #if MSK_VERSION_MAJOR < 9
2146 int err = MSK_getboundslice(getMutableLpPtr(),
2147 MSK_ACC_CON,
2148 (MSKidxt)0,
2149 (MSKidxt)nrows,
2150 (MSKboundkeye*) (dummy_tags),
2151 rowlower_,
2152 rowupper_);
2153
2154 checkMSKerror(err,"MSK_getboundslice","getRowLower");
2155 #else
2156 int err = MSK_getconboundslice(getMutableLpPtr(),
2157 0,
2158 nrows,
2159 (MSKboundkeye*) (dummy_tags),
2160 rowlower_,
2161 rowupper_);
2162
2163 checkMSKerror(err,"MSK_getconboundslice","getRowLower");
2164 #endif
2165 delete[] dummy_tags;
2166 }
2167 }
2168
2169 #if MSK_OSI_DEBUG_LEVEL > 3
2170 debugMessage("End OsiMskSolverInterface::getRowLower()\n");
2171 #endif
2172
2173 return rowlower_;
2174 }
2175
2176
2177 //-----------------------------------------------------------------------------
2178 // Returns upper bounds on rows in MOSEK task.
2179
2180 const double * OsiMskSolverInterface::getRowUpper() const
2181 {
2182 #if MSK_OSI_DEBUG_LEVEL > 3
2183 debugMessage("Begin OsiMskSolverInterface::getRowUpper()\n");
2184 #endif
2185
2186 if( rowupper_ == NULL )
2187 {
2188 MSKassert(3,rowlower_ == NULL,"probtypemip_","getRowUpper");
2189
2190 int nrows = getNumRows();
2191
2192 if( nrows > 0 )
2193 {
2194 rowupper_ = new double[nrows];
2195 rowlower_ = new double[nrows];
2196 int *dummy_tags = new int[nrows];
2197
2198 #if MSK_VERSION_MAJOR < 9
2199 int err = MSK_getboundslice(getMutableLpPtr(),
2200 MSK_ACC_CON,
2201 (MSKidxt)0,
2202 (MSKidxt)nrows,
2203 (MSKboundkeye*) (dummy_tags),
2204 rowlower_,
2205 rowupper_);
2206
2207 checkMSKerror(err,"MSK_getboundslice","getRowUpper");
2208 #else
2209 int err = MSK_getconboundslice(getMutableLpPtr(),
2210 0,
2211 nrows,
2212 (MSKboundkeye*) (dummy_tags),
2213 rowlower_,
2214 rowupper_);
2215
2216 checkMSKerror(err,"MSK_getconboundslice","getRowUpper");
2217 #endif
2218
2219 delete[] dummy_tags;
2220 }
2221 }
2222
2223 #if MSK_OSI_DEBUG_LEVEL > 3
2224 debugMessage("End OsiMskSolverInterface::getRowUpper()\n");
2225 #endif
2226
2227 return rowupper_;
2228 }
2229
2230
2231 //-----------------------------------------------------------------------------
2232 // Returns objective coefficient in MOSEK task.
2233
2234
2235 const double * OsiMskSolverInterface::getObjCoefficients() const
2236 {
2237 #if MSK_OSI_DEBUG_LEVEL > 3
2238 debugMessage("Begin OsiMskSolverInterface::getObjCoefficients()\n");
2239 #endif
2240
2241 if( obj_ == NULL )
2242 {
2243 int ncols = getNumCols();
2244
2245 if( ncols > 0 )
2246 {
2247 obj_ = new double[ncols];
2248 int err = MSK_getc( getMutableLpPtr(), obj_ );
2249
2250 checkMSKerror( err, "MSK_getc", "getObjCoefficients" );
2251 }
2252 }
2253
2254 #if MSK_OSI_DEBUG_LEVEL > 3
2255 debugMessage("End OsiMskSolverInterface::getObjCoefficients()\n");
2256 #endif
2257
2258 return obj_;
2259 }
2260
2261
2262 //-----------------------------------------------------------------------------
2263 // Returns the direction of optimization
2264
2265 double OsiMskSolverInterface::getObjSense() const
2266 {
2267 #if MSK_OSI_DEBUG_LEVEL > 3
2268 debugMessage("Begin OsiMskSolverInterface::getObjSense()\n");
2269 #endif
2270
2271 int err;
2272 MSKobjsensee objsen;
2273
2274 err = MSK_getobjsense(getMutableLpPtr(),
2275 &objsen);
2276
2277 checkMSKerror(err,"MSK_getintparam","getObjSense");
2278
2279 #if MSK_OSI_DEBUG_LEVEL > 3
2280 debugMessage("End OsiMskSolverInterface::getObjSense()\n");
2281 #endif
2282
2283 if( objsen == MSK_OBJECTIVE_SENSE_MAXIMIZE )
2284 return -1.0;
2285 else
2286 return +1.0;
2287 }
2288
2289
2290 //-----------------------------------------------------------------------------
2291 // Returns true if variabel is set to continuous
2292
2293 bool OsiMskSolverInterface::isContinuous( int colNumber ) const
2294 {
2295 #if MSK_OSI_DEBUG_LEVEL > 3
2296 debugMessage("Begin OsiMskSolverInterface::isContinuous(%d)\n", colNumber);
2297 debugMessage("End OsiMskSolverInterface::isContinuous(%d)\n", colNumber);
2298 #endif
2299
2300 return getCtype()[colNumber] == 'C';
2301 }
2302
2303
2304 //-----------------------------------------------------------------------------
2305 // Returns a Coin matrix by row
2306
2307 const CoinPackedMatrix * OsiMskSolverInterface::getMatrixByRow() const
2308 {
2309 #if MSK_OSI_DEBUG_LEVEL > 3
2310 debugMessage("Begin OsiMskSolverInterface::getMatrixByRow()\n");
2311 #endif
2312
2313 if ( matrixByRow_ == NULL )
2314 {
2315 int nc, nr, nz, *sub, *ptrb, *ptre, surp, *len;
2316 double *val;
2317 nc = getNumCols();
2318 nr = getNumRows();
2319 nz = surp = getNumElements();
2320 ptrb = new int[nr+1];
2321 ptre = new int[nr];
2322 sub = new int[nz];
2323 val = new double[nz];
2324 len = new int[nr];
2325 ptrb[nr] = nz;
2326
2327 #if MSK_VERSION_MAJOR < 9
2328 int err = MSK_getaslice(getMutableLpPtr(),
2329 MSK_ACC_CON,
2330 0,
2331 nr,
2332 nz,
2333 &surp,
2334 ptrb,
2335 ptre,
2336 sub,
2337 val);
2338
2339 checkMSKerror(err, "MSK_getaslice", "getMatrixByRow");
2340 #else
2341 int err = MSK_getarowslice(getMutableLpPtr(),
2342 0,
2343 nr,
2344 nz,
2345 &surp,
2346 ptrb,
2347 ptre,
2348 sub,
2349 val);
2350
2351 checkMSKerror(err, "MSK_getarowslice", "getMatrixByRow");
2352 #endif
2353
2354 for(int i=0; i<nr; i++)
2355 len[i] = ptre[i]-ptrb[i];
2356
2357 matrixByRow_ = new CoinPackedMatrix();
2358 matrixByRow_->assignMatrix(false , nc, nr, nz, val, sub, ptrb, len);
2359
2360 MSKassert(3,matrixByRow_->getNumCols()==nc,"matrixByRow_->getNumCols()==nc","getMatrixByRow");
2361 MSKassert(3,matrixByRow_->getNumRows()==nr,"matrixByRow_->getNumRows()==nr","getMatrixByRow");
2362
2363 delete[] ptrb;
2364 delete[] ptre;
2365 delete[] sub;
2366 delete[] val;
2367 delete[] len;
2368 }
2369
2370 #if MSK_OSI_DEBUG_LEVEL > 3
2371 debugMessage("End OsiMskSolverInterface::getMatrixByRow()\n");
2372 #endif
2373
2374 return matrixByRow_;
2375 }
2376
2377
2378 //-----------------------------------------------------------------------------
2379 // Returns a Coin matrix by column
2380
2381 const CoinPackedMatrix * OsiMskSolverInterface::getMatrixByCol() const
2382 {
2383 #if MSK_OSI_DEBUG_LEVEL > 3
2384 debugMessage("Begin OsiMskSolverInterface::getMatrixByCol()\n");
2385 #endif
2386
2387 if ( matrixByCol_ == NULL )
2388 {
2389 int nc, nr, nz, *sub, *ptrb, *ptre, surp, *len;
2390 double *val;
2391
2392 nc = getNumCols();
2393 nr = getNumRows();
2394 nz = surp = getNumElements();
2395 ptrb = new int[nc+1];
2396 ptre = new int[nc];
2397 sub = new int[nz];
2398 val = new double[nz];
2399 len = new int[nc];
2400 ptrb[nc] = nz;
2401
2402 #if MSK_VERSION_MAJOR < 9
2403 int err = MSK_getaslice(getMutableLpPtr(),
2404 MSK_ACC_VAR,
2405 0,
2406 nc,
2407 nz,
2408 &surp,
2409 ptrb,
2410 ptre,
2411 sub,
2412 val);
2413
2414 checkMSKerror(err, "MSK_getaslice", "getMatrixByCol");
2415 #else
2416 int err = MSK_getacolslice(getMutableLpPtr(),
2417 0,
2418 nc,
2419 nz,
2420 &surp,
2421 ptrb,
2422 ptre,
2423 sub,
2424 val);
2425
2426 checkMSKerror(err, "MSK_getacolslice", "getMatrixByCol");
2427 #endif
2428
2429 for(int i=0; i<nc; i++)
2430 len[i] = ptre[i]-ptrb[i];
2431
2432 matrixByCol_ = new CoinPackedMatrix();
2433 matrixByCol_->assignMatrix(true , nr, nc, nz, val, sub, ptrb, len);
2434
2435 MSKassert(3,matrixByCol_->getNumCols()==nc,"matrixByCol_->getNumCols()==nc","getMatrixByCol");
2436 MSKassert(3,matrixByCol_->getNumRows()==nr,"matrixByCol_->getNumRows()==nr","getMatrixByCol");
2437
2438 delete[] ptrb;
2439 delete[] ptre;
2440 delete[] sub;
2441 delete[] val;
2442 delete[] len;
2443 }
2444
2445 #if MSK_OSI_DEBUG_LEVEL > 3
2446 debugMessage("End OsiMskSolverInterface::getMatrixByCol()\n");
2447 #endif
2448
2449 return matrixByCol_;
2450 }
2451
2452
2453 //-----------------------------------------------------------------------------
2454 // Returns the infinity level used in MOSEK.
2455
2456 double OsiMskSolverInterface::getInfinity() const
2457 {
2458 #if MSK_OSI_DEBUG_LEVEL > 3
2459 debugMessage("Begin OsiMskSolverInterface::getInfinity()\n");
2460 debugMessage("End OsiMskSolverInterface::getInfinity()\n");
2461 #endif
2462
2463 return MSK_INFINITY;
2464 }
2465
2466 //#############################################################################
2467 // Problem information methods (results)
2468 //#############################################################################
2469
2470 //-----------------------------------------------------------------------------
2471 // Returns the current col solution.
2472
2473 const double * OsiMskSolverInterface::getColSolution() const
2474 {
2475 #if MSK_OSI_DEBUG_LEVEL > 3
2476 debugMessage("Begin OsiMskSolverInterface::getColSolution() %p\n",(void*)this);
2477 #endif
2478
2479 if( colsol_ != NULL )
2480 {
2481 #if MSK_OSI_DEBUG_LEVEL > 3
2482 debugMessage("colsol_ != NULL\n");
2483 #endif
2484
2485 return colsol_;
2486 }
2487
2488 int i;
2489 int nc = getNumCols();
2490 if( nc > 0 )
2491 {
2492 MSKsoltypee solution = MSK_SOL_END;
2493
2494 if(colsol_ == NULL)
2495 colsol_ = new double[nc];
2496
2497 if( probtypemip_ == false)
2498 {
2499 if( definedSolution( MSK_SOL_BAS ) == true )
2500 solution = MSK_SOL_BAS;
2501 else if( definedSolution( MSK_SOL_ITR) == true )
2502 solution = MSK_SOL_ITR;
2503 }
2504 else if( definedSolution( MSK_SOL_ITG ) == true )
2505 solution = MSK_SOL_ITG;
2506
2507 if ( solution == MSK_SOL_END )
2508 {
2509 double const *cl=getColLower(),*cu=getColLower();
2510
2511 /* this is just plain stupid, but needed to pass unit test */
2512 for( i = 0; i < nc; ++i )
2513 {
2514 if( cl[i] > -getInfinity() )
2515 {
2516 colsol_[i] = cl[i];
2517 }
2518 else if( cu[i] < getInfinity() )
2519 {
2520 colsol_[i] = cu[i];
2521 }
2522 else
2523 {
2524 colsol_[i] = 0.0;
2525 }
2526 }
2527
2528 #if MSK_OSI_DEBUG_LEVEL > 3
2529 debugMessage("colsol_ truncated to zero due to no solution\n");
2530 debugMessage("probtypemip_ %d\n",probtypemip_);
2531 #endif
2532
2533 return colsol_;
2534 }
2535
2536 int err = MSK_getsolution(getMutableLpPtr(),
2537 solution,
2538 NULL,
2539 NULL,
2540 NULL,
2541 NULL,
2542 NULL,
2543 NULL,
2544 colsol_,
2545 NULL,
2546 NULL,
2547 NULL,
2548 NULL,
2549 NULL,
2550 NULL
2551 );
2552
2553 checkMSKerror(err,"MSK_getsolution","getColSolution");
2554 }
2555
2556 #if MSK_OSI_DEBUG_LEVEL > 3
2557 debugMessage("End OsiMskSolverInterface::getColSolution()\n");
2558 #endif
2559
2560 return colsol_;
2561 }
2562
2563 //-----------------------------------------------------------------------------
2564 // Returns the row price / dual variabels in MOSEK task
2565
2566 const double * OsiMskSolverInterface::getRowPrice() const
2567 {
2568 #if MSK_OSI_DEBUG_LEVEL > 3
2569 debugMessage("Begin OsiMskSolverInterface::getRowPrice()\n");
2570 #endif
2571
2572 if( rowsol_ == NULL )
2573 {
2574 int i;
2575 int nr = getNumRows();
2576
2577 if( nr > 0 )
2578 {
2579 MSKsoltypee solution = MSK_SOL_END;
2580 rowsol_ = new double[nr];
2581
2582 if( definedSolution( MSK_SOL_BAS ) == true )
2583 solution = MSK_SOL_BAS;
2584 else if( definedSolution( MSK_SOL_ITR) == true )
2585 solution = MSK_SOL_ITR;
2586
2587 if ( solution == MSK_SOL_END )
2588 {
2589 for( i = 0; i < nr; ++i )
2590 rowsol_[i] = 0.0;
2591
2592 return rowsol_;
2593 }
2594
2595 int err = MSK_getsolution(getMutableLpPtr(),
2596 solution,
2597 NULL,
2598 NULL,
2599 NULL,
2600 NULL,
2601 NULL,
2602 NULL,
2603 NULL,
2604 rowsol_,
2605 NULL,
2606 NULL,
2607 NULL,
2608 NULL,
2609 NULL);
2610
2611
2612 checkMSKerror( err, "MSK_getsolution", "getRowPrice" );
2613 }
2614 }
2615
2616 #if MSK_OSI_DEBUG_LEVEL > 3
2617 debugMessage("End OsiMskSolverInterface::getRowPrice()\n");
2618 #endif
2619
2620 return rowsol_;
2621 }
2622
2623 //-----------------------------------------------------------------------------
2624 // Returns the reduced cost in MOSEK task.
2625
2626 const double * OsiMskSolverInterface::getReducedCost() const
2627 {
2628 #if MSK_OSI_DEBUG_LEVEL > 3
2629 debugMessage("Begin OsiMskSolverInterface::getReducedCost()\n");
2630 #endif
2631
2632 if( redcost_ == NULL )
2633 {
2634 int ncols = getNumCols();
2635
2636 if( ncols > 0 )
2637 {
2638 MSKsoltypee solution = MSK_SOL_END;
2639 if( definedSolution( MSK_SOL_BAS ) == true )
2640 solution = MSK_SOL_BAS;
2641 else if( definedSolution( MSK_SOL_ITR) == true )
2642 solution = MSK_SOL_ITR;
2643
2644 if ( solution == MSK_SOL_END )
2645 return NULL;
2646
2647 redcost_ = new double[ncols];
2648 double *slx = new double[ncols];
2649 double *sux = new double[ncols];
2650
2651 int err = MSK_getsolution(getMutableLpPtr(),
2652 solution,
2653 NULL,
2654 NULL,
2655 NULL,
2656 NULL,
2657 NULL,
2658 NULL,
2659 NULL,
2660 NULL,
2661 NULL,
2662 NULL,
2663 slx,
2664 sux,
2665 NULL);
2666
2667 // Calculate reduced cost
2668 for(int i = 0; i < ncols; i++)
2669 redcost_[i] = slx[i]-sux[i];
2670
2671 delete[] slx;
2672 delete[] sux;
2673
2674 checkMSKerror( err, "MSK_getsolution", "getReducedCost" );
2675 }
2676 }
2677
2678 #if MSK_OSI_DEBUG_LEVEL > 3
2679 debugMessage("End OsiMskSolverInterface::getReducedCost()\n");
2680 #endif
2681
2682 return redcost_;
2683 }
2684
2685
2686 //-----------------------------------------------------------------------------
2687 // Returns the rowactivity in MOSEK task.
2688
2689 const double * OsiMskSolverInterface::getRowActivity() const
2690 {
2691 #if MSK_OSI_DEBUG_LEVEL > 3
2692 debugMessage("Begin OsiMskSolverInterface::getRowActivity()\n");
2693 #endif
2694
2695 if( rowact_ == NULL )
2696 {
2697 int nrows = getNumRows();
2698 if( nrows > 0 )
2699 {
2700 rowact_ = new double[nrows];
2701
2702 const double *x = getColSolution() ;
2703 const CoinPackedMatrix *mtx = getMatrixByRow() ;
2704 mtx->times(x,rowact_) ;
2705 }
2706 }
2707
2708 #if MSK_OSI_DEBUG_LEVEL > 3
2709 debugMessage("End OsiMskSolverInterface::getRowActivity()\n");
2710 #endif
2711
2712 return rowact_;
2713 }
2714
2715 //-----------------------------------------------------------------------------
2716 // Returns the objective for defined solution in MOSEK task.
2717
2718 double OsiMskSolverInterface::getObjValue() const
2719 {
2720 #if MSK_OSI_DEBUG_LEVEL > 3
2721 debugMessage("Begin OsiMskSolverInterface::getObjValue()\n");
2722 #endif
2723
2724 double objval = OsiSolverInterface::getObjValue();
2725
2726 #if MSK_OSI_DEBUG_LEVEL > 3
2727 debugMessage("End OsiMskSolverInterface::getObjValue()\n");
2728 #endif
2729
2730 return objval;
2731 }
2732
2733 //-----------------------------------------------------------------------------
2734 // Returns the iteration used in last call to optimize. Notice that the cross
2735 // over phase in interior methods is not returned, when interior point is
2736 // used, only the interior point iterations.
2737
2738 int OsiMskSolverInterface::getIterationCount() const
2739 {
2740 #if MSK_OSI_DEBUG_LEVEL > 3
2741 debugMessage("Begin OsiMskSolverInterface::getIterationCount()\n");
2742 #endif
2743
2744 int nr = 0, solver, err;
2745 int nrp=0;
2746 solver = solverUsed();
2747
2748 if( solver == MSK_OPTIMIZER_PRIMAL_SIMPLEX )
2749 {
2750 {
2751 err = MSK_getintinf(getMutableLpPtr(), MSK_IINF_SIM_PRIMAL_ITER, &nr);
2752 checkMSKerror(err,"MSK_getintinf","getIterationsCount");
2753 }
2754 }
2755 else if( solver == MSK_OPTIMIZER_DUAL_SIMPLEX )
2756 {
2757 {
2758 err = MSK_getintinf(getMutableLpPtr(), MSK_IINF_SIM_DUAL_ITER, &nr);
2759 checkMSKerror(err,"MSK_getintinf","getIterationsCount");
2760 }
2761 }
2762 else if( solver == MSK_OPTIMIZER_FREE_SIMPLEX )
2763 {
2764 {
2765 err = MSK_getintinf(getMutableLpPtr(), MSK_IINF_SIM_DUAL_ITER, &nr);
2766 checkMSKerror(err,"MSK_getintinf","getIterationsCount");
2767 err = MSK_getintinf(getMutableLpPtr(), MSK_IINF_SIM_PRIMAL_ITER, &nrp);
2768 checkMSKerror(err,"MSK_getintinf","getIterationsCount");
2769 nr = nr+nrp;
2770 }
2771 }
2772 else if( solver == MSK_OPTIMIZER_INTPNT )
2773 {
2774 {
2775 err = MSK_getintinf(getMutableLpPtr(), MSK_IINF_INTPNT_ITER, &nr);
2776 checkMSKerror(err,"MSK_getintinf","getIterationsCount");
2777 }
2778 }
2779
2780 #if MSK_OSI_DEBUG_LEVEL > 3
2781 debugMessage("End OsiMskSolverInterface::getIterationCount()\n");
2782 #endif
2783
2784 return nr;
2785 }
2786
2787 //-----------------------------------------------------------------------------
2788 // Returns one dual ray
2789
2790 std::vector<double*> OsiMskSolverInterface::getDualRays(int maxNumRays,
2791 bool fullRay) const
2792 {
2793 #if MSK_OSI_DEBUG_LEVEL > 3
2794 debugMessage("Begin OsiMskSolverInterface::getDualRays(%d,%s)\n", maxNumRays,
2795 fullRay?"true":"false");
2796 #endif
2797
2798 if (fullRay == true) {
2799 throw CoinError("Full dual rays not yet implemented.","getDualRays",
2800 "OsiMskSolverInterface");
2801 }
2802
2803 OsiMskSolverInterface solver(*this);
2804
2805 int numrows = getNumRows(), r;
2806 MSKsolstae status;
2807 MSKsoltypee solution;
2808
2809 if( probtypemip_ == false )
2810 {
2811 if( definedSolution( MSK_SOL_BAS ) == true )
2812 solution = MSK_SOL_BAS;
2813 else
2814 {
2815 if( definedSolution( MSK_SOL_ITR ) == true )
2816 solution = MSK_SOL_ITR;
2817 else
2818 return std::vector<double*>();
2819 }
2820 }
2821 else
2822 {
2823 if( definedSolution( MSK_SOL_ITG ) == true )
2824 solution = MSK_SOL_ITG;
2825 else
2826 return std::vector<double*>();
2827 }
2828
2829 double *farkasray = new double[numrows];
2830 r = MSK_getsolution(getMutableLpPtr(),
2831 solution,
2832 NULL,
2833 &status,
2834 NULL,
2835 NULL,
2836 NULL,
2837 NULL,
2838 NULL,
2839 farkasray,
2840 NULL,
2841 NULL,
2842 NULL,
2843 NULL,
2844 NULL);
2845
2846 checkMSKerror( r, "MSK_getsolution", "getDualRays" );
2847
2848 #if MSK_OSI_DEBUG_LEVEL > 3
2849 debugMessage("End OsiMskSolverInterface::getDualRays(%d)\n", maxNumRays);
2850 #endif
2851
2852 if( status != MSK_SOL_STA_PRIM_INFEAS_CER )
2853 {
2854 delete[] farkasray;
2855
2856 return std::vector<double*>();
2857 }
2858 else
2859 return std::vector<double*>(1, farkasray);
2860 }
2861
2862 //-----------------------------------------------------------------------------
2863 // Returns one primal ray
2864
2865 std::vector<double*> OsiMskSolverInterface::getPrimalRays(int maxNumRays) const
2866 {
2867 #if MSK_OSI_DEBUG_LEVEL > 3
2868 debugMessage("Begin OsiMskSolverInterface::getPrimalRays(%d)\n", maxNumRays);
2869 #endif
2870
2871 OsiMskSolverInterface solver(*this);
2872
2873 int numcols = getNumCols(), r;
2874 MSKsolstae status;
2875 MSKsoltypee solution;
2876
2877 if( probtypemip_ == false )
2878 {
2879 if( definedSolution( MSK_SOL_BAS ) == true )
2880 solution = MSK_SOL_BAS;
2881 else
2882 {
2883 if( definedSolution( MSK_SOL_ITR ) == true )
2884 solution = MSK_SOL_ITR;
2885 else
2886 return std::vector<double*>();
2887 }
2888 }
2889 else
2890 {
2891 if( definedSolution( MSK_SOL_ITG ) == true )
2892 solution = MSK_SOL_ITG;
2893 else
2894 return std::vector<double*>();
2895 }
2896
2897 double *farkasray = new double[numcols];
2898
2899 r = MSK_getsolution(getMutableLpPtr(),
2900 solution,
2901 NULL,
2902 &status,
2903 NULL,
2904 NULL,
2905 NULL,
2906 NULL,
2907 farkasray,
2908 NULL,
2909 NULL,
2910 NULL,
2911 NULL,
2912 NULL,
2913 NULL);
2914
2915 checkMSKerror( r, "MSK_getsolution", "getPrimalRays" );
2916
2917 #if MSK_OSI_DEBUG_LEVEL > 3
2918 debugMessage("End OsiMskSolverInterface::getPrimalRays(%d)\n", maxNumRays);
2919 #endif
2920
2921 if( status != MSK_SOL_STA_DUAL_INFEAS_CER )
2922 {
2923 delete[] farkasray;
2924
2925 return std::vector<double*>();
2926 }
2927 else
2928 return std::vector<double*>(1, farkasray);
2929 }
2930
2931 //#############################################################################
2932 // Problem modifying methods (rim vectors)
2933 //#############################################################################
2934
2935 //-----------------------------------------------------------------------------
2936 // Sets a variabels objective coeff.
2937
2938 void OsiMskSolverInterface::setObjCoeff( int elementIndex, double elementValue )
2939 {
2940 #if MSK_OSI_DEBUG_LEVEL > 3
2941 debugMessage("Begin OsiMskSolverInterface::setObjCoeff(%d, %g)\n", elementIndex, elementValue);
2942 #endif
2943
2944 const double *oldobj;
2945
2946 if( redcost_ && !obj_ )
2947 oldobj = getObjCoefficients();
2948 else
2949 oldobj = obj_;
2950
2951 int err = MSK_putclist(getMutableLpPtr(),
2952 1,
2953 &elementIndex,
2954 &elementValue);
2955
2956 checkMSKerror(err, "MSK_putclist", "setObjCoeff");
2957
2958 if( obj_ )
2959 {
2960 if( redcost_ )
2961 {
2962 redcost_[elementIndex] += elementValue-oldobj[elementIndex];
2963 obj_[elementIndex] = elementValue;
2964 }
2965 else
2966 {
2967 obj_[elementIndex] = elementValue;
2968 }
2969 }
2970
2971 #if MSK_OSI_DEBUG_LEVEL > 3
2972 debugMessage("End OsiMskSolverInterface::setObjCoeff(%d, %g)\n", elementIndex, elementValue);
2973 #endif
2974 }
2975
2976 //-----------------------------------------------------------------------------
2977 // Sets a list of objective coeff.
2978
2979 void OsiMskSolverInterface::setObjCoeffSet(const int* indexFirst,
2980 const int* indexLast,
2981 const double* coeffList)
2982 {
2983 #if MSK_OSI_DEBUG_LEVEL > 3
2984 debugMessage("Begin OsiMskSolverInterface::setObjCoeffSet(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)coeffList);
2985 #endif
2986
2987 const double *oldobj;
2988 const long int cnt = indexLast - indexFirst;
2989
2990 if( redcost_ && !obj_ )
2991 oldobj = getObjCoefficients();
2992 else
2993 oldobj = obj_;
2994
2995 int err = MSK_putclist(getMutableLpPtr(),
2996 static_cast<int>(cnt),
2997 const_cast<int*>(indexFirst),
2998 const_cast<double*>(coeffList));
2999
3000 checkMSKerror(err, "MSK_putclist", "setObjCoeffSet");
3001
3002 if( obj_ )
3003 {
3004 if( redcost_ )
3005 {
3006 for( int j = 0; j < cnt; ++j)
3007 {
3008 redcost_[j] += coeffList[indexFirst[j]]-oldobj[j];
3009 obj_[j] = coeffList[indexFirst[j]];
3010 }
3011 }
3012 else
3013 {
3014 for( int j = 0; j < cnt; ++j)
3015 {
3016 obj_[j] = coeffList[indexFirst[j]];
3017 }
3018 }
3019 }
3020
3021
3022 #if MSK_OSI_DEBUG_LEVEL > 3
3023 debugMessage("End OsiMskSolverInterface::setObjCoeffSet(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)coeffList);
3024 #endif
3025 }
3026
3027 //-----------------------------------------------------------------------------
3028 // Sets lower bound on one specific column
3029
3030 void OsiMskSolverInterface::setColLower(int elementIndex, double elementValue)
3031 {
3032 #if MSK_OSI_DEBUG_LEVEL > 3
3033 debugMessage("Begin OsiMskSolverInterface::setColLower(%d, %g)\n", elementIndex, elementValue);
3034 #endif
3035
3036 int finite = 1;
3037
3038 if( elementValue <= -getInfinity() )
3039 finite = 0;
3040
3041 #if MSK_VERSION_MAJOR < 9
3042 int err = MSK_chgbound(getMutableLpPtr(),
3043 MSK_ACC_VAR,
3044 elementIndex,
3045 1, /*It is a lower bound*/
3046 finite, /* Is it finite */
3047 elementValue);
3048
3049 checkMSKerror( err, "MSK_chgbound", "setColLower" );
3050 #else
3051 int err = MSK_chgvarbound(getMutableLpPtr(),
3052 elementIndex,
3053 1, /*It is a lower bound*/
3054 finite, /* Is it finite */
3055 elementValue);
3056
3057 checkMSKerror( err, "MSK_chgvarbound", "setColLower" );
3058 #endif
3059
3060 if( collower_ != NULL )
3061 collower_[elementIndex] = elementValue;
3062
3063 #if MSK_OSI_DEBUG_LEVEL > 3
3064 debugMessage("End OsiMskSolverInterface::setColLower(%d, %g)\n", elementIndex, elementValue);
3065 #endif
3066 }
3067
3068 //-----------------------------------------------------------------------------
3069 // Sets upper bound on one specific column.
3070
3071 void OsiMskSolverInterface::setColUpper(int elementIndex, double elementValue)
3072 {
3073 #if MSK_OSI_DEBUG_LEVEL > 3
3074 debugMessage("Begin OsiMskSolverInterface::setColUpper(%d, %g)\n", elementIndex, elementValue);
3075 #endif
3076
3077 int finite = 1;
3078
3079 if( elementValue >= getInfinity() )
3080 finite = 0;
3081
3082 #if MSK_VERSION_MAJOR < 9
3083 int err = MSK_chgbound( getMutableLpPtr(),
3084 MSK_ACC_VAR,
3085 elementIndex,
3086 0, /* It is a upper bound */
3087 finite, /* Is it finite */
3088 elementValue);
3089
3090 checkMSKerror( err, "MSK_chgbound", "setColUpper" );
3091 #else
3092 int err = MSK_chgvarbound( getMutableLpPtr(),
3093 elementIndex,
3094 0, /* It is a upper bound */
3095 finite, /* Is it finite */
3096 elementValue);
3097
3098 checkMSKerror( err, "MSK_chgvarbound", "setColUpper" );
3099 #endif
3100
3101 if( colupper_ != NULL )
3102 colupper_[elementIndex] = elementValue;
3103
3104 #if MSK_OSI_DEBUG_LEVEL > 3
3105 debugMessage("End OsiMskSolverInterface::setColUpper(%d, %g)\n", elementIndex, elementValue);
3106 #endif
3107 }
3108
3109 //-----------------------------------------------------------------------------
3110 // Sets upper and lower bound on one specific column
3111
3112 void OsiMskSolverInterface::setColBounds( int elementIndex, double lower, double upper )
3113 {
3114 #if MSK_OSI_DEBUG_LEVEL > 3
3115 debugMessage("Begin OsiMskSolverInterface::setColBounds(%d, %g, %g)\n", elementIndex, lower, upper);
3116 #endif
3117
3118 setColLower(elementIndex, lower);
3119 setColUpper(elementIndex, upper);
3120
3121 #if MSK_OSI_DEBUG_LEVEL > 3
3122 debugMessage("End OsiMskSolverInterface::setColBounds(%d, %g, %g)\n", elementIndex, lower, upper);
3123 #endif
3124 }
3125
3126 //-----------------------------------------------------------------------------
3127 // Sets upper and lower bounds on a list of columns. Due to the strange storage of
3128 // boundlist, it is not possible to change all the bounds in one call to MOSEK,
3129 // so the standard method is used.
3130
3131 void OsiMskSolverInterface::setColSetBounds(const int* indexFirst,
3132 const int* indexLast,
3133 const double* boundList)
3134 {
3135 #if MSK_OSI_DEBUG_LEVEL > 3
3136 debugMessage("Begin OsiMskSolverInterface::setColSetBounds(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)boundList);
3137 #endif
3138
3139 OsiSolverInterface::setColSetBounds( indexFirst, indexLast, boundList );
3140
3141 #if MSK_OSI_DEBUG_LEVEL > 3
3142 debugMessage("End OsiMskSolverInterface::setColSetBounds(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)boundList);
3143 #endif
3144 }
3145
3146 //-----------------------------------------------------------------------------
3147 // Sets the lower bound on a row
3148
3149 void
3150 OsiMskSolverInterface::setRowLower( int i, double elementValue )
3151 {
3152 #if MSK_OSI_DEBUG_LEVEL > 3
3153 debugMessage("Begin OsiMskSolverInterface::setRowLower(%d, %g)\n", i, elementValue);
3154 #endif
3155
3156 double rhs = getRightHandSide()[i];
3157 double range = getRowRange()[i];
3158 char sense = getRowSense()[i];
3159 double lower=-MSK_INFINITY, upper=MSK_INFINITY;
3160
3161 convertSenseToBound( sense, rhs, range, lower, upper );
3162 if( lower != elementValue )
3163 {
3164 convertBoundToSense( elementValue, upper, sense, rhs, range );
3165 setRowType( i, sense, rhs, range );
3166 }
3167
3168 #if MSK_OSI_DEBUG_LEVEL > 3
3169 debugMessage("End OsiMskSolverInterface::setRowLower(%d, %g)\n", i, elementValue);
3170 #endif
3171 }
3172
3173 //-----------------------------------------------------------------------------
3174 // Sets the upper bound on a row
3175
3176 void
3177 OsiMskSolverInterface::setRowUpper( int i, double elementValue )
3178 {
3179 #if MSK_OSI_DEBUG_LEVEL > 3
3180 debugMessage("Begin OsiMskSolverInterface::setRowUpper(%d, %g)\n", i, elementValue);
3181 #endif
3182
3183 double rhs = getRightHandSide()[i];
3184 double range = getRowRange()[i];
3185 char sense = getRowSense()[i];
3186 double lower=-MSK_INFINITY, upper=MSK_INFINITY;
3187
3188 convertSenseToBound( sense, rhs, range, lower, upper );
3189 if( upper != elementValue )
3190 {
3191 convertBoundToSense( lower, elementValue, sense, rhs, range );
3192 setRowType( i, sense, rhs, range );
3193 }
3194
3195 #if MSK_OSI_DEBUG_LEVEL > 3
3196 debugMessage("End OsiMskSolverInterface::setRowUpper(%d, %g)\n", i, elementValue);
3197 #endif
3198 }
3199
3200 //-----------------------------------------------------------------------------
3201 // Sets the upper and lower bound on a row
3202
3203 void
3204 OsiMskSolverInterface::setRowBounds( int elementIndex, double lower, double upper )
3205 {
3206 #if MSK_OSI_DEBUG_LEVEL > 3
3207 debugMessage("Begin OsiMskSolverInterface::setRowBounds(%d, %g, %g)\n", elementIndex, lower, upper);
3208 #endif
3209
3210 double rhs, range;
3211 char sense;
3212
3213 convertBoundToSense( lower, upper, sense, rhs, range );
3214 setRowType( elementIndex, sense, rhs, range );
3215
3216 #if MSK_OSI_DEBUG_LEVEL > 3
3217 debugMessage("End OsiMskSolverInterface::setRowBounds(%d, %g, %g)\n", elementIndex, lower, upper);
3218 #endif
3219 }
3220
3221 //-----------------------------------------------------------------------------
3222 // Sets the triplet on a row
3223
3224 void
3225 OsiMskSolverInterface::setRowType(int i, char sense, double rightHandSide,double range)
3226 {
3227 #if MSK_OSI_DEBUG_LEVEL > 3
3228 debugMessage("Begin OsiMskSolverInterface::setRowType(%d, %c, %g, %g)\n", i, sense, rightHandSide, range);
3229 #endif
3230
3231 double rub=MSK_INFINITY,rlb=-MSK_INFINITY;
3232 int rtag=MSK_BK_FR;
3233
3234 MskConvertSenseToBound(sense, range, rightHandSide, rlb, rub, rtag);
3235
3236 #if MSK_VERSION_MAJOR < 9
3237 int err = MSK_putbound(getMutableLpPtr(),
3238 MSK_ACC_CON,
3239 i,
3240 (MSKboundkeye)rtag,
3241 rlb,
3242 rub);
3243
3244 checkMSKerror( err, "MSK_putbound", "setRowType" );
3245 #else
3246 int err = MSK_putconbound(getMutableLpPtr(),
3247 i,
3248 (MSKboundkeye)rtag,
3249 rlb,
3250 rub);
3251
3252 checkMSKerror( err, "MSK_putconbound", "setRowType" );
3253 #endif
3254
3255 if( rowsense_ != NULL )
3256 rowsense_[i] = sense;
3257
3258 if( rowrange_ != NULL )
3259 rowrange_[i] = range;
3260
3261 if( rhs_ != NULL )
3262 rhs_[i] = rightHandSide;
3263
3264 #if MSK_OSI_DEBUG_LEVEL > 3
3265 debugMessage("End OsiMskSolverInterface::setRowType(%d, %c, %g, %g)\n", i, sense, rightHandSide, range);
3266 #endif
3267 }
3268
3269 //-----------------------------------------------------------------------------
3270 // Set upper og lower bounds for a lisit of rows. Due to the strange storage of
3271 // boundlist, it is not possible to change all the bounds in one call to MOSEK,
3272 // so the standard method is used.
3273
3274 void OsiMskSolverInterface::setRowSetBounds(const int* indexFirst,
3275 const int* indexLast,
3276 const double* boundList)
3277 {
3278 #if MSK_OSI_DEBUG_LEVEL > 3
3279 debugMessage("Begin OsiMskSolverInterface::setRowSetBounds(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)boundList);
3280 #endif
3281
3282 const long int cnt = indexLast - indexFirst;
3283
3284 if (cnt <= 0)
3285 return;
3286
3287 for (int i = 0; i < cnt; ++i)
3288 setRowBounds(indexFirst[i], boundList[2*i], boundList[2*i+1]);
3289
3290 #if MSK_OSI_DEBUG_LEVEL > 3
3291 debugMessage("End OsiMskSolverInterface::setRowSetBounds(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)boundList);
3292 #endif
3293 }
3294
3295
3296 //-----------------------------------------------------------------------------
3297 // Set triplets for a list of rows
3298
3299 void
3300 OsiMskSolverInterface::setRowSetTypes(const int* indexFirst,
3301 const int* indexLast,
3302 const char* senseList,
3303 const double* rhsList,
3304 const double* rangeList)
3305 {
3306 #if MSK_OSI_DEBUG_LEVEL > 3
3307 debugMessage("Begin OsiMskSolverInterface::setRowSetTypes(%p, %p, %p, %p, %p)\n",
3308 (void *)indexFirst, (void *)indexLast, (void *)senseList, (void *)rhsList, (void *)rangeList);
3309 #endif
3310
3311 const long int cnt = indexLast - indexFirst;
3312
3313 if (cnt <= 0)
3314 return;
3315
3316 for (int i = 0; i < cnt; ++i)
3317 setRowType(indexFirst[i], senseList[i], rhsList[i], rangeList[i]);
3318
3319 #if MSK_OSI_DEBUG_LEVEL > 3
3320 debugMessage("End OsiMskSolverInterface::setRowSetTypes(%p, %p, %p, %p, %p)\n",
3321 (void *)indexFirst, (void *)indexLast, (void *)senseList, (void *)rhsList, (void *)rangeList);
3322 #endif
3323 }
3324
3325
3326 //-----------------------------------------------------------------------------
3327 // Sets a variabel to continuous
3328
3329 void
3330 OsiMskSolverInterface::setContinuous(int index)
3331 {
3332 int numcols = getNumCols();
3333
3334 #if MSK_OSI_DEBUG_LEVEL > 3
3335 debugMessage("Begin OsiMskSolverInterface::setContinuous(%d)\n", index);
3336 #endif
3337
3338 MSKassert(3,coltype_ != NULL,"coltype_ != NULL","setContinuous");
3339 MSKassert(3,coltypesize_ >= getNumCols(),"coltypesize_ >= getNumCols()","setContinuous");
3340
3341 coltype_[index] = 'C';
3342
3343 if( index < numcols )
3344 {
3345 int err = MSK_putvartype( getMutableLpPtr(), index, MSK_VAR_TYPE_CONT);
3346 checkMSKerror( err, "MSK_putvartype", "setContinuous" );
3347 }
3348
3349 #if MSK_OSI_DEBUG_LEVEL > 3
3350 debugMessage("End OsiMskSolverInterface::setContinuous(%d)\n", index);
3351 #endif
3352 }
3353
3354 //-----------------------------------------------------------------------------
3355 // Sets a variabel to integer
3356
3357 void
3358 OsiMskSolverInterface::setInteger(int index)
3359 {
3360 int numcols = getNumCols();
3361
3362 #if MSK_OSI_DEBUG_LEVEL > 3
3363 debugMessage("Begin OsiMskSolverInterface::setInteger(%d)\n", index);
3364 #endif
3365
3366 MSKassert(3,coltype_ != NULL,"coltype_ != NULL","setInteger");
3367 MSKassert(3,coltypesize_ >= getNumCols(),"coltypesize_ >= getNumCols()","setInteger");
3368
3369 coltype_[index] = 'I';
3370
3371 if( index < numcols )
3372 {
3373 int err = MSK_putvartype( getMutableLpPtr(), index, MSK_VAR_TYPE_INT);
3374
3375 checkMSKerror( err, "MSK_putvartype", "setInteger" );
3376 }
3377
3378 #if MSK_OSI_DEBUG_LEVEL > 3
3379 debugMessage("End OsiMskSolverInterface::setInteger(%d)\n", index);
3380 #endif
3381 }
3382
3383 //-----------------------------------------------------------------------------
3384 // Sets a list of variables to continuous
3385
3386 void
3387 OsiMskSolverInterface::setContinuous(const int* indices, int len)
3388 {
3389 #if MSK_OSI_DEBUG_LEVEL > 3
3390 debugMessage("Begin OsiMskSolverInterface::setContinuous(%p, %d)\n", (void *)indices, len);
3391 #endif
3392
3393 for( int i = 0; i < len; ++i )
3394 setContinuous(indices[i]);
3395
3396 #if MSK_OSI_DEBUG_LEVEL > 3
3397 debugMessage("End OsiMskSolverInterface::setContinuous(%p, %d)\n", (void *)indices, len);
3398 #endif
3399 }
3400
3401 //-----------------------------------------------------------------------------
3402 // Sets a list of variables to integer
3403
3404 void
3405 OsiMskSolverInterface::setInteger(const int* indices, int len)
3406 {
3407 #if MSK_OSI_DEBUG_LEVEL > 3
3408 debugMessage("Begin OsiMskSolverInterface::setInteger(%p, %d)\n", (void *)indices, len);
3409 #endif
3410
3411 for( int i = 0; i < len; ++i )
3412 setInteger(indices[i]);
3413
3414 #if MSK_OSI_DEBUG_LEVEL > 3
3415 debugMessage("End OsiMskSolverInterface::setInteger(%p, %d)\n", (void *)indices, len);
3416 #endif
3417 }
3418
3419 //-----------------------------------------------------------------------------
3420 // Sets the direction of optimization
3421
3422 void OsiMskSolverInterface::setObjSense(double s)
3423 {
3424 #if MSK_OSI_DEBUG_LEVEL > 3
3425 debugMessage("Begin OsiMskSolverInterface::setObjSense(%g)\n", s);
3426 #endif
3427
3428 int err;
3429 double pre;
3430
3431 pre = getObjSense();
3432
3433 if( s == +1.0 )
3434 {
3435 err = MSK_putobjsense(getMutableLpPtr(),
3436 MSK_OBJECTIVE_SENSE_MINIMIZE);
3437 }
3438 else
3439 {
3440 err = MSK_putobjsense(getMutableLpPtr(),
3441 MSK_OBJECTIVE_SENSE_MAXIMIZE);
3442 }
3443
3444 checkMSKerror(err,"MSK_putintparam","setObjSense");
3445
3446 if( pre != s )
3447 {
3448 /* A hack to pass unit test, ugly as hell */
3449 /* When objective sense is changed then reset obj cuts */
3450 if( s > 0 )
3451 {
3452 setDblParam(OsiPrimalObjectiveLimit,-COIN_DBL_MAX) ;
3453 setDblParam(OsiDualObjectiveLimit,COIN_DBL_MAX) ;
3454 }
3455 else
3456 {
3457 setDblParam(OsiPrimalObjectiveLimit,COIN_DBL_MAX) ;
3458 setDblParam(OsiDualObjectiveLimit,-COIN_DBL_MAX) ;
3459 }
3460 }
3461
3462 #if MSK_OSI_DEBUG_LEVEL > 3
3463 debugMessage("End OsiMskSolverInterface::setObjSense(%g)\n", s);
3464 #endif
3465 }
3466
3467 //-----------------------------------------------------------------------------
3468 // Sets the col solution. This is very fuzzy WE LIKE STATUS KEYS not numerical values i.e superbasics or basic !!!.
3469
3470 void OsiMskSolverInterface::setColSolution(const double * cs)
3471 {
3472 #if MSK_OSI_DEBUG_LEVEL > 1
3473 debugMessage("Begin OsiMskSolverInterface::setColSolution %p\n", (void*)this);
3474 #endif
3475
3476 int err,nc = getNumCols(),nr = getNumRows(), numbas = 0;
3477 MSKstakeye *tskc,*tskx;
3478 MSKboundkeye *tbkx,*tbkc;
3479 MSKrealt *tblx,*tbux,*tblc,*tbuc;
3480 double *txc;
3481
3482 if( cs == NULL )
3483 {
3484 freeCachedResults();
3485 }
3486 else if( nc > 0 )
3487 {
3488 if( colsol_ != NULL )
3489 delete[] colsol_;
3490
3491 colsol_ = new double[nc];
3492
3493 CoinDisjointCopyN( cs, nc, colsol_ );
3494
3495 tbkx = new MSKboundkeye[nc];
3496 tskx = new MSKstakeye[nc];
3497 tblx = new MSKrealt[nc];
3498 tbux = new MSKrealt[nc];
3499
3500 tskc = new MSKstakeye[nr];
3501 txc = new double[nr];
3502 tbkc = new MSKboundkeye[nr];
3503 tblc = new MSKrealt[nr];
3504 tbuc = new MSKrealt[nr];
3505
3506 const CoinPackedMatrix *mtx = getMatrixByCol() ;
3507 assert(mtx->getNumCols() == nc);
3508 assert(mtx->getNumRows() == nr);
3509 mtx->times(cs,txc) ;
3510
3511 /* Negate due to different Osi and Mosek slack representation */
3512 for( int i = 0; i < nr; ++i )
3513 {
3514 txc[i] = -txc[i];
3515 tskc[i] = MSK_SK_UNK;
3516 }
3517
3518 #if MSK_VERSION_MAJOR < 9
3519 err = MSK_getboundslice(getMutableLpPtr(),
3520 MSK_ACC_CON,
3521 0,
3522 nr,
3523 tbkc,
3524 tblc,
3525 tbuc);
3526
3527 checkMSKerror( err, "MSK_getboundslice", "setColsolution" );
3528
3529 err = MSK_getboundslice(getMutableLpPtr(),
3530 MSK_ACC_VAR,
3531 0,
3532 nc,
3533 tbkx,
3534 tblx,
3535 tbux);
3536
3537 checkMSKerror( err, "MSK_getboundslice", "setColsolution" );
3538 #else
3539 err = MSK_getconboundslice(getMutableLpPtr(),
3540 0,
3541 nr,
3542 tbkc,
3543 tblc,
3544 tbuc);
3545
3546 checkMSKerror( err, "MSK_getconboundslice", "setColsolution" );
3547
3548 err = MSK_getvarboundslice(getMutableLpPtr(),
3549 0,
3550 nc,
3551 tbkx,
3552 tblx,
3553 tbux);
3554
3555 checkMSKerror( err, "MSK_getvarboundslice", "setColsolution" );
3556 #endif
3557
3558 if( definedSolution( MSK_SOL_BAS ) == true )
3559 {
3560 err = MSK_getsolution(getMutableLpPtr(),
3561 MSK_SOL_BAS,
3562 NULL,
3563 NULL,
3564 tskc,
3565 tskx,
3566 NULL,
3567 NULL,
3568 NULL,
3569 NULL,
3570 NULL,
3571 NULL,
3572 NULL,
3573 NULL,
3574 NULL);
3575
3576 checkMSKerror(err,"MSK_getsolution","setColSol");
3577 }
3578
3579 for( int i = 0; i < nr; ++i )
3580 {
3581 if( tbkc[i] == MSK_BK_FX && tblc[i] == txc[i] )
3582 {
3583 tskc[i] = MSK_SK_FIX;
3584 }
3585 else if( ( tbkc[i] == MSK_BK_LO || tbkc[i] == MSK_BK_RA ) && tblc[i] == txc[i] )
3586 {
3587 tskc[i] = MSK_SK_LOW;
3588 }
3589 else if( ( tbkc[i] == MSK_BK_UP || tbkc[i] == MSK_BK_RA ) && tbuc[i] == txc[i] )
3590 {
3591 tskc[i] = MSK_SK_UPR;
3592 }
3593 else if( tbkc[i] == MSK_BK_FR && txc[i] == 0.0 )
3594 {
3595 tskc[i] = MSK_SK_SUPBAS;
3596 }
3597 else
3598 {
3599 #if 0
3600 printf("Slack : %d bkc : %d blc : %-16.10e buc : %-16.10e xc : %-16.10e\n",
3601 i,tbkc[i],tblc[i],tbuc[i],txc[i]);
3602 #endif
3603
3604 ++numbas;
3605
3606 tskc[i] = MSK_SK_BAS;
3607 }
3608 }
3609
3610 for( int j = 0; j < nc; ++j )
3611 {
3612 if( tbkx[j] == MSK_BK_FX && tblx[j] == cs[j] )
3613 {
3614 tskx[j] = MSK_SK_FIX;
3615 }
3616 else if( ( tbkx[j] == MSK_BK_LO || tbkx[j] == MSK_BK_RA ) && tblx[j] == cs[j] )
3617 {
3618 tskx[j] = MSK_SK_LOW;
3619 }
3620 else if( ( tbkx[j] == MSK_BK_UP || tbkx[j] == MSK_BK_RA ) && tbux[j] == cs[j] )
3621 {
3622 tskx[j] = MSK_SK_UPR;
3623 }
3624 else if( ( tbkx[j] == MSK_BK_FR && cs[j] == 0.0 ) || numbas >= nr )
3625 {
3626 tskx[j] = MSK_SK_SUPBAS;
3627 }
3628 else
3629 {
3630 #if 0
3631 printf("Org %d : bkx : %d blx : %-16.10e bux : %-16.10e xx : %-16.10e\n",
3632 j,tbkx[j],tblx[j],tbux[j],cs[j]);
3633 #endif
3634
3635 tskx[j] = MSK_SK_BAS;
3636 }
3637 }
3638
3639 err = MSK_putsolution(getMutableLpPtr(),
3640 MSK_SOL_BAS,
3641 tskc,
3642 tskx,
3643 NULL,
3644 (MSKrealt*) txc,
3645 const_cast<MSKrealt*>(cs),
3646 NULL,
3647 NULL,
3648 NULL,
3649 NULL,
3650 NULL,
3651 NULL);
3652
3653 checkMSKerror(err,"MSK_putsolution","setColSol");
3654
3655
3656 MSKassert(3,definedSolution( MSK_SOL_BAS ) == true,"definedSolution( MSK_SOL_BAS ) == true","setColSolution");
3657
3658 delete [] tbkx;
3659 delete [] tblx;
3660 delete [] tbux;
3661 delete [] tskx;
3662 delete [] tskc;
3663 delete [] txc;
3664 delete [] tbkc;
3665 delete [] tblc;
3666 delete [] tbuc;
3667 }
3668
3669 #if MSK_OSI_DEBUG_LEVEL > 1
3670 debugMessage("End OsiMskSolverInterface::setColSolution(%p)\n", (void *)cs);
3671 #endif
3672 }
3673
3674 //-----------------------------------------------------------------------------
3675 // Sets the rowprices.
3676
3677 void OsiMskSolverInterface::setRowPrice(const double * rs)
3678 {
3679 #if MSK_OSI_DEBUG_LEVEL > 1
3680 debugMessage("Begin OsiMskSolverInterface::setRowPrice(%p)\n", (void *)rs);
3681 #endif
3682
3683 int err,nr = getNumRows(),nc = getNumCols();
3684 MSKstakeye *tskc,*tskx;
3685 MSKrealt *tslc,*tsuc,*tslx,*tsux,*txc,*txx;
3686 double *redcost;
3687
3688 if( rs == NULL )
3689 freeCachedResults();
3690 else if( nr > 0 )
3691 {
3692 if ( rowsol_ == NULL )
3693 rowsol_ = new double[nr];
3694
3695 colsol_ = new double[nc];
3696
3697 tskc = new MSKstakeye[nr];
3698 tslc = new MSKrealt[nr];
3699 tsuc = new MSKrealt[nr];
3700 txc = new MSKrealt[nr];
3701
3702 tskx = new MSKstakeye[nc];
3703 tslx = new MSKrealt[nc];
3704 tsux = new MSKrealt[nc];
3705 txx = new MSKrealt[nc];
3706
3707 redcost = new double[nc];
3708
3709 CoinDisjointCopyN( rs, nr, rowsol_ );
3710
3711 /* Calc reduced costs */
3712 const CoinPackedMatrix *mtx = getMatrixByCol() ;
3713 mtx->transposeTimes(rs,redcost) ;
3714
3715 for( int j = 0; j < nc; ++j )
3716 {
3717 redcost[j] = getObjCoefficients()[j]-redcost[j];
3718
3719 tslx[j] = CoinMax(0.0,redcost[j]);
3720 tsux[j] = CoinMax(0.0,-redcost[j]);
3721 }
3722
3723 if( definedSolution( MSK_SOL_BAS ) == true )
3724 {
3725 for( int i = 0; i < nr; ++i )
3726 {
3727 #if MSK_VERSION_MAJOR < 9
3728 MSKrealt sn;
3729 err = MSK_getsolutioni(getMutableLpPtr(),
3730 MSK_ACC_CON,
3731 i,
3732 MSK_SOL_BAS,
3733 &tskc[i],
3734 &txc[i],
3735 &tslc[i],
3736 &tsuc[i],
3737 &sn);
3738
3739 checkMSKerror(err,"MSK_putsolutioni","setRowPrice");
3740
3741 #else
3742 err = MSK_getskcslice(getMutableLpPtr(), MSK_SOL_BAS, i, i+1, &tskc[i]);
3743 checkMSKerror(err,"MSK_getskcslice","setRowPrice");
3744 err = MSK_getxcslice(getMutableLpPtr(), MSK_SOL_BAS, i, i+1, &txc[i]);
3745 checkMSKerror(err,"MSK_getxcslice","setRowPrice");
3746 err = MSK_getslcslice(getMutableLpPtr(), MSK_SOL_BAS, i, i+1, &tslc[i]);
3747 checkMSKerror(err,"MSK_getslcslice","setRowPrice");
3748 err = MSK_getsucslice(getMutableLpPtr(), MSK_SOL_BAS, i, i+1, &tsuc[i]);
3749 checkMSKerror(err,"MSK_getsucslice","setRowPrice");
3750 #endif
3751
3752 tslc[i] = CoinMax(0.0,rowsol_[i]);
3753 tsuc[i] = CoinMax(0.0,-rowsol_[i]);
3754 }
3755
3756 err = MSK_getsolution(getMutableLpPtr(),
3757 MSK_SOL_BAS,
3758 NULL,
3759 NULL,
3760 NULL,
3761 tskx,
3762 NULL,
3763 NULL,
3764 txx,
3765 NULL,
3766 NULL,
3767 NULL,
3768 NULL,
3769 NULL,
3770 NULL);
3771
3772 checkMSKerror(err,"MSK_getsolution","setRowPrice");
3773
3774
3775 err = MSK_putsolution(getMutableLpPtr(),
3776 MSK_SOL_BAS,
3777 tskc,
3778 tskx,
3779 NULL,
3780 txc,
3781 txx,
3782 rowsol_,
3783 tslc,
3784 tsuc,
3785 tslx,
3786 tsux,
3787 NULL);
3788
3789 checkMSKerror(err,"MSK_putsolution","setRowPrice");
3790 }
3791 else
3792 {
3793 for( int i = 0; i < nr; ++i )
3794 {
3795 tslc[i] = CoinMax(0.0,rowsol_[i]);
3796 tsuc[i] = CoinMax(0.0,-rowsol_[i]);
3797 tskc[i] = MSK_SK_UNK;
3798 }
3799
3800 for( int i = 0; i < nc; ++i )
3801 {
3802 tskx[i] = MSK_SK_UNK;
3803 txx[i] = 0.0;
3804 }
3805
3806 err = MSK_putsolution(getMutableLpPtr(),
3807 MSK_SOL_BAS,
3808 tskc,
3809 tskx,
3810 NULL,
3811 NULL,
3812 NULL,
3813 rowsol_,
3814 tslc,
3815 tsuc,
3816 tslx,
3817 tsux,
3818 NULL);
3819
3820 checkMSKerror(err,"MSK_putsolution","setRowPrice");
3821 }
3822
3823 for( int i = 0; i < nc; ++i )
3824 {
3825 colsol_[i] = txx[i];
3826 }
3827
3828 delete [] tskc;
3829 delete [] tslc;
3830 delete [] tsuc;
3831 delete [] txc;
3832
3833 delete [] tskx;
3834 delete [] tslx;
3835 delete [] tsux;
3836 delete [] txx;
3837 delete [] redcost;
3838 }
3839
3840 #if MSK_OSI_DEBUG_LEVEL > 1
3841 debugMessage("End OsiMskSolverInterface::setRowPrice(%p)\n", (void *)rs);
3842 #endif
3843 }
3844
3845
3846 //#############################################################################
3847 // Problem modifying methods (matrix)
3848 //#############################################################################
3849
3850 //-----------------------------------------------------------------------------
3851 // Adds a column to the MOSEK task
3852
3853 void
3854 OsiMskSolverInterface::addCol(const CoinPackedVectorBase& vec,
3855 const double collb, const double colub,
3856 const double obj)
3857 {
3858 #if MSK_OSI_DEBUG_LEVEL > 3
3859 debugMessage("Begin OsiMskSolverInterface::addCol(%p, %g, %g, %g)\n", (void *)&vec, collb, colub, obj);
3860 #endif
3861
3862 int nc = getNumCols();
3863
3864 MSKassert(3,coltypesize_ >= nc,"coltypesize_ >= nc","addCol");
3865
3866 resizeColType(nc + 1);
3867 coltype_[nc] = 'C';
3868
3869 int ends = vec.getNumElements();
3870 MSKboundkeye tag;
3871 MSKtask_t task=getLpPtr();
3872
3873 double inf = getInfinity();
3874 if(collb > -inf && colub >= inf)
3875 tag = MSK_BK_LO;
3876 else if(collb <= -inf && colub < inf)
3877 tag = MSK_BK_UP;
3878 else if(collb > -inf && colub < inf)
3879 tag = MSK_BK_RA;
3880 else if(collb <= -inf && colub >= inf)
3881 tag = MSK_BK_FR;
3882 else
3883 throw CoinError("Bound error", "addCol", "OsiMSKSolverInterface");
3884
3885 #if MSK_VERSION_MAJOR >= 7
3886 int err;
3887 MSKint32t j;
3888
3889 err = MSK_getnumvar(task,&j);
3890
3891 if ( err==MSK_RES_OK )
3892 err = MSK_appendvars(task,1);
3893
3894 if ( err==MSK_RES_OK )
3895 err = MSK_putcj(task,j,obj);
3896
3897 if ( err==MSK_RES_OK )
3898 err = MSK_putacol(task,j,ends,const_cast<int*>(vec.getIndices()),const_cast<double*>(vec.getElements()));
3899
3900 if ( err==MSK_RES_OK )
3901 err = MSK_putvarbound(task,j,tag,collb,colub);
3902
3903 #else
3904 int start = 0;
3905 int err = MSK_appendvars(task,
3906 1,
3907 const_cast<double*> (&obj),
3908 &start,
3909 &ends,
3910 const_cast<int*>(vec.getIndices()),
3911 const_cast<double*>(vec.getElements()),
3912 (&tag),
3913 const_cast<double*> (&collb),
3914 const_cast<double*> (&colub));
3915
3916 #endif
3917
3918 checkMSKerror( err, "MSK_appendvars", "addCol" );
3919
3920 #if MSK_OSI_DEBUG_LEVEL > 3
3921 debugMessage("End OsiMskSolverInterface::addCol(%p, %g, %g, %g)\n", (void *)&vec, collb, colub, obj);
3922 #endif
3923 }
3924
3925 //-----------------------------------------------------------------------------
3926 // Adds a list of columns to the MOSEK task
3927
3928 void
3929 OsiMskSolverInterface::addCols(const int numcols,
3930 const CoinPackedVectorBase * const * cols,
3931 const double* collb, const double* colub,
3932 const double* obj)
3933 {
3934 #if MSK_OSI_DEBUG_LEVEL > 3
3935 debugMessage("Begin OsiMskSolverInterface::addCols(%d, %p, %p, %p, %p)\n", numcols, (void *)cols, (void *)collb, (void *)colub, (void *)obj);
3936 #endif
3937
3938 int i, nz = 0, err = MSK_RES_OK;
3939
3940 // For efficiency we put hints on the total future size
3941 err = MSK_getmaxnumanz(getLpPtr(),
3942 &nz);
3943
3944 checkMSKerror( err, "MSK_getmaxanz", "addCols" );
3945
3946 for( i = 0; i < numcols; ++i)
3947 nz += cols[i]->getNumElements();
3948
3949 err = MSK_putmaxnumanz(getLpPtr(),
3950 nz);
3951
3952 checkMSKerror( err, "MSK_putmaxanz", "addCols" );
3953
3954 err = MSK_putmaxnumvar(getLpPtr(),
3955 numcols+getNumCols());
3956
3957 checkMSKerror( err, "MSK_putmaxnumvar", "addCols" );
3958
3959 for( i = 0; i < numcols; ++i )
3960 addCol( *(cols[i]), collb[i], colub[i], obj[i] );
3961
3962 #if MSK_OSI_DEBUG_LEVEL > 3
3963 debugMessage("End OsiMskSolverInterface::addCols(%d, %p, %p, %p, %p)\n", numcols, (void *)cols, (void *)collb, (void *)colub, (void *)obj);
3964 #endif
3965 }
3966
3967 //-----------------------------------------------------------------------------
3968 // Deletes a list of columns from the MOSEK task
3969
3970 void
3971 OsiMskSolverInterface::deleteCols(const int num, const int * columnIndices)
3972 {
3973 #if MSK_OSI_DEBUG_LEVEL > 3
3974 debugMessage("Begin OsiMskSolverInterface::deleteCols(%d, %p)\n", num, (void *)columnIndices);
3975 #endif
3976
3977 #if MSK_VERSION_MAJOR >= 7
3978 int err;
3979 err = MSK_removevars(getLpPtr( OsiMskSolverInterface::KEEPCACHED_ROW ),
3980 num,
3981 const_cast<int*>(columnIndices));
3982
3983 checkMSKerror( err, "MSK_removevars", "deleteCols" );
3984
3985 #else
3986 int err;
3987 err = MSK_remove(getLpPtr( OsiMskSolverInterface::KEEPCACHED_ROW ),
3988 MSK_ACC_VAR,
3989 num,
3990 const_cast<int*>(columnIndices));
3991
3992 checkMSKerror( err, "MSK_remove", "deleteCols" );
3993 #endif
3994
3995 #if MSK_OSI_DEBUG_LEVEL > 3
3996 debugMessage("End OsiMskSolverInterface::deleteCols(%d, %p)\n", num, (void *)columnIndices);
3997 #endif
3998 }
3999
4000 //-----------------------------------------------------------------------------
4001 // Adds a row in bound form to the MOSEK task
4002
4003 void
4004 OsiMskSolverInterface::addRow(const CoinPackedVectorBase& vec,
4005 const double rowlb, const double rowub)
4006 {
4007 #if MSK_OSI_DEBUG_LEVEL > 3
4008 debugMessage("Begin OsiMskSolverInterface::addRow(%p, %g, %g)\n", (void *)&vec, rowlb, rowub);
4009 #endif
4010
4011 getNumRows();
4012
4013 int ends = vec.getNumElements();
4014 double inf = getInfinity();
4015 MSKboundkeye tag;
4016 MSKtask_t task = getLpPtr( OsiMskSolverInterface::KEEPCACHED_COLUMN );
4017
4018 if(rowlb > -inf && rowub >= inf)
4019 tag = MSK_BK_LO;
4020 else if(rowlb <= -inf && rowub < inf)
4021 tag = MSK_BK_UP;
4022 else if(rowlb > -inf && rowub < inf)
4023 tag = MSK_BK_RA;
4024 else if(rowlb <= -inf && rowub >= inf)
4025 tag = MSK_BK_FR;
4026 else
4027 throw CoinError("Bound error", "addRow", "OsiMSKSolverInterface");
4028
4029 #if MSK_VERSION_MAJOR >= 7
4030 int err;
4031 MSKint32t i;
4032
4033 err = MSK_getnumcon(task,&i);
4034
4035 if ( err==MSK_RES_OK )
4036 err = MSK_appendcons(task,1);
4037
4038 if ( err==MSK_RES_OK )
4039 err = MSK_putconbound(task,i,tag,rowlb,rowub);
4040
4041 if ( err==MSK_RES_OK )
4042 err = MSK_putarow(task,i,ends,
4043 const_cast<int*>(vec.getIndices()),
4044 const_cast<double*>(vec.getElements()));
4045
4046 #else
4047 int start = 0;
4048 int err = MSK_appendcons(task,
4049 1,
4050 &start,
4051 &ends,
4052 const_cast<int*>(vec.getIndices()),
4053 const_cast<double*>(vec.getElements()),
4054 (&tag),
4055 const_cast<double*> (&rowlb),
4056 const_cast<double*> (&rowub));
4057 #endif
4058
4059 checkMSKerror( err, "MSK_appendcons", "addRow" );
4060
4061 #if MSK_OSI_DEBUG_LEVEL > 3
4062 debugMessage("End OsiMskSolverInterface::addRow(%p, %g, %g)\n", (void *)&vec, rowlb, rowub);
4063 #endif
4064 }
4065
4066 //-----------------------------------------------------------------------------
4067 // Adds a row in triplet form to the MOSEK task
4068
4069 void
4070 OsiMskSolverInterface::addRow(const CoinPackedVectorBase& vec,
4071 const char rowsen, const double rowrhs,
4072 const double rowrng)
4073 {
4074 #if MSK_OSI_DEBUG_LEVEL > 3
4075 debugMessage("Begin OsiMskSolverInterface::addRow(%p, %c, %g, %g)\n", (void *)&vec, rowsen, rowrhs, rowrng);
4076 #endif
4077
4078 int rtag=MSK_BK_FR;
4079 double lb=-MSK_INFINITY,ub=MSK_INFINITY;
4080 MskConvertSenseToBound( rowsen, rowrng, rowrhs, lb, ub, rtag );
4081 addRow(vec, lb, ub);
4082
4083 #if MSK_OSI_DEBUG_LEVEL > 3
4084 debugMessage("End OsiMskSolverInterface::addRow(%p, %c, %g, %g)\n", (void *)&vec, rowsen, rowrhs, rowrng);
4085 #endif
4086 }
4087
4088 //-----------------------------------------------------------------------------
4089 // Adds a serie of rows in bound form to the MOSEK task
4090
4091 void
4092 OsiMskSolverInterface::addRows(const int numrows,
4093 const CoinPackedVectorBase * const * rows,
4094 const double* rowlb, const double* rowub)
4095 {
4096 #if MSK_OSI_DEBUG_LEVEL > 3
4097 debugMessage("Begin OsiMskSolverInterface::addRows(%d, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowlb, (void *)rowub);
4098 #endif
4099
4100 int i,nz = 0, err = MSK_RES_OK;
4101
4102 // For efficiency we put hints on the total future size
4103 err = MSK_getmaxnumanz(
4104 getLpPtr(),
4105 &nz);
4106
4107 checkMSKerror( err, "MSK_getmaxanz", "addRows" );
4108
4109
4110 for( i = 0; i < numrows; ++i)
4111 nz += rows[i]->getNumElements();
4112
4113 err = MSK_putmaxnumanz(getLpPtr(),
4114 nz);
4115
4116 checkMSKerror( err, "MSK_putmaxanz", "addRows" );
4117
4118 err = MSK_putmaxnumcon(getLpPtr(),
4119 numrows+getNumRows());
4120
4121 checkMSKerror( err, "MSK_putmaxnumcon", "addRows" );
4122
4123 for( i = 0; i < numrows; ++i )
4124 addRow( *(rows[i]), rowlb[i], rowub[i] );
4125
4126 #if MSK_OSI_DEBUG_LEVEL > 3
4127 debugMessage("End OsiMskSolverInterface::addRows(%d, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowlb, (void *)rowub);
4128 #endif
4129 }
4130
4131 //-----------------------------------------------------------------------------
4132 // Adds a list of rows in triplet form to the MOSEK task
4133
4134 void
4135 OsiMskSolverInterface::addRows(const int numrows,
4136 const CoinPackedVectorBase * const * rows,
4137 const char* rowsen, const double* rowrhs,
4138 const double* rowrng)
4139 {
4140 #if MSK_OSI_DEBUG_LEVEL > 3
4141 debugMessage("Begin OsiMskSolverInterface::addRows(%d, %p, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
4142 #endif
4143
4144 int i, err = MSK_RES_OK, nz = 0;
4145
4146 // For efficiency we put hints on the total future size
4147 for( i = 0; i < numrows; ++i)
4148 nz += rows[i]->getNumElements();
4149
4150 err = MSK_putmaxnumanz(
4151 getLpPtr(),
4152 nz);
4153
4154 checkMSKerror( err, "MSK_putmaxanz", "addRows" );
4155
4156 err = MSK_putmaxnumcon(
4157 getLpPtr(),
4158 numrows);
4159
4160 checkMSKerror( err, "MSK_putmaxnumcon", "addRows" );
4161
4162 for( i = 0; i < numrows; ++i )
4163 addRow( *(rows[i]), rowsen[i], rowrhs[i], rowrng[i] );
4164
4165 #if MSK_OSI_DEBUG_LEVEL > 3
4166 debugMessage("End OsiMskSolverInterface::addRows(%d, %p, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
4167 #endif
4168 }
4169
4170 //-----------------------------------------------------------------------------
4171 // Deletes a list of rows the MOSEK task
4172
4173 void
4174 OsiMskSolverInterface::deleteRows(const int num, const int * rowIndices)
4175 {
4176 #if MSK_OSI_DEBUG_LEVEL > 3
4177 debugMessage("Begin OsiMskSolverInterface::deleteRows(%d, %p)\n", num, (void *)rowIndices);
4178 #endif
4179
4180 int err;
4181 #if MSK_VERSION_MAJOR >= 7
4182 err = MSK_removecons(getLpPtr( OsiMskSolverInterface::KEEPCACHED_COLUMN ),
4183 num,
4184 const_cast<int*>(rowIndices));
4185
4186 checkMSKerror( err, "MSK_removecons", "deleteRows" );
4187
4188 #else
4189 err = MSK_remove(getLpPtr( OsiMskSolverInterface::KEEPCACHED_COLUMN ),
4190 MSK_ACC_CON,
4191 num,
4192 const_cast<int*>(rowIndices));
4193
4194 checkMSKerror( err, "MSK_remove", "deleteRows" );
4195 #endif
4196
4197 #if MSK_OSI_DEBUG_LEVEL > 3
4198 debugMessage("End OsiMskSolverInterface::deleteRows(%d, %p)\n", num, (void *)rowIndices);
4199 #endif
4200 }
4201
4202 //#############################################################################
4203 // Methods to input a problem
4204 //#############################################################################
4205
4206 //-----------------------------------------------------------------------------
4207 // Loads a problem. Should have its "own" implementation so we don't have to convert
4208 // to triplet, since this is convertet back in the load function called. But
4209 // for simplicity, this is not done.
4210
4211 void
4212 OsiMskSolverInterface::loadProblem(const CoinPackedMatrix& matrix,
4213 const double* collb,
4214 const double* colub,
4215 const double* obj,
4216 const double* rowlb,
4217 const double* rowub )
4218 {
4219 #if MSK_OSI_DEBUG_LEVEL > 1
4220 debugMessage("Begin OsiMskSolverInterface::loadProblem(%p, %p, %p, %p, %p, %p)\n", (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowlb, (void *)rowub);
4221 #endif
4222
4223 const double inf = getInfinity();
4224
4225 int nrows = matrix.getNumRows();
4226
4227 char * rowSense;
4228 double * rowRhs;
4229 double * rowRange;
4230
4231 if( nrows )
4232 {
4233 rowSense = new char [nrows];
4234 rowRhs = new double[nrows];
4235 rowRange = new double[nrows];
4236 }
4237 else
4238 {
4239 rowSense = NULL;
4240 rowRhs = NULL;
4241 rowRange = NULL;
4242 }
4243
4244 int i;
4245 if( rowlb == NULL && rowub == NULL)
4246 for ( i = nrows - 1; i >= 0; --i )
4247 convertBoundToSense( -inf, inf, rowSense[i], rowRhs[i], rowRange[i] );
4248 else if( rowlb == NULL)
4249 for ( i = nrows - 1; i >= 0; --i )
4250 convertBoundToSense( -inf, rowub[i], rowSense[i], rowRhs[i], rowRange[i] );
4251 else if( rowub == NULL)
4252 for ( i = nrows - 1; i >= 0; --i )
4253 convertBoundToSense( rowlb[i], inf, rowSense[i], rowRhs[i], rowRange[i] );
4254 else
4255 for ( i = nrows - 1; i >= 0; --i )
4256 convertBoundToSense( rowlb[i], rowub[i], rowSense[i], rowRhs[i], rowRange[i] );
4257
4258 loadProblem( matrix, collb, colub, obj, rowSense, rowRhs, rowRange );
4259
4260 if( nrows )
4261 {
4262 delete [] rowSense;
4263 delete [] rowRhs;
4264 delete [] rowRange;
4265 }
4266
4267 #if MSK_OSI_DEBUG_LEVEL > 1
4268 debugMessage("End OsiMskSolverInterface::loadProblem(%p, %p, %p, %p, %p, %p)\n", (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowlb, (void *)rowub);
4269 #endif
4270 }
4271
4272 //-----------------------------------------------------------------------------
4273 // Loads a problem
4274
4275 void
4276 OsiMskSolverInterface::assignProblem( CoinPackedMatrix*& matrix,
4277 double*& collb,
4278 double*& colub,
4279 double*& obj,
4280 double*& rowlb,
4281 double*& rowub )
4282 {
4283 #if MSK_OSI_DEBUG_LEVEL > 3
4284 debugMessage("Begin OsiMskSolverInterface::assignProblem()\n");
4285 #endif
4286
4287 loadProblem( *matrix, collb, colub, obj, rowlb, rowub );
4288
4289 delete matrix; matrix = 0;
4290 delete[] collb; collb = 0;
4291 delete[] colub; colub = 0;
4292 delete[] obj; obj = 0;
4293 delete[] rowlb; rowlb = 0;
4294 delete[] rowub; rowub = 0;
4295
4296 #if MSK_OSI_DEBUG_LEVEL > 3
4297 debugMessage("End OsiMskSolverInterface::assignProblem()\n");
4298 #endif
4299 }
4300
4301 //-----------------------------------------------------------------------------
4302 // Loads a problem
4303
4304 void
4305 OsiMskSolverInterface::loadProblem( const CoinPackedMatrix& matrix,
4306 const double* collb,
4307 const double* colub,
4308 const double* obj,
4309 const char* rowsen,
4310 const double* rowrhs,
4311 const double* rowrng )
4312 {
4313 int nc=matrix.getNumCols();
4314 int nr=matrix.getNumRows();
4315
4316 #if MSK_OSI_DEBUG_LEVEL > 1
4317 debugMessage("Begin OsiMskSolverInterface::loadProblem(%p, %p, %p, %p, %p, %p, %p) numcols : %d numrows : %d\n",
4318 (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen, (void *)rowrhs, (void *)rowrng,nc,nr);
4319 #endif
4320
4321 if( nr == 0 && nc == 0 )
4322 gutsOfDestructor();
4323 else
4324 {
4325 /* Warning input pointers can be NULL */
4326
4327 int i,j;
4328
4329 double * ob;
4330 int * rtag = NULL;
4331 double * rlb = NULL;
4332 double * rub = NULL;
4333
4334 int * ctag = NULL;
4335 int * cends = NULL;
4336 const int *len;
4337 const int *start;
4338 double * clb = NULL;
4339 double * cub = NULL;
4340
4341 if( obj != NULL )
4342 ob=const_cast<double*>(obj);
4343 else
4344 {
4345 ob = new double[nc];
4346 CoinFillN(ob, nc, 0.0);
4347 }
4348
4349 if( nr )
4350 {
4351 rtag = new int[nr];
4352 rlb = new double[nr];
4353 rub = new double[nr];
4354 }
4355
4356 if( rowsen && rowrng && rowrhs )
4357 {
4358 for( i=0; i < nr; i++ )
4359 MskConvertSenseToBound( rowsen[i], rowrng[i], rowrhs[i], rlb[i], rub[i], rtag[i]);
4360 }
4361 else
4362 {
4363 for( i=0; i < nr; i++ )
4364 {
4365 rlb[i] = 0.0;
4366 rub[i] = MSK_INFINITY;
4367 rtag[i] = MSK_BK_LO;
4368 }
4369 }
4370
4371 bool freeMatrixRequired = false;
4372 CoinPackedMatrix * m = NULL;
4373 if( !matrix.isColOrdered() )
4374 {
4375 m = new CoinPackedMatrix();
4376 m->reverseOrderedCopyOf(matrix);
4377 freeMatrixRequired = true;
4378 }
4379 else
4380 m = const_cast<CoinPackedMatrix *>(&matrix);
4381
4382 MSKassert(3,nc == m->getNumCols(),"nc == m->getNumCols()","loadProblem");
4383 MSKassert(3,nr == m->getNumRows(),"nr == m->getNumRows()","loadProblem");
4384 MSKassert(3,m->isColOrdered(),"m->isColOrdered()","loadProblem");
4385
4386 double inf =getInfinity();
4387
4388 if( nc )
4389 {
4390 ctag = new int[nc];
4391 cends = new int[nc];
4392 clb = new double[nc];
4393 cub = new double[nc];
4394 }
4395
4396 len = (m->getVectorLengths());
4397 start = (m->getVectorStarts());
4398
4399 if( collb == NULL && colub == NULL )
4400 {
4401 for(j=0; j < nc; j++)
4402 {
4403 cends[j] = start[j] + len[j];
4404 MskConvertColBoundToTag(0, inf, clb[j], cub[j], ctag[j]);
4405 }
4406 }
4407 else if( collb == NULL )
4408 {
4409 for(j=0; j < nc; j++)
4410 {
4411 cends[j] = start[j] + len[j];
4412 MskConvertColBoundToTag( 0, colub[j], clb[j], cub[j], ctag[j]);
4413 }
4414 }
4415 else if( colub == NULL )
4416 {
4417 for(j=0; j < nc; j++)
4418 {
4419 cends[j] = start[j] + len[j];
4420 MskConvertColBoundToTag(collb[j], inf, clb[j], cub[j], ctag[j]);
4421 }
4422 }
4423 else
4424 {
4425 for(j=0; j < nc; j++)
4426 {
4427 cends[j] = start[j] + len[j];
4428 MskConvertColBoundToTag( collb[j], colub[j], clb[j], cub[j], ctag[j]);
4429 }
4430 }
4431
4432 int err=MSK_inputdata(getLpPtr( OsiMskSolverInterface::KEEPCACHED_NONE ),
4433 nr,
4434 nc,
4435 nr,
4436 nc,
4437 ob,
4438 0.0,
4439 const_cast<int *>(m->getVectorStarts()),
4440 cends,
4441 const_cast<int *>(m->getIndices()),
4442 const_cast<double *>(m->getElements()),
4443 (MSKboundkeye*)rtag,
4444 rlb,
4445 rub,
4446 (MSKboundkeye*)ctag,
4447 clb,
4448 cub);
4449
4450 checkMSKerror( err, "MSK_inputdata", "loadProblem" );
4451
4452 if( obj == NULL )
4453 delete[] ob;
4454
4455 if( nr )
4456 {
4457 delete[] rtag;
4458 delete[] rlb;
4459 delete[] rub;
4460 }
4461
4462 if( nc )
4463 {
4464 delete[] ctag;
4465 delete[] clb;
4466 delete[] cub;
4467 delete[] cends;
4468 }
4469
4470 if ( freeMatrixRequired )
4471 delete m;
4472
4473 resizeColType(nc);
4474 CoinFillN(coltype_, nc, 'C');
4475 }
4476
4477 #if MSK_OSI_DEBUG_LEVEL > 1
4478 debugMessage("End OsiMskSolverInterface::loadProblem(%p, %p, %p, %p, %p, %p, %p)\n",
4479 (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
4480 #endif
4481 }
4482
4483 //-----------------------------------------------------------------------------
4484 // Assigns a problem
4485
4486 void
4487 OsiMskSolverInterface::assignProblem( CoinPackedMatrix*& matrix,
4488 double*& collb, double*& colub,
4489 double*& obj,
4490 char*& rowsen, double*& rowrhs,
4491 double*& rowrng )
4492 {
4493 #if MSK_OSI_DEBUG_LEVEL > 3
4494 debugMessage("Begin OsiMskSolverInterface::assignProblem()\n");
4495 #endif
4496
4497 loadProblem( *matrix, collb, colub, obj, rowsen, rowrhs, rowrng );
4498 delete matrix; matrix = 0;
4499 delete[] collb; collb = 0;
4500 delete[] colub; colub = 0;
4501 delete[] obj; obj = 0;
4502 delete[] rowsen; rowsen = 0;
4503 delete[] rowrhs; rowrhs = 0;
4504 delete[] rowrng; rowrng = 0;
4505
4506 #if MSK_OSI_DEBUG_LEVEL > 3
4507 debugMessage("End OsiMskSolverInterface::assignProblem()\n");
4508 #endif
4509 }
4510
4511 //-----------------------------------------------------------------------------
4512 // Loads a problem
4513
4514 void
4515 OsiMskSolverInterface::loadProblem(const int numcols,
4516 const int numrows,
4517 const int* start,
4518 const int* index,
4519 const double* value,
4520 const double* collb,
4521 const double* colub,
4522 const double* obj,
4523 const double* rowlb,
4524 const double* rowub )
4525 {
4526 #if MSK_OSI_DEBUG_LEVEL > 1
4527 debugMessage("Begin OsiMskSolverInterface::loadProblem() numcols : %d numrows : %d\n",numcols,numrows);
4528 #endif
4529
4530 const double inf = getInfinity();
4531
4532 char * rowSense = new char [numrows];
4533 double * rowRhs = new double[numrows];
4534 double * rowRange = new double[numrows];
4535
4536 for ( int i = numrows - 1; i >= 0; --i )
4537 {
4538 const double lower = rowlb ? rowlb[i] : -inf;
4539 const double upper = rowub ? rowub[i] : inf;
4540 convertBoundToSense( lower, upper, rowSense[i], rowRhs[i], rowRange[i] );
4541 }
4542
4543 loadProblem(numcols, numrows, start, index, value, collb, colub, obj,
4544 rowSense, rowRhs, rowRange);
4545
4546 delete [] rowSense;
4547 delete [] rowRhs;
4548 delete [] rowRange;
4549
4550 #if MSK_OSI_DEBUG_LEVEL > 1
4551 debugMessage("End OsiMskSolverInterface::loadProblem()\n");
4552 #endif
4553 }
4554
4555 //-----------------------------------------------------------------------------
4556 // Loads a problem
4557
4558 void
4559 OsiMskSolverInterface::loadProblem(const int numcols,
4560 const int numrows,
4561 const int* start,
4562 const int* index,
4563 const double* value,
4564 const double* collb,
4565 const double* colub,
4566 const double* obj,
4567 const char* rowsen,
4568 const double* rowrhs,
4569 const double* rowrng )
4570 {
4571 #if MSK_OSI_DEBUG_LEVEL > 1
4572 debugMessage("Begin OsiMskSolverInterface::loadProblem(%d, %d, %p, %p, %p, %p, %p, %p, %p, %p, %p)\n",
4573 numcols, numrows, (void *)start, (void *)index, (void *)value, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen,(void *)rowrhs, (void *)rowrng);
4574 #endif
4575
4576 const int nc = numcols;
4577 const int nr = numrows;
4578
4579 if( nr == 0 && nc == 0 )
4580 gutsOfDestructor();
4581 else
4582 {
4583 MSKassert(3,rowsen != NULL,"rowsen != NULL","loadProblem");
4584 MSKassert(3,rowrhs != NULL,"rowrhs != NULL","loadProblem");
4585
4586 int i,j;
4587
4588 double * ob;
4589 int * rtag =NULL;
4590 double * rlb = NULL;
4591 double * rub = NULL;
4592 int * ctag =NULL;
4593 int * cends = NULL;
4594 double * clb = NULL;
4595 double * cub = NULL;
4596
4597 if( obj != NULL )
4598 ob=const_cast<double*>(obj);
4599 else
4600 {
4601 ob = new double[nc];
4602 CoinFillN(ob, nc, 0.0);
4603 }
4604
4605 if( nr )
4606 {
4607 rtag = new int[nr];
4608 rlb = new double[nr];
4609 rub = new double[nr];
4610 }
4611
4612 for( i=0; i < nr; i++ )
4613 MskConvertSenseToBound( rowsen[i], rowrng != NULL ? rowrng[i] : 0.0, rowrhs[i], rlb[i], rub[i], rtag[i]);
4614
4615 double inf = getInfinity();
4616
4617 if( nc )
4618 {
4619 ctag = new int[nc];
4620 cends = new int[nc];
4621 clb = new double[nc];
4622 cub = new double[nc];
4623 }
4624
4625 if( collb == NULL && colub == NULL )
4626 for(j=0; j < nc; j++)
4627 {
4628 cends[j] = start[j+1];
4629 MskConvertColBoundToTag(0, inf, clb[j], cub[j], ctag[j]);
4630 }
4631 else if( collb == NULL )
4632 for(j=0; j < nc; j++)
4633 {
4634 cends[j] = start[j+1];
4635 MskConvertColBoundToTag( 0, colub[j], clb[j], cub[j], ctag[j]);
4636 }
4637 else if( colub == NULL )
4638 for(j=0; j < nc; j++)
4639 {
4640 cends[j] = start[j+1];
4641 MskConvertColBoundToTag(collb[j], inf, clb[j], cub[j], ctag[j]);
4642 }
4643 else
4644 for(j=0; j < nc; j++)
4645 {
4646 cends[j] = start[j+1];
4647 MskConvertColBoundToTag( collb[j], colub[j], clb[j], cub[j], ctag[j]);
4648 }
4649
4650 int err=MSK_inputdata(getLpPtr( OsiMskSolverInterface::KEEPCACHED_NONE ),
4651 nr,
4652 nc,
4653 nr,
4654 nc,
4655 ob,
4656 0.0,
4657 const_cast<int *>(start),
4658 cends,
4659 const_cast<int *>(index),
4660 const_cast<double *>(value),
4661 (MSKboundkeye*)rtag,
4662 rlb,
4663 rub,
4664 (MSKboundkeye*)ctag,
4665 clb,
4666 cub);
4667
4668 checkMSKerror( err, "MSK_inputdata", "loadProblem3" );
4669
4670 if( obj == NULL )
4671 delete[] ob;
4672
4673 if( nr )
4674 {
4675 delete[] rtag;
4676 delete[] rlb;
4677 delete[] rub;
4678 }
4679
4680 if( nc )
4681 {
4682 delete[] ctag;
4683 delete[] cends;
4684 delete[] clb;
4685 delete[] cub;
4686 }
4687
4688 resizeColType(nc);
4689 CoinFillN(coltype_, nc, 'C');
4690 }
4691
4692 #if MSK_OSI_DEBUG_LEVEL > 1
4693 debugMessage("End OsiMskSolverInterface::loadProblem(%d, %d, %p, %p, %p, %p, %p, %p, %p, %p, %p)\n",
4694 numcols, numrows, (void *)start, (void *)index, (void *)value, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen, (void *)rowrhs, (void *)rowrng);
4695 #endif
4696 }
4697
4698 //-----------------------------------------------------------------------------
4699 // Reads a MPS file with Coin native MPS reader. If marked code is switch on
4700 // then MOSEK file reader is used, and .gz files can be read aswell.
4701
4702 int OsiMskSolverInterface::readMps(const char * filename,
4703 const char * extension )
4704 {
4705 #if MSK_OSI_DEBUG_LEVEL > 3
4706 debugMessage("Begin OsiMskSolverInterface::readMps(%s, %s) %p\n", filename, extension,(void*)this);
4707 debugMessage("End OsiMskSolverInterface::readMps(%s, %s)\n", filename, extension);
4708 #endif
4709
4710 return OsiSolverInterface::readMps(filename,extension);
4711 }
4712
4713 //-----------------------------------------------------------------------------
4714 // Writes the problem in MPS format, uses MOSEK writer.
4715
4716 void OsiMskSolverInterface::writeMps( const char * filename,
4717 const char * extension,
4718 double objSense ) const
4719 {
4720 #if MSK_OSI_DEBUG_LEVEL > 3
4721 debugMessage("Begin OsiMskSolverInterface::writeMps(%s, %s, %g)\n", filename, extension, objSense);
4722 #endif
4723
4724 std::string f(filename);
4725 std::string e(extension);
4726 std::string fullname = f + "." + e;
4727
4728 OsiSolverInterface::writeMpsNative(fullname.c_str(),NULL, NULL, 0, 2, objSense);
4729
4730 #if 0
4731 int err = MSK_writedata( getMutableLpPtr(), const_cast<char*>( fullname.c_str() ));
4732 checkMSKerror( err, "MSK_writedatafile", "writeMps" );
4733 #endif
4734
4735 #if MSK_OSI_DEBUG_LEVEL > 3
4736 debugMessage("End OsiMskSolverInterface::writeMps(%s, %s, %g)\n", filename, extension, objSense);
4737 #endif
4738 }
4739
4740 void OsiMskSolverInterface::passInMessageHandler(CoinMessageHandler * handler) {
4741 OsiSolverInterface::passInMessageHandler(handler);
4742
4743 MSK_linkfunctotaskstream(getMutableLpPtr(), MSK_STREAM_LOG, messageHandler(), OsiMskStreamFuncLog);
4744 MSK_linkfunctotaskstream(getMutableLpPtr(), MSK_STREAM_ERR, messageHandler(), OsiMskStreamFuncWarning);
4745 MSK_linkfunctotaskstream(getMutableLpPtr(), MSK_STREAM_WRN, messageHandler(), OsiMskStreamFuncError);
4746 }
4747
4748 //#############################################################################
4749 // MSK specific public interfaces
4750 //#############################################################################
4751
4752 //-----------------------------------------------------------------------------
4753 // Returns MOSEK task in the interface object
4754
4755 MSKenv_t OsiMskSolverInterface::getEnvironmentPtr()
4756 {
4757 MSKassert(3,env_ != NULL,"probtypemip_","getEnvironmentPtr");
4758
4759 return env_;
4760 }
4761
4762 //-----------------------------------------------------------------------------
4763 // Returns MOSEK task in the interface object
4764
4765 MSKtask_t OsiMskSolverInterface::getLpPtr( int keepCached )
4766 {
4767 freeCachedData( keepCached );
4768 return getMutableLpPtr();
4769 }
4770
4771 //-----------------------------------------------------------------------------
4772 // Returns the coltype_ array
4773
4774 const char * OsiMskSolverInterface::getCtype() const
4775 {
4776 #if MSK_OSI_DEBUG_LEVEL > 3
4777 debugMessage("Begin OsiMskSolverInterface::getCtype()\n");
4778 debugMessage("End OsiMskSolverInterface::getCtype()\n");
4779 #endif
4780
4781 return coltype_;
4782 }
4783
4784 //#############################################################################
4785 // Static instance counter methods
4786 //#############################################################################
4787
4788 //-----------------------------------------------------------------------------
4789 // Increment the instance count, so we know when to close and open MOSEK.
4790
4791 void OsiMskSolverInterface::incrementInstanceCounter()
4792 {
4793 #if MSK_OSI_DEBUG_LEVEL > 3
4794 debugMessage("Begin OsiMskSolverInterface::incrementInstanceCounter()\n");
4795 #endif
4796
4797 if ( numInstances_ == 0 )
4798 {
4799 int err=0;
4800
4801 #if MSK_OSI_DEBUG_LEVEL > 1
4802 debugMessage("creating new Mosek environment\n");
4803 #endif
4804
4805 #if MSK_VERSION_MAJOR >= 7
4806 err = MSK_makeenv(&env_,NULL);
4807 #else
4808 err = MSK_makeenv(&env_,NULL,NULL,NULL,NULL);
4809 #endif
4810 checkMSKerror( err, "MSK_makeenv", "incrementInstanceCounter" );
4811
4812 err = MSK_linkfunctoenvstream(env_, MSK_STREAM_LOG, NULL, printlog);
4813
4814 checkMSKerror( err, "MSK_linkfunctoenvstream", "incrementInstanceCounter" );
4815
4816 #if MSK_VERSION_MAJOR < 8
4817 err = MSK_initenv(env_);
4818 checkMSKerror( err, "MSK_initenv", "incrementInstanceCounter" );
4819 #endif
4820 }
4821
4822 numInstances_++;
4823
4824 #if MSK_OSI_DEBUG_LEVEL > 3
4825 debugMessage("End OsiMskSolverInterface::incrementInstanceCounter()\n");
4826 #endif
4827 }
4828
4829 //-----------------------------------------------------------------------------
4830 // Decrement the instance count, so we know when to close and open MOSEK.
4831
4832 void OsiMskSolverInterface::decrementInstanceCounter()
4833 {
4834 #if MSK_OSI_DEBUG_LEVEL > 3
4835 debugMessage("Begin OsiMskSolverInterface::decrementInstanceCounter()\n");
4836 #endif
4837
4838 MSKassert(3,numInstances_ != 0,"numInstances_ != 0","decrementInstanceCounter");
4839
4840 numInstances_--;
4841 if ( numInstances_ == 0 )
4842 {
4843 #if MSK_OSI_DEBUG_LEVEL > 1
4844 debugMessage("deleting Mosek environment\n");
4845 #endif
4846
4847 int err = MSK_deleteenv(&env_);
4848 checkMSKerror( err, "MSK_deleteenv", "decrementInstanceCounter" );
4849 env_ = NULL;
4850 }
4851
4852 #if MSK_OSI_DEBUG_LEVEL > 3
4853 debugMessage("End OsiMskSolverInterface::decrementInstanceCounter()\n");
4854 #endif
4855 }
4856
4857 //-----------------------------------------------------------------------------
4858 // Returns the number of OsiMskSolverInterface objects in play
4859
4860 unsigned int OsiMskSolverInterface::getNumInstances()
4861 {
4862 #if MSK_OSI_DEBUG_LEVEL > 3
4863 debugMessage("Begin OsiMskSolverInterface::getNumInstances()\n");
4864 debugMessage("End OsiMskSolverInterface::getNumInstances()\n");
4865 #endif
4866
4867 return numInstances_;
4868 }
4869
4870 //#############################################################################
4871 // Constructors, destructors clone and assignment
4872 //#############################################################################
4873
4874 //-----------------------------------------------------------------------------
4875 // Constructor
4876
4877 OsiMskSolverInterface::OsiMskSolverInterface(MSKenv_t mskenv)
4878 : OsiSolverInterface(),
4879 Mskerr(MSK_RES_OK),
4880 ObjOffset_(0.0),
4881 InitialSolver(INITIAL_SOLVE),
4882 task_(NULL),
4883 hotStartCStat_(NULL),
4884 hotStartCStatSize_(0),
4885 hotStartRStat_(NULL),
4886 hotStartRStatSize_(0),
4887 hotStartMaxIteration_(1000000),
4888 obj_(NULL),
4889 collower_(NULL),
4890 colupper_(NULL),
4891 rowsense_(NULL),
4892 rhs_(NULL),
4893 rowrange_(NULL),
4894 rowlower_(NULL),
4895 rowupper_(NULL),
4896 colsol_(NULL),
4897 rowsol_(NULL),
4898 redcost_(NULL),
4899 rowact_(NULL),
4900 matrixByRow_(NULL),
4901 matrixByCol_(NULL),
4902 coltype_(NULL),
4903 coltypesize_(0),
4904 probtypemip_(false)
4905 {
4906 #if MSK_OSI_DEBUG_LEVEL > 3
4907 debugMessage("Begin OsiMskSolverInterface::OsiMskSolverinterface()\n");
4908 #endif
4909
4910 if (mskenv) {
4911 if (env_) {
4912 throw CoinError("Already have a global Mosek environment. Cannot use second one.", "OsiMskSolverInterface", "OsiMskSolverInterface");
4913 }
4914 env_ = mskenv;
4915 ++numInstances_;
4916 } else
4917 incrementInstanceCounter();
4918
4919 gutsOfConstructor();
4920
4921 #if MSK_OSI_DEBUG_LEVEL > 3
4922 debugMessage("End OsiMskSolverInterface::OsiMskSolverinterface()\n");
4923 #endif
4924 }
4925
4926
4927
4928 //-----------------------------------------------------------------------------
4929 // Clone from one to another object
4930
4931 OsiSolverInterface * OsiMskSolverInterface::clone(bool copyData) const
4932 {
4933 #if MSK_OSI_DEBUG_LEVEL > 3
4934 debugMessage("Begin OsiMskSolverInterface::clone(%d)\n", copyData);
4935 debugMessage("End OsiMskSolverInterface::clone(%d)\n", copyData);
4936 #endif
4937
4938 return( new OsiMskSolverInterface( *this ) );
4939 }
4940
4941
4942 //-----------------------------------------------------------------------------
4943 // Copy constructor
4944
4945 OsiMskSolverInterface::OsiMskSolverInterface( const OsiMskSolverInterface & source )
4946 : OsiSolverInterface(source),
4947 Mskerr(MSK_RES_OK),
4948 MSKsolverused_(INITIAL_SOLVE),
4949 ObjOffset_(source.ObjOffset_),
4950 InitialSolver(INITIAL_SOLVE),
4951 task_(NULL),
4952 hotStartCStat_(NULL),
4953 hotStartCStatSize_(0),
4954 hotStartRStat_(NULL),
4955 hotStartRStatSize_(0),
4956 hotStartMaxIteration_(source.hotStartMaxIteration_),
4957 obj_(NULL),
4958 collower_(NULL),
4959 colupper_(NULL),
4960 rowsense_(NULL),
4961 rhs_(NULL),
4962 rowrange_(NULL),
4963 rowlower_(NULL),
4964 rowupper_(NULL),
4965 colsol_(NULL),
4966 rowsol_(NULL),
4967 redcost_(NULL),
4968 rowact_(NULL),
4969 matrixByRow_(NULL),
4970 matrixByCol_(NULL),
4971 coltype_(NULL),
4972 coltypesize_(0),
4973 probtypemip_(false)
4974 {
4975 #if MSK_OSI_DEBUG_LEVEL > 3
4976 debugMessage("Begin OsiMskSolverInterface::OsiMskSolverInterface from (%p) to %p\n", (void *)&source,(void*)this);
4977 #endif
4978
4979 incrementInstanceCounter();
4980 gutsOfConstructor();
4981 gutsOfCopy( source );
4982
4983 #if MSK_OSI_DEBUG_LEVEL > 3
4984 debugMessage("End OsiMskSolverInterface::OsiMskSolverInterface(%p)\n", (void *)&source);
4985 #endif
4986 }
4987
4988 //-----------------------------------------------------------------------------
4989 // Destructor
4990
4991 OsiMskSolverInterface::~OsiMskSolverInterface()
4992 {
4993 #if MSK_OSI_DEBUG_LEVEL > 3
4994 debugMessage("Begin OsiMskSolverInterface::~OsiMskSolverInterface()\n");
4995 #endif
4996
4997 gutsOfDestructor();
4998 decrementInstanceCounter();
4999
5000 #if MSK_OSI_DEBUG_LEVEL > 3
5001 debugMessage("End OsiMskSolverInterface::~OsiMskSolverInterface()\n");
5002 #endif
5003 }
5004
5005 //-----------------------------------------------------------------------------
5006 // Assign operator
5007
5008 OsiMskSolverInterface& OsiMskSolverInterface::operator=( const OsiMskSolverInterface& rhs )
5009 {
5010 #if MSK_OSI_DEBUG_LEVEL > 3
5011 debugMessage("Begin OsiMskSolverInterface::operator=(%p)\n", (void *)&rhs);
5012 #endif
5013
5014 if (this != &rhs)
5015 {
5016 OsiSolverInterface::operator=( rhs );
5017 gutsOfDestructor();
5018 gutsOfConstructor();
5019
5020 if ( rhs.task_ !=NULL )
5021 gutsOfCopy( rhs );
5022 }
5023
5024 #if MSK_OSI_DEBUG_LEVEL > 3
5025 debugMessage("End OsiMskSolverInterface::operator=(%p)\n", (void *)&rhs);
5026 #endif
5027
5028 return *this;
5029 }
5030
5031 //#############################################################################
5032 // Applying cuts
5033 //#############################################################################
5034
5035 //-----------------------------------------------------------------------------
5036 // Apply col cut
5037
5038 void OsiMskSolverInterface::applyColCut( const OsiColCut & cc )
5039 {
5040 #if MSK_OSI_DEBUG_LEVEL > 3
5041 debugMessage("Begin OsiMskSolverInterface::applyColCut(%p)\n", (void *)&cc);
5042 #endif
5043
5044 double * MskColLB = new double[getNumCols()];
5045 double * MskColUB = new double[getNumCols()];
5046 const CoinPackedVector & lbs = cc.lbs();
5047 const CoinPackedVector & ubs = cc.ubs();
5048 int i;
5049
5050 for( i = 0; i < getNumCols(); ++i )
5051 {
5052 MskColLB[i] = getColLower()[i];
5053 MskColUB[i] = getColUpper()[i];
5054 }
5055
5056 for( i = 0; i < lbs.getNumElements(); ++i )
5057 if ( lbs.getElements()[i] > MskColLB[lbs.getIndices()[i]] )
5058 setColLower( lbs.getIndices()[i], lbs.getElements()[i] );
5059 for( i = 0; i < ubs.getNumElements(); ++i )
5060 if ( ubs.getElements()[i] < MskColUB[ubs.getIndices()[i]] )
5061 setColUpper( ubs.getIndices()[i], ubs.getElements()[i] );
5062
5063 delete[] MskColLB;
5064 delete[] MskColUB;
5065
5066 #if MSK_OSI_DEBUG_LEVEL > 3
5067 debugMessage("End OsiMskSolverInterface::applyColCut(%p)\n", (void *)&cc);
5068 #endif
5069 }
5070
5071 //-----------------------------------------------------------------------------
5072 // Apply row cut
5073
5074 void OsiMskSolverInterface::applyRowCut( const OsiRowCut & rowCut )
5075 {
5076 #if MSK_OSI_DEBUG_LEVEL > 3
5077 debugMessage("Begin OsiMskSolverInterface::applyRowCut(%p)\n", (void *)&rowCut);
5078 #endif
5079
5080 const CoinPackedVector & row=rowCut.row();
5081 addRow(row , rowCut.lb(),rowCut.ub());
5082
5083 #if MSK_OSI_DEBUG_LEVEL > 3
5084 debugMessage("End OsiMskSolverInterface::applyRowCut(%p)\n", (void *)&rowCut);
5085 #endif
5086 }
5087
5088 //#############################################################################
5089 // Private methods (non-static and static) and static data
5090 //#############################################################################
5091
5092 unsigned int OsiMskSolverInterface::numInstances_ = 0;
5093 MSKenv_t OsiMskSolverInterface::env_=NULL;
5094
5095 //-----------------------------------------------------------------------------
5096 // Returns MOSEK task in object
5097
5098 MSKtask_t OsiMskSolverInterface::getMutableLpPtr() const
5099 {
5100 #if MSK_OSI_DEBUG_LEVEL > 3
5101 debugMessage("Begin OsiMskSolverInterface::getMutableLpPtr()\n");
5102 #endif
5103
5104 if ( task_ == NULL )
5105 {
5106 MSKassert(3,env_ != NULL,"env_ == NULL","getMutableLpPtr");
5107
5108 int err = MSK_makeemptytask(env_,&task_);
5109 checkMSKerror(err, "MSK_makeemptytask","getMutableLpPtr");
5110
5111 err = MSK_linkfunctotaskstream(task_, MSK_STREAM_LOG, messageHandler(), OsiMskStreamFuncLog);
5112 checkMSKerror( err, "MSK_linkfunctotaskstream", "getMutableLpPtr" );
5113
5114 err = MSK_linkfunctotaskstream(task_, MSK_STREAM_WRN, messageHandler(), OsiMskStreamFuncWarning);
5115 checkMSKerror( err, "MSK_linkfunctotaskstream", "getMutableLpPtr" );
5116
5117 err = MSK_linkfunctotaskstream(task_, MSK_STREAM_ERR, messageHandler(), OsiMskStreamFuncError);
5118 checkMSKerror( err, "MSK_linkfunctotaskstream", "getMutableLpPtr" );
5119
5120 err = MSK_putintparam(task_, MSK_IPAR_WRITE_GENERIC_NAMES, MSK_ON);
5121 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5122
5123 err = MSK_putintparam(task_, MSK_IPAR_PRESOLVE_USE, MSK_ON);
5124 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5125
5126 #if MSK_VERSION_MAJOR < 9
5127 err = MSK_putintparam(task_, MSK_IPAR_WRITE_DATA_FORMAT, MSK_DATA_FORMAT_MPS);
5128 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5129 #endif
5130
5131 err = MSK_putintparam(task_, MSK_IPAR_WRITE_GENERIC_NAMES, MSK_ON);
5132 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5133
5134 #if MSK_DO_MOSEK_LOG > 0
5135 char file[] = "MOSEK.log";
5136 err = MSK_linkfiletotaskstream(task_, MSK_STREAM_LOG, file, 0);
5137 checkMSKerror( err, "MSK_linkfiletotaskstream", "getMutableLpPtr" );
5138
5139 err = MSK_putintparam(task_, MSK_IPAR_LOG, 100);
5140 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5141
5142 err = MSK_putintparam(task_, MSK_IPAR_LOG_SIM, 100);
5143 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5144
5145 err = MSK_putintparam(task_, MSK_IPAR_LOG_INTPNT, 100);
5146 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5147 #else
5148 err = MSK_putintparam(task_, MSK_IPAR_LOG, 100);
5149 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5150 #endif
5151
5152 err = MSK_putintparam(task_,MSK_IPAR_SIM_SOLVE_FORM,MSK_SOLVE_PRIMAL);
5153 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5154
5155 err = MSK_putintparam(task_,MSK_IPAR_SIM_HOTSTART,MSK_SIM_HOTSTART_STATUS_KEYS);
5156 checkMSKerror(err,"MSK_putintparam","getMutableLpPtr()");
5157
5158 std::string pn;
5159 getStrParam(OsiProbName,pn);
5160 MSK_puttaskname( task_, const_cast<char*>(pn.c_str()) );
5161 checkMSKerror(err,"MSK_puttaskname","getMutableLpPtr()");
5162 }
5163
5164 #if MSK_OSI_DEBUG_LEVEL > 3
5165 debugMessage("End OsiMskSolverInterface::getMutableLpPtr()\n");
5166 #endif
5167
5168 return task_;
5169 }
5170
5171 //-----------------------------------------------------------------------------
5172 // Makes a copy
5173
5174 void OsiMskSolverInterface::gutsOfCopy( const OsiMskSolverInterface & source )
5175 {
5176 #if MSK_OSI_DEBUG_LEVEL > 1
5177 debugMessage("Begin OsiMskSolverInterface::gutsOfCopy()\n");
5178 #endif
5179
5180 int err;
5181
5182 InitialSolver = source.InitialSolver;
5183
5184 MSKassert(3, task_ == NULL, "task_ == NULL", "gutsOfCopy");
5185 err = MSK_clonetask(source.getMutableLpPtr(),&task_);
5186
5187 checkMSKerror( err, "MSK_clonetask", "gutsOfCopy" );
5188
5189 // Set MIP information
5190 resizeColType(source.coltypesize_);
5191 CoinDisjointCopyN( source.coltype_, source.coltypesize_, coltype_ );
5192
5193 // Updates task MIP information
5194 for( int k = 0; k < source.coltypesize_; ++k )
5195 {
5196 switch(coltype_[k])
5197 {
5198 case 'I':
5199 setInteger(k);
5200 break;
5201
5202 case 'C':
5203 setContinuous(k);
5204 break;
5205 }
5206 }
5207
5208 #if MSK_OSI_DEBUG_LEVEL > 1
5209 debugMessage("End OsiMskSolverInterface::gutsOfCopy()\n");
5210 #endif
5211 }
5212
5213
5214 //-----------------------------------------------------------------------------
5215 // Empty function
5216
5217 void OsiMskSolverInterface::gutsOfConstructor()
5218 {
5219 #if MSK_OSI_DEBUG_LEVEL > 3
5220 debugMessage("Begin OsiMskSolverInterface::gutsOfConstructor()\n");
5221 #endif
5222
5223 #if MSK_OSI_DEBUG_LEVEL > 3
5224 debugMessage("End OsiMskSolverInterface::gutsOfConstructor()\n");
5225 #endif
5226 }
5227
5228
5229 //-----------------------------------------------------------------------------
5230 // Function called from destructor
5231
5232 void OsiMskSolverInterface::gutsOfDestructor()
5233 {
5234 #if MSK_OSI_DEBUG_LEVEL > 3
5235 debugMessage("Begin OsiMskSolverInterface::gutsOfDestructor()\n");
5236 #endif
5237
5238 freeCachedData(KEEPCACHED_NONE);
5239
5240 if ( task_ != NULL )
5241 {
5242 MSK_unlinkfuncfromtaskstream(getMutableLpPtr(), MSK_STREAM_LOG);
5243 MSK_unlinkfuncfromtaskstream(getMutableLpPtr(), MSK_STREAM_ERR);
5244 MSK_unlinkfuncfromtaskstream(getMutableLpPtr(), MSK_STREAM_WRN);
5245
5246 int err = MSK_deletetask(&task_);
5247 checkMSKerror( err, "MSK_deletetask", "gutsOfDestructor" );
5248 task_ = NULL;
5249 freeAllMemory();
5250 }
5251
5252 MSKassert(3,task_==NULL,"task_==NULL","gutsOfDestructor");
5253 MSKassert(3,obj_==NULL,"obj_==NULL","gutsOfDestructor");
5254 MSKassert(3,collower_==NULL,"collower_==NULL","gutsOfDestructor");
5255 MSKassert(3,colupper_==NULL,"colupper_==NULL","gutsOfDestructor");
5256 MSKassert(3,rowsense_==NULL,"rowsense_==NULL","gutsOfDestructor");
5257 MSKassert(3,rhs_==NULL,"rhs_==NULL","gutsOfDestructor");
5258 MSKassert(3,rowrange_==NULL,"rowrange_==NULL","gutsOfDestructor");
5259 MSKassert(3,rowlower_==NULL,"rowlower_==NULL","gutsOfDestructor");
5260 MSKassert(3,rowupper_==NULL,"rowupper_==NULL","gutsOfDestructor");
5261 MSKassert(3,colsol_==NULL,"colsol_==NULL","gutsOfDestructor");
5262 MSKassert(3,rowsol_==NULL,"rowsol_==NULL","gutsOfDestructor");
5263 MSKassert(3,redcost_==NULL,"redcost_==NULL","gutsOfDestructor");
5264 MSKassert(3,rowact_==NULL,"rowact_==NULL","gutsOfDestructor");
5265 MSKassert(3,matrixByRow_==NULL,"probtypemip_","gutsOfDestructor");
5266 MSKassert(3,matrixByCol_==NULL,"matrixByCol_==NULL","gutsOfDestructor");
5267 MSKassert(3,coltype_==NULL,"coltype_==NULL","gutsOfDestructor");
5268 MSKassert(3,coltypesize_==0,"coltypesize_==0","gutsOfDestructor");
5269
5270 #if MSK_OSI_DEBUG_LEVEL > 3
5271 debugMessage("End OsiMskSolverInterface::gutsOfDestructor()\n");
5272 #endif
5273 }
5274
5275
5276 //-----------------------------------------------------------------------------
5277 // Free function
5278
5279 void OsiMskSolverInterface::freeCachedColRim()
5280 {
5281 #if MSK_OSI_DEBUG_LEVEL > 3
5282 debugMessage("Begin OsiMskSolverInterface::freeCachedColRim()\n");
5283 #endif
5284
5285 freeCacheDouble( obj_ );
5286 freeCacheDouble( collower_ );
5287 freeCacheDouble( colupper_ );
5288
5289 MSKassert(3,obj_==NULL,"obj_==NULL","freeCachedColRim");
5290 MSKassert(3,collower_==NULL,"collower_==NULL","freeCachedColRim");
5291 MSKassert(3,colupper_==NULL,"colupper_==NULL","freeCachedColRim");
5292
5293 #if MSK_OSI_DEBUG_LEVEL > 3
5294 debugMessage("End OsiMskSolverInterface::freeCachedColRim()\n");
5295 #endif
5296 }
5297
5298 //-----------------------------------------------------------------------------
5299 // Free function
5300
5301 void OsiMskSolverInterface::freeCachedRowRim()
5302 {
5303 #if MSK_OSI_DEBUG_LEVEL > 3
5304 debugMessage("Begin OsiMskSolverInterface::freeCachedRowRim()\n");
5305 #endif
5306
5307 freeCacheChar( rowsense_ );
5308 freeCacheDouble( rhs_ );
5309 freeCacheDouble( rowrange_ );
5310 freeCacheDouble( rowlower_ );
5311 freeCacheDouble( rowupper_ );
5312
5313 MSKassert(3,rowsense_==NULL,"rowsense_==NULL","freeCachedRowRim");
5314 MSKassert(3,rhs_==NULL,"rhs_==NULL","freeCachedRowRim");
5315 MSKassert(3,rowrange_==NULL,"rowrange_==NULL","freeCachedRowRim");
5316 MSKassert(3,rowlower_==NULL,"rowlower_==NULL","freeCachedRowRim");
5317 MSKassert(3,rowupper_==NULL,"rowupper_==NULL","freeCachedRowRim");
5318
5319 #if MSK_OSI_DEBUG_LEVEL > 3
5320 debugMessage("End OsiMskSolverInterface::freeCachedRowRim()\n");
5321 #endif
5322 }
5323
5324 //-----------------------------------------------------------------------------
5325 // Free function
5326
5327 void OsiMskSolverInterface::freeCachedMatrix()
5328 {
5329 #if MSK_OSI_DEBUG_LEVEL > 3
5330 debugMessage("Begin OsiMskSolverInterface::freeCachedMatrix()\n");
5331 #endif
5332
5333 freeCacheMatrix( matrixByRow_ );
5334 freeCacheMatrix( matrixByCol_ );
5335 MSKassert(3,matrixByRow_==NULL,"matrixByRow_==NULL","freeCachedMatrix");
5336 MSKassert(3,matrixByCol_==NULL,"matrixByCol_==NULL","freeCachedMatrix");
5337
5338 #if MSK_OSI_DEBUG_LEVEL > 3
5339 debugMessage("End OsiMskSolverInterface::freeCachedMatrix()\n");
5340 #endif
5341 }
5342
5343 //-----------------------------------------------------------------------------
5344 // Free function
5345
5346 void OsiMskSolverInterface::freeCachedResults()
5347 {
5348 #if MSK_OSI_DEBUG_LEVEL > 3
5349 debugMessage("Begin OsiMskSolverInterface::freeCachedResults()\n");
5350 #endif
5351
5352 freeCacheDouble( colsol_ );
5353 freeCacheDouble( rowsol_ );
5354 freeCacheDouble( redcost_ );
5355 freeCacheDouble( rowact_ );
5356
5357 MSKassert(3,colsol_==NULL,"colsol_==NULL","freeCachedResults");
5358 MSKassert(3,rowsol_==NULL,"rowsol_==NULL","freeCachedResults");
5359 MSKassert(3,redcost_==NULL,"redcost_==NULL","freeCachedResults");
5360 MSKassert(3,rowact_==NULL,"rowact_==NULL","freeCachedResults");
5361
5362 #if MSK_OSI_DEBUG_LEVEL > 3
5363 debugMessage("End OsiMskSolverInterface::freeCachedResults()\n");
5364 #endif
5365 }
5366
5367 //-----------------------------------------------------------------------------
5368 // Free function
5369
5370 void OsiMskSolverInterface::freeCachedData( int keepCached )
5371 {
5372 #if MSK_OSI_DEBUG_LEVEL > 3
5373 debugMessage("Begin OsiMskSolverInterface::freeCachedResults()\n");
5374 #endif
5375
5376 if( !(keepCached & OsiMskSolverInterface::KEEPCACHED_COLUMN) )
5377 freeCachedColRim();
5378 if( !(keepCached & OsiMskSolverInterface::KEEPCACHED_ROW) )
5379 freeCachedRowRim();
5380 if( !(keepCached & OsiMskSolverInterface::KEEPCACHED_MATRIX) )
5381 freeCachedMatrix();
5382 if( !(keepCached & OsiMskSolverInterface::KEEPCACHED_RESULTS) )
5383 freeCachedResults();
5384
5385 #if MSK_OSI_DEBUG_LEVEL > 3
5386 debugMessage("End OsiMskSolverInterface::freeCachedResults()\n");
5387 #endif
5388 }
5389
5390 //-----------------------------------------------------------------------------
5391 // Free function
5392
5393 void OsiMskSolverInterface::freeAllMemory()
5394 {
5395 #if MSK_OSI_DEBUG_LEVEL > 3
5396 debugMessage("Begin OsiMskSolverInterface::freeCachedResults()\n");
5397 #endif
5398
5399 freeCachedData();
5400
5401 if( hotStartCStat_ != NULL )
5402 delete[] hotStartCStat_;
5403
5404 if( hotStartRStat_ != NULL )
5405 delete[] hotStartRStat_;
5406
5407 hotStartCStat_ = NULL;
5408 hotStartCStatSize_ = 0;
5409 hotStartRStat_ = NULL;
5410 hotStartRStatSize_ = 0;
5411 freeColType();
5412
5413 #if MSK_OSI_DEBUG_LEVEL > 3
5414 debugMessage("End OsiMskSolverInterface::freeCachedResults()\n");
5415 #endif
5416 }