|
1
|
|
|
package it.cnr.istc.pst.platinum.ai.framework.time.solver.apsp; |
|
2
|
|
|
|
|
3
|
|
|
import java.util.ArrayList; |
|
4
|
|
|
import java.util.HashMap; |
|
5
|
|
|
import java.util.List; |
|
6
|
|
|
import java.util.Map; |
|
7
|
|
|
|
|
8
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.time.tn.TimePoint; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* |
|
12
|
|
|
* @author alessandro |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
public class DistanceGraph |
|
16
|
|
|
{ |
|
17
|
|
|
private long infty; // temporal horizon |
|
18
|
|
|
private Map<Integer, TimePoint> nodes; // list of points |
|
19
|
|
|
private Map<TimePoint, Map<TimePoint, Long>> edges; // list of weighted edges |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
protected DistanceGraph() { |
|
25
|
|
|
this.nodes = new HashMap<>(); |
|
26
|
|
|
this.edges = new HashMap<>(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @param horizon |
|
32
|
|
|
*/ |
|
33
|
|
|
public void setInfity(long horizon) { |
|
34
|
|
|
this.infty = horizon + 1; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* |
|
39
|
|
|
* @return |
|
40
|
|
|
*/ |
|
41
|
|
|
public long getInfity() { |
|
42
|
|
|
return this.infty; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* |
|
47
|
|
|
* @return |
|
48
|
|
|
*/ |
|
49
|
|
|
public int size() { |
|
50
|
|
|
return this.nodes.size(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
* @return |
|
56
|
|
|
*/ |
|
57
|
|
|
public List<TimePoint> getPoints() { |
|
58
|
|
|
return new ArrayList<>(this.nodes.values()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
* @param point |
|
64
|
|
|
* @return |
|
65
|
|
|
*/ |
|
66
|
|
|
public List<TimePoint> getAdjacents(TimePoint point) { |
|
67
|
|
|
|
|
68
|
|
|
// list of adjacent time points |
|
69
|
|
|
List<TimePoint> list = new ArrayList<>(); |
|
70
|
|
|
if (this.edges.containsKey(point)) { |
|
71
|
|
|
// add adjacent time points |
|
72
|
|
|
list.addAll(this.edges.get(point).keySet()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// get list |
|
76
|
|
|
return list; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* |
|
81
|
|
|
* @param point |
|
82
|
|
|
*/ |
|
83
|
|
|
public void add(TimePoint point) { |
|
84
|
|
|
this.nodes.put(point.getId(), point); |
|
85
|
|
|
this.edges.put(point, new HashMap<TimePoint, Long>()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* |
|
90
|
|
|
* @param source |
|
91
|
|
|
* @param target |
|
92
|
|
|
* @param weight |
|
93
|
|
|
*/ |
|
94
|
|
|
public void add(TimePoint source, TimePoint target, long weight) { |
|
95
|
|
|
// check graph |
|
96
|
|
|
if (!this.edges.containsKey(source)) { |
|
97
|
|
|
this.edges.put(source, new HashMap<TimePoint, Long>()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// update distance |
|
101
|
|
|
this.edges.get(source).put(target, weight); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Delete the time point from the distance graph and all |
|
106
|
|
|
* the related edges |
|
107
|
|
|
* |
|
108
|
|
|
* @param point |
|
109
|
|
|
*/ |
|
110
|
|
|
public void delete(TimePoint point) { |
|
111
|
|
|
// remove from points |
|
112
|
|
|
this.nodes.remove(point.getId()); |
|
113
|
|
|
// remove outgoing edges |
|
114
|
|
|
this.edges.remove(point); |
|
115
|
|
|
// remove incoming edges |
|
116
|
|
|
for (TimePoint i : this.nodes.values()) { |
|
117
|
|
|
// remove edges that involve the node just deleted |
|
118
|
|
|
this.edges.get(i).remove(point); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Delete the edge from the distance graph |
|
124
|
|
|
* |
|
125
|
|
|
* @param source |
|
126
|
|
|
* @param target |
|
127
|
|
|
*/ |
|
128
|
|
|
public void delete(TimePoint source, TimePoint target) { |
|
129
|
|
|
// remove edge |
|
130
|
|
|
if (this.edges.containsKey(source)) { |
|
131
|
|
|
this.edges.get(source).remove(target); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* |
|
137
|
|
|
* @param source |
|
138
|
|
|
* @param target |
|
139
|
|
|
* @return |
|
140
|
|
|
*/ |
|
141
|
|
|
public long getDistance(TimePoint source, TimePoint target) { |
|
142
|
|
|
|
|
143
|
|
|
// initialize to infinity |
|
144
|
|
|
long distance = this.infty; |
|
145
|
|
|
// check if an edge exists |
|
146
|
|
|
if (this.edges.containsKey(source) && this.edges.get(source).containsKey(target)) { |
|
147
|
|
|
// get distance |
|
148
|
|
|
distance = this.edges.get(source).get(target); |
|
149
|
|
|
} |
|
150
|
|
|
// return distance |
|
151
|
|
|
return distance; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* |
|
156
|
|
|
*/ |
|
157
|
|
|
@Override |
|
158
|
|
|
public String toString() { |
|
159
|
|
|
// print the distance graph |
|
160
|
|
|
String string = "{\n"; |
|
161
|
|
|
|
|
162
|
|
|
for (TimePoint reference : this.edges.keySet()) { |
|
|
|
|
|
|
163
|
|
|
string += "\tpoint: " + reference.getId() + ",\n" |
|
|
|
|
|
|
164
|
|
|
+ "\tedges: [\n"; |
|
165
|
|
|
|
|
166
|
|
|
for (TimePoint target : this.edges.get(reference).keySet()) { |
|
167
|
|
|
string += "\t\t{ point: " + target.getId() +", distance: " + this.edges.get(reference).get(target) + "},\n"; |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
string += "\t]\n"; |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
string += "}\n"; |
|
175
|
|
|
return string; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
|