-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxState.java
134 lines (108 loc) · 3.05 KB
/
boxState.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
import java.util.*;
class boxState extends state {
private final List <box> toFill;
private final List <box> toPut;
private final String assignment; //assignment of one box into another that led to this state
//start state
public boxState(Scanner in) {
super();
List <box> tempToFill = new ArrayList <box>();
List <box> tempToPut = new ArrayList <box>();
//read in boxes
while (in.hasNext()) {
box newBox = new box(in.next(), in.nextFloat(), in.nextFloat(), in.nextFloat());
tempToFill.add(newBox);
tempToPut.add(newBox);
}
//will try to fill one box at a time, in order of increasing volume
Collections.sort(tempToFill);
//toPut must be in descending order for the heuristic function
Collections.sort(tempToPut, Collections.reverseOrder());
toFill = Collections.unmodifiableList(tempToFill);
toPut = Collections.unmodifiableList(tempToPut);
assignment = "";
}
//regular state
public boxState(state parent, double cost, String assignment, List <box> toFill, List <box> toPut) {
super(parent, cost);
this.assignment = assignment;
this.toFill = toFill;
this.toPut = toPut;
}
@Override
protected Collection <state> successors() {
Collection <state> successors = new ArrayList <state>();
List <box> newToFill = new ArrayList <box>(toFill);
boolean boxFilled = false;
while (!newToFill.isEmpty() && !boxFilled) {
//try to fill first box
box om = newToFill.remove(0);
//try every box to put in
for (int i = 0; i < toPut.size(); i++) {
box nom = toPut.get(i);
if (om.canHas(nom)) {
if (!boxFilled) {
//we won't be removing any more from newToFill
newToFill = Collections.unmodifiableList(newToFill);
boxFilled = true;
}
List <box> newToPut = new ArrayList <box>(toPut);
newToPut.remove(i);
newToPut = Collections.unmodifiableList(newToPut);
successors.add(new boxState(
this,
//cost is the amount of extra space left in the outer box
om.volume - nom.volume,
nom.name + " -> " + om.name,
newToFill,
newToPut
));
}
}
}
return successors;
}
//heuristic: least possible amount of space wasted (every remaining box to fill gets the larget box it can hold)
@Override
protected double calcH() {
double h = 0;
for (box om : toFill) {
for (box nom : toPut) {
if (om.canHas(nom)) { //this is the largest box om can has
h += om.volume - nom.volume;
break;
}
}
}
return h;
}
@Override
public String toString() {
return assignment;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final boxState other = (boxState) obj;
if (this.toFill != other.toFill &&
(this.toFill == null || !this.toFill.equals(other.toFill))) {
return false;
}
if (this.toPut != other.toPut && (this.toPut == null || !this.toPut.equals(other.toPut))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + (this.toFill != null ? this.toFill.hashCode() : 0);
hash = 89 * hash + (this.toPut != null ? this.toPut.hashCode() : 0);
return hash;
}
}