File: parameters.c

package info (click to toggle)
pcx 1.1.18-2
  • links: PTS
  • area: non-free
  • in suites: lenny, squeeze
  • size: 2,644 kB
  • ctags: 1,980
  • sloc: ansic: 20,454; fortran: 2,974; makefile: 367; sh: 150; csh: 48
file content (595 lines) | stat: -rw-r--r-- 16,480 bytes parent folder | download | duplicates (2)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* allocate parameter data structure, assign defaults, read
 * specificatons file
 *
 * PCx 1.1 11/97
 *
 * Authors: Joe Czyzyk, Sanjay Mehrotra, Michael Wagner, Steve Wright.
 * 
 * (C) 1996 University of Chicago. See COPYRIGHT in main directory.
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "main.h"
#include "memory.h"
#include "string.h"


/*=====================================================================*/
/* Define strcasecmp for Windows 32 (not supported to the best of my   */
/* knowledge (GDI 11/08/97)                                            */
/*                                                                     */
/* Added by: G. D. Irisarri                                            */
/*           Open Access Technology International                      */
/*           oatigi@winternet.com                                      */
/*                                                                     */
/*           November 1997                                             */
/*=====================================================================*/

#ifdef WIN32

static int strcasecmp(const char *, const char *);

int
strcasecmp(const char *s, const char *t)
{
	for ( ; tolower(*s) == tolower(*t); s++, t++)
		if (*s == '\0')
			return 0;
	return *s - *t;
}

#endif


/***************************************************************************/
/*   Contents:                                                             */
/***************************************************************************/
/*
Parameters  *NewParameters();

void	     FreeParameters(Parameters *Inputs);

int          CheckParameters(Parameters *ptr);

int          ParseSpecsFile(Parameters *parameters, char *infile);
*/
/***************************************************************************/

#define SAME 0

int             LARGEST_INT4 = 1073741824, 
                FLAG_OPERATION_COUNT = 1, 
                DEFAULT_MINIMUM_DEGREE_GRAPH_FACTOR = 3, 
                DEFAULT_MINIMUM_DEGREE_CHOLESKY_FACTOR = 5;

double          DEFAULT_CHOLESKY_PIVOT_TOLERANCE = 1.0e-14, 
                DEFAULT_1x1_PIVOT_TOLERANCE = 1.0 / (16.0 * 16.0 * 16.0 * 
						     16.0 * 16.0 * 16.0 * 
						     16.0 * 16.0 * 16.0 * 
						     16.0);

/***************************************************************************/

/* Allocate space for parameter array and set defaults */

Parameters *
NewParameters()
{
   Parameters     *ptr;
   
   ptr = (Parameters *) Malloc(sizeof(Parameters), "NewParameters");
   
   ptr->IterationLimit = 100;
   ptr->OptTol         = 1.0e-10;
   ptr->PriFeasTol     = 1.0e-8;
   ptr->DualFeasTol    = 1.0e-8;
   ptr->AlphaScale     = 0.9;
   ptr->Diagnostics    = 1;	
   ptr->ReportingLevel = 2;	
   ptr->Refinement     = NO;	/* don't do iterative refinement */
   ptr->Preprocessing  = YES;	/* do preprocessing */
   ptr->Scaling        = YES;   /* do scaling */
   ptr->HOCorrections  = YES;   /* Use Gondzio higher-order correctors */
   ptr->MaxCorrections = 0;     /* Code decides maximum number of Gondzio 
				   corrections */
   ptr->Minimize       = YES;	/* minimize, don't maximize */
   ptr->InputDirectory = NULL;	/* there's not particular place to look for
				 * the MPS input files */
   ptr->WriteSolution  = YES;	/* yes, write the solution to the file
				 * probname.out */
   ptr->WriteHistory   = YES;	/* no, don't write any history info to the
				 * file probname.log */
   ptr->ObjectiveName  = NULL;
   ptr->RHSName        = NULL;
   ptr->RangeName      = NULL;
   ptr->BoundName      = NULL;
   
   ptr->CacheSize      = 16;
   ptr->UnrollingLevel = 4;
   
   ptr->CenterExponent = 3.0;
   
   return ptr;
}


/***************************************************************************/

void		
FreeParameters(Inputs)
     Parameters		*Inputs;
{
   Free ((char*) Inputs->InputDirectory);
   Free ((char*) Inputs->RHSName);
   Free ((char*) Inputs->ObjectiveName);
   Free ((char*) Inputs->RangeName);
   Free ((char*) Inputs->BoundName);
   Free ((char*) Inputs);
}

/***************************************************************************/


/* check parameter list for outrageous values */

int             
CheckParameters(ptr)
     Parameters     *ptr;
{
   int             temp;
   
   temp = 0;
   
   if (ptr->IterationLimit <= 0 || ptr->IterationLimit > 500) 
      {
	 printf(" PARAMETER ERROR: Out-of-Range IterationLimit: %d\n",
		ptr->IterationLimit);
	 temp += 1;
      }
   if (ptr->OptTol <= 1.e-12 || ptr->OptTol > 1) 
      {
	 printf(" PARAMETER ERROR: Out-of-Range OptTol: %12.4e\n", 
		ptr->OptTol);
	 temp += 1;
      }
   if (ptr->PriFeasTol <= 1.e-12 || ptr->PriFeasTol > 1.e-1) 
      {
	 printf(" PARAMETER ERROR: Out-of-Range PriFeasTol: %12.4e\n", 
		ptr->PriFeasTol);
	 temp += 1;
      }
  if (ptr->DualFeasTol <= 1.e-12 || ptr->DualFeasTol > 1.e-1) 
     {
	printf(" PARAMETER ERROR: Out-of-Range DualFeasTol: %12.4e\n", 
	       ptr->DualFeasTol);
	temp += 1;
     }
  if (ptr->AlphaScale <= 0.0 || ptr->AlphaScale > 0.9999999) 
     {
	printf(" PARAMETER ERROR: Out-of-Range AlphaScale: %f\n",
	       ptr->AlphaScale);
	temp += 1;
     }
  if (ptr->UnrollingLevel != 1 && ptr->UnrollingLevel != 2 &&
      ptr->UnrollingLevel != 4 && ptr->UnrollingLevel != 8) 
     {
	printf(" PARAMETER ERROR: Out-of-Range UnrollingLevel: %d\n", 
	       ptr->UnrollingLevel);
	printf("   Choose 1, 2, 4, or 8\n");
	temp += 1;
     }
  if (ptr->CacheSize < 0 || ptr->CacheSize > 2048) 
     {
	printf(" PARAMETER ERROR: Out-of-Range CacheSize: %d\n", 
	       ptr->CacheSize);
	printf("   Choose CacheSize in range (0 - 2048)\n");
	temp += 1;
     }

  if (ptr->CenterExponent < 1.0 || ptr->CenterExponent > 4.0) 
     {
	printf(" PARAMETER ERROR: Out-of-Range CenterExponent: %f\n",
	       ptr->CenterExponent);
	printf("   Choose CenterExponent in range (1.0 - 4.0)\n");
	temp += 1;
     }
  
  if (ptr->MaxCorrections < 0 || ptr->MaxCorrections > 10) 
     {
	printf(" PARAMETER ERROR: Out-of-Range MaxCorrections: %d\n",
	       ptr->MaxCorrections);
	printf("   Choose MaxCorrections in range (0 - 10)\n");
	temp += 1;
     }
  
  return temp;
}

/***************************************************************************/

int 
ParseSpecsFile(parameters, infile)
     Parameters     *parameters;
     char           *infile;
{
   int             match, key;
   char            line[200], rootfilename[200], filename[200];
   char           *first, *second;
   FILE           *fp;
   char           *basename, *suffix; 
   int             len;
   
   int             NumKeys = 23;
   static char    *KeyWord[] = {"max", "min", "solution", 
				"objectivename", "rhsname", "rangename",
				"boundname", "history", "presolve", 
				"preprocess", "inputdirectory", "opttol", 
				"prifeastol", "dualfeastol", "cachesize", 
				"unrollinglevel", "iterationlimit", 
				"centerexp", "refinement", "stepfactor", 
				"scaling", "hocorrections", 
				"maxcorrections"};
   
   /* Try finding specs file under some different names. Give priority to
    * filenames that include this particular problem name */
   
   /* get prefix of file names by stripping off leading path and trailing
    * .mps */
   
   basename = strrchr(infile, '/');
   if (basename == NULL)
      basename = infile;
   else
      basename++;
   
   suffix = strstr(basename, ".mps");
   if (suffix != NULL) 
      {
	 len = strlen(basename) - strlen(suffix);
	 strncpy(rootfilename, basename, len);
	 rootfilename[len] = '\0';
	 strcpy(basename, rootfilename);
      } 
   else 
      strcpy(rootfilename, basename);
   
   strcpy(filename, rootfilename);
   strcat(filename, ".spc");
   fp = fopen(filename, "r");
   if (fp == NULL) 
      {
	 strcpy(filename, rootfilename);
	 strcat(filename, ".specs");
	 fp = fopen(filename, "r");
      }
   if (fp == NULL) 
      {
	 strcpy(filename, "spc");
	 fp = fopen(filename, "r");
      }
   if (fp == NULL) 
      {
	 strcpy(filename, "specs");
	 fp = fopen(filename, "r");
      }
   if (fp == NULL) 
      {
	 strcpy(filename, "PCx.specs");
	 fp = fopen(filename, "r");
      }
/************** have to include this part for the sake of the DOS version ***/
   if (fp == NULL) 
      {
	 strcpy(filename, "PCx.spc");
	 fp = fopen(filename, "r");
      }
/****************************************************************************/

   /* give up at this point */
   if (fp == NULL)
    return 0;
   
   printf("Reading problem specs from file '%s'\n", filename);
   
   /* process lines in file */
   
   do 
      {
	 fgets(line, 200, fp);
      } while (line[0] == '#' && !feof(fp));
   
   while (!feof(fp)) 
      {
	 first = strtok(line, " \t\n");
	 second = strtok(NULL, " \t\n");
	 
	 if (first == NULL)
	    /* encountered blank line */
	    match = -2;
	 else {
	    for (key = 0, match = -1; key < NumKeys; key++) 
	       {
		  if (strcasecmp(KeyWord[key], first) == SAME) 
		     {
			match = key;
			break;
		     }
	       }
	 }
	 
	 switch (match) 
	    {
	    case -2:
	       break;
	    case -1:
	       printf("Unknown keyword '%s' in specs file.\n", first);
	       printf("Ignoring.\n");
	       break;
	    case 0:			/* max */
	       parameters->Minimize = NO;
	       printf("  Maximizing the objective.\n");
	       break;
	    case 1:			/* min */
	       parameters->Minimize = YES;
	       printf("  Minimizing the objective.\n");
	       break;
	    case 2:			/* write solution */
	       if (second == NULL) 
		  {
		     parameters->WriteSolution = YES;
		     printf("  Writing solution file.\n");
		  } 
	       else if ((strcasecmp("no", second) == SAME) ||
			(strcasecmp("n", second) == SAME)) 
		  {
		     parameters->WriteSolution = NO;
		     printf("  Not writing solution file.\n");
		  } 
	       else 
		  {
		     parameters->WriteSolution = YES;
		     printf("  Writing solution file.\n");
		  }
	       break;
	    case 3:			/* objectivename */
	       parameters->ObjectiveName = StrDup(second, "ObjectiveName");
	       printf("  Using objective '%s'.\n", second);
	       break;
	    case 4:			/* rhsname */
	       parameters->RHSName = StrDup(second, "RHSName");
	       printf("  Using RHS '%s'.\n", second);
	       break;
	    case 5:			/* rangename */
	       parameters->RangeName = StrDup(second, "RangeName");
	       printf("  Using Range '%s'.\n", second);
	       break;
	    case 6:			/* boundname */
	       parameters->BoundName = StrDup(second, "BoundName");
	       printf("  Using Bound '%s'.\n", second);
	       break;
	    case 7:			/* history */
	       if (second == NULL) 
		  {
		     parameters->WriteHistory = YES;
		     printf("  Writing history file.\n");
		  } 
	       else if ((strcasecmp("yes", second) == SAME) ||
			(strcasecmp("y", second) == SAME)) 
		  {
		     parameters->WriteHistory = YES;
		     printf("  Writing history file.\n");
		  }
	       else 
		  {
		     parameters->WriteHistory = NO;
		     printf("  Not writing history file.\n");
		  }
	       break;
	    case 8:
	    case 9:			/* presolve/preprocess (synonymous) */
	       if (second == NULL) 
		  {
		     parameters->Preprocessing = YES;
		     printf("  Presolving.\n");
		  } 
	       else if ((strcasecmp("no", second) == SAME) ||
			(strcasecmp("n", second) == SAME)) 
		  {
		     parameters->Preprocessing = NO;
		     printf("  NO Presolving.\n");
		  } 
	       else 
		  {
		     parameters->Preprocessing = YES;
		     printf("  Presolving.\n");
		  }
	       break;
	    case 10:			/* input directory */
	       if (second != NULL) 
		  {
		     parameters->InputDirectory = StrDup(second, 
							 "InputDirectory");
		     printf("  Look in %s if input file not found in current directory\n", second);
		  }
	       break;
	    case 11:			/* opttol */
	       if (second == NULL)
		  {
		     printf("  Missing numerical value for OptTol");
		     printf(" in parameter file.\n");
		  }
	       else 
		  {
		     parameters->OptTol = atof(second);
		     printf("  OptTol = %e\n", parameters->OptTol);
		  }
	       break;
	    case 12:			/* prifeastol */
	       if (second == NULL)
		  {
		     printf("  Missing numerical value for PriFeasTol");
		     printf(" in parameter file.\n");
		  }	       
	       else 
		  {
		     parameters->PriFeasTol = atof(second);
		     printf("  PriFeasTol = %e\n", parameters->PriFeasTol);
		  }
	       break;
	    case 13:			/* dualfeastol */
	       if (second == NULL)
		  {
		     printf("  Missing numerical value for DualFeasTol");
		     printf(" in parameter file.\n");
		  }
	       else 
		  {
		     parameters->DualFeasTol = atof(second);
		     printf("  DualFeasTol = %e\n", parameters->DualFeasTol);
		  }
	       break;
	    case 14:			/* cachesize */
	       if (second == NULL)
		  {
		     printf("  Missing numerical value for ");
		     printf("CacheSize in parameter file.\n");
		  }
	       else 
		  {
		     parameters->CacheSize = atoi(second);
		     printf("  CacheSize = %d\n", parameters->CacheSize);
		  }
	       break;
	    case 15:			/* unrollinglevel */
	       if (second == NULL)
		  {
		     printf("  Missing numerical value for ");
		     printf("UnrollingLevel in parameter file.\n");
		  }
	       else 
		  {
		     parameters->UnrollingLevel = atoi(second);
		     printf("  UnrollingLevel = %d\n", 
			    parameters->UnrollingLevel);
		  }
	       break;
	    case 16:			/* iterationlimit */
	       if (second == NULL)
		  {
		     printf("  Missing numerical value for");
		     printf(" IterationLimit in parameter file.\n");
		  }
	       else 
		  {
		     parameters->IterationLimit = atoi(second);
		     printf("  IterationLimit = %d\n", 
			    parameters->IterationLimit);
		  }
	       break;
	    case 17:
	       if (second == NULL)
		  {
		     printf("  Missing numerical value for CenterExp");
		     printf(" in parameter file.\n");
		  }
	       else 
		  {
		     parameters->CenterExponent = atof(second);
		     printf("  Centering Exponent = %f\n", 
			    parameters->CenterExponent);
		  }
	       break;
	    case 18:    /* iterative refinement */
	       if (second == NULL) 
		  {
		     parameters->Refinement = YES;
		     printf("  Doing iterative refinement.\n");
		  } 
	       else if ((strcasecmp("no", second) == SAME) ||
			     (strcasecmp("n", second) == SAME)) 
		  {
		     parameters->Refinement = NO;
		     printf("  NO iterative refinement.\n");
		  } 
	       else 
		  {
		     parameters->Refinement = YES;
		     printf("  Doing iterative refinement.\n");
		  }
	       break;
	    case 19: 
	       if (second == NULL)
		  {
		     printf("  Missing numerical value for StepFactor"); 
		     printf(" in parameter file.\n");
		  }
	       else 
		  {
		     parameters->AlphaScale = atof(second);
		     printf("  StepFactor = %f\n", parameters->AlphaScale);
		  }
	       break;
	    case 20:    /* scaling */
	       if (second == NULL) 
		  {
		     parameters->Scaling = YES;
		     printf("  Doing Scaling.\n");
		  } 
	       else if ((strcasecmp("no", second) == SAME) ||
			     (strcasecmp("n", second) == SAME)) 
		  {
		     parameters->Scaling = NO;
		     printf("  NO Scaling.\n");
		  } 
	       else 
		  {
		     parameters->Scaling = YES;
		     printf("  Doing Scaling.\n");
		  }
	       break;
	    case 21:    /* Gondzio higher-order corrections */
	       if (second == NULL) 
		  {
		     parameters->HOCorrections = 1;
		     printf("  Doing Gondzio higher-order corrections.\n");
		  } 
	       else if ((strcasecmp("no", second) == SAME) ||
			(strcasecmp("n", second) == SAME)) 
		  {
		     parameters->HOCorrections = 0;
		     printf("  No Gondzio higher-order corrections.\n");
		  } 
	       else 
		  {
		     parameters->HOCorrections = atoi(second);
		     printf("  Doing Gondzio corrections.\n");
		  }
	       break;
	    case 22:    /* Gondzio higher-order corrections */
	       if (second == NULL) 
		  {
		     parameters->MaxCorrections = 0;
		     printf("  Maximum Gondzio corrections to be");
		     printf(" determined automatically by PCx.\n");
		  } 
	       else 
		  {
		     parameters->MaxCorrections = atoi(second);
		     printf("  Maximum Gondzio corrections = %d\n", 
			    parameters->MaxCorrections);
		  }
	       break;
	    } /* end switch */
	 do 
	    {
	       fgets(line, 200, fp);
	    } while (line[0] == '#' && !feof(fp));
      }
   fclose(fp);
   return 0;
}

/***************************************************************************/