File: mip_pri.c

package info (click to toggle)
libamplsolver 0~20190702-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,184 kB
  • sloc: ansic: 40,472; asm: 76; sh: 72; fortran: 51; makefile: 16
file content (328 lines) | stat: -rw-r--r-- 7,887 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/****************************************************************
Copyright (C) 1997, 1999, 2000 Lucent Technologies
All Rights Reserved

Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of Lucent or any of its entities
not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/

#include "nlp.h"

/* Examine $mip_priorities, looking for white-space (or comma) separated
 * name-priority pairs, with white space and/or '=' between
 * each name and value.
 * Return number of valid names in $mip_priorities and, if positive, set
 *		*startp = array of starting subscripts (offset 0)
 *		*prip = array of priorities
 *		*nump = number of variables with this priority.
 * pmax = max allowed priority.  Thus
 *
 *	if ( (n = mip_pri(&start, &num, &pri, pmax)) > 0 )
 *
 * then for i = 0, 1, ..., n-1, there are num[i] consecutive variables
 * with priority pri[i], starting with variable start[i].
 */

 static int
non_int(ASL *asl, int i)
{
	if (i < nlvb)
		return i < nlvb - nlvbi;
	if (nlvo <= nlvc) {
		if (i < nlvb + nlvo)
			return i < nlvb + nlvo - nlvoi;
		return i < nlvb + nlvc + nlvo - nlvci;
		}
	if (i < nlvb + nlvc)
		return i < nlvb + nlvc - nlvci;
	return i < nlvb + nlvc + nlvo - nlvoi;
	}

 int
mip_pri_ASL(ASL *asl, int **startp, int **nump, int **prip, fint pmax)
{
	FILE *f;
	char *s0;
	int bad, echo, eq, i, j, k, len, listsize, nn, nn1, nnames,
		nntk, nreal, nv, quit;
	int *ntk, *num0, *num1, *p, *perm,
		*pv, *pv0, *q, *start0, *start1;
	char *comma, **pn0, **pn, *ps, *s, *s1, *s2, *se;
	fint L;
	char buf[512], buf0[512], namechar[256];

	if (!(s0 = getenv("mip_priorities")))
		return 0;
	bad = nn1 = 0;
	echo = quit = 1;
	while(*s0 && *s0 <= ' ')
		s0++;
	while(*s0 == '!') {
		for(s1 = ++s0; *++s1 > ' '; );
		k = s1 - s0;
		if (k == 4) {
			if (!strncmp(s0, "quit", k))
				quit = bad = 0;
			else if (!strncmp(s0, "echo", k))
				echo = 0;
			else
				goto badc;
			}
		else {
 badc:
			printf(
			 "Bad control word \"!%.*s\" in $mip_priorities\n",
				k, s0);
			bad += quit;
			}
		for(s0 = s1; *s0 && *s0 <= ' '; s0++);
		}
	if (!*s0)
		goto done1;
	s = (char*)(quit ? "can't process" : "ignoring");
	if (!maxcolnamelen) {
		printf("No .col file; %s $mip_priorities.\n%s", s,
			"To get a .col file, in your AMPL session say\n");
		if ((s = getenv("solver")))
			printf("\toption %s_auxfiles c;\n%s", s,
				"before saying\n\tsolve;\nor\n");
		printf("\toption auxfiles c;\nbefore saying\n\twrite ...\n");
		goto bailout;
		}
	strcpy(stub_end, ".col");
	if (!(f = fopen(filename, "r"))) {
		printf("Can't open %s; %s $mip_priorities.\n", filename, s);
 bailout:
		bad += quit;
		need_nl = 0;
		goto done1;
		}
	/* Guess there will only be a small number of names mentioned	*/
	/* in $mip_priorities; first we'll find the names, then we'll	*/
	/* match each new name in the .col file against all the		*/
	/* $mip_priorities names.	*/

	len = nnames = 0;
	need_nl = 0;
	s = se = s0;
	for(;;) {
		eq = 0;
		for(se = s; *s > ' '; s++)
			if (*s == '=') {
				eq = 1;
				break;
				}
		s1 = s;
		if (eq)
			s++;
		while(*s <= ' ')
			if (!*s++) {
 missing:
				printf("Missing priority for %.*s.\n",
					s1-se, se);
				bad += quit;
				goto counted;
				}
		if (*s == '=' && !eq) {
			while(*++s <= ' ')
				if (!*s)
					goto missing;
			}
		comma = 0;
		L = strtol(s2 = s, &s, 10);
		if (s == s2 || L <= 0 || L > pmax)
			goto badnum;
		if (*s > ' ') {
		    if (*s == ',')
			comma = s++;
		    else {
 badnum:
			bad += quit;
			while(*s > ' ' && *s != ',')
				s++;
			printf("\"%.*s\" is not a positive integer <= %ld:\n",
				s-s2, s2, (long)pmax);
			printf("\tbad $mip_priorities entry for %.*s.\n",
				s1-se, se);
			while(*s <= ' ' && *s)
				s++;
			if (*s)
				printf("Ignoring further entries in $mip_priorities.\n");
			goto counted;
			}
		    }
		nnames++;
		len += s1 - se;
		se = s;
 skipwhite:
		while(*s <= ' ')
			if (!*s++)
				goto counted;
		if (*s == ',' && !comma) {
			comma = s++;
			goto skipwhite;
			}
		}
 counted:
	if (!nnames)
		goto done1;
	start0 = start1 = (int *)Malloc(nnames*(3*sizeof(int)));
	num0 = num1 = start0 + nnames;
	pv = pv0 = num0 + nnames;
	nv = n_var;

	len += nv*sizeof(int);
	pn = pn0 = (char **)Malloc(nnames*(sizeof(char*)+sizeof(int)+1) + len);
	p = perm = (int *)(pn0 + nnames);
	q = perm + nnames;
	ps = (char *)(q + nv);
	i = 0;
	for(s = s0; s < se; ) {
		*start1++ = *num1++ = -1;
		*p++ = i++;
		for(*pn++ = ps; (*ps = *s) > ' '; ps++, s++)
			if (*s == '=') {
				++s;
				break;
				}
		*ps++ = 0;
		while(*s <= ' ')
			s++;
		if (*s == '=')
			while(*++s <= ' ');
		*pv++ = (int)strtol(s, &s, 10);
		comma = 0;
 whiteskip:
		while(*s <= ' ')
			if (s++ >= se)
				goto scanned;
		if (*s == ',' && !comma) {
			comma = s++;
			goto whiteskip;
			}
		}
 scanned:
	*buf0 = 0;
	ntk = 0;
	memset(namechar, 1, sizeof(namechar));
	namechar['\n'] = namechar['['/*]*/] = namechar[0] = 0;
	nn = nntk = nnames;
	listsize = 0;
	nreal = n_var - (nbv + niv);
	for(i = j = 0; fgets(buf, sizeof(buf), f); i++) {
		for(s = buf; namechar[*(unsigned char *)s]; s++);
		if (s == buf) {
			printf("Bad .row file %s\n", filename);
			bad += quit;
			goto done;
			}
		*s = 0;
		if (strcmp(buf0, buf)) {
			strcpy(buf0, buf);
			if (ntk) {
				listsize += *ntk = i - j;
				ntk = 0;
				--nntk;
				}
			for(j = 0; j < nn; j++) {
				k = perm[j];
				if (!strcmp(pn0[k],buf)) {
					if (j < --nn) {
						perm[j] = perm[nn];
						perm[nn] = k;
						}
					start0[k] = i;
					ntk = num0 + k;
					/* no break: in case of duplicates, */
					/* let last one win. */
					}
				}
			if (ntk && i < nreal && non_int(asl,i)) {
				printf(
		"%s$mip_priority entry for noninteger variable %s.\n",
					quit ? "" : "Ignoring ", buf0);
				*ntk = -2;
				ntk = 0;
				}
			j = i;
			}
		}
	if (ntk) {
		listsize += *ntk = i - j;
		--nntk;
		}
	if (nn) {
		/* report bad names in $mip_priorities */
		for(j = 0; j < nn; j++) {
			k = perm[j];
			printf(
			"Ignoring $mip_priorities pair \"%s %d\" -- bad name.\n",
				pn0[k], pv0[k]);
			bad += quit;
			}
		}
	if (nntk > nn) { /* Is this necessary? */
		/* report duplicates */
		for(j = nn; j < nnames; j++) {
			k = perm[j];
			if (num0[k] == -1) {
				bad += quit;
				printf(
				"%suplicate $mip_priorities pair \"%s %d\".\n",
					quit? "D" : "Ignoring d",
					pn0[k], pv0[k]);
				}
			}
		}
	if (!listsize)
		goto done;
	if (echo)
		printf(bad ? "Valid $mip_priorities seen:\n"
				: "$mip_priorities used:\n");
	nn = 0;
	if ((p = asl->i.vmap)) {
		for(i = nv; --i >= 0; )
			q[i] = -1;
		for(i = 0; i < nv; ++i)
			if ((j = p[i]) >= 0 && j < nv)
				q[j] = i;
		}
	do if (num0[nn] > 0 && (!p || q[start0[nn]] >= 0)) {
		if (echo)
			printf("\t%s\t%d\n", pn0[nn], pv0[nn1] = pv0[nn]);
		pv0[nn1] = pv0[nn];
		start0[nn1] = start0[nn];
		num0[nn1++] = num0[nn];
		}
		while(++nn < nnames);
 done:
	fclose(f);
	free(pn0);
	if (nn1 && !bad) {
		M1record(*startp = start0);
		*nump = num0;
		*prip = pv0;
		}
	else
		free(start0);
 done1:
	if (bad)
		exit(2);
	return nn1;
	}