[2144] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
[2391] | 5 | * Copyright (C) 2003-2007 |
---|
[2144] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | ///\file |
---|
[2218] | 20 | ///\brief Implementation of the LEMON-GLPK mip solver interface. |
---|
[2144] | 21 | |
---|
| 22 | #include <lemon/mip_glpk.h> |
---|
| 23 | |
---|
[2441] | 24 | #if GLP_MAJOR_VERSION > 4 || (GLP_MAJOR_VERSION == 4 && GLP_MINOR_VERSION > 15) |
---|
| 25 | #define LEMON_glp(func) (glp_##func) |
---|
| 26 | #define LEMON_lpx(func) (lpx_##func) |
---|
| 27 | |
---|
| 28 | #define LEMON_GLP(def) (GLP_##def) |
---|
| 29 | #define LEMON_LPX(def) (LPX_##def) |
---|
| 30 | |
---|
| 31 | #else |
---|
| 32 | |
---|
| 33 | #define LEMON_glp(func) (lpx_##func) |
---|
| 34 | #define LEMON_lpx(func) (lpx_##func) |
---|
| 35 | |
---|
| 36 | #define LEMON_GLP(def) (LPX_##def) |
---|
| 37 | #define LEMON_LPX(def) (LPX_##def) |
---|
| 38 | |
---|
| 39 | #endif |
---|
| 40 | |
---|
[2144] | 41 | namespace lemon { |
---|
| 42 | |
---|
| 43 | MipGlpk::MipGlpk() { |
---|
[2441] | 44 | #if !(GLP_MAJOR_VERSION > 4 || (GLP_MAJOR_VERSION == 4 && GLP_MINOR_VERSION > 15)) |
---|
| 45 | LEMON_lpx(set_class)(lp,LEMON_GLP(MIP)); |
---|
| 46 | #endif |
---|
[2144] | 47 | } |
---|
[2148] | 48 | |
---|
[2149] | 49 | void MipGlpk::_colType(int i, MipGlpk::ColTypes col_type){ |
---|
[2148] | 50 | switch (col_type){ |
---|
[2267] | 51 | case INT: |
---|
[2441] | 52 | LEMON_glp(set_col_kind)(lp,i,LEMON_GLP(IV)); |
---|
[2148] | 53 | break; |
---|
| 54 | case REAL: |
---|
[2441] | 55 | LEMON_glp(set_col_kind)(lp,i,LEMON_GLP(CV)); |
---|
[2148] | 56 | break; |
---|
[2149] | 57 | default:; |
---|
[2148] | 58 | //FIXME problem |
---|
[2144] | 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
[2366] | 62 | MipGlpk::ColTypes MipGlpk::_colType(int i) const { |
---|
[2441] | 63 | switch (LEMON_glp(get_col_kind)(lp,i)){ |
---|
| 64 | case LEMON_GLP(IV): |
---|
[2267] | 65 | return INT;//Or binary |
---|
[2441] | 66 | case LEMON_GLP(CV): |
---|
[2148] | 67 | return REAL; |
---|
| 68 | default: |
---|
| 69 | return REAL;//Error! |
---|
[2144] | 70 | } |
---|
[2148] | 71 | |
---|
[2144] | 72 | } |
---|
| 73 | |
---|
[2366] | 74 | LpGlpk::SolveExitStatus MipGlpk::_solve() { |
---|
[2441] | 75 | int result = LEMON_lpx(simplex)(lp); |
---|
| 76 | |
---|
| 77 | // hack: mip does not contain integer variable |
---|
| 78 | #if GLP_MAJOR_VERSION > 4 || (GLP_MAJOR_VERSION == 4 && GLP_MINOR_VERSION > 15) |
---|
| 79 | int tmp = -1; |
---|
| 80 | if (LEMON_glp(get_num_int(lp)) == 0) { |
---|
| 81 | tmp = LEMON_lpx(add_cols)(lp, 1); |
---|
| 82 | LEMON_glp(set_col_bnds)(lp, tmp, LEMON_GLP(FX), 0.0, 0.0); |
---|
| 83 | LEMON_glp(set_col_kind)(lp, tmp, LEMON_GLP(IV)); |
---|
| 84 | } |
---|
| 85 | #endif |
---|
| 86 | |
---|
| 87 | if (LEMON_lpx(get_status)(lp)==LEMON_LPX(OPT)) { |
---|
[2213] | 88 | //Maybe we could try the routine lpx_intopt(lp), a revised |
---|
| 89 | //version of lpx_integer |
---|
[2441] | 90 | |
---|
| 91 | result = LEMON_lpx(integer)(lp); |
---|
[2213] | 92 | switch (result){ |
---|
[2441] | 93 | case LEMON_LPX(E_OK): |
---|
| 94 | solved = true; |
---|
[2442] | 95 | break; |
---|
[2144] | 96 | default: |
---|
[2441] | 97 | solved = false; |
---|
| 98 | } |
---|
| 99 | } else { |
---|
| 100 | solved = false; |
---|
[2144] | 101 | } |
---|
[2441] | 102 | #if GLP_MAJOR_VERSION > 4 || (GLP_MAJOR_VERSION == 4 && GLP_MINOR_VERSION > 15) |
---|
| 103 | if (tmp != -1) { |
---|
| 104 | int tmpa[2]; |
---|
| 105 | tmpa[1] = tmp; |
---|
| 106 | LEMON_lpx(del_cols)(lp, 1, tmpa); |
---|
| 107 | } |
---|
| 108 | #endif |
---|
| 109 | return solved ? SOLVED : UNSOLVED; |
---|
[2144] | 110 | } |
---|
[2185] | 111 | |
---|
| 112 | |
---|
[2366] | 113 | LpGlpk::SolutionStatus MipGlpk::_getMipStatus() const { |
---|
[2185] | 114 | |
---|
[2441] | 115 | if (LEMON_lpx(get_status)(lp)==LEMON_LPX(OPT)){ |
---|
[2213] | 116 | //Meg kell nezni: ha az LP is infinite, akkor ez is, ha az is |
---|
| 117 | //infeasible, akkor ez is, de ez lehet maskepp is infeasible. |
---|
[2441] | 118 | int stat= LEMON_lpx(mip_status)(lp); |
---|
[2213] | 119 | |
---|
| 120 | switch (stat) { |
---|
[2441] | 121 | case LEMON_LPX(I_UNDEF)://Undefined (no solve has been run yet) |
---|
[2213] | 122 | return UNDEFINED; |
---|
[2441] | 123 | case LEMON_LPX(I_NOFEAS)://There is no feasible integral solution |
---|
[2213] | 124 | return INFEASIBLE; |
---|
[2441] | 125 | // case LEMON_LPX(UNBND)://Unbounded |
---|
[2213] | 126 | // return INFINITE; |
---|
[2441] | 127 | case LEMON_LPX(I_FEAS)://Feasible |
---|
[2213] | 128 | return FEASIBLE; |
---|
[2441] | 129 | case LEMON_LPX(I_OPT)://Feasible |
---|
[2213] | 130 | return OPTIMAL; |
---|
| 131 | default: |
---|
[2441] | 132 | return UNDEFINED; //to avoid gcc warning |
---|
[2185] | 133 | //FIXME error |
---|
[2213] | 134 | } |
---|
[2185] | 135 | } |
---|
[2213] | 136 | else |
---|
| 137 | return UNDEFINED; //Maybe we could refine this: what does the LP |
---|
| 138 | //relaxation look like |
---|
| 139 | |
---|
[2185] | 140 | } |
---|
| 141 | |
---|
[2366] | 142 | MipGlpk::Value MipGlpk::_getPrimal(int i) const { |
---|
[2441] | 143 | return LEMON_glp(mip_col_val)(lp,i); |
---|
[2144] | 144 | } |
---|
| 145 | |
---|
[2366] | 146 | MipGlpk::Value MipGlpk::_getPrimalValue() const { |
---|
[2441] | 147 | return LEMON_glp(mip_obj_val)(lp); |
---|
[2144] | 148 | } |
---|
[2218] | 149 | } //END OF NAMESPACE LEMON |
---|