lemon/mip_glpk.cc
author athos
Mon, 22 Jan 2007 12:13:57 +0000
changeset 2349 c945f577a66d
parent 2253 1645f6cc9667
child 2366 bfbdded3763a
permissions -rw-r--r--
Small bug corrected.
athos@2144
     1
/* -*- C++ -*-
athos@2144
     2
 *
athos@2144
     3
 * This file is a part of LEMON, a generic C++ optimization library
athos@2144
     4
 *
athos@2144
     5
 * Copyright (C) 2003-2006
athos@2144
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
athos@2144
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
athos@2144
     8
 *
athos@2144
     9
 * Permission to use, modify and distribute this software is granted
athos@2144
    10
 * provided that this copyright notice appears in all copies. For
athos@2144
    11
 * precise terms see the accompanying LICENSE file.
athos@2144
    12
 *
athos@2144
    13
 * This software is provided "AS IS" with no warranty of any kind,
athos@2144
    14
 * express or implied, and with no claim as to its suitability for any
athos@2144
    15
 * purpose.
athos@2144
    16
 *
athos@2144
    17
 */
athos@2144
    18
athos@2144
    19
///\file
athos@2218
    20
///\brief Implementation of the LEMON-GLPK mip solver interface.
athos@2144
    21
athos@2144
    22
#include <lemon/mip_glpk.h>
athos@2144
    23
athos@2144
    24
namespace lemon {
athos@2144
    25
  
athos@2144
    26
  MipGlpk::MipGlpk() {
athos@2144
    27
    lpx_set_class(lp,LPX_MIP);
athos@2144
    28
  }
athos@2148
    29
athos@2149
    30
  void MipGlpk::_colType(int i, MipGlpk::ColTypes col_type){
athos@2148
    31
    switch (col_type){
athos@2267
    32
      case INT:
athos@2148
    33
	lpx_set_col_kind(lp,i,LPX_IV);
athos@2148
    34
	break;
athos@2148
    35
      case REAL:
athos@2148
    36
	lpx_set_col_kind(lp,i,LPX_CV);
athos@2148
    37
	break;
athos@2149
    38
    default:;
athos@2148
    39
        //FIXME problem
athos@2144
    40
    }
athos@2144
    41
  }
athos@2144
    42
  
athos@2149
    43
  MipGlpk::ColTypes MipGlpk::_colType(int i){
athos@2148
    44
    switch (lpx_get_col_kind(lp,i)){
athos@2148
    45
    case LPX_IV:
athos@2267
    46
      return INT;//Or binary
athos@2148
    47
    case LPX_CV:
athos@2148
    48
      return REAL;
athos@2148
    49
    default:
athos@2148
    50
      return REAL;//Error!
athos@2144
    51
    }
athos@2148
    52
    
athos@2144
    53
  }
athos@2144
    54
  
athos@2144
    55
  LpGlpk::SolveExitStatus MipGlpk::_solve(){
athos@2144
    56
    int result = lpx_simplex(lp);
athos@2213
    57
    //
athos@2213
    58
    if (lpx_get_status(lp)==LPX_OPT){
athos@2213
    59
      //Maybe we could try the routine lpx_intopt(lp), a revised
athos@2213
    60
      //version of lpx_integer
athos@2213
    61
      result = lpx_integer(lp);
athos@2213
    62
      switch (result){
athos@2144
    63
      case LPX_E_OK:
athos@2213
    64
	return SOLVED;
athos@2144
    65
      default:
athos@2213
    66
	return UNSOLVED;
athos@2213
    67
      }
athos@2213
    68
      
athos@2144
    69
    }
athos@2213
    70
    return UNSOLVED;
athos@2144
    71
  }
athos@2185
    72
athos@2185
    73
athos@2185
    74
  LpGlpk::SolutionStatus MipGlpk::_getMipStatus(){
athos@2185
    75
athos@2213
    76
    if (lpx_get_status(lp)==LPX_OPT){
athos@2213
    77
      //Meg kell nezni: ha az LP is infinite, akkor ez is, ha az is
athos@2213
    78
      //infeasible, akkor ez is, de ez lehet maskepp is infeasible.
athos@2213
    79
      int stat=  lpx_mip_status(lp);
athos@2213
    80
      
athos@2213
    81
      switch (stat) {
athos@2213
    82
      case LPX_I_UNDEF://Undefined (no solve has been run yet)
athos@2213
    83
	return UNDEFINED;
athos@2213
    84
      case LPX_I_NOFEAS://There is no feasible integral solution
athos@2213
    85
	return INFEASIBLE;
athos@2213
    86
	//     case LPX_UNBND://Unbounded
athos@2213
    87
	//       return INFINITE;
athos@2213
    88
      case LPX_I_FEAS://Feasible
athos@2213
    89
	return FEASIBLE;
athos@2213
    90
      case LPX_I_OPT://Feasible
athos@2213
    91
	return OPTIMAL;
athos@2213
    92
      default:
athos@2185
    93
      return UNDEFINED; //to avoid gcc warning
athos@2185
    94
      //FIXME error
athos@2213
    95
      }
athos@2185
    96
    }
athos@2213
    97
    else 
athos@2213
    98
      return UNDEFINED; //Maybe we could refine this: what does the LP
athos@2213
    99
			//relaxation look like
athos@2213
   100
      
athos@2185
   101
  }  
athos@2185
   102
athos@2144
   103
  MipGlpk::Value MipGlpk::_getPrimal(int i){
athos@2144
   104
    return lpx_mip_col_val(lp,i);
athos@2144
   105
  }
athos@2144
   106
  
athos@2144
   107
  MipGlpk::Value MipGlpk::_getPrimalValue(){
athos@2144
   108
    return lpx_mip_obj_val(lp);
athos@2144
   109
  }
athos@2218
   110
} //END OF NAMESPACE LEMON