Rust LP File Parser, Writer, and Diff Tool
Overview
A robust Rust library for parsing, modifying, and writing Linear Programming (LP) files. Built on the NOM parsing framework, 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.
Supported Specifications
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
-
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
:
[]
= "2.4.1"
Basic Usage
Clone and run with a sample file:
# Parse a single LP file
# Compare two LP files (enabling the 'diff' feature)
Using the library directly:
use ;
use Path;
LP File Writing and Modification
use ;
Enable Optional Features
[]
= { = "2.4.1", = ["serde", "diff"] }
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 objectiverename_objective(old_name, new_name)
- Rename an objectiveremove_objective(objective_name)
- Remove an objective
Constraint Modifications
update_constraint_coefficient(constraint_name, variable_name, coefficient)
- Update or add a coefficient in a constraintupdate_constraint_rhs(constraint_name, new_rhs)
- Update the right-hand side valuerename_constraint(old_name, new_name)
- Rename a constraintremove_constraint(constraint_name)
- Remove a constraint
Variable Modifications
rename_variable(old_name, new_name)
- Rename a variable across all objectives and constraintsupdate_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
use ;
// Write with default options
let lp_content = write_lp_string?;
// Write with custom options
let options = LpWriterOptions ;
let lp_content = write_lp_string_with_options?;
Development
Testing
The project uses snapshot testing via insta
for reliable test management:
# Run all tests with all features enabled
# Review snapshot changes
Test Data Sources
The test suite includes data from various open-source projects:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.