1 package cz.cuni.amis.pathfinding.alg.astar;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Set;
7 import java.util.Stack;
8
9 import cz.cuni.amis.utils.heap.Heap;
10
11
12
13
14
15
16
17
18
19
20
21 public class AStarResult<NODE> implements IAStarResult<NODE> {
22
23
24
25
26
27
28 public HashMap<NODE, NODE> previousNode = new HashMap<NODE, NODE>();
29
30
31
32
33
34
35
36 public Heap<NODE> openList;
37
38
39
40
41
42
43
44 public Set<NODE> closeList;
45
46
47
48
49
50 public HashMap<NODE, Integer> pathCost = new HashMap<NODE, Integer>();
51
52
53
54
55
56 public HashMap<NODE, Integer> estimatedCost = new HashMap<NODE, Integer>();
57
58
59
60
61
62
63 public long interations = 0;
64
65
66
67
68 public NODE startNode = null;
69
70
71
72
73
74
75 public NODE goalNode = null;
76
77
78
79
80
81 public boolean success = false;
82
83
84
85
86
87 private List<NODE> path = null;
88
89 @Override
90 public NODE getPreviousNode(NODE node){
91 if (previousNode.containsKey(node)) return previousNode.get(node);
92 else return null;
93 }
94
95
96
97
98
99
100 public void putPreviousNode(NODE node, NODE previous){
101 previousNode.put(node, previous);
102 }
103
104 @Override
105 public int getCostToNode(NODE node){
106 if (pathCost.containsKey(node))
107 return (pathCost.get(node)).intValue();
108 else
109 return -1;
110 }
111
112
113
114
115
116
117 public void putCostToNode(NODE node, Integer cost){
118 pathCost.put(node, cost);
119 }
120
121 @Override
122 public int getEstimatedCostToNode(NODE node){
123 if (estimatedCost.containsKey(node))
124 return estimatedCost.get(node);
125 else
126 return -1;
127 }
128
129
130
131
132
133
134 public void putEstimatedCostToNode(NODE node, Integer cost){
135 estimatedCost.put(node, cost);
136 }
137
138 @Override
139 public List<NODE> getPath(){
140 if (path != null)
141 return path;
142 if (!success)
143 return null;
144
145 Stack<NODE> tempPath = new Stack<NODE>();
146 tempPath.push(goalNode);
147
148 NODE node = goalNode;
149 while (node != startNode){
150 node = getPreviousNode(node);
151 if (node == null)
152 return null;
153 tempPath.push(node);
154 }
155
156 path = new ArrayList<NODE>();
157
158 while (!tempPath.empty()){
159 path.add(tempPath.pop());
160 }
161
162 return path;
163 }
164
165 @Override
166 public int getDistanceToGoal(){
167 if (!this.success) return -1;
168 return (this.pathCost.get(goalNode)).intValue();
169 }
170
171 @Override
172 public boolean isSuccess() {
173 return success;
174 }
175
176 }