forked from Refefer/cloverleaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.rs
546 lines (445 loc) · 14.6 KB
/
graph.rs
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! This is the core graph library.
//! While we define a number of different representations, including mutability, in practice we're
//! only using CSR variants which also assumes static construction. We have a couplte of tricks
//! defined in here to allow for swapping of edges while minimizing the amount of memory we have to
//! copy.
use rayon::prelude::ParallelSliceMut;
pub type NodeID = usize;
pub trait Graph {
/// Get number of nodes in graph
fn len(&self) -> usize;
/// Get number of edges in graph
fn edges(&self) -> usize;
/// Get degree of node in graph
fn degree(&self, idx: NodeID) -> usize;
/// Get edges and corresponding weights
fn get_edges(&self, idx: NodeID) -> (&[NodeID], &[f32]);
/// Get edge offset in graph
fn get_edge_range(&self, idx: NodeID) -> (usize, usize);
}
/// trait which allows graphs to be updated
pub trait ModifiableGraph {
/// Get edges and corresponding weights
fn modify_edges(&mut self, idx: NodeID) -> (&mut [NodeID], &mut [f32]);
}
/// Used for trait bounds. Confirms the underlying weights for each node
/// sum to 1, as in a transition matrix.
pub trait NormalizedGraph: Graph {}
/// Used for trait bounds. Confirms the weights are normalized transition
/// matrix, optimized in cumulative distribution function.
pub trait CDFGraph: Graph {}
/// Compressed Sparse Row Format. We use this for graphs since adjancency
/// lists tend to use more memory.
#[derive(Clone)]
pub struct CSR {
rows: Vec<NodeID>,
columns: Vec<NodeID>,
weights: Vec<f32>
}
impl CSR {
pub fn construct_from_edges(mut edges: Vec<(NodeID, NodeID, f32)>, deduplicate: bool) -> Self {
if deduplicate {
CSR::deduplicate_edges(&mut edges);
}
// Determine the number of rows in the adjacency graph
let max_node = edges.iter().map(|(from_node, to_node, _)| {
*from_node.max(to_node)
}).max().unwrap_or(0);
// Figure out how many out edges per node
let mut rows = vec![0; max_node+2];
edges.iter().for_each(|(from_node, _to_node, _w)| {
rows[*from_node + 1] += 1;
});
// Convert to row offset format
let mut offset = 0;
rows.iter_mut().skip(1).for_each(|count| {
offset += *count;
*count = offset;
});
// Insert columns and weights
let mut counts = vec![0; max_node+1];
let mut columns = vec![0; edges.len()];
let mut data = vec![0f32; edges.len()];
edges.into_iter().for_each(|(from_node, to_node, weight)| {
let idx = rows[from_node] + counts[from_node];
columns[idx] = to_node;
data[idx] = weight;
counts[from_node] += 1;
});
CSR { rows, columns, weights: data }
}
fn deduplicate_edges(
edges: &mut Vec<(NodeID, NodeID, f32)>
) -> () {
edges.par_sort_by_key(|(f_n, t_n, _)| (*f_n, *t_n));
let mut cur_record = 0;
let mut idx = 1;
while idx < edges.len() {
let (f_n, t_n, w) = edges[idx];
let c_r = edges[cur_record];
// Same edge, add the weights.
if f_n == c_r.0 && t_n == c_r.1 {
(&mut edges[cur_record]).2 += w;
} else {
// Different record, move it
cur_record += 1;
edges[cur_record] = edges[idx];
}
idx += 1;
}
edges.truncate(cur_record + 1);
}
}
impl Graph for CSR {
// Get number of nodes in graph
fn len(&self) -> usize {
self.rows.len() - 1
}
// Get number of nodes in graph
fn edges(&self) -> usize {
self.weights.len()
}
// Get degree of node in graph
fn degree(&self, idx: NodeID) -> usize {
self.rows[idx+1] - self.rows[idx]
}
// Get edges and corresponding weights
fn get_edges(&self, idx: NodeID) -> (&[NodeID], &[f32]) {
let (start, stop) = self.get_edge_range(idx);
(&self.columns[start..stop], &self.weights[start..stop])
}
// get edge range
fn get_edge_range(&self, idx: NodeID) -> (usize, usize) {
let start = self.rows[idx];
let stop = self.rows[idx+1];
(start, stop)
}
}
impl ModifiableGraph for CSR {
// Get edges and corresponding weights
fn modify_edges(&mut self, idx: NodeID) -> (&mut [NodeID], &mut [f32]) {
let (start, stop) = self.get_edge_range(idx);
(&mut self.columns[start..stop], &mut self.weights[start..stop])
}
}
/// Normalizes sum of weights for a node to 1
pub struct NormalizedCSR(CSR);
impl NormalizedCSR {
pub fn convert(mut csr: CSR) -> Self {
for start_stop in csr.rows.windows(2) {
let (start, stop) = (start_stop[0], start_stop[1]);
let slice = &mut csr.weights[start..stop];
let denom = slice.iter().sum::<f32>();
if denom > 0f32 {
slice.iter_mut().for_each(|w| *w /= denom);
} else {
let n = slice.len() as f32;
slice.iter_mut().for_each(|w| *w = 1f32 / n );
}
}
NormalizedCSR(csr)
}
}
impl Graph for NormalizedCSR {
/// Get number of nodes in graph
fn len(&self) -> usize {
self.0.len()
}
/// Get number of nodes in graph
fn edges(&self) -> usize {
self.0.edges()
}
/// Get degree of node in graph
fn degree(&self, idx: NodeID) -> usize {
self.0.degree(idx)
}
/// Get edges and corresponding weights
fn get_edges(&self, idx: NodeID) -> (&[NodeID], &[f32]) {
self.0.get_edges(idx)
}
// get edge range
fn get_edge_range(&self, idx: NodeID) -> (usize, usize) {
self.0.get_edge_range(idx)
}
}
impl ModifiableGraph for NormalizedCSR {
/// Get edges and corresponding weights
fn modify_edges(&mut self, idx: NodeID) -> (&mut [NodeID], &mut [f32]) {
self.0.modify_edges(idx)
}
}
impl NormalizedGraph for NormalizedCSR {}
/// This is the main one used in Cloverleaf - converts CSR formatted graphs into CDF format to make sampling from
/// edges efficient (log(N)).
#[derive(Clone)]
pub struct CumCSR(CSR);
impl CumCSR {
pub fn convert(mut csr: CSR) -> Self {
for start_stop in csr.rows.windows(2) {
let (start, stop) = (start_stop[0], start_stop[1]);
if start < stop {
convert_edges_to_cdf(&mut csr.weights[start..stop]);
}
}
CumCSR(csr)
}
pub fn clone_with_edges(&self, weights: Vec<f32>) -> Result<CumCSR,&'static str> {
if weights.len() != self.0.weights.len() {
Err("weights lengths not equal!")?
}
let graph = CSR {
rows: self.0.rows.clone(),
columns: self.0.columns.clone(),
weights: weights
};
// Test that the weights are properly CDF
for node_id in 0..graph.len() {
let weights = self.get_edges(node_id).1;
for pair in weights.windows(2) {
match pair {
&[p, n] => {
if p > 1.0 {
Err("Edge weight exceeds 1.0, illegal in CDF")?
} else if n < p {
Err("Edge weight for node in decreasing order")?
}
},
_ => panic!("Something busted with built in")
}
}
if weights[weights.len() - 1] > 1.0 {
Err("Edge weight exceeds 1.0, illegal in CDF")?
}
}
Ok(CumCSR(graph))
}
}
impl Graph for CumCSR {
/// Get number of nodes in graph
fn len(&self) -> usize {
self.0.len()
}
/// Get number of nodes in graph
fn edges(&self) -> usize {
self.0.edges()
}
/// Get degree of node in graph
fn degree(&self, idx: NodeID) -> usize {
self.0.degree(idx)
}
/// Get edges and corresponding weights
fn get_edges(&self, idx: NodeID) -> (&[NodeID], &[f32]) {
self.0.get_edges(idx)
}
/// Get edge Range
fn get_edge_range(&self, idx: NodeID) -> (usize, usize) {
self.0.get_edge_range(idx)
}
}
impl ModifiableGraph for CumCSR {
/// Get edges and corresponding weights
fn modify_edges(&mut self, idx: NodeID) -> (&mut [NodeID], &mut [f32]) {
self.0.modify_edges(idx)
}
}
impl CDFGraph for CumCSR {}
/// This is a graph which allows us to swap in a new set of edge weights without having to copy the
/// entire graph. We use it in cases where policies update edge transition probabilities.
pub struct OptCDFGraph<'a,G> {
graph: &'a G,
weights: Vec<f32>
}
impl <'a,G:Graph> OptCDFGraph<'a,G> {
pub fn new(graph: &'a G, weights: Vec<f32>) -> Self {
let mut s = OptCDFGraph { graph, weights };
s.convert_edges();
s
}
pub fn into_weights(self) -> Vec<f32> {
self.weights
}
pub fn convert_edges(&mut self) {
for idx in 0..self.len() {
let (start, stop) = self.get_edge_range(idx);
if start < stop {
convert_edges_to_cdf(&mut self.weights[start..stop]);
}
}
}
}
impl <'a,G:CDFGraph> OptCDFGraph<'a,G> {
pub fn clone_from_cdf(graph: &'a G) -> Self {
let mut weights = vec![0f32; graph.edges()];
for node_id in 0..graph.len() {
let w = graph.get_edges(node_id).1;
let (start, stop) = graph.get_edge_range(node_id);
weights[start..stop].clone_from_slice(w);
}
OptCDFGraph { graph, weights }
}
}
impl <'a,G:Graph> Graph for OptCDFGraph<'a,G> {
/// Get number of nodes in graph
fn len(&self) -> usize {
self.graph.len()
}
/// Get number of nodes in graph
fn edges(&self) -> usize {
self.graph.edges()
}
/// Get degree of node in graph
fn degree(&self, idx: NodeID) -> usize {
self.graph.degree(idx)
}
/// Get edges and corresponding weights
fn get_edges(&self, idx: NodeID) -> (&[NodeID], &[f32]) {
let edges = self.graph.get_edges(idx).0;
let (start, stop) = self.get_edge_range(idx);
let weights = &self.weights[start..stop];
(edges, weights)
}
/// Get edge Range
fn get_edge_range(&self, idx: NodeID) -> (usize, usize) {
self.graph.get_edge_range(idx)
}
}
impl <'a,G:Graph> CDFGraph for OptCDFGraph<'a,G> {}
/// Struct which converts CDF format to transition probabilities.
#[derive(Clone,Copy)]
pub struct CDFtoP<'a> {
cdf: &'a [f32],
idx: usize
}
impl <'a> CDFtoP<'a> {
pub fn new(weights: &'a [f32]) -> Self {
CDFtoP { cdf: weights, idx: 0 }
}
pub fn prob(&self, idx: usize) -> f32 {
if idx == 0 {
self.cdf[idx]
} else {
self.cdf[idx] - self.cdf[idx - 1]
}
}
}
impl <'a> Iterator for CDFtoP<'a> {
type Item = f32;
fn next(&mut self) -> Option<Self::Item> {
if self.idx < self.cdf.len() {
let p = if self.idx == 0 {
Some(self.cdf[self.idx])
} else {
Some(self.cdf[self.idx] - self.cdf[self.idx-1])
};
self.idx += 1;
p
} else {
None
}
}
}
/// Converts a set of weights to CDF
pub fn convert_edges_to_cdf(weights: &mut [f32]) {
let mut denom = weights.iter().sum::<f32>();
if denom == 0f32 {
// If we have no weights, set all weights to uniform.
weights.iter_mut().for_each(|w| {
*w = 1.
});
denom = weights.len() as f32;
}
let mut acc = 0.;
weights.iter_mut().for_each(|w| {
acc += *w;
*w = acc / denom;
});
weights[weights.len() - 1] = 1.;
}
/// Converts an iterator of weights to CDF
pub fn collect_weights_into(
weights: impl Iterator<Item=f32>,
cdf_weights: &mut Vec<f32>
) {
cdf_weights.clear();
if let (_, Some(ul)) = weights.size_hint() {
cdf_weights.reserve(ul);
}
let mut acc = 0f32;
cdf_weights.push(0f32);
for wi in weights {
acc += wi;
cdf_weights.push(acc);
}
for wi in cdf_weights.iter_mut() {
*wi /= acc;
}
let l = cdf_weights.len();
cdf_weights[l - 1] = 1.;
}
#[cfg(test)]
mod csr_tests {
use super::*;
fn build_edges() -> Vec<(usize, usize, f32)> {
vec![
(0, 1, 1.),
(1, 1, 3.),
(1, 2, 2.),
(2, 0, 2.5),
(1, 0, 10.),
]
}
#[test]
fn construct_csr() {
let edges = build_edges();
let csr = CSR::construct_from_edges(edges);
assert_eq!(csr.rows, vec![0, 1, 4, 5]);
assert_eq!(csr.columns, vec![1, 1, 2, 0, 0]);
assert_eq!(csr.weights, vec![1., 3., 2., 10., 2.5]);
}
#[test]
fn test_graph() {
let edges = build_edges();
let mut csr = CSR::construct_from_edges(edges);
assert_eq!(csr.len(), 3);
assert_eq!(csr.degree(0), 1);
assert_eq!(csr.degree(1), 3);
assert_eq!(csr.degree(2), 1);
assert_eq!(csr.get_edges(2), (vec![0].as_slice(), vec![2.5].as_slice()));
assert_eq!(csr.get_edges(1), (vec![1,2,0].as_slice(), vec![3., 2., 10.].as_slice()));
{
let (_edges, weights) = csr.modify_edges(1);
weights[2] = 20.;
}
assert_eq!(csr.get_edges(1), (vec![1,2,0].as_slice(), vec![3., 2., 20.].as_slice()));
}
#[test]
fn construct_mk() {
let edges = build_edges();
let csr = CSR::construct_from_edges(edges);
let mk = NormalizedCSR::convert(csr);
assert_eq!(mk.get_edges(0), (vec![1].as_slice(), vec![1.].as_slice()));
assert_eq!(mk.get_edges(1), (vec![1,2,0].as_slice(), vec![3./15., 2./15., 10./15.].as_slice()));
assert_eq!(mk.get_edges(2), (vec![0].as_slice(), vec![1.].as_slice()));
}
#[test]
fn construct_cdf() {
let edges = build_edges();
let csr = CSR::construct_from_edges(edges);
let ccsr = CumCSR::convert(csr);
assert_eq!(ccsr.0.rows, vec![0, 1, 4, 5]);
assert_eq!(ccsr.0.columns, vec![1, 1, 2, 0, 0]);
assert_eq!(ccsr.0.weights, vec![1., 3./15., 5./15., 15./15., 1.]);
}
#[test]
fn construct_cdf_to_p() {
let edges = build_edges();
let csr = CSR::construct_from_edges(edges);
let ccsr = CumCSR::convert(csr);
let weights = ccsr.get_edges(1).1;
let ps = CDFtoP::new(weights);
let exp = vec![3./15., 2./15., 10./15.];
ps.zip(exp.iter()).for_each(|(p, exp_p)| {
assert!((p - exp_p).abs() < 1e-7);
});
}
}