lemon/mip_cplex.cc
changeset 458 7afc121e0689
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/mip_cplex.cc	Tue Dec 02 21:40:33 2008 +0100
     1.3 @@ -0,0 +1,141 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +///\file
    1.23 +///\brief Implementation of the LEMON-CPLEX mip solver interface.
    1.24 +
    1.25 +#include <lemon/mip_cplex.h>
    1.26 +
    1.27 +extern "C" {
    1.28 +#include <ilcplex/cplex.h>
    1.29 +}
    1.30 +
    1.31 +namespace lemon {
    1.32 +
    1.33 +  MipCplex::MipCplex() {
    1.34 +    //This is unnecessary: setting integrality constraints on
    1.35 +    //variables will set this, too
    1.36 +
    1.37 +    ///\todo The constant CPXPROB_MIP is
    1.38 +    ///called CPXPROB_MILP in later versions
    1.39 +#if CPX_VERSION < 800
    1.40 +    CPXchgprobtype( env,  lp, CPXPROB_MIP);
    1.41 +#else
    1.42 +    CPXchgprobtype( env,  lp, CPXPROB_MILP);
    1.43 +#endif
    1.44 +
    1.45 +  }
    1.46 +
    1.47 +  void MipCplex::_colType(int i, MipCplex::ColTypes col_type){
    1.48 +
    1.49 +    // Note If a variable is to be changed to binary, a call to CPXchgbds
    1.50 +    // should also be made to change the bounds to 0 and 1.
    1.51 +
    1.52 +    int indices[1];
    1.53 +    indices[0]=i;
    1.54 +    char ctype[1];
    1.55 +    switch (col_type){
    1.56 +      case INT:
    1.57 +        ctype[0]=CPX_INTEGER;//'I'
    1.58 +        break;
    1.59 +      case REAL:
    1.60 +        ctype[0]=CPX_CONTINUOUS        ;//'C'
    1.61 +        break;
    1.62 +    default:;
    1.63 +        //FIXME problem
    1.64 +    }
    1.65 +    CPXchgctype (env, lp, 1, indices, ctype);
    1.66 +  }
    1.67 +
    1.68 +  MipCplex::ColTypes MipCplex::_colType(int i) const {
    1.69 +
    1.70 +    char ctype[1];
    1.71 +    CPXgetctype (env, lp, ctype, i, i);
    1.72 +    switch (ctype[0]){
    1.73 +
    1.74 +    case CPX_INTEGER:
    1.75 +      return INT;
    1.76 +    case CPX_CONTINUOUS:
    1.77 +      return REAL;
    1.78 +    default:
    1.79 +      return REAL;//Error!
    1.80 +    }
    1.81 +
    1.82 +  }
    1.83 +
    1.84 +  LpCplex::SolveExitStatus MipCplex::_solve(){
    1.85 +
    1.86 +    status = CPXmipopt (env, lp);
    1.87 +    if (status==0)
    1.88 +      return SOLVED;
    1.89 +    else
    1.90 +      return UNSOLVED;
    1.91 +
    1.92 +  }
    1.93 +
    1.94 +
    1.95 +  LpCplex::SolutionStatus MipCplex::_getMipStatus() const {
    1.96 +
    1.97 +    int stat = CPXgetstat(env, lp);
    1.98 +
    1.99 +    //Fortunately, MIP statuses did not change for cplex 8.0
   1.100 +    switch (stat)
   1.101 +    {
   1.102 +      case CPXMIP_OPTIMAL:
   1.103 +        // Optimal integer solution has been found.
   1.104 +      case CPXMIP_OPTIMAL_TOL:
   1.105 +        // Optimal soluton with the tolerance defined by epgap or epagap has
   1.106 +        // been found.
   1.107 +        return OPTIMAL;
   1.108 +        //This also exists in later issues
   1.109 +        //    case CPXMIP_UNBOUNDED:
   1.110 +        //return INFINITE;
   1.111 +      case CPXMIP_INFEASIBLE:
   1.112 +        return INFEASIBLE;
   1.113 +      default:
   1.114 +        return UNDEFINED;
   1.115 +    }
   1.116 +    //Unboundedness not treated well: the following is from cplex 9.0 doc
   1.117 +    // About Unboundedness
   1.118 +
   1.119 +    // The treatment of models that are unbounded involves a few
   1.120 +    // subtleties. Specifically, a declaration of unboundedness means that
   1.121 +    // ILOG CPLEX has determined that the model has an unbounded
   1.122 +    // ray. Given any feasible solution x with objective z, a multiple of
   1.123 +    // the unbounded ray can be added to x to give a feasible solution
   1.124 +    // with objective z-1 (or z+1 for maximization models). Thus, if a
   1.125 +    // feasible solution exists, then the optimal objective is
   1.126 +    // unbounded. Note that ILOG CPLEX has not necessarily concluded that
   1.127 +    // a feasible solution exists. Users can call the routine CPXsolninfo
   1.128 +    // to determine whether ILOG CPLEX has also concluded that the model
   1.129 +    // has a feasible solution.
   1.130 +
   1.131 +  }
   1.132 +
   1.133 +  MipCplex::Value MipCplex::_getPrimal(int i) const {
   1.134 +    Value x;
   1.135 +    CPXgetmipx(env, lp, &x, i, i);
   1.136 +    return x;
   1.137 +  }
   1.138 +
   1.139 +  MipCplex::Value MipCplex::_getPrimalValue() const {
   1.140 +    Value objval;
   1.141 +    CPXgetmipobjval(env, lp, &objval);
   1.142 +    return objval;
   1.143 +  }
   1.144 +} //END OF NAMESPACE LEMON