Skip to main content

A LP file format parser for Python, powered by Rust

Project description

parse_lp

PyPI version

A LP file format parser, writer, and modifier for Python, powered by Rust.

Features

  • Complete LP Support: Handles all standard LP file format features
  • Problem Modification: Programmatically modify objectives, constraints, and variables
  • LP File Writing: Generate LP files from modified problems with round-trip compatibility
  • Problem Analysis: Comprehensive statistics, structure analysis, and issue detection with configurable thresholds
  • Easy Data Access: Direct access to problem components (variables, constraints, objectives)
  • CSV Export: Export parsed data to CSV files for further analysis
  • Problem Comparison: Compare two LP problems to identify differences
  • Type Safety: Full type hints for better IDE support and development experience

Installation

pip install parse_lp

Quick Start

from parse_lp import LpParser

# Parse an LP file
parser = LpParser("path/to/problem.lp")
parser.parse()

# Access problem information
print(f"Problem: {parser.name}")
print(f"Sense: {parser.sense}")
print(f"Variables: {parser.variable_count()}")
print(f"Constraints: {parser.constraint_count()}")

# Modify the problem
parser.update_objective_coefficient("OBJ", "x1", 5.0)
parser.rename_variable("x2", "production")
parser.update_constraint_rhs("C1", 100.0)

# Write back to LP format
modified_lp = parser.to_lp_string()
parser.save_to_file("modified_problem.lp")

# Analyze problem structure and detect issues
analysis = parser.analyze()
print(f"Matrix density: {analysis['summary']['density']:.4f}")
print(f"Issues found: {len(analysis['issues'])}")

# Export to CSV files
parser.to_csv("output_directory/")

Usage Examples

Basic Parsing and Information

from parse_lp import LpParser

parser = LpParser("optimization_problem.lp")
parser.parse()

# Get problem overview
print(f"Problem Name: {parser.name}")
print(f"Optimization Sense: {parser.sense}")
print(f"Variables: {parser.variable_count()}")
print(f"Constraints: {parser.constraint_count()}")
print(f"Objectives: {parser.objective_count()}")

Accessing Problem Data

# Access objectives
for i, objective in enumerate(parser.objectives):
    print(f"Objective {i+1}: {objective['name']}")
    for coef in objective['coefficients']:
        print(f"  {coef['name']}: {coef['value']}")

# Access variables
for var_name, var_info in parser.variables.items():
    print(f"Variable {var_name}:")
    print(f"  Type: {var_info['var_type']}")
    if 'bounds' in var_info:
        bounds = var_info['bounds']
        if 'lower' in bounds:
            print(f"  Lower bound: {bounds['lower']}")
        if 'upper' in bounds:
            print(f"  Upper bound: {bounds['upper']}")

# Access constraints
for constraint in parser.constraints:
    print(f"Constraint {constraint['name']}:")
    print(f"  Type: {constraint['type']}")
    if constraint['type'] == 'standard':
        print(f"  Operator: {constraint['operator']}")
        print(f"  RHS: {constraint['rhs']}")
        print(f"  Coefficients: {len(constraint['coefficients'])}")

CSV Export

import os

# Create output directory
os.makedirs("output", exist_ok=True)

# Export to CSV files
parser.to_csv("output/")

# Files created:
# - output/variables.csv
# - output/constraints.csv
# - output/objectives.csv

Problem Analysis

Analyze problem structure, detect potential issues, and get comprehensive statistics.

from parse_lp import LpParser

parser = LpParser("problem.lp")
parser.parse()

# Get complete analysis
analysis = parser.analyze()

# Summary statistics
summary = analysis['summary']
print(f"Problem: {summary['name']}")
print(f"Sense: {summary['sense']}")
print(f"Variables: {summary['variable_count']}")
print(f"Constraints: {summary['constraint_count']}")
print(f"Nonzeros: {summary['total_nonzeros']}")
print(f"Matrix density: {summary['density']:.4f}")

# Sparsity metrics
sparsity = analysis['sparsity']
print(f"Variables per constraint: {sparsity['min_vars_per_constraint']} - {sparsity['max_vars_per_constraint']}")

# Variable type distribution
var_types = analysis['variables']['type_distribution']
print(f"Variable types: {var_types}")

# Coefficient ranges
coeffs = analysis['coefficients']
print(f"Constraint coefficients: {coeffs['constraint_coeff_range']}")
print(f"Objective coefficients: {coeffs['objective_coeff_range']}")
print(f"Coefficient ratio: {coeffs['coefficient_ratio']:.2f}")

# Check for issues (warnings and errors)
for issue in analysis['issues']:
    print(f"[{issue['severity']}] {issue['category']}: {issue['message']}")

Analysis with custom thresholds:

# Customize thresholds for issue detection
analysis = parser.analyze_with_config(
    large_coeff_threshold=1e8,      # Flag coefficients above this
    small_coeff_threshold=1e-10,    # Flag coefficients below this
    ratio_threshold=1e5             # Flag if max/min ratio exceeds this
)

Get issues only:

# Get just the detected issues without full analysis
issues = parser.get_issues()

for issue in issues:
    print(f"[{issue['severity']}] {issue['category']}: {issue['message']}")
    if issue['details']:
        print(f"  Details: {issue['details']}")

Issue types detected:

  • Invalid variable bounds (lower > upper)
  • Numerical scaling warnings (large coefficients, high ratios)
  • Empty constraints (no variables)
  • Unused variables (not in any constraint or objective)
  • Fixed variables (lower bound = upper bound)
  • Singleton constraints (only one variable)

Problem Modification

from parse_lp import LpParser

# Parse an existing LP file
parser = LpParser("optimization_problem.lp")
parser.parse()

# Modify objectives
parser.update_objective_coefficient("profit", "x1", 5.0)
parser.rename_objective("profit", "total_profit")

# Modify constraints
parser.update_constraint_coefficient("capacity", "x1", 2.0)
parser.update_constraint_rhs("capacity", 200.0)
parser.rename_constraint("capacity", "production_limit")

# Modify variables
parser.rename_variable("x1", "production_a")
parser.update_variable_type("production_a", "integer")

# Set problem properties
parser.set_problem_name("Modified Optimization Problem")
parser.set_sense("minimize")

# Write back to LP format
modified_lp_content = parser.to_lp_string()
parser.save_to_file("modified_problem.lp")

# Verify round-trip compatibility
new_parser = LpParser("modified_problem.lp")
new_parser.parse()
print(f"Successfully modified and re-parsed: {new_parser.name}")

Problem Comparison

# Parse two different versions of a problem
parser1 = LpParser("problem_v1.lp")
parser1.parse()

parser2 = LpParser("problem_v2.lp")
parser2.parse()

# Compare the problems
diff = parser1.compare(parser2)

print("Comparison Results:")
print(f"Name changed: {diff['name_changed']}")
print(f"Sense changed: {diff['sense_changed']}")
print(f"Variable count difference: {diff['variable_count_diff']}")
print(f"Added variables: {diff['added_variables']}")
print(f"Removed variables: {diff['removed_variables']}")
print(f"Modified variables: {diff['modified_variables']}")
print(f"Added constraints: {diff['added_constraints']}")
print(f"Removed constraints: {diff['removed_constraints']}")

Objectives Structure

[
    {
        "name": "objective_name",
        "coefficients": [
            {"name": "variable_name", "value": 1.5},
            {"name": "another_var", "value": -2.0}
        ]
    }
]

Variables Structure

{
    "variable_name": {
        "name": "variable_name",
        "var_type": "Continuous",  # or "Binary", "Integer", etc.
        "bounds": {  # Optional
            "lower": 0.0,  # Optional
            "upper": 100.0  # Optional
        }
    }
}

Constraints Structure

[
    {
        "name": "constraint_name",
        "type": "standard",  # or "sos"
        "sense": "LessOrEqual",  # "Equal", "GreaterOrEqual"
        "rhs": 10.0,
        "coefficients": [
            {"name": "variable_name", "value": 2.0}
        ]
    },
    {
        "name": "sos_constraint",
        "type": "sos",
        "sos_type": "S1",  # or "S2"
        "weights": [
            {"name": "var1", "value": 1.0},
            {"name": "var2", "value": 2.0}
        ]
    }
]

Modification Methods

Objective Methods

  • update_objective_coefficient(obj_name, var_name, coefficient) - Update or add coefficient
  • rename_objective(old_name, new_name) - Rename an objective
  • remove_objective(obj_name) - Remove an objective

Constraint Methods

  • update_constraint_coefficient(const_name, var_name, coefficient) - Update or add coefficient
  • update_constraint_rhs(const_name, new_rhs) - Update right-hand side value
  • rename_constraint(old_name, new_name) - Rename a constraint
  • remove_constraint(const_name) - Remove a constraint

Variable Methods

  • rename_variable(old_name, new_name) - Rename variable across problem
  • update_variable_type(var_name, var_type) - Change variable type
  • remove_variable(var_name) - Remove variable from problem

Problem Methods

  • set_problem_name(name) - Set problem name
  • set_sense(sense) - Set optimization sense ("maximize" or "minimize")

Writing Methods

  • to_lp_string() - Generate LP format string
  • to_lp_string_with_options(**options) - Generate with custom formatting
  • save_to_file(filepath) - Save to LP file

Analysis Methods

  • analyze() - Get complete problem analysis including statistics and issues
  • analyze_with_config(large_coeff_threshold, small_coeff_threshold, ratio_threshold) - Analysis with custom thresholds
  • get_issues() - Get only detected issues/warnings without full analysis

Variable Types

Supported variable types for update_variable_type():

  • "binary" - Binary variables (0 or 1)
  • "integer" - General integer variables
  • "general" - Continuous variables (default)
  • "free" - Free variables (no bounds)
  • "semicontinuous" - Semi-continuous variables

Supported LP Format Features

  • Multiple objective functions
  • Standard constraints (≤, =, ≥)
  • Variable bounds
  • Variable types (continuous, binary, integer)
  • SOS (Special Ordered Sets) constraints
  • Problem names and comments
  • Scientific notation in coefficients

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Contributing

Issues and pull requests are welcome at: https://github.com/dandxy89/lp_parser_rs

make build
make install
make unit-test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parse_lp-3.3.0.tar.gz (1.2 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

parse_lp-3.3.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (500.6 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

parse_lp-3.3.0-cp39-abi3-win_amd64.whl (365.2 kB view details)

Uploaded CPython 3.9+Windows x86-64

parse_lp-3.3.0-cp39-abi3-win32.whl (346.3 kB view details)

Uploaded CPython 3.9+Windows x86

parse_lp-3.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (518.7 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

parse_lp-3.3.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (560.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ s390x

parse_lp-3.3.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (640.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ppc64le

parse_lp-3.3.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (513.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARMv7l

parse_lp-3.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (506.6 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

parse_lp-3.3.0-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl (556.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.5+ i686

parse_lp-3.3.0-cp39-abi3-macosx_11_0_arm64.whl (468.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

parse_lp-3.3.0-cp39-abi3-macosx_10_12_x86_64.whl (477.4 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file parse_lp-3.3.0.tar.gz.

File metadata

  • Download URL: parse_lp-3.3.0.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for parse_lp-3.3.0.tar.gz
Algorithm Hash digest
SHA256 6a3f99e0d9dcf89463518bcdbcc0685d02e4951b3f8d8651a26901999a28bdf1
MD5 0a4b0f3799a8195e6c2d9ab3bbfcdf1d
BLAKE2b-256 a61bdde3475385e605cf6d15ba5220ee2d66caf06f0a5f18673ea7f7443f8bf1

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0.tar.gz:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for parse_lp-3.3.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 87e1eaa5a2787f7b7f2c8f58f84549e0d146a1eb8f991870e546bec9619b4245
MD5 c6284738209bcb9cc5312ee38a1de933
BLAKE2b-256 ebe77104942fb37ec86539afaa0e83ba5e904f8a27860d8ec0f839e68b84862a

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: parse_lp-3.3.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 365.2 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 36b5068919db93df657c05a71bc6b0715e668d066225a00ea353bc2cbd6a1a1b
MD5 dcb4687a163b3ce84f06707a9894782a
BLAKE2b-256 c2c049a451fb140b2542aa63cd7ec8d8ee81082beb394f91af16fce9edc5337a

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-win_amd64.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-win32.whl.

File metadata

  • Download URL: parse_lp-3.3.0-cp39-abi3-win32.whl
  • Upload date:
  • Size: 346.3 kB
  • Tags: CPython 3.9+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 9cb25fbc1cd75e7f0493a71c73f5658c7cf9d4a0261ab89c6a88da022ed182be
MD5 03165629026f844ed3cc634c459c71c6
BLAKE2b-256 f0edb9d14078b7b5f472fe0da80489e137a62d5a1505d90b61ed3474d97fb200

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-win32.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 50449fda359c7c742fda6a4890986cc8bc6c55074bab09123f1a5629200af813
MD5 dc7f944c44f12366adbf7621421816e3
BLAKE2b-256 da5cec5112c613de1ce0421df989141a729a775b1fe339b0cfe4b41efa0a107a

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 05d6eadd92406ad1191e942f86760d3c85e0f6e8c34e8c0e8a20ddd9832a1095
MD5 0f7b10f94df8781b8000fb1a5113a09c
BLAKE2b-256 809228e4e57b2db4ba4f46e80ea90c80fb658c2fcc5c27805660b9e7d67dada7

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 c2ed2160c641e3bee1cebd3ab20d613d50376f90932e5775dafd2499bf8d1b01
MD5 aad602cb37d3e080d9eec7e00753a34c
BLAKE2b-256 bc3731ef69a2b2e7a0ef53552e18e7611b5a20653c4d83235159641c76304725

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 9899c8af3a2c52cf940a4bdf6a95d001c09ef8c90fd971a17be59682dada4040
MD5 e052215787f4e344efa03046bbe5ecc1
BLAKE2b-256 7c1646b9ff817170038b63f392b4fa2586b81c1a6a08a5015b8c91bbf273c32d

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 845761cf58ac53e2e4697c26ed222fb341fba7e7b3e2595cfd9e10be48e9ab40
MD5 2dd1b70bb505a4b72a4326e5270f8243
BLAKE2b-256 23e6af1e6e513d9edfea731c0f86b68261a88f73a155ad9b3f0de82ccd265c91

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 5f650b1ec660aa43692900547d433f4441e927ca09d8cdc232be60d8ca6c2879
MD5 7c3b34f5bd6e48d7aa6af5ae62760cf9
BLAKE2b-256 72b215f8d5c04e307c375552d6d17936888c599ff5c8f68c815fa6c963cbfa7f

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 502ae04251396e9bc80dbeee9ac07e0c0abd4c829f385718e3abf7ed23ac17a3
MD5 6abc434ea3cfa2133d93e2c1b44555bc
BLAKE2b-256 f84e1487b6e44a15b748c9d3f7d8228eed1e119f4aa865aaabcc86211adfc012

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parse_lp-3.3.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for parse_lp-3.3.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3c3e5058e92dfd5fcf5c69dd91ccf4f1fb1f9a25c218f31be8bbf63256b7daa3
MD5 946a93f8ec468fb7ba82ac4737835c03
BLAKE2b-256 474fe19a194227a9cfeb169a114c557e6de912d4ec7a6c5fb8bacd3faac9874c

See more details on using hashes here.

Provenance

The following attestation bundles were made for parse_lp-3.3.0-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: python_deploy.yml on dandxy89/lp_parser_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page