File: readsol.c

package info (click to toggle)
libamplsolver 0~20190702-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,156 kB
  • sloc: ansic: 40,471; asm: 76; sh: 72; fortran: 51; makefile: 16
file content (361 lines) | stat: -rw-r--r-- 8,240 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*******************************************************************
Copyright (C) 2017 AMPL Optimization, Inc.; written by David M. Gay.

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.

The author and AMPL Optimization, Inc. disclaim all warranties with
regard to this software, including all implied warranties of
merchantability and fitness.  In no event shall the author 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"

#define MSGGULP 1024

 typedef struct
msginfo {
	char *msg;
	char *msg0;
	char *msgend;
	ftnlen msglen;
	} msginfo;

 static void
msgput(msginfo *m, const char *b, int n)
{
	char *msg, *msg0, *msgend;
	const char *be;
	ftnlen msglen0;

	msg = m->msg;
	msgend = m->msgend;
	be = b + n;
	while(b < be) {
		if (msg >= msgend) {
			msglen0 = m->msglen;
			msg0 = m->msg0 = (char*)
				Realloc(m->msg0, m->msglen += MSGGULP);
			msg = msg0 + msglen0;
			m->msgend = msg0 + m->msglen;
			}
		*msg++ = *b++;
		}
	m->msg = msg;
	}

 static void
badnumber(ASL *asl, fint a, fint b, const char *fname, const char *what)
{
	fprintf(Stderr, "%s indicates %ld rather than %ld %s\n",
		fname, (long)a, (long)b, what);
	fflush(Stderr);
	}

 static int
decstring(char *buf, real *val)
{
	char *be;
	int c;

	*val = strtod(buf, &be);
	return be <= buf || (((c = be[-1]) < '0' || c > '9') && c != '.');
	}

 char *
fread_sol_ASL(ASL *asl, const char *fname, real **xp, real **yp)
{
	int binary, flag1, i, j, je, n, need_vbtol;
	FILE *f;
	char buf[512], *s, *se;
	real vbtol, *y;
	ftnlen L, L1, L2;
	fint Objno[2], Options[14], nOpts, *z;
	msginfo mi;

	if (!asl || asl->i.ASLtype < 1 || asl->i.ASLtype > 5)
		badasl_ASL(asl,0,"fread_soln");
	f = fopen(fname, "rb");
	if (!f) {
		fprintf(Stderr, "Can't open %s\n", fname);
		fflush(Stderr);
		return 0;
		}
	if (fread(&L, sizeof(ftnlen), 1, f) && L == 6) {
		/* binary files may be written by Fortran unformatted writes */
		binary = 1;
		if (!fread(buf, 6, 1, f)
		 || strncmp(buf,"binary",6)
		 || !fread(&L, sizeof(ftnlen), 1, f)
		 || L != 6) {
 badbinary:
			fprintf(Stderr, "bad binary file %s\n", fname);
			fflush(Stderr);
			goto done;
			}
		}
	else {
		binary = 0;
		rewind(f);
		}

	L1 = 0;
	z = 0;

	/* Read termination msg */
	nOpts = i = need_vbtol = 0;
	mi.msg = mi.msg0 = (char *)Malloc(mi.msglen = MSGGULP);
	mi.msgend = mi.msg0 + MSGGULP;
	if (binary) {
		for(;; i++) {
			if (!fread(&L,sizeof(ftnlen),1,f))
				goto early_eof;
			if ((L1 = L)) {
				do {
					n = L < sizeof(buf) ? (int)L : (int)sizeof(buf);
					L -= n;
					if (!fread(buf, n, 1, f))
						goto early_eof;
					if (!L) {
						while(--n >= 0 && buf[n] == ' ');
						n++;
						}
					msgput(&mi, buf, n);
					}
					while(L);
				msgput(&mi, "\n", 1);
				}
			if (!fread(&L, sizeof(ftnlen), 1, f))
				goto early_eof;
			if (L != L1)
				goto badbinary;
			if (!L)
				break;
			}
		L1 = n_con * sizeof(real);
		if (!fread(&L, sizeof(ftnlen), 1, f))
			goto badbinary;
		if (L >= 3*sizeof(fint) + 7) {
			/* check for Options */
			if (!fread(buf, 7, 1, f))
				goto badbinary;
			if (strncmp(buf, "Options", 7))
				goto badbinary;
			if (!fread(&Options, sizeof(fint), 3, f))
				goto badbinary;
			nOpts = Options[0];
			if (nOpts < 3 || nOpts > 9) {
 bad_nOpts:
				fprintf(Stderr,
				"expected nOpts between 3 and 9; got %ld: ",
					(long)nOpts);
				goto badbinary;
				}
			if (Options[2] == 3) {
				nOpts -= 2;
				need_vbtol = 1;
				}
			L2 = (nOpts + 5)*sizeof(fint) + 7;
			if (need_vbtol)
				L2 += sizeof(real);
			if (L != L2)
				goto badbinary;
			if (!fread(Options+3, sizeof(fint), nOpts + 2, f))
				goto badbinary;
			if (need_vbtol
			 && !fread(&vbtol, sizeof(real), 1, f))
				goto badbinary;
			if (!fread(&L2, sizeof(ftnlen), 1, f)
				|| L != L2)
				goto badbinary;
			}
		else if (L != L1)
			goto badbinary;
		}
	else {
		for(;; i++) {
			if (!fgets(buf, sizeof(buf), f)) {
 early_eof:
				fprintf(Stderr,
					"early end of file reading %s\n", fname);
				fflush(Stderr);
 done:
				fclose(f);
				return 0;
				}
			if (*buf == '\n' || (*buf == '\r' && buf[1] == '\n'))
				break;
			msgput(&mi, buf, strlen(buf));
			}
		while((j = getc(f)) == '\n');
		if (j != 'O')
			ungetc(j,f);
		else {
			if (!fgets(buf, sizeof(buf), f))
				goto early_eof;
			if (!strncmp(buf, "ptions", 6)) {
				for(j = 0; j <3; j++) {
					if (!fgets(buf, sizeof(buf), f))
						goto early_eof;
					Options[j] = strtol(buf,&se,10);
					if (se == buf)
						goto badline;
					}
				nOpts = Options[0];
				if (nOpts < 3 || nOpts > 9)
					goto bad_nOpts;
				if (Options[2] == 3) {
					nOpts -= 2;
					need_vbtol = 1;
					}
				je = (int)nOpts + 4;
				for(j = 3; j <= je; j++) {
					if (!fgets(buf, sizeof(buf), f))
						goto early_eof;
					Options[j] = strtol(buf,&se,10);
					if (se == buf)
						goto badline;
					}
				if (need_vbtol && !fgets(buf, sizeof(buf), f))
					goto early_eof;
				/* We don't do anything here with vbtol, */
				/* so we don't bother converting it. */
				}
			}
		}
	memcpy(ampl_options, Options, (nOpts+1)*sizeof(fint));
	msgput(&mi, "", 1);	/* add null to end */

	if (i)
		fflush(stdout);

	if (nOpts) {
		z = Options + nOpts + 1;
		j = (int)z[3];
		if (j > n_var || j < 0) {
			badnumber(asl, j, n_var, fname, "variables");
			goto done;
			}
		j = (int)z[1];
		if (j > n_con || j < 0) {
			badnumber(asl, j, n_con, fname, "constraints");
			goto done;
			}
		if (binary) {
			L1 = j * sizeof(real);
			if (!fread(&L, sizeof(ftnlen), 1, f))
				goto badbinary;
			if (L != L1)
				goto badbinary;
			}
		}
	else
		j = n_con;
	if (!j) {
		*yp = 0;
		goto get_x;
		}
	y = *yp = (real *)Malloc(n_con * sizeof(real));
	if (binary) {
		if (fread(y, sizeof(real), j, f) != j)
			goto early_eof;
		if (!fread(&L, sizeof(ftnlen), 1, f) || L != L1)
			goto badbinary;
		y += j;
		}
	else for(i = 0; i < j; i++) {
		if (!fgets(buf, sizeof(buf), f))
			goto early_eof;
		if (!decstring(buf, y++))
			continue;
 badline:
		fprintf(Stderr, "bad line in %s: %s", fname, buf);
		fflush(Stderr);
		goto done;
		}
	y = *yp;
	while(j < n_con)
		y[j++] = 0;
 get_x:
	Objno[0] = 0;
	Objno[1] = -1;
	flag1 = asl->i.flags & ~1;;
	if (!(j = nOpts ? (int)z[3] : n_var)) {
		*xp = 0;
		goto ret;
		}
	y = *xp = (real *)Malloc(n_var*sizeof(real));

	if (binary) {
		L1 = j * sizeof(real);
		if (!fread(&L, sizeof(ftnlen), 1, f) || L != L1)
			goto badbinary;
		if (fread(y, sizeof(real), j, f) != j)
			goto early_eof;
		y += j;
		/* do we have an obj_no ? */
		if (!fread(&L, sizeof(ftnlen), 1, f) || L != L1)
			goto badbinary;
		if (fread(&L, sizeof(ftnlen), 1, f)) {
			i = 1;
			if (L == 2*sizeof(fint)) {
				i = 2;
				flag1 |= 1;
				}
			else if (L != sizeof(fint))
				goto badbinary;
			if (!fread(Objno, i*sizeof(fint), 1, f))
				goto badbinary;
			}
		}
	else {
		for(i = j; i > 0; i--) {
			if (!fgets(buf, sizeof(buf), f))
				goto early_eof;
			if (decstring(buf, y++))
				goto badline;
			}
		if (fgets(buf,sizeof(buf), f)) {
			if (strncmp(buf,"objno ",6)) {
 extra_line:
				fprintf(Stderr, "Bug: extra line in %s:\n%s",
					fname, buf);
				fflush(Stderr);
				}
			else {
				Objno[0] = strtol(buf+6, &se, 10);
				if (se == buf+6 || *se > ' ')
					goto extra_line;
				if (*se == ' ') {
					Objno[1] = strtol(se,&s,10);
					if (s == se || *s > ' ')
						goto extra_line;
					flag1 |= 1;
					}
				}
			}
		}
	asl->i.flags = flag1;
	obj_no = (int)Objno[0];
	solve_result_num = (int)Objno[1];
	y = *xp;
	while(j < n_var)
		y[j++] = 0;
 ret:
	fclose(f);
	return (char*)Realloc(mi.msg0, mi.msglen);
	}

 char *
read_sol_ASL(ASL *asl, real **xp, real **yp)
{
	strcpy(stub_end, ".sol");
	return fread_sol_ASL(asl, filename, xp, yp);
	}