lemon/mip_cplex.cc
author deba
Thu, 20 Mar 2008 16:25:47 +0000
changeset 2596 9c00e972cdfd
parent 2465 df09310da558
permissions -rw-r--r--
Back porting commit 81563e019fa4
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     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 ///\file
    20 ///\brief Implementation of the LEMON-CPLEX mip solver interface.
    21 
    22 #include <lemon/mip_cplex.h>
    23 
    24 namespace lemon {
    25   
    26   MipCplex::MipCplex() {
    27     //This is unnecessary: setting integrality constraints on
    28     //variables will set this, too 
    29 
    30     ///\todo The constant CPXPROB_MIP is
    31     ///called CPXPROB_MILP in later versions
    32 #if CPX_VERSION < 800
    33     CPXchgprobtype( env,  lp, CPXPROB_MIP);
    34 #else
    35     CPXchgprobtype( env,  lp, CPXPROB_MILP);
    36 #endif
    37 
    38   }
    39 
    40   void MipCplex::_colType(int i, MipCplex::ColTypes col_type){
    41 
    42     // Note If a variable is to be changed to binary, a call to CPXchgbds
    43     // should also be made to change the bounds to 0 and 1.
    44 
    45     int indices[1];
    46     indices[0]=i;
    47     char ctype[1];
    48     switch (col_type){
    49       case INT:
    50 	ctype[0]=CPX_INTEGER;//'I'
    51 	break;
    52       case REAL:
    53 	ctype[0]=CPX_CONTINUOUS	;//'C'
    54 	break;
    55     default:;
    56         //FIXME problem
    57     }
    58     CPXchgctype (env, lp, 1, indices, ctype);
    59   }
    60   
    61   MipCplex::ColTypes MipCplex::_colType(int i) const {
    62     
    63     char ctype[1];
    64     CPXgetctype (env, lp, ctype, i, i);
    65     switch (ctype[0]){
    66 
    67     case CPX_INTEGER:
    68       return INT;
    69     case CPX_CONTINUOUS:
    70       return REAL;
    71     default:
    72       return REAL;//Error!
    73     }
    74 
    75   }
    76   
    77   LpCplex::SolveExitStatus MipCplex::_solve(){
    78 
    79     status = CPXmipopt (env, lp);
    80     if (status==0)
    81       return SOLVED;
    82     else
    83       return UNSOLVED;
    84 
    85   }
    86 
    87 
    88   LpCplex::SolutionStatus MipCplex::_getMipStatus() const {
    89 
    90     int stat = CPXgetstat(env, lp);
    91 
    92     //Fortunately, MIP statuses did not change for cplex 8.0
    93     switch (stat)
    94     {
    95       case CPXMIP_OPTIMAL:
    96         // Optimal integer solution has been found.
    97       case CPXMIP_OPTIMAL_TOL:
    98         // Optimal soluton with the tolerance defined by epgap or epagap has 
    99         // been found.
   100         return OPTIMAL;
   101 	//This also exists in later issues
   102 	//    case CPXMIP_UNBOUNDED:
   103         //return INFINITE;
   104       case CPXMIP_INFEASIBLE:
   105         return INFEASIBLE;
   106       default:
   107         return UNDEFINED;
   108     }
   109     //Unboundedness not treated well: the following is from cplex 9.0 doc
   110     // About Unboundedness
   111 
   112     // The treatment of models that are unbounded involves a few
   113     // subtleties. Specifically, a declaration of unboundedness means that
   114     // ILOG CPLEX has determined that the model has an unbounded
   115     // ray. Given any feasible solution x with objective z, a multiple of
   116     // the unbounded ray can be added to x to give a feasible solution
   117     // with objective z-1 (or z+1 for maximization models). Thus, if a
   118     // feasible solution exists, then the optimal objective is
   119     // unbounded. Note that ILOG CPLEX has not necessarily concluded that
   120     // a feasible solution exists. Users can call the routine CPXsolninfo
   121     // to determine whether ILOG CPLEX has also concluded that the model
   122     // has a feasible solution.
   123       
   124   }  
   125 
   126   MipCplex::Value MipCplex::_getPrimal(int i) const {
   127     Value x;
   128     CPXgetmipx(env, lp, &x, i, i);
   129     return x;
   130   }
   131   
   132   MipCplex::Value MipCplex::_getPrimalValue() const {
   133     Value objval;
   134     CPXgetmipobjval(env, lp, &objval);
   135     return objval;
   136   }
   137 } //END OF NAMESPACE LEMON