1 | function output = callglpk(interfacedata) |
---|
2 | |
---|
3 | % Author Johan Löfberg |
---|
4 | % $Id: callglpk.m,v 1.18 2005/09/20 11:27:32 joloef Exp $ |
---|
5 | |
---|
6 | % Retrieve needed data |
---|
7 | options = interfacedata.options; |
---|
8 | F_struc = interfacedata.F_struc; |
---|
9 | c = interfacedata.c; |
---|
10 | K = interfacedata.K; |
---|
11 | integer_variables = interfacedata.integer_variables; |
---|
12 | binary_variables = interfacedata.binary_variables; |
---|
13 | ub = interfacedata.ub; |
---|
14 | lb = interfacedata.lb; |
---|
15 | interfacedata.gettime = 0; |
---|
16 | n = length(c); |
---|
17 | |
---|
18 | if ~isempty(ub) |
---|
19 | LB = lb; |
---|
20 | UB = ub; |
---|
21 | LB(binary_variables) = round(LB(binary_variables)); |
---|
22 | UB(binary_variables) = round(UB(binary_variables)); |
---|
23 | LB(integer_variables) = round(LB(integer_variables)); |
---|
24 | UB(integer_variables) = round(UB(integer_variables)); |
---|
25 | |
---|
26 | if all(isinf(LB)) |
---|
27 | LB=repmat(-1e6,n,1); % just for sure |
---|
28 | end |
---|
29 | if all(isinf(UB)) |
---|
30 | UB=repmat(1e6,n,1); % just for sure |
---|
31 | end |
---|
32 | else |
---|
33 | LB=repmat(-1e6,n,1); % just for sure |
---|
34 | UB=repmat(1e6,n,1); |
---|
35 | end |
---|
36 | |
---|
37 | % GLPK does not like lb==ub |
---|
38 | equality_in_bound = find((abs(LB-UB)<1e-12) & ~isinf(LB)); |
---|
39 | m = length(equality_in_bound); |
---|
40 | if ~isempty(equality_in_bound) |
---|
41 | F_struc = [-LB(equality_in_bound) sparse(1:m,equality_in_bound,ones(m,1),m,n);F_struc]; |
---|
42 | UB(equality_in_bound) = UB(equality_in_bound) + 1; |
---|
43 | K.f = K.f + m; |
---|
44 | end |
---|
45 | |
---|
46 | if options.showprogress;showprogress('Calling GLPK',options.showprogress);end |
---|
47 | |
---|
48 | % GLPK notation for sure... |
---|
49 | SENSE = 1; % Minimize |
---|
50 | C = full(c); % Must be full |
---|
51 | B = full(F_struc(:,1)); % Must be full |
---|
52 | A =-F_struc(:,2:end); |
---|
53 | if length(B)==0; |
---|
54 | A = C'; |
---|
55 | B = 1e6; |
---|
56 | end |
---|
57 | % Optimized code, make a lot of difference when you make this call 10000 |
---|
58 | % times in a branch and bound setting... |
---|
59 | CTYPE = [char(ones(K.f,1)*83); char(ones(K.l,1)*85)]; |
---|
60 | VARTYPE = char(ones(n,1)*67); |
---|
61 | VARTYPE(integer_variables) = 'I'; |
---|
62 | VARTYPE(binary_variables) = 'I'; % Should not happen except from bmibnb |
---|
63 | |
---|
64 | if options.savedebug |
---|
65 | save glpkmexdebug |
---|
66 | end |
---|
67 | options.glpk.msglev = options.verbose; |
---|
68 | if options.glpk.msglev==1 |
---|
69 | options.glpk.msglev = 2; |
---|
70 | end |
---|
71 | |
---|
72 | % Call mex-interface |
---|
73 | solvertime = clock; |
---|
74 | [x,FMIN,STATUS,LAMBDA_EXTRA] = glpkmex(SENSE,C,A,B,CTYPE,LB,UB,VARTYPE,options.glpk,options.glpk.lpsolver,options.glpk.save); |
---|
75 | if interfacedata.getsolvertime solvertime = etime(clock,solvertime);else solvertime = 0;end |
---|
76 | problem = 0; |
---|
77 | |
---|
78 | if options.saveduals |
---|
79 | if isstruct(LAMBDA_EXTRA) |
---|
80 | LAMBDA = LAMBDA_EXTRA.lambda; |
---|
81 | EXTRA = LAMBDA_EXTRA; |
---|
82 | else |
---|
83 | LAMBDA = LAMBDA_EXTRA; |
---|
84 | EXTRA = 'Not saved in old GLPKMEX version, update...'; |
---|
85 | end |
---|
86 | D_struc = -LAMBDA(m+1:end,:); |
---|
87 | else |
---|
88 | D_struc = []; |
---|
89 | end |
---|
90 | |
---|
91 | % Hack |
---|
92 | if isempty([ub;lb]) & (any(LB==x & C>0) | any(UB==x & C<0)) |
---|
93 | STATUS = 214; |
---|
94 | end |
---|
95 | |
---|
96 | % Check, currently not exhaustive... |
---|
97 | switch STATUS |
---|
98 | case {171,180,151,200} |
---|
99 | problem = 0; |
---|
100 | case {182,173,183,0,213,204,209} |
---|
101 | problem = 1; |
---|
102 | case 214 |
---|
103 | problem = 2; |
---|
104 | case 207 |
---|
105 | problem = 3; |
---|
106 | case {210,211,212} |
---|
107 | problem = 4; |
---|
108 | case 170 |
---|
109 | problem = 11; |
---|
110 | otherwise |
---|
111 | problem = -1; |
---|
112 | end |
---|
113 | |
---|
114 | % Save all data sent to solver? |
---|
115 | if options.savesolverinput |
---|
116 | solverinput.A = A; |
---|
117 | solverinput.C = C; |
---|
118 | solverinput.B = B; |
---|
119 | solverinput.CTYPE = CTYPE; |
---|
120 | solverinput.LB = LB; |
---|
121 | solverinput.UB = UB; |
---|
122 | solverinput.param = options.glpk; |
---|
123 | solverinput.lpsolver = options.glpk.lpsolver; |
---|
124 | else |
---|
125 | solverinput = []; |
---|
126 | end |
---|
127 | |
---|
128 | % Save all data from the solver? |
---|
129 | if options.savesolveroutput |
---|
130 | |
---|
131 | % Stupid redo, but this saves some time in bnb calls |
---|
132 | if isstruct(LAMBDA_EXTRA) |
---|
133 | LAMBDA = LAMBDA_EXTRA.lambda; |
---|
134 | EXTRA = LAMBDA_EXTRA; |
---|
135 | else |
---|
136 | LAMBDA = LAMBDA_EXTRA; |
---|
137 | EXTRA = 'Not saved in old GLPKMEX version, update...'; |
---|
138 | end |
---|
139 | |
---|
140 | solveroutput.x = x; |
---|
141 | solveroutput.FMIN = FMIN; |
---|
142 | solveroutput.STATUS = STATUS; |
---|
143 | solveroutput.LAMBDA=LAMBDA; |
---|
144 | solveroutput.EXTRA = EXTRA; |
---|
145 | else |
---|
146 | solveroutput = []; |
---|
147 | end |
---|
148 | |
---|
149 | % Standard interface |
---|
150 | output.Primal = x; |
---|
151 | output.Dual = D_struc; |
---|
152 | output.Slack = []; |
---|
153 | output.problem = problem; |
---|
154 | output.solverinput = solverinput; |
---|
155 | output.solveroutput= solveroutput; |
---|
156 | output.solvertime = solvertime; |
---|