APPFS
Advanced practical programming for scientists
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Typedefs | Functions
bip_enum.h File Reference

BIP Enumerator Header. More...

Go to the source code of this file.

Typedefs

typedef struct binary_program BIP
 

Functions

void bip_init (void)
 
void bip_free (BIP *bip)
 Deallocate BIP data structure. More...
 
BIPbip_read (const char *filename)
 Read a bip file. More...
 
void bip_print (const BIP *bip, FILE *fp)
 Print Binary Program from BIP. More...
 
int bip_enumerate (const BIP *bip, bool with_output)
 Enumerate all possible solution of a BIP. More...
 

Detailed Description

Author
Thorsten Koch
Date
20Nov2014

Definition in file bip_enum.h.

Typedef Documentation

typedef struct binary_program BIP

Definition at line 9 of file bip_enum.h.

Function Documentation

int bip_enumerate ( const BIP bip,
bool  with_output 
)

Prints all solutions.

Returns
number of feasible solutions found.

Definition at line 439 of file bip_enum.c.

440 {
441  assert(bip_is_valid(bip));
442 
443  clock_t start = clock();
444  int solus = 0;
445  int count = 0;
446 
447  /* generate all permutation
448  */
449  for(unsigned long long bitvec = 0; bitvec < (1uL << bip->cols); bitvec++)
450  {
451  unsigned long long mask = 1; // long long because long is 32 bit on 32 bit computers
452  double x[BIP_MAX_COLS]; // having this always double is questionable
453 
454  assert(sizeof(bitvec) * 8 > BIP_MAX_COLS); //lint !e506
455 
456  /* construct solution vector
457  */
458  for(int j = 0; j < bip->cols; j++, mask <<= 1)
459  x[j] = (bitvec & mask) ? 1.0 : 0.0;
460 
461  if (bip_is_feasible(bip, &x[0])) //lint !e772
462  {
463  if (with_output)
464  print_solu(stdout, bip->cols, &x[0]);
465 
466  solus++;
467  }
468  count++;
469  }
470  assert((unsigned)count == (1u << bip->cols));
471 
472  double elapsed = GET_SEC(start, clock());
473 
474  printf("Checked %d vectors in %.3f s = %.3f kvecs/s\n",
475  count, elapsed, count / elapsed / 1000.0);
476 
477  return solus;
478 }
#define GET_SEC(a, b)
Definition: bip_enum.c:57
static void print_solu(FILE *fp, int vars, const double *x)
Print solution vector.
Definition: bip_enum.c:376
static bool bip_is_feasible(const BIP *bip, const double *x)
Check whether a particular vector is a feasible solution to a BIP.
Definition: bip_enum.c:394
int cols
number of columns (variables)
Definition: bip_enum.c:48
#define BIP_MAX_COLS
Definition: bip_enum.c:37
static bool bip_is_valid(const BIP *bip)
Check whether BIP data structure is consistent.
Definition: bip_enum.c:66
void bip_free ( BIP bip)

Definition at line 133 of file bip_enum.c.

134 {
135  assert(bip_is_valid(bip));
136 
137  mem_check(bip);
138 
139  free(bip);
140 }
#define mem_check(a)
Definition: mshell.h:71
static bool bip_is_valid(const BIP *bip)
Check whether BIP data structure is consistent.
Definition: bip_enum.c:66
#define free(a)
Definition: mshell.h:57
void bip_init ( void  )

Definition at line 91 of file bip_enum.c.

92 {
93 #if defined(USE_INT)
94 
95  max_coef_val = INT_MAX;
96  min_coef_val = INT_MIN;
97  int_coef = true;
98 
99 #elif defined(USE_LONGLONG)
100 
101  max_coef_val = LLONG_MAX;
102  min_coef_val = LLONG_MIN;
103  int_coef = true;
104 
105 #elif defined(USE_FLOAT)
106 
107  max_coef_val = powf(10, (float)FLT_DIG);
108  min_coef_val = -powf(10, (float)FLT_DIG);
109  int_coef = false;
110 
111 #elif defined(USE_DOUBLE)
112 
113  max_coef_val = pow(10, (double)DBL_DIG);
114  min_coef_val = -pow(10, (double)DBL_DIG);
115  int_coef = false;
116 
117 #elif defined(USE_LONGDOUBLE)
118 
119  max_coef_val = powl(10, (long double)LDBL_DIG);
120  min_coef_val = -powl(10, (long double)LDBL_DIG);
121  int_coef = false;
122 
123 #else
124 #error "No number type defined"
125 #endif
126 
127  // printf("Min/Max coefficient values [%.0Lf,%.0Lf]\n",
128  // (long double)min_coef_val, (long double)max_coef_val);
129 }
static bool int_coef
Definition: bip_enum.c:61
static Numb max_coef_val
Definition: bip_enum.c:59
static Numb min_coef_val
Definition: bip_enum.c:60
void bip_print ( const BIP bip,
FILE *  fp 
)

Definition at line 359 of file bip_enum.c.

360 {
361  const char* sense[3] = { "<=", ">=", "==" };
362 
363  assert(NULL != fp);
364  assert(bip_is_valid(bip));
365 
366  for(int r = 0; r < bip->rows; r++)
367  {
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]);
371  }
372 }
int cols
number of columns (variables)
Definition: bip_enum.c:48
Numb a[BIP_MAX_ROWS][BIP_MAX_COLS]
coefficient matrix
Definition: bip_enum.c:49
int rows
number of rows (constraints)
Definition: bip_enum.c:46
Numb b[BIP_MAX_ROWS]
right hand side
Definition: bip_enum.c:50
Sense sense[BIP_MAX_ROWS]
equation sense
Definition: bip_enum.c:51
static bool bip_is_valid(const BIP *bip)
Check whether BIP data structure is consistent.
Definition: bip_enum.c:66
BIP* bip_read ( const char *  filename)

Format example:

4 # cols (variables) 3 # rows (constraints) 2 3 5 4 <= 8 3 6 0 8 <= 10 0 0 1 1 <= 1

comments (#) and empty lines are ignored.

Parameters
filenamename of file to read
Returns
ptr to BIP data structure

Definition at line 296 of file bip_enum.c.

297 {
298  assert(NULL != filename);
299  assert(0 < strlen(filename));
300 
301  char buf[MAX_LINE_LEN];
302  char* s;
303  FILE* fp;
304  BIP* bip = calloc(1, sizeof(*bip));
305  LFS* lfs = NULL;
306  int lines = 0;
307  LINE_MODE mode = READ_COLS;
308 
309  if (NULL == (fp = fopen(filename, "r")))
310  {
311  fprintf(stderr, "Can't open file %s\n", filename);
312  free(bip);
313  return NULL;
314  }
315  printf("Reading %s\n", filename);
316 
317  while(mode != READ_ERROR && NULL != (s = fgets(buf, sizeof(buf), fp)))
318  {
319  lines++;
320 
321  lfs = lfs_split_line(lfs, s, "#");
322  mode = process_line(mode, lfs, lines, bip);
323  }
324  fclose(fp);
325 
326  if (NULL != lfs)
327  lfs_free(lfs);
328 
329  if (READ_ERROR == mode)
330  {
331  free(bip);
332 
333  return NULL;
334  }
335  if (bip->cols == 0 || bip->max_rows == 0 || bip->rows < bip->max_rows)
336  {
337  fprintf(stderr, "Error: unexpected EOF\n");
338  free(bip);
339  return NULL;
340  }
341 
342  assert(bip->rows == bip->max_rows);
343 
344  assert(bip_is_valid(bip));
345 
346  printf("Read %d rows, %d cols\n", bip->rows, bip->cols);
347 
348  if (bip_can_overflow(bip))
349  {
350  bip_free(bip);
351 
352  return NULL;
353  }
354  return bip;
355 }
#define MAX_LINE_LEN
Maximum input line length.
Definition: bip_enum.c:55
int max_rows
maximum number of rows
Definition: bip_enum.c:47
int cols
number of columns (variables)
Definition: bip_enum.c:48
void bip_free(BIP *bip)
Deallocate BIP data structure.
Definition: bip_enum.c:133
static LINE_MODE process_line(LINE_MODE mode, const LFS *lfs, const int lines, BIP *bip)
Definition: bip_enum.c:179
void lfs_free(LFS *lfs)
Definition: splitline.c:35
static bool bip_can_overflow(const BIP *bip)
Definition: bip_enum.c:142
int rows
number of rows (constraints)
Definition: bip_enum.c:46
LINE_MODE
Definition: bip_enum.c:40
#define calloc(a, b)
Definition: mshell.h:54
LFS * lfs_split_line(LFS *lfs, const char *line, const char *comment)
Definition: splitline.c:44
static bool bip_is_valid(const BIP *bip)
Check whether BIP data structure is consistent.
Definition: bip_enum.c:66
#define free(a)
Definition: mshell.h:57