lp_parser_rs 3.3.0

A Rust parser for the LP file format.
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
# Rust LP File Parser, Writer, and Diff Tool

[![Cargo Test](https://github.com/dandxy89/congenial-enigma/actions/workflows/cargo_test.yml/badge.svg)](https://github.com/dandxy89/congenial-enigma/actions/workflows/cargo_test.yml)
[![Crates.io](https://img.shields.io/crates/v/lp_parser_rs.svg)](https://crates.io/crates/lp_parser_rs)
[![Documentation](https://docs.rs/lp_parser_rs/badge.svg)](https://docs.rs/lp_parser_rs/)
[![PyPI version](https://badge.fury.io/py/parse-lp.svg)](https://badge.fury.io/py/parse-lp)
[![PyPI Downloads](https://static.pepy.tech/personalized-badge/parse-lp?period=total&units=NONE&left_color=BLACK&right_color=GREEN&left_text=downloads)](https://pepy.tech/projects/parse-lp)

## Interactive TUI Diff Viewer (`lp_diff`)

An interactive terminal-based diff viewer for LP files, built with [ratatui](https://ratatui.rs). Compare two LP files with coefficient-level detail, fuzzy search, filtering, and integrated solving via [HiGHS](https://highs.dev).

### Install

```bash
# From a cloned repo
git clone https://github.com/dandxy89/lp_parser_rs.git
cd lp_parser_rs
cargo install --path tui
```

### Quick Start

```bash
lp_diff base.lp modified.lp
```

For a non interactive summary:

```bash
lp_diff base.lp modified.lp --summary
```

### Features

- **Three-panel layout** — section selector, name list, and detail panel with coefficient-level side-by-side diffs
- **Four sections** — Summary, Variables, Constraints, and Objectives
- **Filtering** — show all (`a`), added (`+`), removed (`-`), or modified (`m`) entries
- **Telescope-style search** — fuzzy (default), regex (`r:`), or substring (`s:`) across all sections
- **HiGHS solver** — press `S` to solve either or both files and compare results
- **Vim-style navigation**`j`/`k`, `g`/`G`, `Ctrl+d`/`Ctrl+u`, jumplist (`Ctrl+o`/`Ctrl+i`)
- **Clipboard & export** — yank names/details (`y`/`Y`), write solve diffs to CSV (`w`)
- **Built-in help** — press `?` for full keybindings reference

See [`tui/README.md`](tui/README.md) for the complete keybindings reference and detailed documentation.

## Overview

A robust Rust library for parsing, modifying, and writing Linear Programming (LP) files. Built on the [LALRPOP](https://github.com/lalrpop/lalrpop) parser generator, this crate provides comprehensive support for the LP file format with the ability to parse, programmatically modify, and regenerate LP files according to major industry specifications.

The Grammar is defined with the [lp.lalrpop](/rust/src/lp.lalrpop) file - should you be curious...

### Supported Specifications

- [IBM CPLEX v22.1.1]https://www.ibm.com/docs/en/icos/22.1.1?topic=cplex-lp-file-format-algebraic-representation
- [FICO Xpress]https://www.fico.com/fico-xpress-optimization/docs/dms2020-03/solver/optimizer/HTML/chapter10_sec_section102.html
- [Gurobi]https://www.gurobi.com/documentation/current/refman/lp_format.html
- Mosek

## Features

### Core Functionality

- **Problem Definition**
  - Problem name and sense specification
  - Single and multi-objective optimisation support
  - Comprehensive constraint handling

- **Variable Support**
  - Integer, general, bounded, free, semi-continuous variables

- **LP File Writing and Modification**
  - Generate LP files from parsed problems
  - Modify objectives, constraints, and variables programmatically
  - Round-trip compatibility (parse → modify → write → parse)
  - Maintain proper LP format specifications

### Advanced Features

- **Problem Analysis**
  - Comprehensive statistics: variable/constraint counts, matrix density, sparsity metrics
  - Variable analysis: type distribution, unused/fixed variables, invalid bounds detection
  - Constraint analysis: type distribution, empty/singleton constraints, RHS ranges
  - Coefficient analysis: ranges, large/small coefficient detection, scaling ratios
  - Issue detection with severity levels (errors, warnings, info)
  - Configurable thresholds for different problem domains

- **LP File Comparison (`diff` feature)**
  - Identify added, removed, and modified elements
  - Useful for model version control and validation

- **Serialisation (`serde` feature)**
  - Full serialisation support for all model structures
  - Compatible with various data formats
  - Enables integration with other tools and systems

## Quick Start

### Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
lp_parser_rs = "3.0.0" # x-release-please-version
```

### Basic Usage

Using the library directly:

```rust
use lp_parser_rs::{parser::parse_file, problem::LpProblem};
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Parse LP file content
    let content = parse_file(Path::new("problem.lp"))?;

    // Parse into LP problem structure
    let problem = LpProblem::parse(&content)?;

    // Access problem components
    println!("Problem name: {:?}", problem.name());
    println!("Objective count: {}", problem.objective_count());
    println!("Constraint count: {}", problem.constraint_count());
    println!("Variable count: {}", problem.variable_count());

    Ok(())
}
```

### LP File Writing and Modification

```rust
use lp_parser_rs::{problem::LpProblem, writer::write_lp_string, model::*};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Parse an existing LP file
    let lp_content = std::fs::read_to_string("problem.lp")?;
    let mut problem = LpProblem::parse(&lp_content)?;

    // Modify objectives
    problem.update_objective_coefficient("profit", "x1", 5.0)?;
    problem.rename_objective("profit", "total_profit")?;

    // Modify constraints
    problem.update_constraint_coefficient("capacity", "x1", 2.0)?;
    problem.update_constraint_rhs("capacity", 200.0)?;
    problem.rename_constraint("capacity", "production_limit")?;

    // Modify variables
    problem.rename_variable("x1", "production_a")?;
    problem.update_variable_type("production_a", VariableType::Integer)?;

    // Write back to LP format
    let modified_lp = write_lp_string(&problem)?;
    std::fs::write("modified_problem.lp", modified_lp)?;

    Ok(())
}
```

### Enable Optional Features

```toml
[dependencies]
lp_parser_rs = { version = "3.0.0", features = ["serde", "diff"] } # x-release-please-version
```

## Command-Line Interface

The `lp_parser` binary provides a comprehensive CLI for working with LP files.

### Installation

```bash
# Install with all features
cargo install lp_parser_rs --all-features

# Or build from source
git clone https://github.com/dandxy89/lp_parser_rs.git
cd lp_parser_rs/rust
cargo build --release --all-features
```

### Commands

```
lp_parser <COMMAND>

Commands:
  parse    Parse an LP file and display its structure
  info     Show detailed statistics about an LP problem
  analyze  Analyze problem structure, detect issues, and report statistics
  diff     Compare two LP files (requires 'diff' feature)
  convert  Convert LP file to another format
  solve    Solve an LP problem using external solvers (requires 'lp-solvers' feature)

Global Options:
  -v, --verbose    Increase output verbosity
  -q, --quiet      Suppress non-essential output
  -h, --help       Print help
  -V, --version    Print version
```

### Examples

**Parse and display an LP file:**

```bash
lp_parser parse problem.lp
```

**Get problem statistics:**

```bash
lp_parser info problem.lp
# With detailed listings
lp_parser info problem.lp --variables --constraints --objectives
```

**Analyze problem structure and detect issues:**

```bash
# Full analysis with statistics and issue detection
lp_parser analyze problem.lp

# Show only warnings and errors
lp_parser analyze problem.lp --issues-only

# Output as JSON or YAML
lp_parser analyze problem.lp --format json --pretty
lp_parser analyze problem.lp --format yaml -o analysis.yaml

# Custom thresholds for numerical issue detection
lp_parser analyze problem.lp --large-coeff-threshold 1e8 --ratio-threshold 1e5
```

Example analysis output:
```yaml
summary:
  name: diet
  sense: Minimize
  objective_count: 1
  constraint_count: 7
  variable_count: 16
  total_nonzeros: 64
  density: 0.571
sparsity:
  min_vars_per_constraint: 6
  max_vars_per_constraint: 10
variables:
  type_distribution:
    upper_bounded: 9
    double_bounded: 7
  discrete_variable_count: 0
constraints:
  type_distribution:
    equality: 7
  rhs_range:
    min: 30.0
    max: 50000.0
coefficients:
  constraint_coeff_range:
    min: 0.1
    max: 3055.2
  coefficient_ratio: 101840.0
issues: []
```

**Output as JSON or YAML:**

```bash
lp_parser info problem.lp --format json --pretty
lp_parser parse problem.lp --format yaml -o problem.yaml
```

**Compare two LP files:**

```bash
lp_parser diff old_model.lp new_model.lp
lp_parser diff old.lp new.lp --format json --pretty
```

**Convert between formats:**

```bash
# To LP (with formatting options)
lp_parser convert problem.lp --format lp --precision 4 --compact

# To CSV (creates constraints.csv, objectives.csv, variables.csv)
lp_parser convert problem.lp --format csv --output ./output_dir

# To JSON/YAML
lp_parser convert problem.lp --format json --pretty -o problem.json
lp_parser convert problem.lp --format yaml -o problem.yaml
```

**Solve with external solvers:**
```bash
# Using CBC (default)
lp_parser solve problem.lp

# Using GLPK
lp_parser solve problem.lp --solver glpk

# Output solution as JSON
lp_parser solve problem.lp --format json --pretty
```

## Solving with External Solvers (`lp-solvers` feature)

Enable the `lp-solvers` feature to solve parsed LP problems using external solvers like CBC, Gurobi, CPLEX, or GLPK via the [lp-solvers](https://crates.io/crates/lp-solvers) crate:

```toml
[dependencies]
lp_parser_rs = { version = "3.0.0", features = ["lp-solvers"] } # x-release-please-version
lp-solvers = "1.1"
```

```rust
use lp_parser_rs::{problem::LpProblem, ToLpSolvers};
use lp_solvers::solvers::{CbcSolver, SolverTrait};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let lp_content = r"
Minimize
 obj: 2 x + 3 y
Subject To
 c1: x + y <= 10
 c2: x >= 2
Bounds
 x >= 0
 y >= 0
End
";

    let problem = LpProblem::parse(lp_content)?;
    let compat = problem.to_lp_solvers()?;

    // Check for any compatibility warnings
    for warning in compat.warnings() {
        eprintln!("Warning: {}", warning);
    }

    // Solve using CBC solver (must be installed on your system)
    let solver = CbcSolver::new();
    let solution = solver.run(&compat)?;
    println!("Solution status: {:?}", solution.status);

    Ok(())
}
```

**Limitations:** The lp-solvers compatibility layer does not support multiple objectives (returns an error), strict inequalities (`<`, `>`), or SOS constraints (ignored with a warning).

## API Reference

### Problem Modification Methods

The `LpProblem` struct provides comprehensive methods for modifying LP problems:

#### Objective Modifications
- `update_objective_coefficient(objective_name, variable_name, coefficient)` - Update or add a coefficient in an objective
- `rename_objective(old_name, new_name)` - Rename an objective
- `remove_objective(objective_name)` - Remove an objective

#### Constraint Modifications
- `update_constraint_coefficient(constraint_name, variable_name, coefficient)` - Update or add a coefficient in a constraint
- `update_constraint_rhs(constraint_name, new_rhs)` - Update the right-hand side value
- `rename_constraint(old_name, new_name)` - Rename a constraint
- `remove_constraint(constraint_name)` - Remove a constraint

#### Variable Modifications
- `rename_variable(old_name, new_name)` - Rename a variable across all objectives and constraints
- `update_variable_type(variable_name, new_type)` - Change variable type (Binary, Integer, etc.)
- `remove_variable(variable_name)` - Remove a variable from all objectives and constraints

### Writing LP Files

```rust
use lp_parser_rs::writer::{write_lp_string, write_lp_string_with_options, LpWriterOptions};

// Write with default options
let lp_content = write_lp_string(&problem)?;

// Write with custom options
let options = LpWriterOptions {
    include_problem_name: true,
    max_line_length: 80,
    decimal_precision: 6,
    include_section_spacing: true,
};
let lp_content = write_lp_string_with_options(&problem, &options)?;
```

## Development

### Testing

The project uses snapshot testing via `insta` for reliable test management:

```bash
# Run all tests with all features enabled
cargo insta test --all-features

# Review snapshot changes
cargo insta review
```

## Test Data Sources

The test suite includes data from various open-source projects:

- [Jplex]https://github.com/asbestian/jplex/blob/main/instances/afiro.lp
- [LPWriter.jl]https://github.com/odow/LPWriter.jl/blob/master/test/model2.lp
- [Lp-Parser]https://github.com/aphi/Lp-Parser

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.