File: solver.c

package info (click to toggle)
qsopt-ex 2.5.10.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,152 kB
  • sloc: ansic: 43,343; sh: 427; makefile: 212
file content (356 lines) | stat: -rw-r--r-- 8,255 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
/****************************************************************************/
/*                                                                          */
/*  This file is part of QSopt_ex.                                          */
/*                                                                          */
/*  (c) Copyright 2006 by David Applegate, William Cook, Sanjeeb Dash,      */
/*  and Daniel Espinoza                                                     */
/*                                                                          */
/*  Sanjeeb Dash ownership of copyright in QSopt_ex is derived from his     */
/*  copyright in QSopt.                                                     */
/*                                                                          */
/*  This code may be used under the terms of the GNU General Public License */
/*  (Version 2.1 or later) as published by the Free Software Foundation.    */
/*                                                                          */
/*  Alternatively, use is granted for research purposes only.               */
/*                                                                          */
/*  It is your choice of which of these two licenses you are operating      */
/*  under.                                                                  */
/*                                                                          */
/*  We make no guarantees about the correctness or usefulness of this code. */
/*                                                                          */
/****************************************************************************/

/* RCS_INFO = "$RCSfile: solver.c,v $ $Revision: 1.2 $ $Date: 2003/11/05 16:49:52 $"; */

#include "qs_config.h"
#include "exact.h"
#include "solver.h"
#include "iqsutil.h"
#include "util.h"
#include "lpdefs.h"							/* for PRIMAL_SIMPLEX */
#include "qstruct.h"
#include "qsopt.h"
#include "binary.h"
#include "editor.h"
#include "price.h"
#include "lib.h"								/* for ILLmip_binary_dfs */

static char *fname = 0;
static int lpfile = 0;
static int solvemip = 0;
static int interactive = 0;
static int usescaling = 1;
static int showversion = 0;
static int simplexalgo = PRIMAL_SIMPLEX;
static int pstrategy = QS_PRICE_PSTEEP;
static int dstrategy = QS_PRICE_DSTEEP;
static unsigned precision = 128;
static int printsol = 0;
static char *readbasis = 0;
static char *writebasis = 0;

static void usage (
	char *s),
  get_ftype (
	char *name,
	int *ftype);

#ifdef TEST_FEEDBACK
static int feedback (
	FILE * dest,
	const char *str);
#endif
static int parseargs (
	int ac,
	char **av);

#ifndef WIN32
QSLIB_INTERFACE int main ( int ac, char **av);
int main ( int ac, char **av)
{
	int rval;
	QSopt_ex_version();
	QSexactStart();
	rval = solver_main (ac, av);
	QSexactClear();
	return rval;
}
#endif

QSLIB_INTERFACE int solver_main (
	int ac,
	char **av)
{
	int rval = 0;
	QSdata *p = 0;
	ILLutil_timer timer_solve;
	ILLutil_timer timer_read;
	int ftype = 0;								/* 0 mps, 1 lp */
	EGlpNum_t *x = 0;
	EGlpNum_t val;
	double szeit;

	rval = parseargs (ac, av);
	if (rval)
		ILL_CLEANUP;

	QSset_precision (precision);
	EGlpNumInitVar (val);
	if (showversion)
	{
		char *buf = 0;

		buf = QSversion ();
		if (buf == 0)
		{
			ILL_CLEANUP;
		}
		else
		{
			printf ("%s\n", buf);
			QSfree ((void *) buf);
		}
	}

	if (lpfile)
	{
		ftype = 1;
	}
	else
	{
		get_ftype (fname, &ftype);
	}

	ILLutil_init_timer (&timer_read, "SOLVER_READ");
	ILLutil_start_timer (&timer_read);
	if (ftype == 1)
	{
		p = QSread_prob ((const char *) fname, "LP");
		if (p == 0)
		{
			fprintf (stderr, "Could not read lp file.\n");
			rval = 1;
			ILL_CLEANUP_IF (rval);
		}
	}
	else
	{
		p = QSread_prob ((const char *) fname, "MPS");
		if (p == 0)
		{
			fprintf (stderr, "Could not read mps file.\n");
			rval = 1;
			ILL_CLEANUP_IF (rval);
		}
	}

	if (readbasis)
	{
		rval = QSread_and_load_basis (p, (const char *) readbasis);
		ILL_CLEANUP_IF (rval);
	}
	ILLutil_stop_timer (&timer_read, 1);
	rval = QSset_param (p, QS_PARAM_SIMPLEX_DISPLAY, 1)
		|| QSset_param (p, QS_PARAM_PRIMAL_PRICING, pstrategy)
		|| QSset_param (p, QS_PARAM_DUAL_PRICING, dstrategy)
		|| QSset_param (p, QS_PARAM_SIMPLEX_SCALING, usescaling);
	ILL_CLEANUP_IF (rval);

	if (interactive)
	{
		ILLeditor_init ();
		ILLeditor (p);
		goto CLEANUP;
	}

	szeit = ILLutil_zeit();
	ILLutil_init_timer (&timer_solve, "SOLVER");
	ILLutil_start_timer (&timer_solve);

#ifdef TEST_FEEDBACK
	QSset_reporter (p, (void *) feedback, stdout);
#endif
	rval = ILLeditor_solve (p, simplexalgo);
	ILLutil_stop_timer (&timer_solve, 1);
	ILL_CLEANUP_IF (rval);

	printf ("ZZ %s %.2f\n", p->lp->O->probname, ILLutil_zeit () - szeit);
 	fflush (stdout);

	if (printsol)
		x = EGlpNumAllocArray (p->lp->O->nstruct);

	if (solvemip)
	{
		rval = ILLmip_bfs (p->lp, &val, x, &(p->itcnt));
		ILL_CLEANUP_IF (rval);
		printf ("MIP Objective Value: %.6f\n", EGlpNumToLf (val));
		fflush (stdout);
		if (printsol && EGlpNumIsNeqq (val, ILL_MAXDOUBLE) &&
				EGlpNumIsNeqq (val, ILL_MINDOUBLE))
		{
			EGioFile_t*out = EGioOpenFILE(stdout);
			rval = ILLlib_print_x (out, p->lp, 0, x, 1);
			EGioClose(out);
			ILL_CLEANUP_IF (rval);
		}
	}
	else
	{
		if (writebasis)
		{
			rval = QSwrite_basis (p, 0, writebasis);
			ILL_CLEANUP_IF (rval);
		}
		if (printsol)
		{
			EGioFile_t*out = EGioOpenFILE(stdout);
			rval = ILLlib_print_x (out, p->lp, 0, 0, 1);
			EGioClose(out);
			ILL_CLEANUP_IF (rval);
		}
	}

CLEANUP:
	EGlpNumFreeArray (x);
	QSfree_prob (p);
	EGlpNumClearVar (val);
	return rval;									/* main return */
}

static void usage (
	char *s)
{
	char *buf = 0;

	buf = QSversion ();
	if (buf)
	{
		fprintf (stderr, "%s\n", buf);
		QSfree ((void *) buf);
	}

	fprintf (stderr, "Usage: %s [- below -] prob_file\n", s);
	fprintf (stderr, "   -b f  write basis to file f\n");
	fprintf (stderr, "   -B f  read initial basis from file f\n");
#if 0
	fprintf (stderr, "   -I    solve the MIP using BestBound\n");
	fprintf (stderr, "   -E    edit problem after solving initial version\n");
#endif
	fprintf (stderr, "   -L    input file is in lp format (default: mps)\n");
	fprintf (stderr, "   -O    print the final solution\n");
	fprintf (stderr, "   -p #  run primal simplex with pricing rule #\n");
	fprintf (stderr,
					 "         (%d-Dantzig, %d-Devex, %d-Steep (default), %d-Partial\n",
					 QS_PRICE_PDANTZIG, QS_PRICE_PDEVEX, QS_PRICE_PSTEEP,
					 QS_PRICE_PMULTPARTIAL);
	fprintf (stderr, "   -d #  run dual simplex with pricing rule #\n");
	fprintf (stderr, "         (%d-Dantzig, %d-Steep, %d-Partial, %d-Devex)\n",
					 QS_PRICE_DDANTZIG, QS_PRICE_DSTEEP, QS_PRICE_DMULTPARTIAL,
					 QS_PRICE_DDEVEX);
	fprintf (stderr, "   -S    do NOT scale the initial LP\n");
	fprintf (stderr, "   -v    print QSopt version number\n");
}

static int parseargs (
	int ac,
	char **av)
{
	int c;
	int boptind = 1;
	char *boptarg = 0;

	while ((c =
					ILLutil_bix_getopt (ac, av, "b:B:d:p:P:IELOSv", &boptind,
															&boptarg)) != EOF)
		switch (c)
		{
		case 'b':
			writebasis = boptarg;
			break;
		case 'B':
			readbasis = boptarg;
			break;
		case 'P':
			precision = atoi (boptarg);
			break;
		case 'd':
			simplexalgo = DUAL_SIMPLEX;
			dstrategy = atoi (boptarg);
			break;
#if 0
		case 'E':
			interactive = 1;
			break;
		case 'I':
			solvemip = 1;
			break;
#endif
		case 'L':
			lpfile = 1;
			break;
		case 'O':
			printsol = 1;
			break;
		case 'p':
			simplexalgo = PRIMAL_SIMPLEX;
			pstrategy = atoi (boptarg);
			break;
		case 'S':
			usescaling = 0;
			break;
		case 'v':
			showversion = 1;
			break;
		case '?':
		default:
			usage (av[0]);
			return 1;
		}
	if (boptind != (ac - 1))
	{
		usage (av[0]);
		return 1;
	}

	fname = av[boptind++];
	fprintf (stderr, "Reading problem from %s\n", fname);
	return 0;
}

static void get_ftype (
	char *name,
	int *ftype)
{
	char *q;

	q = strrchr (name, '.');
	if (q)
	{
		q++;
		if (!strcmp (q, "lp") || !strcmp (q, "LP"))
		{
			*ftype = 1;
		}
		else
		{
			*ftype = 0;
		}
	}
}

#ifdef TEST_FEEDBACK
static int feedback (
	FILE * dest,
	const char *str)
{
	if (str != NULL)
	{
		int rc = fprintf ((FILE *) dest, "FEEDBACK: %s", str);

		fflush (dest);
		return rc;
	}
	return 0;
}
#endif