-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node2.java
85 lines (63 loc) · 1.98 KB
/
Node2.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
import java.util.ArrayList;
import java.util.List;
/**
* A class defining the Node2 data structure. It differs from the Node structure by
* adding two object parameters heuristicValue and the evaluationValue of the given node.
*
* @author greedywind / James Archer
*
*/
public class Node2 extends Node {
public Node current;
private String goalState;
public int heuristicValue;
public int evaluationValue;
public Node2 (Node current, String goalState){
super(current.getState());
this.current= current;
this.goalState=goalState;
}
/**
* Computes the heuristic by counting the number of people or (ones) in a state and subtracts by 2 if
* the boat is present.
*/
public final void computeHeuristic() {
String temp=this.current.getState();
int count = temp.length() - temp.replace("1", "").length();
if(temp.endsWith("0")){
this.heuristicValue=count;
}else{
this.heuristicValue=count-1;
}
}
/**
* Computes the evaluation function by adding the heuristic value and the path cost to the node.
*/
public final void computeEvaluation(){
if (this.heuristicValue!=0){
this.evaluationValue= this.heuristicValue + this.current.getPathCost();
}
}
public final Node2 newInstance(){
Node2 n= new Node2(this, this.goalState);
return n;
}
/**
* Returns the cost of the path from the initial state to this node as
* indicated by the parent pointers.
*
* @return the cost of the path from the initial state to this node as
* indicated by the parent pointers.
*/
public List<Node2> getPathFromRoot2() {
List<Node2> path = new ArrayList<>();
Node2 currentNode = this;
while (!currentNode.current.isRootNode()) {
path.add(0, currentNode);
currentNode = new Node2(currentNode.current.getParent(),currentNode.goalState);
}
// ensure the root node is added
path.add(0, currentNode);
return path;
}
}