lemon/mip_cplex.cc
changeset 2224 f973894da54e
child 2226 0411ac8a2d87
equal deleted inserted replaced
-1:000000000000 0:003355fec661
       
     1 /* -*- C++ -*-
       
     2  *
       
     3  * This file is a part of LEMON, a generic C++ optimization library
       
     4  *
       
     5  * Copyright (C) 2003-2006
       
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
       
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
       
     8  *
       
     9  * Permission to use, modify and distribute this software is granted
       
    10  * provided that this copyright notice appears in all copies. For
       
    11  * precise terms see the accompanying LICENSE file.
       
    12  *
       
    13  * This software is provided "AS IS" with no warranty of any kind,
       
    14  * express or implied, and with no claim as to its suitability for any
       
    15  * purpose.
       
    16  *
       
    17  */
       
    18 
       
    19 #ifndef LEMON_MIP_CPLEX_CC
       
    20 #define LEMON_MIP_CPLEX_CC
       
    21 
       
    22 ///\file
       
    23 ///\brief Implementation of the LEMON-CPLEX mip solver interface.
       
    24 
       
    25 #include <lemon/mip_cplex.h>
       
    26 
       
    27 namespace lemon {
       
    28   
       
    29   MipCplex::MipCplex() {
       
    30     //This is unnecessary: setting integrality constraints on
       
    31     //variables will set this, too 
       
    32 
       
    33     ///\todo The constant CPXPROB_MIP is
       
    34     ///called CPXPROB_MILP in later versions
       
    35     CPXchgprobtype( env,  lp, CPXPROB_MIP);
       
    36   }
       
    37 
       
    38   void MipCplex::_colType(int i, MipCplex::ColTypes col_type){
       
    39 
       
    40     // Note If a variable is to be changed to binary, a call to CPXchgbds
       
    41     // should also be made to change the bounds to 0 and 1.
       
    42 
       
    43     int indices[1];
       
    44     indices[0]=i;
       
    45     char ctype[1];
       
    46     switch (col_type){
       
    47       case LEMON_INTEGER:
       
    48 	ctype[0]=CPX_INTEGER;//'I'
       
    49 	break;
       
    50       case REAL:
       
    51 	ctype[0]=CPX_CONTINUOUS	;//'C'
       
    52 	break;
       
    53     default:;
       
    54         //FIXME problem
       
    55     }
       
    56     CPXchgctype (env, lp, 1, indices, ctype);
       
    57   }
       
    58   
       
    59   MipCplex::ColTypes MipCplex::_colType(int i){
       
    60     
       
    61     char ctype[1];
       
    62     status = CPXgetctype (env, lp, ctype, i, i);
       
    63     switch (ctype[0]){
       
    64 
       
    65     case CPX_INTEGER:
       
    66       return LEMON_INTEGER;
       
    67     case CPX_CONTINUOUS:
       
    68       return REAL;
       
    69     default:
       
    70       return REAL;//Error!
       
    71     }
       
    72 
       
    73   }
       
    74   
       
    75   LpCplex::SolveExitStatus MipCplex::_solve(){
       
    76 
       
    77     status = CPXmipopt (env, lp);
       
    78     if (status==0)
       
    79       return SOLVED;
       
    80     else
       
    81       return UNSOLVED;
       
    82 
       
    83   }
       
    84 
       
    85 
       
    86   LpCplex::SolutionStatus MipCplex::_getMipStatus(){
       
    87 
       
    88     int stat = CPXgetstat(env, lp);
       
    89 
       
    90     //Fortunately, MIP statuses did not change for cplex 8.0
       
    91     switch (stat)
       
    92     {
       
    93       case CPXMIP_OPTIMAL:
       
    94         return OPTIMAL;
       
    95 	//This also exists in later issues
       
    96 	//    case CPXMIP_UNBOUNDED:
       
    97         //return INFINITE;
       
    98       case CPXMIP_INFEASIBLE:
       
    99         return INFEASIBLE;
       
   100       default:
       
   101         return UNDEFINED;
       
   102     }
       
   103     //Unboundedness not treated well: the following is from cplex 9.0 doc
       
   104     // About Unboundedness
       
   105 
       
   106     // The treatment of models that are unbounded involves a few
       
   107     // subtleties. Specifically, a declaration of unboundedness means that
       
   108     // ILOG CPLEX has determined that the model has an unbounded
       
   109     // ray. Given any feasible solution x with objective z, a multiple of
       
   110     // the unbounded ray can be added to x to give a feasible solution
       
   111     // with objective z-1 (or z+1 for maximization models). Thus, if a
       
   112     // feasible solution exists, then the optimal objective is
       
   113     // unbounded. Note that ILOG CPLEX has not necessarily concluded that
       
   114     // a feasible solution exists. Users can call the routine CPXsolninfo
       
   115     // to determine whether ILOG CPLEX has also concluded that the model
       
   116     // has a feasible solution.
       
   117       
       
   118   }  
       
   119 
       
   120   MipCplex::Value MipCplex::_getPrimal(int i){
       
   121     Value x;
       
   122     CPXgetmipx(env, lp, &x, i, i);
       
   123     return x;
       
   124   }
       
   125   
       
   126   MipCplex::Value MipCplex::_getPrimalValue(){
       
   127     Value objval;
       
   128     status = CPXgetmipobjval(env, lp, &objval);
       
   129     return objval;
       
   130   }
       
   131 } //END OF NAMESPACE LEMON
       
   132 
       
   133 #endif //END OF MIP_CPLEX_CC