alpar@1
|
1 |
/* glpini01.c */
|
alpar@1
|
2 |
|
alpar@1
|
3 |
/***********************************************************************
|
alpar@1
|
4 |
* This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@1
|
5 |
*
|
alpar@1
|
6 |
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@1
|
7 |
* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
|
alpar@1
|
8 |
* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@1
|
9 |
* E-mail: <mao@gnu.org>.
|
alpar@1
|
10 |
*
|
alpar@1
|
11 |
* GLPK is free software: you can redistribute it and/or modify it
|
alpar@1
|
12 |
* under the terms of the GNU General Public License as published by
|
alpar@1
|
13 |
* the Free Software Foundation, either version 3 of the License, or
|
alpar@1
|
14 |
* (at your option) any later version.
|
alpar@1
|
15 |
*
|
alpar@1
|
16 |
* GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@1
|
17 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@1
|
18 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@1
|
19 |
* License for more details.
|
alpar@1
|
20 |
*
|
alpar@1
|
21 |
* You should have received a copy of the GNU General Public License
|
alpar@1
|
22 |
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@1
|
23 |
***********************************************************************/
|
alpar@1
|
24 |
|
alpar@1
|
25 |
#include "glpapi.h"
|
alpar@1
|
26 |
|
alpar@1
|
27 |
/*----------------------------------------------------------------------
|
alpar@1
|
28 |
-- triang - find maximal triangular part of a rectangular matrix.
|
alpar@1
|
29 |
--
|
alpar@1
|
30 |
-- *Synopsis*
|
alpar@1
|
31 |
--
|
alpar@1
|
32 |
-- int triang(int m, int n,
|
alpar@1
|
33 |
-- void *info, int (*mat)(void *info, int k, int ndx[]),
|
alpar@1
|
34 |
-- int rn[], int cn[]);
|
alpar@1
|
35 |
--
|
alpar@1
|
36 |
-- *Description*
|
alpar@1
|
37 |
--
|
alpar@1
|
38 |
-- For a given rectangular (sparse) matrix A with m rows and n columns
|
alpar@1
|
39 |
-- the routine triang tries to find such permutation matrices P and Q
|
alpar@1
|
40 |
-- that the first rows and columns of the matrix B = P*A*Q form a lower
|
alpar@1
|
41 |
-- triangular submatrix of as greatest size as possible:
|
alpar@1
|
42 |
--
|
alpar@1
|
43 |
-- 1 n
|
alpar@1
|
44 |
-- 1 * . . . . . . x x x x x x
|
alpar@1
|
45 |
-- * * . . . . . x x x x x x
|
alpar@1
|
46 |
-- * * * . . . . x x x x x x
|
alpar@1
|
47 |
-- * * * * . . . x x x x x x
|
alpar@1
|
48 |
-- B = P*A*Q = * * * * * . . x x x x x x
|
alpar@1
|
49 |
-- * * * * * * . x x x x x x
|
alpar@1
|
50 |
-- * * * * * * * x x x x x x
|
alpar@1
|
51 |
-- x x x x x x x x x x x x x
|
alpar@1
|
52 |
-- x x x x x x x x x x x x x
|
alpar@1
|
53 |
-- m x x x x x x x x x x x x x
|
alpar@1
|
54 |
--
|
alpar@1
|
55 |
-- where: '*' - elements of the lower triangular part, '.' - structural
|
alpar@1
|
56 |
-- zeros, 'x' - other (either non-zero or zero) elements.
|
alpar@1
|
57 |
--
|
alpar@1
|
58 |
-- The parameter info is a transit pointer passed to the formal routine
|
alpar@1
|
59 |
-- mat (see below).
|
alpar@1
|
60 |
--
|
alpar@1
|
61 |
-- The formal routine mat specifies the given matrix A in both row- and
|
alpar@1
|
62 |
-- column-wise formats. In order to obtain an i-th row of the matrix A
|
alpar@1
|
63 |
-- the routine triang calls the routine mat with the parameter k = +i,
|
alpar@1
|
64 |
-- 1 <= i <= m. In response the routine mat should store column indices
|
alpar@1
|
65 |
-- of (non-zero) elements of the i-th row to the locations ndx[1], ...,
|
alpar@1
|
66 |
-- ndx[len], where len is number of non-zeros in the i-th row returned
|
alpar@1
|
67 |
-- on exit. Analogously, in order to obtain a j-th column of the matrix
|
alpar@1
|
68 |
-- A, the routine mat is called with the parameter k = -j, 1 <= j <= n,
|
alpar@1
|
69 |
-- and should return pattern of the j-th column in the same way as for
|
alpar@1
|
70 |
-- row patterns. Note that the routine mat may be called more than once
|
alpar@1
|
71 |
-- for the same rows and columns.
|
alpar@1
|
72 |
--
|
alpar@1
|
73 |
-- On exit the routine computes two resultant arrays rn and cn, which
|
alpar@1
|
74 |
-- define the permutation matrices P and Q, respectively. The array rn
|
alpar@1
|
75 |
-- should have at least 1+m locations, where rn[i] = i' (1 <= i <= m)
|
alpar@1
|
76 |
-- means that i-th row of the original matrix A corresponds to i'-th row
|
alpar@1
|
77 |
-- of the matrix B = P*A*Q. Similarly, the array cn should have at least
|
alpar@1
|
78 |
-- 1+n locations, where cn[j] = j' (1 <= j <= n) means that j-th column
|
alpar@1
|
79 |
-- of the matrix A corresponds to j'-th column of the matrix B.
|
alpar@1
|
80 |
--
|
alpar@1
|
81 |
-- *Returns*
|
alpar@1
|
82 |
--
|
alpar@1
|
83 |
-- The routine triang returns the size of the lower tringular part of
|
alpar@1
|
84 |
-- the matrix B = P*A*Q (see the figure above).
|
alpar@1
|
85 |
--
|
alpar@1
|
86 |
-- *Complexity*
|
alpar@1
|
87 |
--
|
alpar@1
|
88 |
-- The time complexity of the routine triang is O(nnz), where nnz is
|
alpar@1
|
89 |
-- number of non-zeros in the given matrix A.
|
alpar@1
|
90 |
--
|
alpar@1
|
91 |
-- *Algorithm*
|
alpar@1
|
92 |
--
|
alpar@1
|
93 |
-- The routine triang starts from the matrix B = P*Q*A, where P and Q
|
alpar@1
|
94 |
-- are unity matrices, so initially B = A.
|
alpar@1
|
95 |
--
|
alpar@1
|
96 |
-- Before the next iteration B = (B1 | B2 | B3), where B1 is partially
|
alpar@1
|
97 |
-- built a lower triangular submatrix, B2 is the active submatrix, and
|
alpar@1
|
98 |
-- B3 is a submatrix that contains rejected columns. Thus, the current
|
alpar@1
|
99 |
-- matrix B looks like follows (initially k1 = 1 and k2 = n):
|
alpar@1
|
100 |
--
|
alpar@1
|
101 |
-- 1 k1 k2 n
|
alpar@1
|
102 |
-- 1 x . . . . . . . . . . . . . # # #
|
alpar@1
|
103 |
-- x x . . . . . . . . . . . . # # #
|
alpar@1
|
104 |
-- x x x . . . . . . . . . . # # # #
|
alpar@1
|
105 |
-- x x x x . . . . . . . . . # # # #
|
alpar@1
|
106 |
-- x x x x x . . . . . . . # # # # #
|
alpar@1
|
107 |
-- k1 x x x x x * * * * * * * # # # # #
|
alpar@1
|
108 |
-- x x x x x * * * * * * * # # # # #
|
alpar@1
|
109 |
-- x x x x x * * * * * * * # # # # #
|
alpar@1
|
110 |
-- x x x x x * * * * * * * # # # # #
|
alpar@1
|
111 |
-- m x x x x x * * * * * * * # # # # #
|
alpar@1
|
112 |
-- <--B1---> <----B2-----> <---B3-->
|
alpar@1
|
113 |
--
|
alpar@1
|
114 |
-- On each iteartion the routine looks for a singleton row, i.e. some
|
alpar@1
|
115 |
-- row that has the only non-zero in the active submatrix B2. If such
|
alpar@1
|
116 |
-- row exists and the corresponding non-zero is b[i,j], where (by the
|
alpar@1
|
117 |
-- definition) k1 <= i <= m and k1 <= j <= k2, the routine permutes
|
alpar@1
|
118 |
-- k1-th and i-th rows and k1-th and j-th columns of the matrix B (in
|
alpar@1
|
119 |
-- order to place the element in the position b[k1,k1]), removes the
|
alpar@1
|
120 |
-- k1-th column from the active submatrix B2, and adds this column to
|
alpar@1
|
121 |
-- the submatrix B1. If no row singletons exist, but B2 is not empty
|
alpar@1
|
122 |
-- yet, the routine chooses a j-th column, which has maximal number of
|
alpar@1
|
123 |
-- non-zeros among other columns of B2, removes this column from B2 and
|
alpar@1
|
124 |
-- adds it to the submatrix B3 in the hope that new row singletons will
|
alpar@1
|
125 |
-- appear in the active submatrix. */
|
alpar@1
|
126 |
|
alpar@1
|
127 |
static int triang(int m, int n,
|
alpar@1
|
128 |
void *info, int (*mat)(void *info, int k, int ndx[]),
|
alpar@1
|
129 |
int rn[], int cn[])
|
alpar@1
|
130 |
{ int *ndx; /* int ndx[1+max(m,n)]; */
|
alpar@1
|
131 |
/* this array is used for querying row and column patterns of the
|
alpar@1
|
132 |
given matrix A (the third parameter to the routine mat) */
|
alpar@1
|
133 |
int *rs_len; /* int rs_len[1+m]; */
|
alpar@1
|
134 |
/* rs_len[0] is not used;
|
alpar@1
|
135 |
rs_len[i], 1 <= i <= m, is number of non-zeros in the i-th row
|
alpar@1
|
136 |
of the matrix A, which (non-zeros) belong to the current active
|
alpar@1
|
137 |
submatrix */
|
alpar@1
|
138 |
int *rs_head; /* int rs_head[1+n]; */
|
alpar@1
|
139 |
/* rs_head[len], 0 <= len <= n, is the number i of the first row
|
alpar@1
|
140 |
of the matrix A, for which rs_len[i] = len */
|
alpar@1
|
141 |
int *rs_prev; /* int rs_prev[1+m]; */
|
alpar@1
|
142 |
/* rs_prev[0] is not used;
|
alpar@1
|
143 |
rs_prev[i], 1 <= i <= m, is a number i' of the previous row of
|
alpar@1
|
144 |
the matrix A, for which rs_len[i] = rs_len[i'] (zero marks the
|
alpar@1
|
145 |
end of this linked list) */
|
alpar@1
|
146 |
int *rs_next; /* int rs_next[1+m]; */
|
alpar@1
|
147 |
/* rs_next[0] is not used;
|
alpar@1
|
148 |
rs_next[i], 1 <= i <= m, is a number i' of the next row of the
|
alpar@1
|
149 |
matrix A, for which rs_len[i] = rs_len[i'] (zero marks the end
|
alpar@1
|
150 |
this linked list) */
|
alpar@1
|
151 |
int cs_head;
|
alpar@1
|
152 |
/* is a number j of the first column of the matrix A, which has
|
alpar@1
|
153 |
maximal number of non-zeros among other columns */
|
alpar@1
|
154 |
int *cs_prev; /* cs_prev[1+n]; */
|
alpar@1
|
155 |
/* cs_prev[0] is not used;
|
alpar@1
|
156 |
cs_prev[j], 1 <= j <= n, is a number of the previous column of
|
alpar@1
|
157 |
the matrix A with the same or greater number of non-zeros than
|
alpar@1
|
158 |
in the j-th column (zero marks the end of this linked list) */
|
alpar@1
|
159 |
int *cs_next; /* cs_next[1+n]; */
|
alpar@1
|
160 |
/* cs_next[0] is not used;
|
alpar@1
|
161 |
cs_next[j], 1 <= j <= n, is a number of the next column of
|
alpar@1
|
162 |
the matrix A with the same or lesser number of non-zeros than
|
alpar@1
|
163 |
in the j-th column (zero marks the end of this linked list) */
|
alpar@1
|
164 |
int i, j, ii, jj, k1, k2, len, t, size = 0;
|
alpar@1
|
165 |
int *head, *rn_inv, *cn_inv;
|
alpar@1
|
166 |
if (!(m > 0 && n > 0))
|
alpar@1
|
167 |
xerror("triang: m = %d; n = %d; invalid dimension\n", m, n);
|
alpar@1
|
168 |
/* allocate working arrays */
|
alpar@1
|
169 |
ndx = xcalloc(1+(m >= n ? m : n), sizeof(int));
|
alpar@1
|
170 |
rs_len = xcalloc(1+m, sizeof(int));
|
alpar@1
|
171 |
rs_head = xcalloc(1+n, sizeof(int));
|
alpar@1
|
172 |
rs_prev = xcalloc(1+m, sizeof(int));
|
alpar@1
|
173 |
rs_next = xcalloc(1+m, sizeof(int));
|
alpar@1
|
174 |
cs_prev = xcalloc(1+n, sizeof(int));
|
alpar@1
|
175 |
cs_next = xcalloc(1+n, sizeof(int));
|
alpar@1
|
176 |
/* build linked lists of columns of the matrix A with the same
|
alpar@1
|
177 |
number of non-zeros */
|
alpar@1
|
178 |
head = rs_len; /* currently rs_len is used as working array */
|
alpar@1
|
179 |
for (len = 0; len <= m; len ++) head[len] = 0;
|
alpar@1
|
180 |
for (j = 1; j <= n; j++)
|
alpar@1
|
181 |
{ /* obtain length of the j-th column */
|
alpar@1
|
182 |
len = mat(info, -j, ndx);
|
alpar@1
|
183 |
xassert(0 <= len && len <= m);
|
alpar@1
|
184 |
/* include the j-th column in the corresponding linked list */
|
alpar@1
|
185 |
cs_prev[j] = head[len];
|
alpar@1
|
186 |
head[len] = j;
|
alpar@1
|
187 |
}
|
alpar@1
|
188 |
/* merge all linked lists of columns in one linked list, where
|
alpar@1
|
189 |
columns are ordered by descending of their lengths */
|
alpar@1
|
190 |
cs_head = 0;
|
alpar@1
|
191 |
for (len = 0; len <= m; len++)
|
alpar@1
|
192 |
{ for (j = head[len]; j != 0; j = cs_prev[j])
|
alpar@1
|
193 |
{ cs_next[j] = cs_head;
|
alpar@1
|
194 |
cs_head = j;
|
alpar@1
|
195 |
}
|
alpar@1
|
196 |
}
|
alpar@1
|
197 |
jj = 0;
|
alpar@1
|
198 |
for (j = cs_head; j != 0; j = cs_next[j])
|
alpar@1
|
199 |
{ cs_prev[j] = jj;
|
alpar@1
|
200 |
jj = j;
|
alpar@1
|
201 |
}
|
alpar@1
|
202 |
/* build initial doubly linked lists of rows of the matrix A with
|
alpar@1
|
203 |
the same number of non-zeros */
|
alpar@1
|
204 |
for (len = 0; len <= n; len++) rs_head[len] = 0;
|
alpar@1
|
205 |
for (i = 1; i <= m; i++)
|
alpar@1
|
206 |
{ /* obtain length of the i-th row */
|
alpar@1
|
207 |
rs_len[i] = len = mat(info, +i, ndx);
|
alpar@1
|
208 |
xassert(0 <= len && len <= n);
|
alpar@1
|
209 |
/* include the i-th row in the correspondng linked list */
|
alpar@1
|
210 |
rs_prev[i] = 0;
|
alpar@1
|
211 |
rs_next[i] = rs_head[len];
|
alpar@1
|
212 |
if (rs_next[i] != 0) rs_prev[rs_next[i]] = i;
|
alpar@1
|
213 |
rs_head[len] = i;
|
alpar@1
|
214 |
}
|
alpar@1
|
215 |
/* initially all rows and columns of the matrix A are active */
|
alpar@1
|
216 |
for (i = 1; i <= m; i++) rn[i] = 0;
|
alpar@1
|
217 |
for (j = 1; j <= n; j++) cn[j] = 0;
|
alpar@1
|
218 |
/* set initial bounds of the active submatrix */
|
alpar@1
|
219 |
k1 = 1, k2 = n;
|
alpar@1
|
220 |
/* main loop starts here */
|
alpar@1
|
221 |
while (k1 <= k2)
|
alpar@1
|
222 |
{ i = rs_head[1];
|
alpar@1
|
223 |
if (i != 0)
|
alpar@1
|
224 |
{ /* the i-th row of the matrix A is a row singleton, since
|
alpar@1
|
225 |
it has the only non-zero in the active submatrix */
|
alpar@1
|
226 |
xassert(rs_len[i] == 1);
|
alpar@1
|
227 |
/* determine the number j of an active column of the matrix
|
alpar@1
|
228 |
A, in which this non-zero is placed */
|
alpar@1
|
229 |
j = 0;
|
alpar@1
|
230 |
t = mat(info, +i, ndx);
|
alpar@1
|
231 |
xassert(0 <= t && t <= n);
|
alpar@1
|
232 |
for (t = t; t >= 1; t--)
|
alpar@1
|
233 |
{ jj = ndx[t];
|
alpar@1
|
234 |
xassert(1 <= jj && jj <= n);
|
alpar@1
|
235 |
if (cn[jj] == 0)
|
alpar@1
|
236 |
{ xassert(j == 0);
|
alpar@1
|
237 |
j = jj;
|
alpar@1
|
238 |
}
|
alpar@1
|
239 |
}
|
alpar@1
|
240 |
xassert(j != 0);
|
alpar@1
|
241 |
/* the singleton is a[i,j]; move a[i,j] to the position
|
alpar@1
|
242 |
b[k1,k1] of the matrix B */
|
alpar@1
|
243 |
rn[i] = cn[j] = k1;
|
alpar@1
|
244 |
/* shift the left bound of the active submatrix */
|
alpar@1
|
245 |
k1++;
|
alpar@1
|
246 |
/* increase the size of the lower triangular part */
|
alpar@1
|
247 |
size++;
|
alpar@1
|
248 |
}
|
alpar@1
|
249 |
else
|
alpar@1
|
250 |
{ /* the current active submatrix has no row singletons */
|
alpar@1
|
251 |
/* remove an active column with maximal number of non-zeros
|
alpar@1
|
252 |
from the active submatrix */
|
alpar@1
|
253 |
j = cs_head;
|
alpar@1
|
254 |
xassert(j != 0);
|
alpar@1
|
255 |
cn[j] = k2;
|
alpar@1
|
256 |
/* shift the right bound of the active submatrix */
|
alpar@1
|
257 |
k2--;
|
alpar@1
|
258 |
}
|
alpar@1
|
259 |
/* the j-th column of the matrix A has been removed from the
|
alpar@1
|
260 |
active submatrix */
|
alpar@1
|
261 |
/* remove the j-th column from the linked list */
|
alpar@1
|
262 |
if (cs_prev[j] == 0)
|
alpar@1
|
263 |
cs_head = cs_next[j];
|
alpar@1
|
264 |
else
|
alpar@1
|
265 |
cs_next[cs_prev[j]] = cs_next[j];
|
alpar@1
|
266 |
if (cs_next[j] == 0)
|
alpar@1
|
267 |
/* nop */;
|
alpar@1
|
268 |
else
|
alpar@1
|
269 |
cs_prev[cs_next[j]] = cs_prev[j];
|
alpar@1
|
270 |
/* go through non-zeros of the j-th columns and update active
|
alpar@1
|
271 |
lengths of the corresponding rows */
|
alpar@1
|
272 |
t = mat(info, -j, ndx);
|
alpar@1
|
273 |
xassert(0 <= t && t <= m);
|
alpar@1
|
274 |
for (t = t; t >= 1; t--)
|
alpar@1
|
275 |
{ i = ndx[t];
|
alpar@1
|
276 |
xassert(1 <= i && i <= m);
|
alpar@1
|
277 |
/* the non-zero a[i,j] has left the active submatrix */
|
alpar@1
|
278 |
len = rs_len[i];
|
alpar@1
|
279 |
xassert(len >= 1);
|
alpar@1
|
280 |
/* remove the i-th row from the linked list of rows with
|
alpar@1
|
281 |
active length len */
|
alpar@1
|
282 |
if (rs_prev[i] == 0)
|
alpar@1
|
283 |
rs_head[len] = rs_next[i];
|
alpar@1
|
284 |
else
|
alpar@1
|
285 |
rs_next[rs_prev[i]] = rs_next[i];
|
alpar@1
|
286 |
if (rs_next[i] == 0)
|
alpar@1
|
287 |
/* nop */;
|
alpar@1
|
288 |
else
|
alpar@1
|
289 |
rs_prev[rs_next[i]] = rs_prev[i];
|
alpar@1
|
290 |
/* decrease the active length of the i-th row */
|
alpar@1
|
291 |
rs_len[i] = --len;
|
alpar@1
|
292 |
/* return the i-th row to the corresponding linked list */
|
alpar@1
|
293 |
rs_prev[i] = 0;
|
alpar@1
|
294 |
rs_next[i] = rs_head[len];
|
alpar@1
|
295 |
if (rs_next[i] != 0) rs_prev[rs_next[i]] = i;
|
alpar@1
|
296 |
rs_head[len] = i;
|
alpar@1
|
297 |
}
|
alpar@1
|
298 |
}
|
alpar@1
|
299 |
/* other rows of the matrix A, which are still active, correspond
|
alpar@1
|
300 |
to rows k1, ..., m of the matrix B (in arbitrary order) */
|
alpar@1
|
301 |
for (i = 1; i <= m; i++) if (rn[i] == 0) rn[i] = k1++;
|
alpar@1
|
302 |
/* but for columns this is not needed, because now the submatrix
|
alpar@1
|
303 |
B2 has no columns */
|
alpar@1
|
304 |
for (j = 1; j <= n; j++) xassert(cn[j] != 0);
|
alpar@1
|
305 |
/* perform some optional checks */
|
alpar@1
|
306 |
/* make sure that rn is a permutation of {1, ..., m} and cn is a
|
alpar@1
|
307 |
permutation of {1, ..., n} */
|
alpar@1
|
308 |
rn_inv = rs_len; /* used as working array */
|
alpar@1
|
309 |
for (ii = 1; ii <= m; ii++) rn_inv[ii] = 0;
|
alpar@1
|
310 |
for (i = 1; i <= m; i++)
|
alpar@1
|
311 |
{ ii = rn[i];
|
alpar@1
|
312 |
xassert(1 <= ii && ii <= m);
|
alpar@1
|
313 |
xassert(rn_inv[ii] == 0);
|
alpar@1
|
314 |
rn_inv[ii] = i;
|
alpar@1
|
315 |
}
|
alpar@1
|
316 |
cn_inv = rs_head; /* used as working array */
|
alpar@1
|
317 |
for (jj = 1; jj <= n; jj++) cn_inv[jj] = 0;
|
alpar@1
|
318 |
for (j = 1; j <= n; j++)
|
alpar@1
|
319 |
{ jj = cn[j];
|
alpar@1
|
320 |
xassert(1 <= jj && jj <= n);
|
alpar@1
|
321 |
xassert(cn_inv[jj] == 0);
|
alpar@1
|
322 |
cn_inv[jj] = j;
|
alpar@1
|
323 |
}
|
alpar@1
|
324 |
/* make sure that the matrix B = P*A*Q really has the form, which
|
alpar@1
|
325 |
was declared */
|
alpar@1
|
326 |
for (ii = 1; ii <= size; ii++)
|
alpar@1
|
327 |
{ int diag = 0;
|
alpar@1
|
328 |
i = rn_inv[ii];
|
alpar@1
|
329 |
t = mat(info, +i, ndx);
|
alpar@1
|
330 |
xassert(0 <= t && t <= n);
|
alpar@1
|
331 |
for (t = t; t >= 1; t--)
|
alpar@1
|
332 |
{ j = ndx[t];
|
alpar@1
|
333 |
xassert(1 <= j && j <= n);
|
alpar@1
|
334 |
jj = cn[j];
|
alpar@1
|
335 |
if (jj <= size) xassert(jj <= ii);
|
alpar@1
|
336 |
if (jj == ii)
|
alpar@1
|
337 |
{ xassert(!diag);
|
alpar@1
|
338 |
diag = 1;
|
alpar@1
|
339 |
}
|
alpar@1
|
340 |
}
|
alpar@1
|
341 |
xassert(diag);
|
alpar@1
|
342 |
}
|
alpar@1
|
343 |
/* free working arrays */
|
alpar@1
|
344 |
xfree(ndx);
|
alpar@1
|
345 |
xfree(rs_len);
|
alpar@1
|
346 |
xfree(rs_head);
|
alpar@1
|
347 |
xfree(rs_prev);
|
alpar@1
|
348 |
xfree(rs_next);
|
alpar@1
|
349 |
xfree(cs_prev);
|
alpar@1
|
350 |
xfree(cs_next);
|
alpar@1
|
351 |
/* return to the calling program */
|
alpar@1
|
352 |
return size;
|
alpar@1
|
353 |
}
|
alpar@1
|
354 |
|
alpar@1
|
355 |
/*----------------------------------------------------------------------
|
alpar@1
|
356 |
-- adv_basis - construct advanced initial LP basis.
|
alpar@1
|
357 |
--
|
alpar@1
|
358 |
-- *Synopsis*
|
alpar@1
|
359 |
--
|
alpar@1
|
360 |
-- #include "glpini.h"
|
alpar@1
|
361 |
-- void adv_basis(glp_prob *lp);
|
alpar@1
|
362 |
--
|
alpar@1
|
363 |
-- *Description*
|
alpar@1
|
364 |
--
|
alpar@1
|
365 |
-- The routine adv_basis constructs an advanced initial basis for an LP
|
alpar@1
|
366 |
-- problem object, which the parameter lp points to.
|
alpar@1
|
367 |
--
|
alpar@1
|
368 |
-- In order to build the initial basis the routine does the following:
|
alpar@1
|
369 |
--
|
alpar@1
|
370 |
-- 1) includes in the basis all non-fixed auxiliary variables;
|
alpar@1
|
371 |
--
|
alpar@1
|
372 |
-- 2) includes in the basis as many as possible non-fixed structural
|
alpar@1
|
373 |
-- variables preserving triangular form of the basis matrix;
|
alpar@1
|
374 |
--
|
alpar@1
|
375 |
-- 3) includes in the basis appropriate (fixed) auxiliary variables
|
alpar@1
|
376 |
-- in order to complete the basis.
|
alpar@1
|
377 |
--
|
alpar@1
|
378 |
-- As a result the initial basis has minimum of fixed variables and the
|
alpar@1
|
379 |
-- corresponding basis matrix is triangular. */
|
alpar@1
|
380 |
|
alpar@1
|
381 |
static int mat(void *info, int k, int ndx[])
|
alpar@1
|
382 |
{ /* this auxiliary routine returns the pattern of a given row or
|
alpar@1
|
383 |
a given column of the augmented constraint matrix A~ = (I|-A),
|
alpar@1
|
384 |
in which columns of fixed variables are implicitly cleared */
|
alpar@1
|
385 |
LPX *lp = info;
|
alpar@1
|
386 |
int m = lpx_get_num_rows(lp);
|
alpar@1
|
387 |
int n = lpx_get_num_cols(lp);
|
alpar@1
|
388 |
int typx, i, j, lll, len = 0;
|
alpar@1
|
389 |
if (k > 0)
|
alpar@1
|
390 |
{ /* the pattern of the i-th row is required */
|
alpar@1
|
391 |
i = +k;
|
alpar@1
|
392 |
xassert(1 <= i && i <= m);
|
alpar@1
|
393 |
#if 0 /* 22/XII-2003 */
|
alpar@1
|
394 |
/* if the auxiliary variable x[i] is non-fixed, include its
|
alpar@1
|
395 |
element (placed in the i-th column) in the pattern */
|
alpar@1
|
396 |
lpx_get_row_bnds(lp, i, &typx, NULL, NULL);
|
alpar@1
|
397 |
if (typx != LPX_FX) ndx[++len] = i;
|
alpar@1
|
398 |
/* include in the pattern elements placed in columns, which
|
alpar@1
|
399 |
correspond to non-fixed structural varables */
|
alpar@1
|
400 |
i_beg = aa_ptr[i];
|
alpar@1
|
401 |
i_end = i_beg + aa_len[i] - 1;
|
alpar@1
|
402 |
for (i_ptr = i_beg; i_ptr <= i_end; i_ptr++)
|
alpar@1
|
403 |
{ j = m + sv_ndx[i_ptr];
|
alpar@1
|
404 |
lpx_get_col_bnds(lp, j-m, &typx, NULL, NULL);
|
alpar@1
|
405 |
if (typx != LPX_FX) ndx[++len] = j;
|
alpar@1
|
406 |
}
|
alpar@1
|
407 |
#else
|
alpar@1
|
408 |
lll = lpx_get_mat_row(lp, i, ndx, NULL);
|
alpar@1
|
409 |
for (k = 1; k <= lll; k++)
|
alpar@1
|
410 |
{ lpx_get_col_bnds(lp, ndx[k], &typx, NULL, NULL);
|
alpar@1
|
411 |
if (typx != LPX_FX) ndx[++len] = m + ndx[k];
|
alpar@1
|
412 |
}
|
alpar@1
|
413 |
lpx_get_row_bnds(lp, i, &typx, NULL, NULL);
|
alpar@1
|
414 |
if (typx != LPX_FX) ndx[++len] = i;
|
alpar@1
|
415 |
#endif
|
alpar@1
|
416 |
}
|
alpar@1
|
417 |
else
|
alpar@1
|
418 |
{ /* the pattern of the j-th column is required */
|
alpar@1
|
419 |
j = -k;
|
alpar@1
|
420 |
xassert(1 <= j && j <= m+n);
|
alpar@1
|
421 |
/* if the (auxiliary or structural) variable x[j] is fixed,
|
alpar@1
|
422 |
the pattern of its column is empty */
|
alpar@1
|
423 |
if (j <= m)
|
alpar@1
|
424 |
lpx_get_row_bnds(lp, j, &typx, NULL, NULL);
|
alpar@1
|
425 |
else
|
alpar@1
|
426 |
lpx_get_col_bnds(lp, j-m, &typx, NULL, NULL);
|
alpar@1
|
427 |
if (typx != LPX_FX)
|
alpar@1
|
428 |
{ if (j <= m)
|
alpar@1
|
429 |
{ /* x[j] is non-fixed auxiliary variable */
|
alpar@1
|
430 |
ndx[++len] = j;
|
alpar@1
|
431 |
}
|
alpar@1
|
432 |
else
|
alpar@1
|
433 |
{ /* x[j] is non-fixed structural variables */
|
alpar@1
|
434 |
#if 0 /* 22/XII-2003 */
|
alpar@1
|
435 |
j_beg = aa_ptr[j];
|
alpar@1
|
436 |
j_end = j_beg + aa_len[j] - 1;
|
alpar@1
|
437 |
for (j_ptr = j_beg; j_ptr <= j_end; j_ptr++)
|
alpar@1
|
438 |
ndx[++len] = sv_ndx[j_ptr];
|
alpar@1
|
439 |
#else
|
alpar@1
|
440 |
len = lpx_get_mat_col(lp, j-m, ndx, NULL);
|
alpar@1
|
441 |
#endif
|
alpar@1
|
442 |
}
|
alpar@1
|
443 |
}
|
alpar@1
|
444 |
}
|
alpar@1
|
445 |
/* return the length of the row/column pattern */
|
alpar@1
|
446 |
return len;
|
alpar@1
|
447 |
}
|
alpar@1
|
448 |
|
alpar@1
|
449 |
static void adv_basis(glp_prob *lp)
|
alpar@1
|
450 |
{ int m = lpx_get_num_rows(lp);
|
alpar@1
|
451 |
int n = lpx_get_num_cols(lp);
|
alpar@1
|
452 |
int i, j, jj, k, size;
|
alpar@1
|
453 |
int *rn, *cn, *rn_inv, *cn_inv;
|
alpar@1
|
454 |
int typx, *tagx = xcalloc(1+m+n, sizeof(int));
|
alpar@1
|
455 |
double lb, ub;
|
alpar@1
|
456 |
xprintf("Constructing initial basis...\n");
|
alpar@1
|
457 |
#if 0 /* 13/V-2009 */
|
alpar@1
|
458 |
if (m == 0)
|
alpar@1
|
459 |
xerror("glp_adv_basis: problem has no rows\n");
|
alpar@1
|
460 |
if (n == 0)
|
alpar@1
|
461 |
xerror("glp_adv_basis: problem has no columns\n");
|
alpar@1
|
462 |
#else
|
alpar@1
|
463 |
if (m == 0 || n == 0)
|
alpar@1
|
464 |
{ glp_std_basis(lp);
|
alpar@1
|
465 |
return;
|
alpar@1
|
466 |
}
|
alpar@1
|
467 |
#endif
|
alpar@1
|
468 |
/* use the routine triang (see above) to find maximal triangular
|
alpar@1
|
469 |
part of the augmented constraint matrix A~ = (I|-A); in order
|
alpar@1
|
470 |
to prevent columns of fixed variables to be included in the
|
alpar@1
|
471 |
triangular part, such columns are implictly removed from the
|
alpar@1
|
472 |
matrix A~ by the routine adv_mat */
|
alpar@1
|
473 |
rn = xcalloc(1+m, sizeof(int));
|
alpar@1
|
474 |
cn = xcalloc(1+m+n, sizeof(int));
|
alpar@1
|
475 |
size = triang(m, m+n, lp, mat, rn, cn);
|
alpar@1
|
476 |
if (lpx_get_int_parm(lp, LPX_K_MSGLEV) >= 3)
|
alpar@1
|
477 |
xprintf("Size of triangular part = %d\n", size);
|
alpar@1
|
478 |
/* the first size rows and columns of the matrix P*A~*Q (where
|
alpar@1
|
479 |
P and Q are permutation matrices defined by the arrays rn and
|
alpar@1
|
480 |
cn) form a lower triangular matrix; build the arrays (rn_inv
|
alpar@1
|
481 |
and cn_inv), which define the matrices inv(P) and inv(Q) */
|
alpar@1
|
482 |
rn_inv = xcalloc(1+m, sizeof(int));
|
alpar@1
|
483 |
cn_inv = xcalloc(1+m+n, sizeof(int));
|
alpar@1
|
484 |
for (i = 1; i <= m; i++) rn_inv[rn[i]] = i;
|
alpar@1
|
485 |
for (j = 1; j <= m+n; j++) cn_inv[cn[j]] = j;
|
alpar@1
|
486 |
/* include the columns of the matrix A~, which correspond to the
|
alpar@1
|
487 |
first size columns of the matrix P*A~*Q, in the basis */
|
alpar@1
|
488 |
for (k = 1; k <= m+n; k++) tagx[k] = -1;
|
alpar@1
|
489 |
for (jj = 1; jj <= size; jj++)
|
alpar@1
|
490 |
{ j = cn_inv[jj];
|
alpar@1
|
491 |
/* the j-th column of A~ is the jj-th column of P*A~*Q */
|
alpar@1
|
492 |
tagx[j] = LPX_BS;
|
alpar@1
|
493 |
}
|
alpar@1
|
494 |
/* if size < m, we need to add appropriate columns of auxiliary
|
alpar@1
|
495 |
variables to the basis */
|
alpar@1
|
496 |
for (jj = size + 1; jj <= m; jj++)
|
alpar@1
|
497 |
{ /* the jj-th column of P*A~*Q should be replaced by the column
|
alpar@1
|
498 |
of the auxiliary variable, for which the only unity element
|
alpar@1
|
499 |
is placed in the position [jj,jj] */
|
alpar@1
|
500 |
i = rn_inv[jj];
|
alpar@1
|
501 |
/* the jj-th row of P*A~*Q is the i-th row of A~, but in the
|
alpar@1
|
502 |
i-th row of A~ the unity element belongs to the i-th column
|
alpar@1
|
503 |
of A~; therefore the disired column corresponds to the i-th
|
alpar@1
|
504 |
auxiliary variable (note that this column doesn't belong to
|
alpar@1
|
505 |
the triangular part found by the routine triang) */
|
alpar@1
|
506 |
xassert(1 <= i && i <= m);
|
alpar@1
|
507 |
xassert(cn[i] > size);
|
alpar@1
|
508 |
tagx[i] = LPX_BS;
|
alpar@1
|
509 |
}
|
alpar@1
|
510 |
/* free working arrays */
|
alpar@1
|
511 |
xfree(rn);
|
alpar@1
|
512 |
xfree(cn);
|
alpar@1
|
513 |
xfree(rn_inv);
|
alpar@1
|
514 |
xfree(cn_inv);
|
alpar@1
|
515 |
/* build tags of non-basic variables */
|
alpar@1
|
516 |
for (k = 1; k <= m+n; k++)
|
alpar@1
|
517 |
{ if (tagx[k] != LPX_BS)
|
alpar@1
|
518 |
{ if (k <= m)
|
alpar@1
|
519 |
lpx_get_row_bnds(lp, k, &typx, &lb, &ub);
|
alpar@1
|
520 |
else
|
alpar@1
|
521 |
lpx_get_col_bnds(lp, k-m, &typx, &lb, &ub);
|
alpar@1
|
522 |
switch (typx)
|
alpar@1
|
523 |
{ case LPX_FR:
|
alpar@1
|
524 |
tagx[k] = LPX_NF; break;
|
alpar@1
|
525 |
case LPX_LO:
|
alpar@1
|
526 |
tagx[k] = LPX_NL; break;
|
alpar@1
|
527 |
case LPX_UP:
|
alpar@1
|
528 |
tagx[k] = LPX_NU; break;
|
alpar@1
|
529 |
case LPX_DB:
|
alpar@1
|
530 |
tagx[k] =
|
alpar@1
|
531 |
(fabs(lb) <= fabs(ub) ? LPX_NL : LPX_NU);
|
alpar@1
|
532 |
break;
|
alpar@1
|
533 |
case LPX_FX:
|
alpar@1
|
534 |
tagx[k] = LPX_NS; break;
|
alpar@1
|
535 |
default:
|
alpar@1
|
536 |
xassert(typx != typx);
|
alpar@1
|
537 |
}
|
alpar@1
|
538 |
}
|
alpar@1
|
539 |
}
|
alpar@1
|
540 |
for (k = 1; k <= m+n; k++)
|
alpar@1
|
541 |
{ if (k <= m)
|
alpar@1
|
542 |
lpx_set_row_stat(lp, k, tagx[k]);
|
alpar@1
|
543 |
else
|
alpar@1
|
544 |
lpx_set_col_stat(lp, k-m, tagx[k]);
|
alpar@1
|
545 |
}
|
alpar@1
|
546 |
xfree(tagx);
|
alpar@1
|
547 |
return;
|
alpar@1
|
548 |
}
|
alpar@1
|
549 |
|
alpar@1
|
550 |
/***********************************************************************
|
alpar@1
|
551 |
* NAME
|
alpar@1
|
552 |
*
|
alpar@1
|
553 |
* glp_adv_basis - construct advanced initial LP basis
|
alpar@1
|
554 |
*
|
alpar@1
|
555 |
* SYNOPSIS
|
alpar@1
|
556 |
*
|
alpar@1
|
557 |
* void glp_adv_basis(glp_prob *lp, int flags);
|
alpar@1
|
558 |
*
|
alpar@1
|
559 |
* DESCRIPTION
|
alpar@1
|
560 |
*
|
alpar@1
|
561 |
* The routine glp_adv_basis constructs an advanced initial basis for
|
alpar@1
|
562 |
* the specified problem object.
|
alpar@1
|
563 |
*
|
alpar@1
|
564 |
* The parameter flags is reserved for use in the future and must be
|
alpar@1
|
565 |
* specified as zero. */
|
alpar@1
|
566 |
|
alpar@1
|
567 |
void glp_adv_basis(glp_prob *lp, int flags)
|
alpar@1
|
568 |
{ if (flags != 0)
|
alpar@1
|
569 |
xerror("glp_adv_basis: flags = %d; invalid flags\n", flags);
|
alpar@1
|
570 |
if (lp->m == 0 || lp->n == 0)
|
alpar@1
|
571 |
glp_std_basis(lp);
|
alpar@1
|
572 |
else
|
alpar@1
|
573 |
adv_basis(lp);
|
alpar@1
|
574 |
return;
|
alpar@1
|
575 |
}
|
alpar@1
|
576 |
|
alpar@1
|
577 |
/* eof */
|