lemon/mip_cplex.cc
author deba
Sat, 14 Oct 2006 15:26:05 +0000
changeset 2242 16523135943d
parent 2219 c263168e0964
child 2253 1645f6cc9667
permissions -rw-r--r--
New random interface
Switching to the new interface
     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 #if CPX_VERSION < 800
    36     CPXchgprobtype( env,  lp, CPXPROB_MIP);
    37 #else
    38     CPXchgprobtype( env,  lp, CPXPROB_MILP);
    39 #endif
    40 
    41   }
    42 
    43   void MipCplex::_colType(int i, MipCplex::ColTypes col_type){
    44 
    45     // Note If a variable is to be changed to binary, a call to CPXchgbds
    46     // should also be made to change the bounds to 0 and 1.
    47 
    48     int indices[1];
    49     indices[0]=i;
    50     char ctype[1];
    51     switch (col_type){
    52       case LEMON_INTEGER:
    53 	ctype[0]=CPX_INTEGER;//'I'
    54 	break;
    55       case REAL:
    56 	ctype[0]=CPX_CONTINUOUS	;//'C'
    57 	break;
    58     default:;
    59         //FIXME problem
    60     }
    61     CPXchgctype (env, lp, 1, indices, ctype);
    62   }
    63   
    64   MipCplex::ColTypes MipCplex::_colType(int i){
    65     
    66     char ctype[1];
    67     status = CPXgetctype (env, lp, ctype, i, i);
    68     switch (ctype[0]){
    69 
    70     case CPX_INTEGER:
    71       return LEMON_INTEGER;
    72     case CPX_CONTINUOUS:
    73       return REAL;
    74     default:
    75       return REAL;//Error!
    76     }
    77 
    78   }
    79   
    80   LpCplex::SolveExitStatus MipCplex::_solve(){
    81 
    82     status = CPXmipopt (env, lp);
    83     if (status==0)
    84       return SOLVED;
    85     else
    86       return UNSOLVED;
    87 
    88   }
    89 
    90 
    91   LpCplex::SolutionStatus MipCplex::_getMipStatus(){
    92 
    93     int stat = CPXgetstat(env, lp);
    94 
    95     //Fortunately, MIP statuses did not change for cplex 8.0
    96     switch (stat)
    97     {
    98       case CPXMIP_OPTIMAL:
    99         return OPTIMAL;
   100 	//This also exists in later issues
   101 	//    case CPXMIP_UNBOUNDED:
   102         //return INFINITE;
   103       case CPXMIP_INFEASIBLE:
   104         return INFEASIBLE;
   105       default:
   106         return UNDEFINED;
   107     }
   108     //Unboundedness not treated well: the following is from cplex 9.0 doc
   109     // About Unboundedness
   110 
   111     // The treatment of models that are unbounded involves a few
   112     // subtleties. Specifically, a declaration of unboundedness means that
   113     // ILOG CPLEX has determined that the model has an unbounded
   114     // ray. Given any feasible solution x with objective z, a multiple of
   115     // the unbounded ray can be added to x to give a feasible solution
   116     // with objective z-1 (or z+1 for maximization models). Thus, if a
   117     // feasible solution exists, then the optimal objective is
   118     // unbounded. Note that ILOG CPLEX has not necessarily concluded that
   119     // a feasible solution exists. Users can call the routine CPXsolninfo
   120     // to determine whether ILOG CPLEX has also concluded that the model
   121     // has a feasible solution.
   122       
   123   }  
   124 
   125   MipCplex::Value MipCplex::_getPrimal(int i){
   126     Value x;
   127     CPXgetmipx(env, lp, &x, i, i);
   128     return x;
   129   }
   130   
   131   MipCplex::Value MipCplex::_getPrimalValue(){
   132     Value objval;
   133     status = CPXgetmipobjval(env, lp, &objval);
   134     return objval;
   135   }
   136 } //END OF NAMESPACE LEMON
   137 
   138 #endif //END OF MIP_CPLEX_CC