RsBundle  Diff

Differences From Artifact [4206da236f]:

  • File src/mcf/problem.rs — part of check-in [f1406e69ec] at 2019-07-15 19:53:19 on branch aggregatable — mcf.problem: use `iter` instead of `into_iter` (user: fifr size: 10841) [more...]

To Artifact [af9c7cb9db]:

  • File src/mcf/problem.rs — part of check-in [1e94fdd305] at 2019-07-17 11:23:44 on branch async — Make all errors Send + Sync (user: fifr size: 12044)

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
//

use crate::mcf;
use crate::{Aggregatable, DVector, FirstOrderProblem, Minorant, Real, SimpleEvaluation};

use log::debug;

use std::error::Error;
use std::f64::INFINITY;
use std::fmt;
use std::fs::File;
use std::io::Read;
use std::result;

/// An error in the mmcf file format.
#[derive(Debug)]
pub struct MMCFFormatError {
    msg: String,


}

impl fmt::Display for MMCFFormatError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {


        write!(fmt, "Format error: {}", self.msg)


    }
}


impl Error for MMCFFormatError {}





































/// Result type of the MMCFProblem.
pub type Result<T> = result::Result<T, Box<Error>>;

#[derive(Clone, Copy, Debug)]
struct ArcInfo {
    arc: usize,
    src: usize,
    snk: usize,
}







<








|
|
>
>


|

>
>
|
>
>
|
|
|
>
|
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|







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
//

use crate::mcf;
use crate::{Aggregatable, DVector, FirstOrderProblem, Minorant, Real, SimpleEvaluation};

use log::debug;


use std::f64::INFINITY;
use std::fmt;
use std::fs::File;
use std::io::Read;
use std::result;

/// An error in the mmcf file format.
#[derive(Debug)]
pub enum MMCFReadError {
    Format(String),
    Solver(mcf::solver::Error),
    Io(std::io::Error),
}

impl fmt::Display for MMCFReadError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        use MMCFReadError::*;
        match self {
            Format(msg) => write!(fmt, "Format error: {}", msg),
            Solver(err) => err.fmt(fmt),
            Io(err) => err.fmt(fmt),
        }
    }
}

impl std::error::Error for MMCFReadError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        use MMCFReadError::*;
        match self {
            Solver(err) => Some(err),
            Io(err) => Some(err),
            _ => None,
        }
    }
}

impl From<mcf::solver::Error> for MMCFReadError {
    fn from(err: mcf::solver::Error) -> MMCFReadError {
        MMCFReadError::Solver(err)
    }
}

impl From<std::io::Error> for MMCFReadError {
    fn from(err: std::io::Error) -> MMCFReadError {
        MMCFReadError::Io(err)
    }
}

impl From<std::num::ParseIntError> for MMCFReadError {
    fn from(err: std::num::ParseIntError) -> MMCFReadError {
        MMCFReadError::Format(format!("Parse error: {}", err))
    }
}

impl From<std::num::ParseFloatError> for MMCFReadError {
    fn from(err: std::num::ParseFloatError) -> MMCFReadError {
        MMCFReadError::Format(format!("Parse error: {}", err))
    }
}

/// Type of errors of the MMCFProblem.
pub type Error = crate::mcf::solver::Error;

/// Result type of the MMCFProblem.
pub type Result<T> = result::Result<T, Error>;

#[derive(Clone, Copy, Debug)]
struct ArcInfo {
    arc: usize,
    src: usize,
    snk: usize,
}
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
    rhs: DVector,
    rhsval: Real,
    cbase: Vec<DVector>,
    c: Vec<DVector>,
}

impl MMCFProblem {
    pub fn read_mnetgen(basename: &str) -> Result<MMCFProblem> {
        let mut buffer = String::new();
        {
            let mut f = File::open(&format!("{}.nod", basename))?;
            f.read_to_string(&mut buffer)?;
        }
        let fnod = buffer
            .split_whitespace()
            .map(|x| x.parse::<usize>().unwrap())
            .collect::<Vec<_>>();

        if fnod.len() != 4 {
            return Err(MMCFFormatError {
                msg: format!("Expected 4 numbers in {}.nod, but got {}", basename, fnod.len()),
            }


            .into());
        }

        let ncom = fnod[0];
        let nnodes = fnod[1];
        let narcs = fnod[2];
        let ncaps = fnod[3];








|











|
|
<
>
>
|







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
    rhs: DVector,
    rhsval: Real,
    cbase: Vec<DVector>,
    c: Vec<DVector>,
}

impl MMCFProblem {
    pub fn read_mnetgen(basename: &str) -> std::result::Result<MMCFProblem, MMCFReadError> {
        let mut buffer = String::new();
        {
            let mut f = File::open(&format!("{}.nod", basename))?;
            f.read_to_string(&mut buffer)?;
        }
        let fnod = buffer
            .split_whitespace()
            .map(|x| x.parse::<usize>().unwrap())
            .collect::<Vec<_>>();

        if fnod.len() != 4 {
            return Err(MMCFReadError::Format(format!(
                "Expected 4 numbers in {}.nod, but got {}",

                basename,
                fnod.len()
            )));
        }

        let ncom = fnod[0];
        let nnodes = fnod[1];
        let narcs = fnod[2];
        let ncaps = fnod[3];

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
        }

        x
    }
}

impl FirstOrderProblem for MMCFProblem {
    type Err = Box<dyn Error>;

    type Primal = Vec<DVector>;

    type EvalResult = SimpleEvaluation<Vec<DVector>>;

    fn num_variables(&self) -> usize {
        self.lhs.len()







|







288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
        }

        x
    }
}

impl FirstOrderProblem for MMCFProblem {
    type Err = Error;

    type Primal = Vec<DVector>;

    type EvalResult = SimpleEvaluation<Vec<DVector>>;

    fn num_variables(&self) -> usize {
        self.lhs.len()