-
Notifications
You must be signed in to change notification settings - Fork 2
/
GreedySolution.java
51 lines (47 loc) · 1.51 KB
/
GreedySolution.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
import java.util.*;
/**
* Greedy algorithm - Used as the incumbent in the branch and bound procedure
* @author Shalin Shah
* Email: shalin@alumni.usc.edu
*/
public class GreedySolution
{
public static void main(String [] args) throws Exception
{
DataProcessor.processData();
System.out.println(runGreedyAlgorithm().value());
System.out.println(runGreedyAlgorithm().getKnapsackContents());
}
/** Run the greedy algorithm for the 0/1 knapsack problem */
public static KNode runGreedyAlgorithm()
{
List l = new ArrayList();
int greedyEstimate = 0;
for(int i=0; i < Constants.NUMBER_OBJECTS; i++)
{
int w = Constants.WEIGHTS[i];
int v = Constants.VALUES[i];
double ratio = (double)v/(double)w;
CompareObject o = new CompareObject();
o.valueWeightRatio = ratio;
o.objectNumber = i;
l.add(o);
}
Collections.sort(l);
KNode sack = new KNode(Util.createEmptyKnapsack(), 0, 0, 0);
BitSet s = sack.getKnapsackContents();
Iterator it = l.iterator();
while(it.hasNext())
{
CompareObject obj = (CompareObject)it.next();
int ono = obj.objectNumber;
s.set(ono, true);
KNode temp = new KNode(s, 0);
if(!Util.isValidSolution(temp.getKnapsackContents()))
{
s.set(ono, false);
}
}
return new KNode(s, 0);
}
}