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
  • 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")

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

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.0.3.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.0.3-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (461.3 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

parse_lp-3.0.3-cp39-abi3-win_amd64.whl (329.8 kB view details)

Uploaded CPython 3.9+Windows x86-64

parse_lp-3.0.3-cp39-abi3-win32.whl (311.8 kB view details)

Uploaded CPython 3.9+Windows x86

parse_lp-3.0.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (478.4 kB view details)

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

parse_lp-3.0.3-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (512.7 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ s390x

parse_lp-3.0.3-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (589.0 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ppc64le

parse_lp-3.0.3-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (478.6 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARMv7l

parse_lp-3.0.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (469.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

parse_lp-3.0.3-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl (513.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.5+ i686

parse_lp-3.0.3-cp39-abi3-macosx_11_0_arm64.whl (435.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

parse_lp-3.0.3-cp39-abi3-macosx_10_12_x86_64.whl (445.7 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: parse_lp-3.0.3.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for parse_lp-3.0.3.tar.gz
Algorithm Hash digest
SHA256 1678c7c42974f9a9e21ab786684ebcc19a8068fc9ad81d0f11966529a8808a75
MD5 66f49e0fe540b4eff1572963902ea44b
BLAKE2b-256 47dfdc742d06ebf589f23c3defd6600f7d6dfe17dc616bea1f6bafca72414419

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.3-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1c394b3ce240f9f4532af2cb355087138a55b1c6c3ddff3f0bb26693baa1ccd3
MD5 aed03f8dc318f9d575a140e42a839bc8
BLAKE2b-256 e6777dbc97da449c5f335ae04a31de75d543f930ed0b5f9062d569b24f9ad49a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: parse_lp-3.0.3-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 329.8 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 781fe5d69f034b39aadaffedd92067594df6d972fa1c860a0077575c83df53e6
MD5 18bc07b5535e77e42c87ae2ca1f0449b
BLAKE2b-256 b006657f1412a482f961923e303412fe2a70823da9de9a863f395122359732e7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: parse_lp-3.0.3-cp39-abi3-win32.whl
  • Upload date:
  • Size: 311.8 kB
  • Tags: CPython 3.9+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 6a15f76d9ee5ce61d2bd33023759ff12160ff3f9629ae227695bca425dd08f54
MD5 30a7140ac46bae7826ee8d86f9328f0d
BLAKE2b-256 9c46d041bffe0735a8a2f473cfed7b7b3b4f9052f6410fde59e534baaf2aa4aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aa044da8d9dc53e8d2d4d2faa540ec8518316157e88d674ac5665e1d1dcccc60
MD5 a4846753ccfda02a247f80c99143b276
BLAKE2b-256 2d9d3d0961fe7c825d758a8fb0ac020667785a3e75b6674689c0e1df4f7c736f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 128656c25194ac891d773b941f6f7f46ee75137c3af0ca1fa008168048947a8a
MD5 848e3247b252434714e918bec8dd17c8
BLAKE2b-256 87b27610d51e72c326b9d2cb2dfd39325a5e339543c98a388c9deaeb912e43e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 0c43f125618c5816a94957ee7966f4bf3dc7282dc9066cb0daee2c13bc38f04b
MD5 64e2c687425f3aaee27c5d814a88acf3
BLAKE2b-256 6ac946edf0b653bea8b933666b98c286fc1bd0c05a0ffa9048d150033e98bd99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 bd7d366175ac47d1ca189176b9054e4e54d9d13d4970fe9ff63b9cc207a9cb82
MD5 5e0984ec596eaf8c75a06db7729349de
BLAKE2b-256 dae99cc1886b153e0b591596de0ddc1632cd939f058c06088c54d6be5cbdc3d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 97d5012c953c2272f0934d7f877abcadd0c58d61994466b7bbf3a642e180a63c
MD5 e637d51c8f0b19c067835fd28bdd5206
BLAKE2b-256 1349d83bb2390936ba93562aae0a5e3dfb793b270c979af2cd2f978c7a7f7ef5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 d535cee826f6c86eb5f9814687e869f91a6b30172728b8e00931e9c49b6e29f5
MD5 1d6afcf5566cfd832a4b49324f0aa5cd
BLAKE2b-256 a7bf172c4515c71cf1d6563c419f01da0cbe2d2336636a141631ef9fffa12124

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 719cbc4afd3d2ec58de3147f08b0a7317f9127f955274a03912c287e856fe16c
MD5 e675b6360672243a4d2e6931eb61a302
BLAKE2b-256 232ca0bd3d98f344d35a1515e0756a80719aa28b96420bdf3c0bd2f9412cfde3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.3-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0f56bc2c33d5d4b3f9fc26a694f56279c91b6f111eecdcf3730aa491eca23624
MD5 8d1ff1237d5398a7af9a404c888a1fc2
BLAKE2b-256 29067936689fa0227313d24d0a1e776fd3306083a6f44f81df3a045c93812987

See more details on using hashes here.

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