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