-
Notifications
You must be signed in to change notification settings - Fork 2
/
KNode.java
122 lines (100 loc) · 2.32 KB
/
KNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.util.*;
/**
* The knapsack node in the branch and bound tree.
* @author Shalin Shah
* Email: [email protected]
*/
public class KNode implements Comparable
{
private BitSet knapsack = null;
private double maxValue = 0;
private boolean pruned = false;
private boolean fathomed = false;
private int size = 0;
private int index = 0;
private int value;
private int weight;
/** Creates a new instance of KNode */
public KNode(BitSet k, int ind, int w, int v) {
knapsack = k;
index = ind;
weight = w;
value = v;
}
public KNode(BitSet set, int ind)
{
knapsack = set;
index = ind;
weight = Util.calculateValue(set);
value = Util.calculateValue(set);
}
public int getIndex()
{
return index;
}
public int weight()
{
if(knapsack == null)
return 0;
return weight;
}
public int value()
{
if(knapsack == null)
return 0;
return value;
}
public void fathom()
{
fathomed = true;
}
public void prune()
{
pruned = true;
}
public boolean isFathomed()
{
return fathomed;
}
public boolean isPruned()
{
return pruned;
}
public void addOne()
{
value+=Constants.VALUES[index];
weight+=Constants.WEIGHTS[index];
knapsack.set(index, true);
index++;
maxValue = Relaxation.calculateValue(knapsack, index);
}
public void addZero()
{
knapsack.set(index, false);
index++;
maxValue = Relaxation.calculateValue(knapsack, index);
}
public int size()
{
return size;
}
public double maxValue()
{
return maxValue;
}
public int compareTo(Object obj) {
KNode o = (KNode)obj;
if(o.maxValue() > this.maxValue())
return 1;
else if(o.maxValue() < this.maxValue())
return -1;
if(knapsack.equals(o.getKnapsackContents()) && o.getIndex() == index)
return 0;
// unreachable
return -1;
}
public BitSet getKnapsackContents()
{
return knapsack;
}
}