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