Dip
0.92.4
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
tmp
Dip-0.92.4
Dip
examples
ExternalSolvers
Knapsack
Pisinger
mcknap.h
Go to the documentation of this file.
1
2
#ifndef MCKNAP_INCLUDED
3
#define MCKNAP_INCLUDED
4
5
/* ====================================================================== */
6
#define MCKNAP_RC_OK 0
7
#define MCKNAP_RC_INF 1
8
#define MCKNAP_RC_TRIVIAL_MAXSUM 2
9
10
/* ======================================================================
11
definitions
12
====================================================================== */
13
14
#define TRACELEVEL 10
/* level of debug information */
15
#define START 1
/* first test to be run */
16
#define TESTS 100
/* last test to be run */
17
18
#include <stdlib.h>
19
#include <stdio.h>
20
#include <time.h>
21
#include <stdarg.h>
22
/*#include <values.h>*/
23
#include <math.h>
24
#include <string.h>
25
/*#include <values.h>*/
26
#include <limits.h>
27
#include <memory.h>
28
#define _INCLUDE_POSIX_SOURCE
29
#ifndef _MSC_VER
30
#include <sys/times.h>
31
#include <unistd.h>
32
#endif
33
34
35
/* ======================================================================
36
macros
37
====================================================================== */
38
39
#ifndef _MSC_VER
40
#define srand(x) srand48(x)
41
#define random(x) (lrand48() % (x))
42
#else
43
#define random(x) (rand() % (x))
44
#endif
45
46
#define SYNC 5
/* when to switch to linear scan in binary scan */
47
#define MEDIMAX 15
48
#define MAXSTACK 100
49
#define MAXLIST 32
50
#define MAXVTYPE ULONG_MAX
51
52
#define TRUE 1
53
#define FALSE 0
54
55
#define MAXIMIZE 1
56
#define MINIMIZE 0
57
58
#define DET(a1, a2, b1, b2) ((a1) * (stype) (b2) - (a2) * (stype) (b1))
59
#define SWAPS(a,b) { register itemset t; t=*(a); *(a)=*(b); *(b)=t; }
60
#define SWAPI(a,b) { register itemrec t; t=*(a); *(a)=*(b); *(b)=t; }
61
#define SWAPO(a,b) { register ordrec t; t=*(a); *(a)=*(b); *(b)=t; }
62
#define SIZE(a) ((int) (((a)->lset+1)-(a)->fset))
63
64
65
/* ======================================================================
66
type declarations
67
====================================================================== */
68
69
typedef
int
boolean
;
/* logical variable */
70
typedef
int
ntype
;
/* number of stages */
71
72
73
typedef
int
itype
;
/* item profits and weights */
74
/*typedef long stype;*/
/* sum of pofit or weight */
75
typedef
double
stype
;
/* sum of pofit or weight */
76
typedef
unsigned
long
vtype
;
/* solution vector */
77
78
79
/* partial vector */
80
typedef
struct
{
81
stype
psum
;
82
stype
wsum
;
83
vtype
vect
;
84
}
partvect
;
85
86
/* item */
87
typedef
struct
{
88
int
i
;
/*MVG*/
89
int
j
;
/*MVG*/
90
itype
psum
;
91
itype
wsum
;
92
}
itemrec
;
93
94
/* set of partial vectors */
95
typedef
struct
{
96
ntype
size
;
97
itemrec
*
fset
;
98
itemrec
*
lset
;
99
itemrec
*
no
;
100
itemrec
f,
l
;
101
boolean
used
;
102
}
itemset
;
103
104
/* set of partial vectors */
105
typedef
struct
{
106
ntype
size
;
107
partvect
*
fset
;
108
partvect
*
lset
;
109
}
partset
;
110
111
/* set of itemsets */
112
typedef
struct
{
113
itemset
*
fset
;
114
itemset
*
lset
;
115
ntype
size
;
116
}
isetset
;
117
118
/* order record */
119
typedef
struct
{
120
itype
dp
;
121
itype
dw
;
122
itemset
*
ref
;
123
}
ordrec
;
124
125
/* order interval */
126
typedef
struct
{
127
ordrec
*
f
;
128
ordrec
*
l
;
129
}
ordintv
;
130
131
/* order stack */
132
typedef
struct
{
133
ordintv
intv[
MAXSTACK
];
134
int
level
;
135
int
optim
;
136
ordrec
*
first
;
137
ordrec
*
last
;
138
ordrec
*
i
;
139
}
ordstack
;
140
141
/* solution record */
142
typedef
struct
{
143
ntype
size
;
144
itemset
*
set
;
145
}
solrec
;
146
147
/* solution structure */
148
typedef
struct
{
149
solrec
list[
MAXLIST
];
150
ntype
size
;
151
stype
psum
;
152
stype
wsum
;
153
vtype
vect
;
154
vtype
vmax
;
155
ordrec
*
a
;
156
ordrec
*
b
;
157
}
solstruct
;
158
159
typedef
int (*
funcptr
) (
const
void
*,
const
void
*);
160
161
162
typedef
struct
{
/* all problem information */
163
ntype
k
;
164
ntype
n
;
165
int
type
;
166
itype
range
;
167
168
stype
capacity
;
/* capacity of knapsack */
169
stype
dantzig
;
/* the dantzig upper bound */
170
stype
zstar
;
/* optimal solution */
171
stype
summul
;
/* sum of multiplications */
172
stype
antmul
;
/* number of multiplications */
173
stype
maxmul
;
/* max multiplied set */
174
stype
redusets
;
/* sum of reduced sets */
175
stype
reduitems
;
/* sum of items which are tested for reduce */
176
stype
redukill
;
/* sum of tested items which were reduced */
177
stype
gap
;
/* current gap */
178
stype
partitions
;
/* number of partitions */
179
stype
domikill
;
/* number of dominated-kills */
180
stype
lpkill
;
/* number of lp-kills */
181
long
timepar
;
/* time used for partitioning */
182
long
timesort
;
/* time used for sorting of gradients */
183
long
time
;
/* time used for all solution */
184
long
welldef
;
/* is the found solution correct */
185
long
checked
;
/* optimal solution checked */
186
long
iterates
;
/* number of iterations to find optimal sol */
187
}
allinfo
;
188
189
190
191
192
extern
int
minmcknapSolve
(
int
cap,
193
isetset
* head,
194
itemrec
* solRec,
195
stype
* minObj);
196
extern
void
visitems
(
itemset
*d);
197
extern
void
inittrace
(
char
*ext);
198
199
200
201
/* ======================================================================
202
global variables
203
====================================================================== */
204
/*MVG: TODO - not reentrant*/
205
206
/* #define DEBUG(x) x */
207
#define DEBUG(x)
208
209
/*solstruct solution;*/
210
/*solstruct optsol;*/
211
212
#endif
allinfo
Definition:
mcknap.h:162
funcptr
int(* funcptr)(const void *, const void *)
Definition:
mcknap.h:159
solrec
Definition:
mcknap.h:142
itype
int itype
Definition:
mcknap.h:73
MAXSTACK
#define MAXSTACK
Definition:
mcknap.h:48
allinfo::redukill
stype redukill
Definition:
mcknap.h:176
ordrec
Definition:
mcknap.h:119
partvect
Definition:
mcknap.h:80
allinfo::summul
stype summul
Definition:
mcknap.h:171
itemset::size
ntype size
Definition:
mcknap.h:96
allinfo::timesort
long timesort
Definition:
mcknap.h:182
solstruct::wsum
stype wsum
Definition:
mcknap.h:152
ordstack::level
int level
Definition:
mcknap.h:134
allinfo::gap
stype gap
Definition:
mcknap.h:177
ordrec::dp
itype dp
Definition:
mcknap.h:120
allinfo::partitions
stype partitions
Definition:
mcknap.h:178
solstruct::size
ntype size
Definition:
mcknap.h:150
partset::lset
partvect * lset
Definition:
mcknap.h:108
allinfo::capacity
stype capacity
Definition:
mcknap.h:168
itemset::fset
itemrec * fset
Definition:
mcknap.h:97
solstruct
Definition:
mcknap.h:148
ordstack::last
ordrec * last
Definition:
mcknap.h:137
allinfo::reduitems
stype reduitems
Definition:
mcknap.h:175
itemset::no
itemrec * no
Definition:
mcknap.h:99
ordstack
Definition:
mcknap.h:132
partset::fset
partvect * fset
Definition:
mcknap.h:107
ordstack::optim
int optim
Definition:
mcknap.h:135
itemrec
Definition:
mcknap.h:87
ordrec::ref
itemset * ref
Definition:
mcknap.h:122
isetset::lset
itemset * lset
Definition:
mcknap.h:114
allinfo::antmul
stype antmul
Definition:
mcknap.h:172
allinfo::welldef
long welldef
Definition:
mcknap.h:184
partset
Definition:
mcknap.h:105
solrec::size
ntype size
Definition:
mcknap.h:143
partset::size
ntype size
Definition:
mcknap.h:106
isetset
Definition:
mcknap.h:112
ntype
int ntype
Definition:
mcknap.h:70
itemset::l
itemrec l
Definition:
mcknap.h:100
allinfo::timepar
long timepar
Definition:
mcknap.h:181
ordstack::i
ordrec * i
Definition:
mcknap.h:138
MAXLIST
#define MAXLIST
Definition:
mcknap.h:49
itemset
Definition:
mcknap.h:95
itemrec::wsum
itype wsum
Definition:
mcknap.h:91
partvect::psum
stype psum
Definition:
mcknap.h:81
solstruct::b
ordrec * b
Definition:
mcknap.h:156
solstruct::vmax
vtype vmax
Definition:
mcknap.h:154
solstruct::a
ordrec * a
Definition:
mcknap.h:155
allinfo::k
ntype k
Definition:
mcknap.h:163
allinfo::domikill
stype domikill
Definition:
mcknap.h:179
itemrec::j
int j
Definition:
mcknap.h:89
allinfo::iterates
long iterates
Definition:
mcknap.h:186
inittrace
void inittrace(char *ext)
allinfo::maxmul
stype maxmul
Definition:
mcknap.h:173
allinfo::redusets
stype redusets
Definition:
mcknap.h:174
ordrec::dw
itype dw
Definition:
mcknap.h:121
allinfo::time
long time
Definition:
mcknap.h:183
ordintv
Definition:
mcknap.h:126
itemrec::i
int i
Definition:
mcknap.h:88
solstruct::psum
stype psum
Definition:
mcknap.h:151
partvect::vect
vtype vect
Definition:
mcknap.h:83
itemset::lset
itemrec * lset
Definition:
mcknap.h:98
itemset::used
boolean used
Definition:
mcknap.h:101
solrec::set
itemset * set
Definition:
mcknap.h:144
allinfo::checked
long checked
Definition:
mcknap.h:185
allinfo::range
itype range
Definition:
mcknap.h:166
ordstack::first
ordrec * first
Definition:
mcknap.h:136
boolean
int boolean
Definition:
mcknap.h:69
ordintv::l
ordrec * l
Definition:
mcknap.h:128
allinfo::zstar
stype zstar
Definition:
mcknap.h:170
allinfo::lpkill
stype lpkill
Definition:
mcknap.h:180
itemrec::psum
itype psum
Definition:
mcknap.h:90
stype
double stype
Definition:
mcknap.h:75
isetset::fset
itemset * fset
Definition:
mcknap.h:113
solstruct::vect
vtype vect
Definition:
mcknap.h:153
allinfo::type
int type
Definition:
mcknap.h:165
allinfo::n
ntype n
Definition:
mcknap.h:164
allinfo::dantzig
stype dantzig
Definition:
mcknap.h:169
visitems
void visitems(itemset *d)
partvect::wsum
stype wsum
Definition:
mcknap.h:82
minmcknapSolve
int minmcknapSolve(int cap, isetset *head, itemrec *solRec, stype *minObj)
isetset::size
ntype size
Definition:
mcknap.h:115
vtype
unsigned long vtype
Definition:
mcknap.h:76
ordintv::f
ordrec * f
Definition:
mcknap.h:127
Generated by
1.8.5