-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolver.java
180 lines (170 loc) · 3.94 KB
/
Solver.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
/**
* A class to solve using items and cargo
*
* @author Sarah Waseem
* @author Pierre Bongrand
* @version 0.1, 09-01-2018
*
* @author Julián Marrades
* @version 0.2, 09-01-2018
*
* @author Julián Marrades
* @version 0.3, 10-01-2018
*
* @author Julián Marrades
* @version 0.4, 12-01-2018
*
* @author Pierre Bongrand
* @version 0.5, 13-01-2018
*/
public class Solver
{
private Item[] items;
private Cargo cargo;
private String name;
/**
* Default Solver constructor
* @param name the name of the solver
* @param items the array of items
* @param cargo the cargo object
*/
public Solver(String name, Item[] items, Cargo cargo)
{
this.name = name;
this.items = items;
this.cargo = cargo;
}
/**
* Get access to the name
* @return the name of the solver
*/
public String getName() {
return this.name;
}
/**
* Set a new name for the solver
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get access to the items
* @return the items
*/
public Item[] getItems()
{
return this.items;
}
/**
* Sets the items
* @param items the new array of items
*/
public void setItems(Item[] items)
{
this.items = items;
}
/**
* Gets access to cargo
* @return cargo
*/
public Cargo getCargo()
{
return this.cargo;
}
/**
* Sets cargo
* @param cargo the new cargo
*/
public void setCargo(Cargo cargo)
{
this.cargo = cargo;
}
/**
* Get a string representation
* @return a string containing the detailed information
*/
public String toString()
{
String result = this.getClass().getName() + ":\n";
result += " - Name -> " + this.name;
result += "\n - Cargo -> " + this.cargo.toString();
result += "\n - Items:";
for (Item item : this.items) {
result += "\n\t- " + item.toString();
}
return result;
}
/**
* Clone the current Solver
* @return the cloned solver object
*/
public Solver clone() {
Item[] new_items = Arrays.cloneArray(this.items);
return new Solver(this.name, new_items, this.cargo.clone());
}
/**
* Fill the cargo with the greedy algorithm
* @param random is random filling wanted?
* @param output want to get detailed solution?
*/
public void fillGreedyCargo(boolean random, boolean output) {
Item[] sorted = null;
Item[] all = null;
if (random) {
sorted = Item.getAllShapes(this.items);
all = shuffle(sorted);
}
else {
sorted = Item.jSort(this.items);
all = Item.getAllShapes(sorted);
}
// Loop through the whole cargo and fill any empty space
for (int j = 0; j < this.cargo.getHeight(); j++) {
for (int i = 0; i < this.cargo.getWidth(); i++) {
for (int k = 0; k < this.cargo.getDepth(); k++) {
if (this.cargo.check(i, j, k) == null) {
this.tryFill(all, i, j, k);
}
}
}
}
// Backtracking.print3DArray(this.cargo.getShape());
this.cargo.printSolution(this.items, false, output);
}
/**
* Shuffle an array of elements
* @param ori original arrey
* @return shuffled array
*/
public static Item[] shuffle(Item[] ori) {
Item[] result = new Item[ori.length];
int i = 0;
while(i < ori.length) {
int j = (int) (Math.random()*ori.length);
if(result[j] == null){
result[j] = ori[i].clone();
i++;
}
}
return result;
}
/**
* Try to fit an item from a set in a position
* @param items the set of items which can be put in the cargo
* @param i the position along the x-axis
* @param j the position along the y-axis
* @param k the position along the z-axis
*/
private void tryFill(Item[] items, int i, int j, int k) {
boolean filled = false;
int index = 0;
while (!filled && index < items.length) {
if (this.cargo.canBePut(items[index], i, j, k)) {
this.cargo.put(items[index], i, j, k);
filled = true;
}
index++;
}
}
}