-
Notifications
You must be signed in to change notification settings - Fork 368
/
aostar.java
144 lines (135 loc) · 3.53 KB
/
aostar.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
import java.util.*;
public class aostar {
public static Map<String, Integer>
Cost(Map<String, Integer> H,
Map<String, List<String> > condition, int weight)
{
Map<String, Integer> cost = new HashMap<>();
if (condition.containsKey("AND")) {
List<String> AND_nodes = condition.get("AND");
String Path_A = String.join(" AND ", AND_nodes);
int PathA
= AND_nodes.stream()
.mapToInt(
node -> H.get(node) + weight)
.sum();
cost.put(Path_A, PathA);
}
if (condition.containsKey("OR")) {
List<String> OR_nodes = condition.get("OR");
String Path_B = String.join(" OR ", OR_nodes);
int PathB
= OR_nodes.stream()
.mapToInt(
node -> H.get(node) + weight)
.min()
.getAsInt();
cost.put(Path_B, PathB);
}
return cost;
}
public static Map<String, Map<String, Integer> >
UpdateCost(
Map<String, Integer> H,
Map<String, Map<String, List<String> > > Conditions,
int weight)
{
List<String> Main_nodes
= new ArrayList<>(Conditions.keySet());
Collections.reverse(Main_nodes);
Map<String, Map<String, Integer> > least_cost
= new HashMap<>();
for (String key : Main_nodes) {
Map<String, List<String> > condition
= Conditions.get(key);
System.out.printf("%s: %s >>> %s%n", key,
condition,
Cost(H, condition, weight));
Map<String, Integer> c
= Cost(H, condition, weight);
H.put(key, Collections.min(c.values()));
least_cost.put(key, Cost(H, condition, weight));
}
return least_cost;
}
public static String ShortestPath(
String Start,
Map<String, Map<String, Integer> > Updated_cost,
Map<String, Integer> H)
{
String Path = Start;
if (Updated_cost.containsKey(Start)) {
int Min_cost = Collections.min(
Updated_cost.get(Start).values());
List<String> key = new ArrayList<>(
Updated_cost.get(Start).keySet());
List<Integer> values = new ArrayList<>(
Updated_cost.get(Start).values());
int Index = values.indexOf(Min_cost);
List<String> Next
= Arrays.asList(key.get(Index).split(" "));
if (Next.size() == 1) {
Start = Next.get(0);
Path += "<--"
+ ShortestPath(Start, Updated_cost,
H);
}
else {
Path += "<--(" + key.get(Index) + ") ";
Start = Next.get(0);
Path += "["
+ ShortestPath(Start, Updated_cost,
H)
+ " + ";
Start = Next.get(Next.size() - 1);
Path += ShortestPath(Start, Updated_cost, H)
+ "]";
}
}
return Path;
}
public static void main(String[] args)
{
Map<String, Integer> H = new HashMap<>();
H.put("A", -1);
H.put("B", 5);
H.put("C", 2);
H.put("D", 4);
H.put("E", 7);
H.put("F", 9);
H.put("G", 3);
H.put("H", 0);
H.put("I", 0);
H.put("J", 0);
Map<String, Map<String, List<String> > > Conditions
= new HashMap<>();
Map<String, List<String> > aConditions
= new HashMap<>();
aConditions.put("OR", Arrays.asList("B"));
aConditions.put("AND", Arrays.asList("C", "D"));
Conditions.put("A", aConditions);
Map<String, List<String> > bConditions
= new HashMap<>();
bConditions.put("OR", Arrays.asList("E", "F"));
Conditions.put("B", bConditions);
Map<String, List<String> > cConditions
= new HashMap<>();
cConditions.put("OR", Arrays.asList("G"));
cConditions.put("AND", Arrays.asList("H", "I"));
Conditions.put("C", cConditions);
Map<String, List<String> > dConditions
= new HashMap<>();
dConditions.put("OR", Arrays.asList("J"));
Conditions.put("D", dConditions);
// weight
int weight = 1;
// Updated cost
System.out.println("Updated Cost :");
Map<String, Map<String, Integer> > Updated_cost
= UpdateCost(H, Conditions, weight);
System.out.println("*".repeat(75));
System.out.println("Shortest Path :");
System.out.println(
ShortestPath("A", Updated_cost, H));
}
}