25 #elif defined(USE_LONGLONG)
26 typedef long long Numb;
27 #elif defined(USE_FLOAT)
29 #elif defined(USE_DOUBLE)
31 #elif defined(USE_LONGDOUBLE)
32 typedef long double Numb;
34 #error "No number type defined"
37 #define BIP_MAX_COLS 32
38 #define BIP_MAX_ROWS 32
55 #define MAX_LINE_LEN 512
57 #define GET_SEC(a, b) ((b - a) / (double)CLOCKS_PER_SEC)
79 for(
int r = 0; r < bip->
rows; r++)
81 for(
int c = 0; c < bip->
cols; c++)
99 #elif defined(USE_LONGLONG)
105 #elif defined(USE_FLOAT)
111 #elif defined(USE_DOUBLE)
117 #elif defined(USE_LONGDOUBLE)
124 #error "No number type defined"
146 for(
int r = 0; r < bip->
rows; r++)
151 for(
int c = 0; c < bip->
cols; c++)
153 Numb val = bip->
a[r][c];
160 return fprintf(stderr,
"Error: row %d numerical overflow\n", r),
true;
167 return fprintf(stderr,
"Error: row %d numerical negative overflow\n", r),
true;
195 return fprintf(stderr,
"Error line %d: Got %d fields, expected 1\n",
201 if (errno == ERANGE || isnan(val) || isinf(val) || rintl(val) != val || val < 1.0 || val > (
long double)
BIP_MAX_COLS || *t !=
'\0')
202 return fprintf(stderr,
"Error line=%d Number of cols %s=%Lf out of range or not integer %s\n",
203 lines,
lfs_get_field(lfs, 0), val, errno ? strerror(errno) :
""),
206 bip->
cols = (int)val;
211 return fprintf(stderr,
"Error line %d: Got %d fields, expected 1\n",
217 if (errno == ERANGE || isnan(val) || isinf(val) || rintl(val) != val || val < 1.0 || val > (
long double)
BIP_MAX_ROWS || *t !=
'\0')
218 return fprintf(stderr,
"Error line=%d Number of rows %s=%Lf out of range or not integer %s\n",
219 lines,
lfs_get_field(lfs, 0), val, errno ? strerror(errno) :
""),
227 return fprintf(stderr,
"Error line=%d Expetced %d rows, got more\n",
232 return fprintf(stderr,
"Error line %d: Got %d fields, expected %d\n",
236 for(
int col = 0; col < bip->
cols + 2; col++)
238 if (col != bip->
cols)
242 if (errno == ERANGE || isnan(val) || isinf(val)
247 return fprintf(stderr,
"Error line=%d Number %s=%Lf out of range %s: %s\n",
248 lines,
lfs_get_field(lfs, col), val,
int_coef ?
"or not integer" :
"", errno ? strerror(errno) :
""),
252 bip->
a[bip->
rows][col] = (Numb)val;
254 bip->
b[bip->
rows] = (Numb)val;
260 if (!strcmp(sense,
"<="))
262 else if (!strcmp(sense,
">="))
264 else if (!strcmp(sense,
"=") || !strcmp(sense,
"=="))
267 return fprintf(stderr,
"Error line=%d Expected <=, >=, ==, got \"%s\"\n",
298 assert(NULL != filename);
299 assert(0 < strlen(filename));
309 if (NULL == (fp = fopen(filename,
"r")))
311 fprintf(stderr,
"Can't open file %s\n", filename);
315 printf(
"Reading %s\n", filename);
317 while(mode !=
READ_ERROR && NULL != (s = fgets(buf,
sizeof(buf), fp)))
337 fprintf(stderr,
"Error: unexpected EOF\n");
346 printf(
"Read %d rows, %d cols\n", bip->
rows, bip->
cols);
361 const char* sense[3] = {
"<=",
">=",
"==" };
366 for(
int r = 0; r < bip->
rows; r++)
368 for(
int c = 0; c < bip->
cols; c++)
369 fprintf(fp,
"%Lf ", (
long double)bip->
a[r][c]);
370 fprintf(fp,
"%s %Lf\n", sense[bip->
sense[r]], (
long double)bip->
b[r]);
386 for(
int r = 0; r < vars; r++)
387 fprintf(fp,
"%2g ", x[r]);
400 for(r = 0; r < bip->
rows; r++)
404 for(
int c = 0; c < bip->
cols; c++)
406 assert(x[c] == 0.0 || x[c] == 1.0);
408 lhs += bip->
a[r][c] * x[c];
410 assert(fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT) == 0);
413 switch(bip->
sense[r])
416 if (lhs <= bip->b[r])
420 if (lhs >= bip->
b[r])
424 if (lhs == bip->
b[r])
432 return r == bip->
rows;
443 clock_t start = clock();
449 for(
unsigned long long bitvec = 0; bitvec < (1uL << bip->
cols); bitvec++)
451 unsigned long long mask = 1;
458 for(
int j = 0; j < bip->
cols; j++, mask <<= 1)
459 x[j] = (bitvec & mask) ? 1.0 : 0.0;
470 assert((
unsigned)count == (1u << bip->
cols));
472 double elapsed =
GET_SEC(start, clock());
474 printf(
"Checked %d vectors in %.3f s = %.3f kvecs/s\n",
475 count, elapsed, count / elapsed / 1000.0);
#define MAX_LINE_LEN
Maximum input line length.
static void print_solu(FILE *fp, int vars, const double *x)
Print solution vector.
static bool bip_is_feasible(const BIP *bip, const double *x)
Check whether a particular vector is a feasible solution to a BIP.
int max_rows
maximum number of rows
void bip_print(const BIP *bip, FILE *fp)
Print Binary Program from BIP.
int cols
number of columns (variables)
Split line into fields Header.
Numb a[BIP_MAX_ROWS][BIP_MAX_COLS]
coefficient matrix
BIP * bip_read(const char *filename)
Read a bip file.
const char * lfs_get_field(const LFS *lfs, int fno)
void bip_free(BIP *bip)
Deallocate BIP data structure.
static LINE_MODE process_line(LINE_MODE mode, const LFS *lfs, const int lines, BIP *bip)
static bool bip_can_overflow(const BIP *bip)
int rows
number of rows (constraints)
Numb b[BIP_MAX_ROWS]
right hand side
int lfs_used_fields(const LFS *lfs)
Sense sense[BIP_MAX_ROWS]
equation sense
int bip_enumerate(const BIP *bip, bool with_output)
Enumerate all possible solution of a BIP.
LFS * lfs_split_line(LFS *lfs, const char *line, const char *comment)
static bool bip_is_valid(const BIP *bip)
Check whether BIP data structure is consistent.