-
Notifications
You must be signed in to change notification settings - Fork 9
/
HillClimbing.java
54 lines (41 loc) · 1.21 KB
/
HillClimbing.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
import java.util.ArrayList;
public class HillClimbing {
private ArrayList<Fringe_element> fringe;
private ArrayList<Node> nodes;
private Utilities util;
HillClimbing(ArrayList<Node> nodes) {
this.nodes = nodes;
this.fringe = new ArrayList<Fringe_element>();
this.util = new Utilities();
}
public void solve(int source, int destiny, int show) {
Fringe_element f = new Fringe_element(nodes.get(source), 0);
this.fringe.add(f);
f.visited.add(source);
Fringe_element aux;
while(fringe.size() > 0) {
if (show == 1)
this.util.show_fringe(this.fringe);
aux = this.fringe.remove(0);
if (aux.n.id == destiny) {
this.util.end(aux, "Hill Climbing");
return;
}
int min = getMin(aux.n);
Node n = this.nodes.get(aux.n.childs.get(min));
f = new Fringe_element(n, aux.f_function + aux.n.costs.get(min), this.util.copy(aux.visited));
f.visited.add(n.id);
this.fringe.add(f);
}
System.out.println("ERROR! SOLUTION NOT FOUND!");
}
private int getMin(Node n) {
int min = 0;
for (int i = 1; i < n.childs.size(); i++) {
if (this.nodes.get(n.childs.get(i)).heuristic < this.nodes.get(n.childs.get(min)).heuristic) {
min = i;
}
}
return min;
}
}