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
// Copyright (c) 2016-2020 Frank Fischer <frank-fischer@shadow-soft.de>
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see  <http://www.gnu.org/licenses/>
//

use super::{read_dimacs, DimacsError};

use crate::{Buildable, Builder};

use std::cmp::{max, min};
use std::collections::HashSet;
use std::f64;
use std::fs;
use std::io::{BufRead, BufReader};

// Possible metrics
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Metric {
    // infinity norm,
    LINF,
    // l_p norm
    L(f64),
    // squared euclidean norm
    L2S,
}

// Problem parameters
#[derive(PartialEq, Debug)]
pub enum Param {
    // edge included only if length greater than or equal to this
    MinLength(f64),
    // edge included only if length less than or equal to this
    MaxLength(f64),
}

/// Read DIMACS file.
///
/// - `fname` is the name of the file to be read
/// - `weights` is the array of node weights
pub fn read<G>(fname: &str) -> Result<(G, Vec<f64>), DimacsError>
where
    G: Buildable,
{
    read_from_buf(&mut BufReader::new(fs::File::open(fname)?))
}

/// Read DIMACS file from a buffered reader.
///
/// - `buf` the buffer with the file content
pub fn read_from_buf<R, G>(buf: &mut R) -> Result<(G, Vec<f64>), DimacsError>
where
    R: BufRead,
    G: Buildable,
{
    let mut g = G::new_builder();
    let mut weights = vec![];
    let mut nnodes = 0;
    let mut nedges = 0;
    let mut gnodes = vec![];
    let mut nodes = HashSet::new();
    let mut edges = HashSet::new();
    let mut dim = 0;
    let mut metric = None;
    let mut vertices = vec![];
    let mut minlength = None;
    let mut maxlength = None;

    let mut start = true;
    read_dimacs(buf, &mut |d, toks| {
        if start {
            if d != 'p' {
                return Err(DimacsError::Format {
                    line: toks.line,
                    msg: format!("expected problem line starting with 'p', got: {}", d),
                });
            }
            let edge = toks.next().unwrap_or("end of line");
            if edge != "edge" {
                return Err(DimacsError::Format {
                    line: toks.line,
                    msg: format!("expected 'edge' in p line, got: {}", edge),
                });
            }
            nnodes = toks.number()?;
            nedges = toks.number()?;
            toks.end()?;

            g.reserve(nnodes, nedges);
            gnodes = g.add_nodes(nnodes);
            weights.clear();
            weights.resize(nnodes, 0.0);

            start = false;
        } else {
            match d {
                'n' => {
                    let id: usize = toks.number()?;
                    let val = toks.number()?;
                    toks.end()?;
                    if id < 1 || id > nnodes {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: format!("invalid node id {} (must be <= {})", id, nnodes),
                        });
                    }
                    if !nodes.insert(id) {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: format!("duplicate node id {}", id),
                        });
                    }
                    weights[id - 1] = val;
                }
                'e' => {
                    let u: usize = toks.number()?;
                    let v: usize = toks.number()?;
                    toks.end()?;
                    if u < 1 || u > nnodes {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: format!("invalid node id {} in edge", u),
                        });
                    }
                    if v < 1 || v > nnodes {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: format!("invalid node id {} in edge", v),
                        });
                    }
                    if u == v {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: format!("invalid loop ({},{}) in edge", u, u),
                        });
                    }
                    if !edges.insert((min(u, v), max(u, v))) {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: format!("duplicate edge ({},{})", u, v),
                        });
                    }
                    g.add_edge(gnodes[u - 1], gnodes[v - 1]);
                }
                'd' => {
                    let d: usize = toks.number()?;
                    let m = match toks.next() {
                        Some("LINF") => Metric::LINF,
                        Some("L2S") => Metric::L2S,
                        Some(m) if !m.is_empty() && m.starts_with('L') => {
                            let p: f64 = match m[1..].parse() {
                                Ok(x) => x,
                                Err(_) => {
                                    return Err(DimacsError::Format {
                                        line: toks.line,
                                        msg: "invalid norm".to_string(),
                                    });
                                }
                            };
                            if p <= 0.0 {
                                return Err(DimacsError::Format {
                                    line: toks.line,
                                    msg: format!("p of p-norm must be > 0 (got: {})", p),
                                });
                            }
                            Metric::L(p)
                        }
                        Some(_) => {
                            return Err(DimacsError::Format {
                                line: toks.line,
                                msg: "invalid norm".to_string(),
                            });
                        }
                        None => {
                            return Err(DimacsError::Format {
                                line: toks.line,
                                msg: "missing norm".to_string(),
                            });
                        }
                    };
                    toks.end()?;

                    if d < 1 {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: format!("invalid dimension {} < 1", d),
                        });
                    }
                    if dim > 0 {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: "duplicate dimension".to_string(),
                        });
                    }

                    dim = d;
                    metric = Some(m);
                    vertices.reserve(nnodes);
                }
                'v' => {
                    if dim == 0 {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: "missing dimension line before vertex line".to_string(),
                        });
                    }

                    let mut vs: Vec<f64> = Vec::new();
                    for _ in 0..dim {
                        vs.push(toks.number()?);
                    }
                    toks.end()?;

                    if dim != vs.len() {
                        return Err(DimacsError::Format {
                            line: toks.line,
                            msg: format!("vertex line has too many entries {} (expected: {})", vs.len(), dim),
                        });
                    }

                    vertices.push(vs);
                }
                'x' => {
                    let par = toks.str()?;
                    let l: f64 = toks.number()?;
                    toks.end()?;
                    match par {
                        "MINLENGTH" => {
                            if minlength.is_some() {
                                return Err(DimacsError::Format {
                                    line: toks.line,
                                    msg: "duplicate parameter MINLENGTH".to_string(),
                                });
                            }
                            minlength = Some(l)
                        }
                        "MAXLENGTH" => {
                            if maxlength.is_some() {
                                return Err(DimacsError::Format {
                                    line: toks.line,
                                    msg: "duplicate parameter MAXLENGTH".to_string(),
                                });
                            }
                            maxlength = Some(l)
                        }
                        _ => {
                            return Err(DimacsError::Format {
                                line: toks.line,
                                msg: format!("unknown parameter {}", par),
                            });
                        }
                    };
                }
                _ => {
                    return Err(DimacsError::Format {
                        line: toks.line,
                        msg: format!("invalid command {}", d),
                    });
                }
            }
        }

        Ok(())
    })?;

    // verify consistency with vertex data
    if dim > 0 {
        let metric = metric.unwrap();
        let minl = match minlength {
            Some(l) => l,
            _ => 0.0,
        };
        let maxl = match maxlength {
            Some(l) => l,
            _ => f64::INFINITY,
        };
        for u in 0..nnodes {
            for v in u + 1..nnodes {
                let d = dist(&vertices[u], &vertices[v], metric);
                // invalid edges might have infinite lengths
                if !edges.contains(&(u + 1, v + 1)) {
                    // edge does not exist, the distance should be invalid
                    if d >= minl && d <= maxl {
                        return Err(DimacsError::Format {
                            line: 0,
                            msg: format!("edge ({},{}) should be contained in the graph", u, v),
                        });
                    }
                } else {
                    // edge exists, the distance must be valid
                    if d < minl {
                        return Err(DimacsError::Format {
                            line: 0,
                            msg: format!("edge ({},{}) should not be contained in the graph (too short)", u, v),
                        });
                    }
                    if d > maxl {
                        return Err(DimacsError::Format {
                            line: 0,
                            msg: format!("edge ({},{}) should not be contained in the graph (too long)", u, v),
                        });
                    }
                }
            }
        }
    }

    Ok((g.to_graph(), weights))
}

fn dist(x: &[f64], y: &[f64], metric: Metric) -> f64 {
    let mut d: f64 = 0.0;
    match metric {
        Metric::LINF => {
            for i in 0..x.len() {
                d = f64::max(d, f64::abs(x[i] - y[i]));
            }
        }
        Metric::L2S => {
            for i in 0..x.len() {
                d += (x[i] - y[i]) * (x[i] - y[i]);
            }
        }
        Metric::L(p) => {
            for i in 0..x.len() {
                d += (x[i] - y[i]).abs().powf(p);
            }
            d = d.powf(1.0 / p);
        }
    }
    d
}

#[cfg(test)]
mod tests {
    use super::read_dimacs;
    use super::read_from_buf;
    use crate::traits::{GraphSize, IndexGraph, Undirected};
    use crate::vec::NodeVec;
    use crate::LinkedListGraph;
    use std::io::Cursor;

    #[test]
    fn test_read() {
        let file = "
c some comment

p edge 12 5
";
        read_dimacs(&mut Cursor::new(file), &mut |c, toks| {
            assert_eq!(c, 'p');
            assert_eq!(toks.next(), Some("edge"));
            assert_eq!(toks.number::<usize>().unwrap(), 12);
            assert_eq!(toks.number::<usize>().unwrap(), 5);
            assert_eq!(toks.next(), None);
            Ok(())
        })
        .unwrap();
    }

    #[test]
    fn parse_file_test() {
        let file = "c this is a test file

p edge 6 9
  n 1 12
n 6 42.23

c there might be empty lines

e 1 4
e 1 5
e 1 6
e 2 4
e 2 5
e 2 6
e 3 4
e 3 5
e 3 6

d 2 L2
v 1 1
v 1 2
v 1 3
v 10 1
v 10 2
v 10 3

x MINLENGTH 5
x MAXLENGTH 100

c end of the file
";
        let (g, weights) = read_from_buf::<_, LinkedListGraph>(&mut Cursor::new(file)).unwrap();
        let weights = NodeVec::new_from_vec(&g, weights);

        assert_eq!(g.num_nodes(), 6);
        assert_eq!(g.num_edges(), 9);

        for u in 0..3 {
            let mut vs: Vec<_> = g.neighs(g.id2node(u)).map(|(_, v)| g.node_id(v)).collect();
            vs.sort();
            assert_eq!(vs, vec![3, 4, 5]);
        }

        for v in 3..6 {
            let mut vs: Vec<_> = g.neighs(g.id2node(v)).map(|(_, v)| g.node_id(v)).collect();
            vs.sort();
            assert_eq!(vs, vec![0, 1, 2]);
        }

        for u in g.nodes() {
            match (g.node_id(u), weights[u]) {
                (0, w) => assert_eq!(w, 12.0),
                (5, w) => assert_eq!(w, 42.23),
                (_, w) => assert_eq!(w, 0.0),
            }
        }
    }
}