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