-
Notifications
You must be signed in to change notification settings - Fork 2
/
Util.java
81 lines (69 loc) · 1.71 KB
/
Util.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
import java.util.*;
/**
* Utility functions
* @author Shalin Shah
* Email: shalin@alumni.usc.edu
*/
public class Util {
/** Creates a new instance of Util */
public Util() {
}
public static Random random = new Random();
public static int generateRandomNumber(int min, int max)
{
int r;
r = (int) (random.nextDouble() * (max - min + 1)) + min;
return r;
}
public static double generateDoubleRandom(double min, double max)
{
double r;
r = (double) (random.nextDouble() * (max - min)) + min;
return r;
}
public static boolean isValidSolution(KNode sack)
{
if(sack.weight() <= Constants.MAX_WEIGHT)
{
return true;
}
return false;
}
public static boolean isValidSolution(BitSet sack)
{
int weight = calculateWeight(sack);
if(weight <= Constants.MAX_WEIGHT)
{
return true;
}
return false;
}
public static int calculateWeight(BitSet set)
{
int weight = 0;
for(int i=0; i<Constants.NUMBER_OBJECTS; i++)
{
if(set.get(i) == true)
{
weight+=Constants.WEIGHTS[i];
}
}
return weight;
}
public static int calculateValue(BitSet set)
{
int value = 0;
for(int i=0; i<Constants.NUMBER_OBJECTS; i++)
{
if(set.get(i) == true)
{
value+=Constants.VALUES[i];
}
}
return value;
}
public static BitSet createEmptyKnapsack()
{
return new BitSet(Constants.NUMBER_OBJECTS);
}
}