1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2007 |
---|
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 |
---|
20 | ///\brief Implementation of the LEMON-GLPK mip solver interface. |
---|
21 | |
---|
22 | #include <lemon/mip_glpk.h> |
---|
23 | |
---|
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 | |
---|
41 | namespace lemon { |
---|
42 | |
---|
43 | MipGlpk::MipGlpk() { |
---|
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 |
---|
47 | } |
---|
48 | |
---|
49 | void MipGlpk::_colType(int i, MipGlpk::ColTypes col_type){ |
---|
50 | switch (col_type){ |
---|
51 | case INT: |
---|
52 | LEMON_glp(set_col_kind)(lp,i,LEMON_GLP(IV)); |
---|
53 | break; |
---|
54 | case REAL: |
---|
55 | LEMON_glp(set_col_kind)(lp,i,LEMON_GLP(CV)); |
---|
56 | break; |
---|
57 | default:; |
---|
58 | //FIXME problem |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | MipGlpk::ColTypes MipGlpk::_colType(int i) const { |
---|
63 | switch (LEMON_glp(get_col_kind)(lp,i)){ |
---|
64 | case LEMON_GLP(IV): |
---|
65 | return INT;//Or binary |
---|
66 | case LEMON_GLP(CV): |
---|
67 | return REAL; |
---|
68 | default: |
---|
69 | return REAL;//Error! |
---|
70 | } |
---|
71 | |
---|
72 | } |
---|
73 | |
---|
74 | LpGlpk::SolveExitStatus MipGlpk::_solve() { |
---|
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)) { |
---|
88 | //Maybe we could try the routine lpx_intopt(lp), a revised |
---|
89 | //version of lpx_integer |
---|
90 | |
---|
91 | result = LEMON_lpx(integer)(lp); |
---|
92 | switch (result){ |
---|
93 | case LEMON_LPX(E_OK): |
---|
94 | solved = true; |
---|
95 | default: |
---|
96 | solved = false; |
---|
97 | } |
---|
98 | } else { |
---|
99 | solved = false; |
---|
100 | } |
---|
101 | #if GLP_MAJOR_VERSION > 4 || (GLP_MAJOR_VERSION == 4 && GLP_MINOR_VERSION > 15) |
---|
102 | if (tmp != -1) { |
---|
103 | int tmpa[2]; |
---|
104 | tmpa[1] = tmp; |
---|
105 | LEMON_lpx(del_cols)(lp, 1, tmpa); |
---|
106 | } |
---|
107 | #endif |
---|
108 | return solved ? SOLVED : UNSOLVED; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | LpGlpk::SolutionStatus MipGlpk::_getMipStatus() const { |
---|
113 | |
---|
114 | if (LEMON_lpx(get_status)(lp)==LEMON_LPX(OPT)){ |
---|
115 | //Meg kell nezni: ha az LP is infinite, akkor ez is, ha az is |
---|
116 | //infeasible, akkor ez is, de ez lehet maskepp is infeasible. |
---|
117 | int stat= LEMON_lpx(mip_status)(lp); |
---|
118 | |
---|
119 | switch (stat) { |
---|
120 | case LEMON_LPX(I_UNDEF)://Undefined (no solve has been run yet) |
---|
121 | return UNDEFINED; |
---|
122 | case LEMON_LPX(I_NOFEAS)://There is no feasible integral solution |
---|
123 | return INFEASIBLE; |
---|
124 | // case LEMON_LPX(UNBND)://Unbounded |
---|
125 | // return INFINITE; |
---|
126 | case LEMON_LPX(I_FEAS)://Feasible |
---|
127 | return FEASIBLE; |
---|
128 | case LEMON_LPX(I_OPT)://Feasible |
---|
129 | return OPTIMAL; |
---|
130 | default: |
---|
131 | return UNDEFINED; //to avoid gcc warning |
---|
132 | //FIXME error |
---|
133 | } |
---|
134 | } |
---|
135 | else |
---|
136 | return UNDEFINED; //Maybe we could refine this: what does the LP |
---|
137 | //relaxation look like |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | MipGlpk::Value MipGlpk::_getPrimal(int i) const { |
---|
142 | return LEMON_glp(mip_col_val)(lp,i); |
---|
143 | } |
---|
144 | |
---|
145 | MipGlpk::Value MipGlpk::_getPrimalValue() const { |
---|
146 | return LEMON_glp(mip_obj_val)(lp); |
---|
147 | } |
---|
148 | } //END OF NAMESPACE LEMON |
---|