-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolutionMinimizeRemainingCapacity.java
64 lines (51 loc) · 2 KB
/
SolutionMinimizeRemainingCapacity.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SolutionMinimizeRemainingCapacity {
private Problem p;
private ArrayList<Truck> routes;
public static void main(String[] args) {
SolutionMinimizeRemainingCapacity s = new SolutionMinimizeRemainingCapacity();
s.solution();
}
public void solution() {
int numberOfCustomers = 10;
p = new Problem();
// Generate problem, using birthday (9 feb 1998) as random seed.
int seed = 9021998;
p.createAllNodesAndCustomerLists(numberOfCustomers, seed);
// We need to start adding the customers with the highest demand first
// so that we can minimize the remaining capacity in our trucks.
sortByHighestDemand(p.getCustomers());
routes = new ArrayList<Truck>();
Node depot = p.getAllNodes().get(0);
Truck truck = new Truck(depot);
for (Node cust : p.getCustomers()) {
// Will only add a customer if capacity allows.
boolean ok = truck.addCustomer(cust);
// When we reach full capacity, we need a new truck.
if (!ok) {
routes.add(truck);
truck = new Truck(depot);
continue;
}
}
// Add remaining truck.
routes.add(truck);
System.out.println("To minimize our truck remaining capacity, we added the customers with highest demand first.");
System.out.printf("We ended up with %d trucks:\n\n", routes.size());
for (Truck t : routes) {
System.out.println("-> " + t);
}
}
private void sortByHighestDemand(ArrayList<Node> customers) {
Collections.sort(customers, new Comparator<Node>(){
public int compare(Node a, Node b){
if (a.demand == b.demand) {
return 0;
}
return a.demand > b.demand ? -1 : 1;
}
});
}
}