home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / math / lpsolves / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-28  |  5.0 KB  |  154 lines

  1. #include <signal.h>
  2. #include "defines.h"
  3. #include "globals.h"
  4. #include "patchlev.h"
  5.  
  6. /* declare all global variables here */
  7. int        Rows, Columns, Nonnuls, Sum;
  8. double     Extrad;
  9. double     *Pcol;
  10. nstring       Probname;
  11. int       Totnum, Classnr, Linenr;
  12. short       Bounds, Ranges, Verbose, Debug, Show_results;
  13. unsigned   Cur_eta_size;
  14. double     *Eta_value;
  15. int        *Eta_rownr;
  16.  
  17. /* Variables which contain the original problem statement */
  18. matrec     *Mat; /* initial problem matrix */
  19. double     *Upbo, *Lowbo; /* bounds on variables */
  20. nstring    *Names; /* contains the names of rows and columns */
  21. int        *Cend; /* Column start indexes in Mat */
  22. double     *Rh; /* Right hand side of original problem */
  23. short      *Relat; /* Relational operators of original problem */
  24.  
  25. tmp_store_struct tmp_store;
  26. rside      *First_rside;
  27. hashelem   *Hash_tab[HASH_SIZE];
  28.  
  29. /* Variables which are used during solving */
  30. short      *Chsign; /* tells which row was multiplied by -1 before solving */
  31. int        *Endetacol; /* Column start indexes in Eta */
  32. int        *Rend, *Bas; /* ?? */
  33. double     *Rhs; /* right hand side of Eta ?? */
  34. int        *Colno; /* ?? */
  35. short      *Basis, *Lower; /* ?? */
  36.  
  37. /* New variables for mixed integer solving */
  38. double     *Solution, *Best_solution; /* to store solutions */
  39. short      *Must_be_int; /* indicates whether variable should be int */
  40. matrec     *Orig_mat; /* To store original version of Mat */
  41. double     *Orig_upbo; /* To store original version of Upbo */
  42. double     *Orig_lowbo; /* To store original version of Lowbo */
  43. double     *Orig_rh; /* To store original version of Rh */
  44. int        Level; /* the recursion level of solve */
  45. intrec     *First_int;
  46. short      Ignore_decl;
  47.  
  48. short is_int(double value)
  49. {
  50.   double tmp;
  51.  
  52.   tmp = value - floor(value);
  53.   if(tmp < EPSILON)
  54.     return(1);
  55.   if((tmp > 0.5) && ((1.0 - tmp) < EPSILON))
  56.     return(1);
  57.   return(0);
  58. }
  59.  
  60. void allocate_globals(void)
  61. {
  62.   Sum = Rows + Columns;
  63.   Cur_eta_size = ETA_START_SIZE;
  64.  
  65.   CALLOC(Eta_value, Cur_eta_size, double);
  66.   CALLOC(Eta_rownr, Cur_eta_size, int);
  67.   CALLOC(Pcol, Rows + 2, double);
  68.   CALLOC(Cend, Columns + 2, int); /* column boundaries in Mat */
  69.   CALLOC(Endetacol, Sum + 2, int);
  70.   CALLOC(Rend, Rows + 2, int);
  71.   CALLOC(Bas, Rows + 2, int);
  72.   CALLOC(Lowbo, Sum + 2, double);
  73.   CALLOC(Orig_lowbo, Sum + 2, double);
  74.   CALLOC(Upbo, Sum + 2, double);
  75.   CALLOC(Orig_upbo, Sum + 2, double);
  76.   CALLOC(Names, Sum + 2, nstring);
  77.   CALLOC(Rh, Rows + 2, double); /* rhs for Mat */
  78.   CALLOC(Orig_rh, Rows + 2, double); /* rhs for Mat */
  79.   CALLOC(Rhs, Rows + 2, double);
  80.   CALLOC(Colno, Nonnuls + 2, int);
  81.   CALLOC(Mat, Nonnuls + 1, matrec); /* (initial) problem matrix */
  82.   CALLOC(Orig_mat, Nonnuls + 1, matrec); /* (initial) problem matrix */
  83.   CALLOC(Relat, Rows + 2, short);
  84.   CALLOC(Basis, Sum + 2, short);
  85.   CALLOC(Lower, Sum + 2, short);
  86.   CALLOC(Chsign, Rows + 2, short);
  87.  
  88.   CALLOC(Solution, Sum + 1, double);
  89.   CALLOC(Best_solution, Sum + 1, double);
  90.   CALLOC(Must_be_int, Columns + 1, short);
  91. } /* allocate_globals */
  92.  
  93.  
  94. void signal_handler(int sig)
  95. {
  96.   fprintf(stderr, "Caught signal %d, will print intermediate result\n", sig);
  97.   print_solution(stderr, Best_solution);
  98. }
  99.  
  100.  
  101. int  main (int argc, char *argv[])
  102. {
  103.   int i, failure;
  104.   double obj_bound = -INFINITE;
  105.  
  106.   for(i = 1; i < argc; i++)
  107.     {
  108.       if(strcmp(argv[i], "-v") == 0)
  109.     Verbose = TRUE;
  110.       else if(strcmp(argv[i], "-d") == 0)
  111.     Debug = TRUE;
  112.       else if(strcmp(argv[i], "-i") == 0)
  113.     Show_results = TRUE;
  114.       else if(strcmp(argv[i], "-b") == 0)
  115.     obj_bound = atof(argv[++i]);
  116.       else if(strcmp(argv[i], "-h") == 0)
  117.     {
  118.       printf("Usage of %s version %s:\n", argv[0], PATCHLEVEL);
  119.       printf("%s [-v] [-d] [-h] [-b <bound>] [-i] \"<\" <input_file>\n",
  120.          argv[0]);
  121.       printf("-h:\t\tprints this message\n");
  122.       printf("-v:\t\tverbose mode, gives flow through the program\n");
  123.       printf("-d:\t\tdebug mode, all intermediate results are printed,\n\t\tand the branch-and-bound decisions\n");
  124.       printf("-b <bound>:\tspecify a lower limit for the value of the objective function\n\t\tto the program. If close enough, may speed up the calculations.\n");
  125.       printf("-i:\t\tprint all intermediate valid solutions. Can give you useful\n\t\tsolutions even if the total run time is too long\n");
  126.       exit(0);
  127.     }
  128.       else
  129.     fprintf(stderr, "Unrecognized command line option %s, ignored\n",
  130.         argv[i]);
  131.     }
  132.   /* construct basic problem */
  133.   yyparse();
  134.   Rows--; /* @!? MB */
  135.   allocate_globals();
  136.   readinput(Cend, Orig_rh, Relat, Orig_lowbo, Orig_upbo, Orig_mat, Names); 
  137.  
  138.  
  139.   /* from now on, catch interrupt signal to print best solution sofar */
  140.   signal(SIGINT, signal_handler);
  141.  
  142.   /* solve it */
  143.   Best_solution[0] = obj_bound; /* lower bound of objective function */
  144.   failure = solve(Orig_upbo, Orig_lowbo);
  145.  
  146.   /* print result */
  147.   if(!failure)
  148.     print_solution(stdout, Best_solution);
  149.   else
  150.     fprintf(stderr, "No solution.\n");
  151.  
  152.   return (0);
  153. } /* main */
  154.