mcknap.h

Go to the documentation of this file.
00001 
00002 #ifndef MCKNAP_INCLUDED
00003 #define MCKNAP_INCLUDED
00004 
00005 /* ====================================================================== */
00006 #define MCKNAP_RC_OK             0
00007 #define MCKNAP_RC_INF            1
00008 #define MCKNAP_RC_TRIVIAL_MAXSUM 2
00009 
00010 /* ======================================================================
00011                                   definitions
00012    ====================================================================== */
00013 
00014 #define TRACELEVEL  10                /* level of debug information */
00015 #define START       1                /* first test to be run */
00016 #define TESTS       100              /* last test to be run */
00017 
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <time.h>
00021 #include <stdarg.h>
00022 /*#include <values.h>*/
00023 #include <math.h>
00024 #include <string.h>
00025 /*#include <values.h>*/
00026 #include <limits.h>
00027 #include <memory.h>
00028 #define _INCLUDE_POSIX_SOURCE
00029 #ifndef _MSC_VER
00030 #include <sys/times.h>
00031 #include <unistd.h>
00032 #endif
00033 
00034 
00035 /* ======================================================================
00036                    macros
00037    ====================================================================== */
00038 
00039 #ifndef _MSC_VER
00040 #define srand(x)    srand48(x)
00041 #define random(x)   (lrand48() % (x))
00042 #else
00043 #define random(x)   (rand() % (x))
00044 #endif
00045 
00046 #define SYNC           5   /* when to switch to linear scan in binary scan */
00047 #define MEDIMAX        15
00048 #define MAXSTACK       100
00049 #define MAXLIST        32
00050 #define MAXVTYPE       ULONG_MAX
00051 
00052 #define TRUE           1
00053 #define FALSE          0
00054 
00055 #define MAXIMIZE       1
00056 #define MINIMIZE       0
00057 
00058 #define DET(a1, a2, b1, b2)    ((a1) * (stype) (b2) - (a2) * (stype) (b1))
00059 #define SWAPS(a,b)      { register itemset t; t=*(a); *(a)=*(b); *(b)=t; }
00060 #define SWAPI(a,b)      { register itemrec t; t=*(a); *(a)=*(b); *(b)=t; }
00061 #define SWAPO(a,b)      { register ordrec  t; t=*(a); *(a)=*(b); *(b)=t; }
00062 #define SIZE(a)                          ((int) (((a)->lset+1)-(a)->fset))
00063 
00064 
00065 /* ======================================================================
00066                  type declarations
00067    ====================================================================== */
00068 
00069 typedef int           boolean; /* logical variable */
00070 typedef int           ntype;   /* number of stages */
00071 
00072 
00073 typedef int           itype;   /* item profits and weights */
00074 /*typedef long          stype;*/   /* sum of pofit or weight */
00075 typedef double          stype;   /* sum of pofit or weight */
00076 typedef unsigned long vtype;   /* solution vector */
00077 
00078 
00079 /* partial vector */
00080 typedef struct {
00081   stype    psum;
00082   stype    wsum;
00083   vtype    vect;
00084 } partvect;
00085 
00086 /* item */
00087 typedef struct {
00088    int i;/*MVG*/
00089    int j;/*MVG*/
00090    itype    psum;
00091    itype    wsum;
00092 } itemrec;
00093 
00094 /* set of partial vectors */
00095 typedef struct {
00096   ntype    size;
00097   itemrec  *fset;
00098   itemrec  *lset;
00099   itemrec  *no;
00100   itemrec  f,l;
00101   boolean  used;
00102 } itemset;
00103 
00104 /* set of partial vectors */
00105 typedef struct {
00106   ntype    size;
00107   partvect *fset;
00108   partvect *lset;
00109 } partset;
00110 
00111 /* set of itemsets */
00112 typedef struct {
00113   itemset  *fset;
00114   itemset  *lset;
00115   ntype    size;
00116 } isetset;
00117 
00118 /* order record */
00119 typedef struct {
00120   itype    dp;
00121   itype    dw;
00122   itemset  *ref;
00123 } ordrec;
00124 
00125 /* order interval */
00126 typedef struct {
00127   ordrec   *f;
00128   ordrec   *l;
00129 } ordintv;
00130 
00131 /* order stack */
00132 typedef struct {
00133   ordintv  intv[MAXSTACK];
00134   int      level;
00135   int      optim;
00136   ordrec   *first;
00137   ordrec   *last;
00138   ordrec   *i;
00139 } ordstack;
00140 
00141 /* solution record */
00142 typedef struct {
00143   ntype    size;
00144   itemset  *set;
00145 } solrec;
00146 
00147 /* solution structure */
00148 typedef struct {
00149   solrec   list[MAXLIST];
00150   ntype    size;
00151   stype    psum;
00152   stype    wsum;
00153   vtype    vect;
00154   vtype    vmax;
00155   ordrec   *a;
00156   ordrec   *b;
00157 } solstruct;
00158 
00159 typedef int (*funcptr) (const void *, const void *);
00160 
00161 
00162 typedef struct { /* all problem information */
00163   ntype k;
00164   ntype n;
00165   int   type;
00166   itype range;
00167 
00168   stype capacity;         /* capacity of knapsack */
00169   stype dantzig;              /* the dantzig upper bound */
00170   stype zstar;                /* optimal solution */
00171   stype summul;           /* sum of multiplications */
00172   stype antmul;           /* number of multiplications */
00173   stype maxmul;               /* max multiplied set */
00174   stype redusets;             /* sum of reduced sets */
00175   stype reduitems;            /* sum of items which are tested for reduce */
00176   stype redukill;             /* sum of tested items which were reduced */
00177   stype gap;                  /* current gap */
00178   stype partitions;           /* number of partitions */
00179   stype domikill;             /* number of dominated-kills */
00180   stype lpkill;               /* number of lp-kills */
00181   long  timepar;              /* time used for partitioning */
00182   long  timesort;             /* time used for sorting of gradients */
00183   long  time;                 /* time used for all solution */
00184   long  welldef;              /* is the found solution correct */
00185   long  checked;              /* optimal solution checked */
00186   long  iterates;             /* number of iterations to find optimal sol */
00187 } allinfo;
00188 
00189 
00190 
00191 
00192 extern int minmcknapSolve(int cap,
00193               isetset * head,
00194               itemrec * solRec,
00195               stype   * minObj);
00196 extern void visitems(itemset *d);
00197 extern void inittrace(char *ext);
00198 
00199 
00200 
00201 /* ======================================================================
00202                   global variables
00203    ====================================================================== */
00204 /*MVG: TODO - not reentrant*/
00205 
00206 /* #define DEBUG(x) x */
00207 #define DEBUG(x)
00208 
00209 /*solstruct solution;*/
00210 /*solstruct optsol;*/
00211 
00212 #endif

Generated on 3 Jun 2015 for Dip-All by  doxygen 1.6.1