Debian Codegraph @

external/glpk/glpini02.c

raw symbols SCIP
1 /* glpini02.c */cxx . . $ `<file>/external/glpk/glpini02.c`/
2
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
5 *
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <[email protected]>.
10 *
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
24
25 #include "glpapi.h"cxx . . $ `<file>/external/glpk/glpapi.h`/
26
27 struct varcxx . . $ var#
28 { /* structural variable */
29 int jcxx . . $ var#j.;
30 /* ordinal number */
31 double qcxx . . $ var#q.;
32 /* penalty value */
33 };
34
35 static int fcmpcxx . . $ fcmp(9148c8c3ff42c9d8).(const void *ptr1, const void *ptr2)
36 { /* this routine is passed to the qsort() function */
37 struct varcxx . . $ var# *col1 = (void *)ptr1, *col2 = (void *)ptr2;
38 if (col1->qcxx . . $ var#q. < col2->qcxx . . $ var#q.) return -1;
39 if (col1->qcxx . . $ var#q. > col2->qcxx . . $ var#q.) return +1;
40 return 0;
41 }
42
43 static int get_columncxx . . $ get_column(223ff0a4a32a5807).(glp_probcxx . . $ glp_prob# *lp, int j, int ind[], double val[])
44 { /* Bixby's algorithm assumes that the constraint matrix is scaled
45 such that the maximum absolute value in every non-zero row and
46 column is 1 */
47 int k, len;
48 double big;
49 len = glp_get_mat_colcxx . . $ glp_get_mat_col(223ff0a4a32a5807).(lp, j, ind, val);
50 big = 0.0;
51 for (k = 1; k <= len; k++)
52 if (big < fabs(val[k])) big = fabs(val[k]);
53 if (big == 0.0) big = 1.0;
54 for (k = 1; k <= len; k++) val[k] /= big;
55 return len;
56 }
57
58 static void cpx_basiscxx . . $ cpx_basis(84c60d68cac39b9a).(glp_probcxx . . $ glp_prob# *lp)
59 { /* main routine */
60 struct varcxx . . $ var# *C, *C2, *C3, *C4;
61 int m, n, i, j, jk, k, l, ll, t, n2, n3, n4, type, len, *I, *r,
62 *ind;
63 double alpha, gamma, cmax, temp, *v, *val;
64 xprintfcxx . . $ `external/glpk/glpenv.h:145:9`!("Constructing initial basis...\n");
65 /* determine the number of rows and columns */
66 m = glp_get_num_rowscxx . . $ glp_get_num_rows(317ecc1aeadfe2b7).(lp);
67 n = glp_get_num_colscxx . . $ glp_get_num_cols(317ecc1aeadfe2b7).(lp);
68 /* allocate working arrays */
69 C = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(struct varcxx . . $ var#));
70 I = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+m, sizeof(int));
71 r = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+m, sizeof(int));
72 v = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+m, sizeof(double));
73 ind = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+m, sizeof(int));
74 val = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+m, sizeof(double));
75 /* make all auxiliary variables non-basic */
76 for (i = 1; i <= m; i++)
77 { if (glp_get_row_typecxx . . $ glp_get_row_type(423829e847ef1a83).(lp, i) != GLP_DBcxx . . $ `external/glpk/glpk.h:62:9`!)
78 glp_set_row_statcxx . . $ glp_set_row_stat(b083f42914639ad0).(lp, i, GLP_NScxx . . $ `external/glpk/glpk.h:70:9`!);
79 else if (fabs(glp_get_row_lbcxx . . $ glp_get_row_lb(68c5770eb0f32231).(lp, i)) <=
80 fabs(glp_get_row_ubcxx . . $ glp_get_row_ub(68c5770eb0f32231).(lp, i)))
81 glp_set_row_statcxx . . $ glp_set_row_stat(b083f42914639ad0).(lp, i, GLP_NLcxx . . $ `external/glpk/glpk.h:67:9`!);
82 else
83 glp_set_row_statcxx . . $ glp_set_row_stat(b083f42914639ad0).(lp, i, GLP_NUcxx . . $ `external/glpk/glpk.h:68:9`!);
84 }
85 /* make all structural variables non-basic */
86 for (j = 1; j <= n; j++)
87 { if (glp_get_col_typecxx . . $ glp_get_col_type(423829e847ef1a83).(lp, j) != GLP_DBcxx . . $ `external/glpk/glpk.h:62:9`!)
88 glp_set_col_statcxx . . $ glp_set_col_stat(b083f42914639ad0).(lp, j, GLP_NScxx . . $ `external/glpk/glpk.h:70:9`!);
89 else if (fabs(glp_get_col_lbcxx . . $ glp_get_col_lb(68c5770eb0f32231).(lp, j)) <=
90 fabs(glp_get_col_ubcxx . . $ glp_get_col_ub(68c5770eb0f32231).(lp, j)))
91 glp_set_col_statcxx . . $ glp_set_col_stat(b083f42914639ad0).(lp, j, GLP_NLcxx . . $ `external/glpk/glpk.h:67:9`!);
92 else
93 glp_set_col_statcxx . . $ glp_set_col_stat(b083f42914639ad0).(lp, j, GLP_NUcxx . . $ `external/glpk/glpk.h:68:9`!);
94 }
95 /* C2 is a set of free structural variables */
96 n2 = 0, C2 = C + 0;
97 for (j = 1; j <= n; j++)
98 { type = glp_get_col_typecxx . . $ glp_get_col_type(423829e847ef1a83).(lp, j);
99 if (type == GLP_FRcxx . . $ `external/glpk/glpk.h:59:9`!)
100 { n2++;
101 C2[n2].jcxx . . $ var#j. = j;
102 C2[n2].qcxx . . $ var#q. = 0.0;
103 }
104 }
105 /* C3 is a set of structural variables having excatly one (lower
106 or upper) bound */
107 n3 = 0, C3 = C2 + n2;
108 for (j = 1; j <= n; j++)
109 { type = glp_get_col_typecxx . . $ glp_get_col_type(423829e847ef1a83).(lp, j);
110 if (type == GLP_LOcxx . . $ `external/glpk/glpk.h:60:9`!)
111 { n3++;
112 C3[n3].jcxx . . $ var#j. = j;
113 C3[n3].qcxx . . $ var#q. = + glp_get_col_lbcxx . . $ glp_get_col_lb(68c5770eb0f32231).(lp, j);
114 }
115 else if (type == GLP_UPcxx . . $ `external/glpk/glpk.h:61:9`!)
116 { n3++;
117 C3[n3].jcxx . . $ var#j. = j;
118 C3[n3].qcxx . . $ var#q. = - glp_get_col_ubcxx . . $ glp_get_col_ub(68c5770eb0f32231).(lp, j);
119 }
120 }
121 /* C4 is a set of structural variables having both (lower and
122 upper) bounds */
123 n4 = 0, C4 = C3 + n3;
124 for (j = 1; j <= n; j++)
125 { type = glp_get_col_typecxx . . $ glp_get_col_type(423829e847ef1a83).(lp, j);
126 if (type == GLP_DBcxx . . $ `external/glpk/glpk.h:62:9`!)
127 { n4++;
128 C4[n4].jcxx . . $ var#j. = j;
129 C4[n4].qcxx . . $ var#q. = glp_get_col_lbcxx . . $ glp_get_col_lb(68c5770eb0f32231).(lp, j) - glp_get_col_ubcxx . . $ glp_get_col_ub(68c5770eb0f32231).(lp, j);
130 }
131 }
132 /* compute gamma = max{|c[j]|: 1 <= j <= n} */
133 gamma = 0.0;
134 for (j = 1; j <= n; j++)
135 { temp = fabs(glp_get_obj_coefcxx . . $ glp_get_obj_coef(68c5770eb0f32231).(lp, j));
136 if (gamma < temp) gamma = temp;
137 }
138 /* compute cmax */
139 cmax = (gamma == 0.0 ? 1.0 : 1000.0 * gamma);
140 /* compute final penalty for all structural variables within sets
141 C2, C3, and C4 */
142 switch (glp_get_obj_dircxx . . $ glp_get_obj_dir(317ecc1aeadfe2b7).(lp))
143 { case GLP_MINcxx . . $ `external/glpk/glpk.h:50:9`!: temp = +1.0; break;
144 case GLP_MAXcxx . . $ `external/glpk/glpk.h:51:9`!: temp = -1.0; break;
145 default: xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(lp != lp);
146 }
147 for (k = 1; k <= n2+n3+n4; k++)
148 { j = C[k].jcxx . . $ var#j.;
149 C[k].qcxx . . $ var#q. += (temp * glp_get_obj_coefcxx . . $ glp_get_obj_coef(68c5770eb0f32231).(lp, j)) / cmax;
150 }
151 /* sort structural variables within C2, C3, and C4 in ascending
152 order of penalty value */
153 qsortcxx . . $ qsort(ebb60b13a65e77d7).(C2+1, n2, sizeof(struct varcxx . . $ var#), fcmpcxx . . $ fcmp(9148c8c3ff42c9d8).);
154 for (k = 1; k < n2; k++) xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(C2[k].q <= C2[k+1].q);
155 qsortcxx . . $ qsort(ebb60b13a65e77d7).(C3+1, n3, sizeof(struct varcxx . . $ var#), fcmpcxx . . $ fcmp(9148c8c3ff42c9d8).);
156 for (k = 1; k < n3; k++) xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(C3[k].q <= C3[k+1].q);
157 qsortcxx . . $ qsort(ebb60b13a65e77d7).(C4+1, n4, sizeof(struct varcxx . . $ var#), fcmpcxx . . $ fcmp(9148c8c3ff42c9d8).);
158 for (k = 1; k < n4; k++) xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(C4[k].q <= C4[k+1].q);
159 /*** STEP 1 ***/
160 for (i = 1; i <= m; i++)
161 { type = glp_get_row_typecxx . . $ glp_get_row_type(423829e847ef1a83).(lp, i);
162 if (type != GLP_FXcxx . . $ `external/glpk/glpk.h:63:9`!)
163 { /* row i is either free or inequality constraint */
164 glp_set_row_statcxx . . $ glp_set_row_stat(b083f42914639ad0).(lp, i, GLP_BScxx . . $ `external/glpk/glpk.h:66:9`!);
165 I[i] = 1;
166 r[i] = 1;
167 }
168 else
169 { /* row i is equality constraint */
170 I[i] = 0;
171 r[i] = 0;
172 }
173 v[i] = +DBL_MAXcxx . . $ `<external>/18143172452841944454/float.h:105:9`!;
174 }
175 /*** STEP 2 ***/
176 for (k = 1; k <= n2+n3+n4; k++)
177 { jk = C[k].jcxx . . $ var#j.;
178 len = get_columncxx . . $ get_column(223ff0a4a32a5807).(lp, jk, ind, val);
179 /* let alpha = max{|A[l,jk]|: r[l] = 0} and let l' be such
180 that alpha = |A[l',jk]| */
181 alpha = 0.0, ll = 0;
182 for (t = 1; t <= len; t++)
183 { l = ind[t];
184 if (r[l] == 0 && alpha < fabs(val[t]))
185 alpha = fabs(val[t]), ll = l;
186 }
187 if (alpha >= 0.99)
188 { /* B := B union {jk} */
189 glp_set_col_statcxx . . $ glp_set_col_stat(b083f42914639ad0).(lp, jk, GLP_BScxx . . $ `external/glpk/glpk.h:66:9`!);
190 I[ll] = 1;
191 v[ll] = alpha;
192 /* r[l] := r[l] + 1 for all l such that |A[l,jk]| != 0 */
193 for (t = 1; t <= len; t++)
194 { l = ind[t];
195 if (val[t] != 0.0) r[l]++;
196 }
197 /* continue to the next k */
198 continue;
199 }
200 /* if |A[l,jk]| > 0.01 * v[l] for some l, continue to the
201 next k */
202 for (t = 1; t <= len; t++)
203 { l = ind[t];
204 if (fabs(val[t]) > 0.01 * v[l]) break;
205 }
206 if (t <= len) continue;
207 /* otherwise, let alpha = max{|A[l,jk]|: I[l] = 0} and let l'
208 be such that alpha = |A[l',jk]| */
209 alpha = 0.0, ll = 0;
210 for (t = 1; t <= len; t++)
211 { l = ind[t];
212 if (I[l] == 0 && alpha < fabs(val[t]))
213 alpha = fabs(val[t]), ll = l;
214 }
215 /* if alpha = 0, continue to the next k */
216 if (alpha == 0.0) continue;
217 /* B := B union {jk} */
218 glp_set_col_statcxx . . $ glp_set_col_stat(b083f42914639ad0).(lp, jk, GLP_BScxx . . $ `external/glpk/glpk.h:66:9`!);
219 I[ll] = 1;
220 v[ll] = alpha;
221 /* r[l] := r[l] + 1 for all l such that |A[l,jk]| != 0 */
222 for (t = 1; t <= len; t++)
223 { l = ind[t];
224 if (val[t] != 0.0) r[l]++;
225 }
226 }
227 /*** STEP 3 ***/
228 /* add an artificial variable (auxiliary variable for equality
229 constraint) to cover each remaining uncovered row */
230 for (i = 1; i <= m; i++)
231 if (I[i] == 0) glp_set_row_statcxx . . $ glp_set_row_stat(b083f42914639ad0).(lp, i, GLP_BScxx . . $ `external/glpk/glpk.h:66:9`!);
232 /* free working arrays */
233 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(C);
234 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(I);
235 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(r);
236 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(v);
237 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(ind);
238 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(val);
239 return;
240 }
241
242 /***********************************************************************
243 * NAME
244 *
245 * glp_cpx_basis - construct Bixby's initial LP basis
246 *
247 * SYNOPSIS
248 *
249 * void glp_cpx_basis(glp_prob *lp);
250 *
251 * DESCRIPTION
252 *
253 * The routine glp_cpx_basis constructs an advanced initial basis for
254 * the specified problem object.
255 *
256 * The routine is based on Bixby's algorithm described in the paper:
257 *
258 * Robert E. Bixby. Implementing the Simplex Method: The Initial Basis.
259 * ORSA Journal on Computing, Vol. 4, No. 3, 1992, pp. 267-84. */
260
261 void glp_cpx_basiscxx . . $ glp_cpx_basis(84c60d68cac39b9a).(glp_probcxx . . $ glp_prob# *lp)
262 { if (lp->mcxx . . $ glp_prob#m. == 0 || lp->ncxx . . $ glp_prob#n. == 0)
263 glp_std_basiscxx . . $ glp_std_basis(84c60d68cac39b9a).(lp);
264 else
265 cpx_basiscxx . . $ cpx_basis(84c60d68cac39b9a).(lp);
266 return;
267 }
268
269 /* eof */