27 #define BIP_SID 0x42495078
42 #define MAX_LINE_LEN 512
44 #define MAX_COEF_VAL 1e20
46 #define GET_SEC(a, b) ((b - a) / (double)CLOCKS_PER_SEC)
63 for(
int r = 0; r < bip->
rows; r++)
65 for(
int c = 0;
c < bip->
cols;
c++)
104 assert(NULL != filename);
105 assert(0 < strlen(filename));
108 char input[] =
"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf";
120 if (NULL == (fp = fopen(filename,
"r")))
122 fprintf(stderr,
"Can't open file %s\n", filename);
125 printf(
"Reading %s\n", filename);
127 while(NULL != (s = fgets(buf,
sizeof(buf), fp)))
129 char* t = strpbrk(s,
"#\n\r");
150 nums = sscanf(s, input,
151 &v[ 0], &v[ 1], &v[ 2], &v[ 3], &v[ 4], &v[ 5], &v[ 6], &v[ 7],
152 &v[ 8], &v[ 9], &v[10], &v[11], &v[12], &v[13], &v[14], &v[15],
153 &v[16], &v[17], &v[18], &v[19], &v[20], &v[21], &v[22], &v[23],
154 &v[24], &v[25], &v[26], &v[27], &v[28], &v[29], &v[30], &v[31]);
158 fprintf(stderr,
"Error line %d: Expected number, got \"%s\"\n", lines, s);
168 bip->
cols = (int)v[0];
172 fprintf(stderr,
"Error line %d: 1 <= cols=%d <= %d\n", lines, bip->
cols,
BIP_COLS);
177 strncpy(input + bip->
cols * 4,
"<= ", 3);
182 bip->
rows = (int)v[0];
186 fprintf(stderr,
"Error line %d: 1 <= rows <= %d\n", lines,
BIP_ROWS);
192 if (rows >= bip->
rows)
194 fprintf(stderr,
"Error line=%d too many rows (expetced %d)\n",
198 if (nums != bip->
cols + 1)
200 fprintf(stderr,
"Error line=%d Expected %d cols, got %d\n",
201 lines, bip->
cols, nums);
204 for(cols = 0; cols < bip->
cols; cols++)
205 bip->
a[rows][cols] = v[cols];
207 bip->
b[rows] = v[cols];
225 fprintf(stderr,
"Error line %d: 1 <= cols=%d <= %d\n", lines, bip->
cols,
BIP_COLS);
235 fprintf(stderr,
"Error line %d: 1 <= rows <= %d\n", lines,
BIP_ROWS);
241 if (rows >= bip->
rows)
243 fprintf(stderr,
"Error line=%d too many rows (expetced %d)\n",
247 #ifdef USE_STRTOK // not thread safe, rarely a problem in reading
248 const char* delim =
" \t";
251 for(s = strtok(s, delim); s != NULL; s = strtok(NULL, delim))
253 if (cols == bip->
cols && strcmp(s,
"<="))
254 bip->
b[rows] = atof(s);
255 else if (cols < bip->cols)
262 for(cols = 0; cols < bip->
cols; cols++)
264 bip->
a[
rows][
cols] = (double)strtol(s, &t, 10);
268 fprintf(stderr,
"Error line=%d Expected %d cols, got %d\n",
269 lines, bip->
cols, cols);
277 if (strncmp(s,
"<=", 2))
279 fprintf(stderr,
"Error line=%d Expected <= , got \"%s\"\n",
284 bip->
b[
rows] = (double)strtol(s, &t, 10);
288 fprintf(stderr,
"Error line=%d Expected rhs\n", lines);
291 #endif // !USE_STRTOK
301 if (rows != bip->
rows)
303 fprintf(stderr,
"Error line=%d Expetced %d rows, got %d\n",
304 lines, bip->
rows, rows);
312 printf(
"Read %d rows, %d cols\n", bip->
rows, bip->
cols);
328 for(
int r = 0; r < bip->
rows; r++)
330 for(
int c = 0;
c < bip->
cols;
c++)
331 fprintf(fp,
"%5g ", bip->
a[r][
c]);
332 fprintf(fp,
"<= %5g\n", bip->
b[r]);
348 for(
int r = 0; r < vars; r++)
349 fprintf(fp,
"%2g ", x[r]);
362 for(r = 0; r < bip->
rows; r++)
366 for(
int c = 0;
c < bip->
cols;
c++)
368 assert(x[
c] == 0.0 || x[
c] == 1.0);
370 lhs += bip->
a[r][
c] * x[
c];
375 return r == bip->
rows;
386 clock_t start = clock();
392 for(
unsigned long long bitvec = 0; bitvec < (1uL << bip->
cols); bitvec++)
394 unsigned long long mask = 1;
397 assert(
sizeof(bitvec) * 8 >
BIP_COLS);
401 for(
int j = 0; j < bip->
cols; j++, mask <<= 1)
402 x[j] = (bitvec & mask) ? 1.0 : 0.0;
413 assert((
unsigned)count == (1u << bip->
cols));
415 double elapsed =
GET_SEC(start, clock());
417 printf(
"Checked %d vectors in %.3f s = %.3f kvecs/s\n",
418 count, elapsed, count / elapsed / 1000.0);
425 int main(
int argc,
const char** argv)
429 fprintf(stderr,
"usage: %s filename", argv[0]);
#define MAX_LINE_LEN
Maximum input line length.
int main(int argc, const char **argv)
Usage: ex4_enum bip_file.
#define MAX_COEF_VAL
Maximum absolute value of a coefficient.
double a[BIP_ROWS][BIP_COLS]
coefficient matrix
void print_solu(FILE *fp, int vars, const double *x)
Print solution vector.
void deallocate(void *p)
Free allocated memory.
SID int rows
number of rows (constraints)
int cols
number of columns (variables)
BIP * bip_read(const char *filename)
Read a bip file.
void bip_print(FILE *fp, const BIP *bip)
Print Binary Program from BIP.
void * allocate(int elems, int size)
Allocate memory.
void bip_free(BIP *bip)
Deallocate BIP data structure.
void mem_maximum(FILE *fp)
int bip_enumerate(const BIP *bip, bool with_output)
Enumerate all possible solution of a BIP.
bool bip_is_feasible(const BIP *bip, const double *x)
Check whether a particular vector is a feasible solution to a BIP.
double b[BIP_ROWS]
right hand side
bool bip_is_valid(const BIP *bip)
Check whether BIP data structure is consistent.