-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaggingProblem.java
198 lines (156 loc) · 6.33 KB
/
BaggingProblem.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import java.util.*;
import java.io.*;
public class BaggingProblem {
boolean[][] canPackWith;
Vector<Bag> bags = new Vector<Bag>();
HashMap<String, Item> items = new HashMap<String, Item>();
public class Item implements Comparable<Item> {
int id = items.size();
String name;
int size;
Bag bag = null;
StringTokenizer st_constraints;
Vector<Integer> domain; // bag ids I could possibly pack it into
int numConstraints = 0;
public Item(String name, int size, StringTokenizer st_constraints) {
this.name = name;
this.size = size;
this.st_constraints = st_constraints;
}
@Override
public int compareTo(BaggingProblem.Item i) {
int sizeDiff = this.size - i.size;
if (sizeDiff == 0) { // if equal in size, pick by constraint
return this.numConstraints - i.numConstraints;
}
return i.size - this.size;
}
}
public class Bag {
int id = bags.size();
int maxSize;
int currSize = 0;
HashMap<String, Item> packedInMe = new HashMap<String, Item>();
public Bag(int maxBagSize) {
this.maxSize = maxBagSize;
}
public boolean pack(Item i) {
if (i.size > maxSize-currSize) {
return false; // does not fit
}
// check constraints
for (Item j : packedInMe.values()) {
if (!canPackWith[i.id][j.id]) {
return false; // cannot pack with item already inside bag
}
}
// no problems add to bag
currSize += i.size;
packedInMe.put(i.name, i);
i.bag = this;
return true;
}
public void unpack(Item i) {
// remove item from bag
packedInMe.remove(i.name);
currSize -= i.size;
i.bag = null;
}
}
public BaggingProblem(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(args[0]));
int nb = Integer.parseInt(br.readLine());
int maxBagSize = Integer.parseInt(br.readLine());
for (int x=0; x < nb; x++) bags.add(new Bag(maxBagSize));
String line;
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
String name = st.nextToken();
int size = Integer.parseInt(st.nextToken());
if(items.get(name)!=null) throw new RuntimeException("Duplicate item name in file.");
items.put(name, new Item(name, size, st));
}
canPackWith = new boolean[items.size()][items.size()];
for (int x=0; x < canPackWith.length; x++) Arrays.fill(canPackWith[x], true);
for (Item i : items.values()) {
if (i.st_constraints.hasMoreTokens()) { // has a '+' or '-'
if (i.st_constraints.nextToken().equals("+")) { // is '+'
Vector<String> goodStuff = new Vector<String>();
while (i.st_constraints.hasMoreTokens()) {
goodStuff.add(i.st_constraints.nextToken());
}
for (String itemName : items.keySet()) { // others cannot be with
if (!goodStuff.contains(itemName)) {
var j = items.get(itemName);
if (canPackWith[i.id][j.id]) { // only set if true
i.numConstraints++;
j.numConstraints++;
canPackWith[i.id][j.id] = false; // cannot pack i and j
canPackWith[j.id][i.id] = false; // cannot pack j and i
}
}
}
} else { // is "-", cant pack with
while (i.st_constraints.hasMoreTokens()) {
Item j = items.get(i.st_constraints.nextToken());
if (canPackWith[i.id][j.id]) { // only set if true
i.numConstraints++;
j.numConstraints++;
canPackWith[i.id][j.id] = false; // cannot pack i and j
canPackWith[j.id][i.id] = false; // cannot pack j and i
}
}
}
}
}
} catch (FileNotFoundException ex) {
System.out.println("File not found. Please enter a valid file path.");
} catch (IOException ex) {
System.out.println("File format not correct. Here is the error: " + ex);
}
}
public boolean search(PriorityQueue<Item> pq) {
if (pq.isEmpty()) { // BASE CASE: packed all the items!
return true;
}
Item i = pq.remove();
for (Bag b : bags) {
if (b.pack(i)) {
if (search(pq)) {
return true; // worked in this bag!
} else {
// that item didn't work in the bag
// try in another bag
b.unpack(i);
}
}
}
pq.add(i);
return false; // failed, item cannot go in any bag
}
public boolean search() {
PriorityQueue<Item> pq = new PriorityQueue<Item>();
for (Item i : items.values()) {
pq.add(i);
}
return search(pq);
}
public String printPacking() {
// This method will only be called on success
StringBuilder result = new StringBuilder("success\n");
for (Bag b : bags) {
boolean firstItem = true;
for (Item i : b.packedInMe.values()) {
if (!firstItem) { // don't put tab before first item
result.append("\t");
}
result.append(i.name);
firstItem = false;
}
result.append("\n"); // next line
}
System.out.println(result);
return result.toString();
}
}