-
Notifications
You must be signed in to change notification settings - Fork 9
/
Utilities.java
113 lines (98 loc) · 3.31 KB
/
Utilities.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
import java.util.ArrayList;
class Utilities {
public void show_fringe_astar(ArrayList<Fringe_element> fringe) {
System.out.println("Showing Fringe:");
for (Fringe_element f : fringe) {
System.out.println("id: "+letter(f.n.id)+"\nheuristic: "+f.n.heuristic+"\nfunction f: "+f.f_function);
System.out.println("--------------");
}
System.out.println("");
}
public void show_fringe(ArrayList<Fringe_element> fringe) {
System.out.println("Showing Fringe:");
for (Fringe_element f : fringe) {
System.out.println("id: "+letter(f.n.id)+"\nfunction f: "+f.f_function);
System.out.println("--------------");
}
System.out.println("");
}
public void show_fringe_greedy(ArrayList<Fringe_element> fringe) {
System.out.println("Showing Fringe:");
for (Fringe_element f : fringe) {
System.out.println("id: "+letter(f.n.id)+"\nheuristic: "+f.n.heuristic);
System.out.println("--------------");
}
System.out.println("");
}
public ArrayList<Integer> copy(ArrayList<Integer> i) {
ArrayList<Integer> ret = new ArrayList<Integer>();
for (int j = 0; j < i.size(); j++) {
ret.add(i.get(j));
}
return ret;
}
public String letter(int n) {
String letter[] = {"A","B","C","D","F","I"};
return letter[n];
}
public void sort_astar(ArrayList<Fringe_element> fringe) {
for(int i = 0; i < fringe.size(); i++) {
for(int j = 0; j < fringe.size()-i-1; j++) {
if (fringe.get(j).get_g_function() > fringe.get(j+1).get_g_function()) {
Fringe_element temp = fringe.get(j);
fringe.set(j, fringe.get(j+1));
fringe.set(j+1, temp);
} else if (fringe.get(j).get_g_function() == fringe.get(j+1).get_g_function()) {
if (fringe.get(j).n.id > fringe.get(j+1).n.id) {
Fringe_element temp = fringe.get(j);
fringe.set(j, fringe.get(j+1));
fringe.set(j+1, temp);
}
}
}
}
}
public void sort(ArrayList<Fringe_element> fringe) {
for(int i = 0; i < fringe.size(); i++) {
for(int j = 0; j < fringe.size()-i-1; j++) {
if (fringe.get(j).get_f_function() > fringe.get(j+1).get_f_function()) {
Fringe_element temp = fringe.get(j);
fringe.set(j, fringe.get(j+1));
fringe.set(j+1, temp);
} else if (fringe.get(j).get_f_function() == fringe.get(j+1).get_f_function()) {
if (fringe.get(j).n.id > fringe.get(j+1).n.id) {
Fringe_element temp = fringe.get(j);
fringe.set(j, fringe.get(j+1));
fringe.set(j+1, temp);
}
}
}
}
}
public void sort_greedy(ArrayList<Fringe_element> fringe) {
for(int i = 0; i < fringe.size(); i++) {
for(int j = 0; j < fringe.size()-i-1; j++) {
if (fringe.get(j).n.heuristic > fringe.get(j+1).n.heuristic) {
Fringe_element temp = fringe.get(j);
fringe.set(j, fringe.get(j+1));
fringe.set(j+1, temp);
} else if (fringe.get(j).n.heuristic == fringe.get(j+1).n.heuristic) {
if (fringe.get(j).n.id > fringe.get(j+1).n.id) {
Fringe_element temp = fringe.get(j);
fringe.set(j, fringe.get(j+1));
fringe.set(j+1, temp);
}
}
}
}
}
public void end(Fringe_element aux, String name) {
System.out.println(name+" found a solution!");
for (int i = 0; i < aux.visited.size(); i++) {
System.out.print(letter(aux.visited.get(i)));
if (i+1 < aux.visited.size())
System.out.print("->");
}
System.out.println("\nCost: "+aux.f_function);
}
}