/
FastSuboptimalFinder.java
160 lines (134 loc) · 5.62 KB
/
FastSuboptimalFinder.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package net.coderodde.cskit.graph.p2psp.general;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static net.coderodde.cskit.Utilities.tracebackPathBidirectional;
import net.coderodde.cskit.ds.pq.PriorityQueue;
import net.coderodde.cskit.graph.DirectedGraphNode;
import net.coderodde.cskit.graph.DirectedGraphWeightFunction;
/**
* This class implements a bidirectional <tt>A*</tt>-search algorithm that is
* fast yet suboptimal in general case.
*
* @author Rodion Efremov
* @version 1.618 (16.12.2013)
*/
public class FastSuboptimalFinder extends GeneralPathFinder {
private HeuristicFunction h;
private HeuristicFunction h2;
private PriorityQueue<DirectedGraphNode, Double> OPEN2;
private Set<DirectedGraphNode> CLOSED2;
private Map<DirectedGraphNode, Double> GSCORE_MAP2;
private Map<DirectedGraphNode, DirectedGraphNode> PARENT_MAP2;
public FastSuboptimalFinder(PriorityQueue<DirectedGraphNode, Double> OPEN,
HeuristicFunction h,
HeuristicFunction h2) {
super(OPEN);
this.h = h;
this.h2 = h2;
OPEN2 = OPEN.newInstance();
CLOSED2 = new HashSet<DirectedGraphNode>();
GSCORE_MAP2 = new HashMap<DirectedGraphNode, Double>();
PARENT_MAP2 = new HashMap<DirectedGraphNode, DirectedGraphNode>();
}
@Override
public List<DirectedGraphNode> find(DirectedGraphNode source,
DirectedGraphNode target,
DirectedGraphWeightFunction w) {
h.setTarget(target);
OPEN.clear();
CLOSED.clear();
GSCORE_MAP.clear();
PARENT_MAP.clear();
h2.setTarget(source);
OPEN2.clear();
CLOSED2.clear();
GSCORE_MAP2.clear();
PARENT_MAP2.clear();
OPEN.insert(source, h.get(source));
PARENT_MAP.put(source, null);
GSCORE_MAP.put(source, 0.0);
OPEN2.insert(target, h2.get(target));
PARENT_MAP2.put(target, null);
GSCORE_MAP2.put(target, 0.0);
double m = Double.POSITIVE_INFINITY;
DirectedGraphNode touch = null;
while ((OPEN.isEmpty() == false) && (OPEN2.isEmpty() == false)) {
if (touch != null) {
double L1 = GSCORE_MAP.get(touch);
double L2 = GSCORE_MAP2.get(touch);
if (m >= L1 + L2) {
return tracebackPathBidirectional(touch,
PARENT_MAP,
PARENT_MAP2);
}
}
DirectedGraphNode current = OPEN.extractMinimum();
CLOSED.add(current);
for (DirectedGraphNode child : current) {
if (CLOSED.contains(child)) {
continue;
}
double tmpg = GSCORE_MAP.get(current) + w.get(current, child);
if (GSCORE_MAP.containsKey(child) == false) {
OPEN.insert(child, tmpg + h.get(child));
GSCORE_MAP.put(child, tmpg);
PARENT_MAP.put(child, current);
if (CLOSED2.contains(child)) {
if (m > tmpg + GSCORE_MAP2.get(child)) {
m = tmpg + GSCORE_MAP2.get(child);
touch = child;
}
}
} else if (tmpg < GSCORE_MAP.get(child)) {
OPEN.decreasePriority(child, tmpg + h.get(child));
GSCORE_MAP.put(child, tmpg);
PARENT_MAP.put(child, current);
if (CLOSED2.contains(child)) {
if (m > tmpg + GSCORE_MAP2.get(child)) {
m = tmpg + GSCORE_MAP2.get(child);
touch = child;
}
}
}
}
current = OPEN2.extractMinimum();
CLOSED2.add(current);
for (DirectedGraphNode parent : current.parentIterable()) {
if (CLOSED2.contains(parent)) {
continue;
}
double tmpg = GSCORE_MAP2.get(current) + w.get(parent, current);
if (GSCORE_MAP2.containsKey(parent) == false) {
OPEN2.insert(parent, tmpg + h2.get(parent));
GSCORE_MAP2.put(parent, tmpg);
PARENT_MAP2.put(parent, current);
if (CLOSED.contains(parent)) {
if (m > tmpg + GSCORE_MAP.get(parent)) {
m = tmpg + GSCORE_MAP.get(parent);
touch = parent;
}
}
} else if (tmpg < GSCORE_MAP2.get(parent)) {
OPEN2.decreasePriority(parent, tmpg + h2.get(parent));
GSCORE_MAP2.put(parent, tmpg);
PARENT_MAP2.put(parent, current);
if (CLOSED.contains(parent)) {
if (m > tmpg + GSCORE_MAP.get(parent)) {
m = tmpg + GSCORE_MAP.get(parent);
touch = parent;
}
}
}
}
}
if (touch != null) {
return tracebackPathBidirectional(touch,
PARENT_MAP,
PARENT_MAP2);
}
return java.util.Collections.<DirectedGraphNode>emptyList();
}
}