ellp 0.2.0

Linear programming library that provides primal and dual simplex solvers.
Documentation
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
use crate::problem::{Bound, ConstraintOp, Problem};

use log::{debug, error};
use thiserror::Error;

use std::{collections::HashMap, iter::Peekable};

type Rows<'a> = HashMap<&'a str, Row<'a>>;
type Cols<'a> = HashMap<&'a str, Col>;

#[derive(Error, Debug)]
#[error("MPS parsing error. {msg}")]
pub struct MpsParsingError {
    msg: String,
}

impl MpsParsingError {
    fn new(msg: String) -> Self {
        Self { msg }
    }
}

pub fn parse_mps(mps: &str) -> Result<Problem, MpsParsingError> {
    let (rows, cols) = parse_rows_and_cols(mps)?;

    let mut prob = Problem::new();
    let mut var_ids = HashMap::new();

    for (var_name, col) in cols {
        let var_name = var_name.to_string();
        let bound = col.bound.unwrap_or(Bound::Lower(0.));

        //should never panic, we know that the var names are unique
        let var_id = prob
            .add_var(col.obj_coeff, bound, Some(var_name.clone()))
            .unwrap();

        var_ids.insert(var_name, var_id);
    }

    for (row_name, row) in rows {
        match row {
            Row::Objective => continue,
            Row::Constraint { op, coeffs, rhs } => {
                let rhs = rhs.unwrap_or(0.);

                let coeffs = coeffs
                    .into_iter()
                    .map(|(var_name, coeff)| match var_ids.get(var_name) {
                        Some(var_id) => Ok((*var_id, coeff)),

                        None => Err(MpsParsingError::new(format!(
                            "for row {}, column {} does not exist",
                            row_name, var_name
                        ))),
                    })
                    .collect::<Result<_, _>>()?;

                //should never panic, we know the variable ids in coeffs exist
                prob.add_constraint(coeffs, op, rhs).unwrap();
            }
        }
    }

    Ok(prob)
}

fn parse_rows_and_cols(mps: &str) -> Result<(Rows, Cols), MpsParsingError> {
    let mut lines = mps
        .split_terminator('\n')
        .filter(|s| !s.trim().is_empty())
        .peekable();

    match lines.next() {
        Some(line) => parse_name(line)?,
        None => return Err(MpsParsingError::new("could not find NAME line".to_string())),
    }

    let mut rows = parse_rows(&mut lines)?;
    let mut cols = parse_columns(&mut lines, &mut rows)?;

    parse_rhs(&mut lines, &mut rows)?;
    parse_bounds(&mut lines, &mut cols)?;

    match lines.next() {
        Some(line) => {
            let line = line.trim();

            if line != "ENDATA" {
                return Err(MpsParsingError::new(format!(
                    "expected 'ENDATA', found '{}'",
                    line
                )));
            }
        }

        None => {
            return Err(MpsParsingError::new(
                "could not find ENDATA line".to_string(),
            ))
        }
    }

    if let Some(line) = lines.next() {
        return Err(MpsParsingError::new(format!("unexpected line: {}", line)));
    }

    debug!(
        "parsed {} rows and {} columns from mps file",
        rows.len(),
        cols.len()
    );

    Ok((rows, cols))
}

fn parse_name(line: &str) -> Result<(), MpsParsingError> {
    let mut split_line = line.split_whitespace();

    let name = split_line.next().and_then(|tag| {
        if tag == "NAME" {
            split_line.next()
        } else {
            None
        }
    });

    if name.is_none() {
        return Err(MpsParsingError::new(format!(
            "could not find name in NAME line: {}",
            line
        )));
    }

    //ignore everything after NAME
    Ok(())
}

fn parse_rows<'a, I>(lines: &mut Peekable<I>) -> Result<Rows<'a>, MpsParsingError>
where
    I: Iterator<Item = &'a str>,
{
    match lines.next() {
        Some(line) => {
            let line = line.trim();

            if line != "ROWS" {
                return Err(MpsParsingError::new(format!(
                    "expected 'ROWS', found '{}'",
                    line
                )));
            }
        }

        None => return Err(MpsParsingError::new("could not find ROWS line".to_string())),
    }

    let mut rows = HashMap::new();

    while let Some(&line) = lines.peek() {
        let line = line.trim_start();

        if line.starts_with("COLUMNS") {
            break;
        }

        lines.next();

        let (name, row) = parse_row_line(line)?;

        if rows.insert(name, row).is_some() {
            return Err(MpsParsingError::new(format!("row name repeated: {}", name)));
        }
    }

    Ok(rows)
}

fn parse_row_line(line: &str) -> Result<(&str, Row), MpsParsingError> {
    let mut split_line = line.split_whitespace();

    let constraint_op_char = split_line.next().ok_or_else(|| {
        MpsParsingError::new(format!(
            "expected a row type character in this line: {}",
            line
        ))
    })?;

    let new_constraint = |op| Row::Constraint {
        op,
        coeffs: HashMap::new(),
        rhs: None,
    };

    let row = match constraint_op_char {
        "L" => new_constraint(ConstraintOp::Lte),
        "G" => new_constraint(ConstraintOp::Gte),
        "E" => new_constraint(ConstraintOp::Eq),
        "N" => Row::Objective,
        _ => {
            return Err(MpsParsingError::new(format!(
                "unexpected row type: {}",
                constraint_op_char
            )))
        }
    };

    let row_name = split_line.next().ok_or_else(|| {
        MpsParsingError::new(format!("expected a row name in this line: {}", line))
    })?;

    match split_line.next() {
        Some(s) => Err(MpsParsingError::new(format!(
            "unexpected input in row line: {}",
            s
        ))),

        None => Ok((row_name, row)),
    }
}

fn parse_columns<'a, I>(
    lines: &mut Peekable<I>,
    rows: &mut Rows<'a>,
) -> Result<Cols<'a>, MpsParsingError>
where
    I: Iterator<Item = &'a str>,
{
    match lines.next() {
        Some(line) => {
            let line = line.trim();

            if line != "COLUMNS" {
                return Err(MpsParsingError::new(format!(
                    "expected 'COLUMNS', found '{}'",
                    line
                )));
            }
        }

        None => {
            return Err(MpsParsingError::new(
                "could not find COLUMNS line".to_string(),
            ))
        }
    }

    let mut cols = HashMap::new();

    while let Some(&line) = lines.peek() {
        let line = line.trim_start();

        if line.starts_with("RHS") {
            break;
        }

        lines.next();
        parse_column_line(line, rows, &mut cols)?;
    }

    Ok(cols)
}

fn parse_column_line<'a>(
    line: &'a str,
    rows: &mut Rows<'a>,
    cols: &mut Cols<'a>,
) -> Result<(), MpsParsingError> {
    let mut split_line = line.split_whitespace();

    let var_name = split_line.next().ok_or_else(|| {
        MpsParsingError::new(format!("expected a column name in this line: {}", line))
    })?;

    let row_name = split_line.next().ok_or_else(|| {
        MpsParsingError::new(format!("expected a row name in this line: {}", line))
    })?;

    let coeff = split_line.next().ok_or_else(|| {
        MpsParsingError::new(format!("expected a coefficient in this line: {}", line))
    })?;

    let coeff: f64 = coeff.parse().map_err(|err| {
        MpsParsingError::new(format!(
            "could not parse the coefficient {}\nerror: {}\nline: {}",
            coeff, err, line
        ))
    })?;

    if let Some(s) = split_line.next() {
        return Err(MpsParsingError::new(format!(
            "unexpected input '{}' in column line: {}",
            s, line
        )));
    }

    let col_entry = cols.entry(var_name).or_insert_with(|| Col {
        obj_coeff: 0.,
        bound: None,
    });

    match rows.get_mut(row_name) {
        Some(row) => match row {
            Row::Objective => col_entry.obj_coeff = coeff,
            Row::Constraint { coeffs, .. } => {
                if coeffs.insert(var_name, coeff).is_some() {
                    return Err(MpsParsingError::new(format!(
                        "specified constraint coefficient for the column {} and row {} more than once",
                        var_name, row_name
                    )));
                }
            }
        },

        None => {
            return Err(MpsParsingError::new(format!(
                "could not find the row {}",
                row_name
            )))
        }
    }

    match split_line.next() {
        Some(s) => Err(MpsParsingError::new(format!(
            "unexpected input in column line: {}",
            s
        ))),

        None => Ok(()),
    }
}

fn parse_rhs<'a, I>(lines: &mut Peekable<I>, rows: &mut Rows) -> Result<(), MpsParsingError>
where
    I: Iterator<Item = &'a str>,
{
    match lines.next() {
        Some(line) => {
            let line = line.trim();

            if line != "RHS" {
                return Err(MpsParsingError::new(format!(
                    "expected 'RHS', found '{}'",
                    line
                )));
            }
        }

        None => return Err(MpsParsingError::new("could not find RHS line".to_string())),
    }

    while let Some(&line) = lines.peek() {
        let line = line.trim_start();

        if line.starts_with("BOUNDS") || line.starts_with("ENDATA") {
            break;
        }

        lines.next();
        parse_rhs_line(line, rows)?;
    }

    Ok(())
}

fn parse_rhs_line(line: &str, rows: &mut Rows) -> Result<(), MpsParsingError> {
    let split_line_iter = line.split_whitespace();
    let mut split_line = split_line_iter.clone();

    if split_line_iter.count() == 3 {
        split_line.next(); //skip the name
    }

    let row_name = split_line.next().ok_or_else(|| {
        MpsParsingError::new(format!("expected a row name in this line: {}", line))
    })?;

    let rhs_val = split_line.next().ok_or_else(|| {
        MpsParsingError::new(format!("expected a rhs value in this line: {}", line))
    })?;

    let rhs_val: f64 = rhs_val.parse().map_err(|err| {
        MpsParsingError::new(format!(
            "could not parse the rhs value {}\nerror: {}\nline: {}",
            rhs_val, err, line
        ))
    })?;

    match rows.get_mut(row_name) {
        Some(row) => match row {
            Row::Objective => {
                return Err(MpsParsingError::new(
                    "should not specify rhs value for the objective".to_string(),
                ));
            }

            Row::Constraint { rhs, .. } => {
                if rhs.is_some() {
                    return Err(MpsParsingError::new(format!(
                        "specified rhs for {} more than once",
                        row_name
                    )));
                }

                *rhs = Some(rhs_val);
            }
        },

        None => {
            return Err(MpsParsingError::new(format!(
                "could not find the row {}",
                row_name
            )))
        }
    }

    match split_line.next() {
        Some(s) => Err(MpsParsingError::new(format!(
            "unexpected input in column line: {}",
            s
        ))),

        None => Ok(()),
    }
}

fn parse_bounds<'a, I>(lines: &mut Peekable<I>, cols: &mut Cols) -> Result<(), MpsParsingError>
where
    I: Iterator<Item = &'a str>,
{
    if let Some(line) = lines.peek() {
        if *line == "ENDATA" {
            return Ok(()); //MPS file does not have a BOUNDS section
        }
    }

    match lines.next() {
        Some(line) => {
            let line = line.trim();

            if line == "ENDATA" {
                return Ok(());
            } else if line != "BOUNDS" {
                return Err(MpsParsingError::new(format!(
                    "expected 'BOUNDS', found '{}'",
                    line
                )));
            }
        }

        None => {
            return Err(MpsParsingError::new(
                "could not find BOUNDS line".to_string(),
            ))
        }
    }

    while let Some(&line) = lines.peek() {
        let line = line.trim_start();

        if line.starts_with("ENDATA") {
            break;
        }

        lines.next();
        parse_bound_line(line, cols)?;
    }

    Ok(())
}

fn parse_bound_line(line: &str, cols: &mut Cols) -> Result<(), MpsParsingError> {
    let mut split_line = line.split_whitespace();

    let bound_type = split_line.next().ok_or_else(|| {
        MpsParsingError::new(format!("expected a bound type in this line: {}", line))
    })?;

    split_line.next(); //skip bound name

    let col_name = split_line.next().ok_or_else(|| {
        MpsParsingError::new(format!("expected a column name in this line: {}", line))
    })?;

    let bound_val = split_line
        .next()
        .map(str::parse::<f64>)
        .transpose()
        .map_err(|err| {
            MpsParsingError::new(format!(
                "could not parse the bound value\nerror: {}\nline: {}",
                err, line
            ))
        })?;

    let bound = match (bound_type, bound_val) {
        ("UP", Some(v)) => Bound::Upper(v),
        ("LO", Some(v)) => Bound::Lower(v),
        ("FR", None) => Bound::Free,
        _ => {
            return Err(MpsParsingError::new(format!(
                "invalid bound specification: {}",
                line
            )))
        }
    };

    let col = match cols.get_mut(col_name) {
        Some(col) => col,

        None => {
            return Err(MpsParsingError::new(format!(
                "found bound for the column {}, but it does not exist",
                col_name,
            )))
        }
    };

    match (col.bound, bound) {
        (Some(Bound::Upper(ub)), Bound::Lower(lb)) => {
            col.bound.replace(Bound::TwoSided(lb, ub));
        }

        (Some(Bound::Lower(lb)), Bound::Upper(ub)) => {
            col.bound.replace(Bound::TwoSided(lb, ub));
        }

        (None, _) => col.bound = Some(bound),

        _ => {
            return Err(MpsParsingError::new(format!(
                "invalid bounds for {}",
                col_name,
            )))
        }
    }

    match split_line.next() {
        Some(s) => Err(MpsParsingError::new(format!(
            "unexpected input in column line: {}",
            s
        ))),

        None => Ok(()),
    }
}

#[derive(Debug, Clone)]
enum Row<'a> {
    Objective,

    Constraint {
        op: ConstraintOp,
        coeffs: HashMap<&'a str, f64>, //keys are variable names
        rhs: Option<f64>,
    },
}

#[derive(Debug, Clone)]
struct Col {
    obj_coeff: f64,
    bound: Option<Bound>,
}

#[cfg(test)]
mod tests {
    use std::f64::EPSILON;

    use super::*;

    #[test]
    fn parse_mps_example() {
        let mps_str = r#"
            NAME          TESTPROB
            ROWS
            N  COST
            L  LIM1
            G  LIM2
            E  MYEQN
            COLUMNS
                XONE      COST                 1
                XONE      LIM1                 1
                XONE      LIM2                 1
                YTWO      COST                 4
                YTWO      LIM1                 1
                YTWO      MYEQN               -1
                ZTHREE    COST                 9
                ZTHREE    LIM2                 1
                ZTHREE    MYEQN                1
            RHS
                RHS1      LIM1                 5
                RHS1      LIM2                10
                RHS1      MYEQN                7
            BOUNDS
            UP BND1      XONE                 4
            LO BND1      YTWO                -1
            UP BND1      YTWO                 1
            ENDATA
        "#;

        let prob: Problem = parse_mps(mps_str).unwrap();

        for var in &prob.variables {
            match var.name.as_ref().unwrap().as_str() {
                "XONE" => {
                    assert!((var.obj_coeff - 1.) < EPSILON);
                    assert_eq!(var.bound, Bound::Upper(4.));
                }

                "YTWO" => {
                    assert!((var.obj_coeff - 4.) < EPSILON);
                    assert_eq!(var.bound, Bound::TwoSided(-1., 1.));
                }

                "ZTHREE" => {
                    assert!((var.obj_coeff - 9.) < EPSILON);
                    assert_eq!(var.bound, Bound::Lower(0.));
                }

                _ => panic!("unexpected variable: {}", var.name.as_ref().unwrap()),
            }
        }

        for constraint in &prob.constraints {
            match constraint.op {
                ConstraintOp::Lte => {
                    assert!((constraint.rhs - 5.) < EPSILON);
                    assert_eq!(constraint.coeffs.len(), 2);
                }

                ConstraintOp::Gte => {
                    assert!((constraint.rhs - 10.) < EPSILON);
                    assert_eq!(constraint.coeffs.len(), 2);
                }

                ConstraintOp::Eq => {
                    assert!((constraint.rhs - 7.) < EPSILON);
                    assert_eq!(constraint.coeffs.len(), 2);
                }
            }
        }
    }
}