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"  Sense: {constraint['sense']}")
        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": "SOS1",  # or "SOS2"
        "variables": ["var1", "var2", "var3"]
    }
]

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-2.6.2.tar.gz (1.3 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-2.6.2-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (448.0 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

parse_lp-2.6.2-cp39-abi3-win_amd64.whl (307.8 kB view details)

Uploaded CPython 3.9+Windows x86-64

parse_lp-2.6.2-cp39-abi3-win32.whl (297.0 kB view details)

Uploaded CPython 3.9+Windows x86

parse_lp-2.6.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (462.7 kB view details)

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

parse_lp-2.6.2-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (494.3 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ s390x

parse_lp-2.6.2-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (581.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ppc64le

parse_lp-2.6.2-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (468.0 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARMv7l

parse_lp-2.6.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (454.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

parse_lp-2.6.2-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl (498.7 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.5+ i686

parse_lp-2.6.2-cp39-abi3-macosx_11_0_arm64.whl (415.0 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

parse_lp-2.6.2-cp39-abi3-macosx_10_12_x86_64.whl (427.9 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for parse_lp-2.6.2.tar.gz
Algorithm Hash digest
SHA256 b0a4c738897d015665ccab6991c30672f84f9b532e85a78d0a64197f01dbfaeb
MD5 805a20f3199e15028697a0cd45b81536
BLAKE2b-256 df374e82cd4c66ee148fb552e9dadc62264e83907ac4b7e3740343120c000b70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-2.6.2-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a03030202dfaf317b34ecad6626e610148bf6dee0c76219d4efe5bf958d5196f
MD5 48bb1caedb98d7d94b1219725ad4f8a8
BLAKE2b-256 ffb9a4932146e37cc72809412b1788f8b8a7f891ead4b69c83835a4b51a24dc3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: parse_lp-2.6.2-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 307.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-2.6.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 70498c1da1bc1d54d940295414bf99fe48ffde11f21cc1e7aaebe0d8a178c355
MD5 d325931cba8c7abe9492a780b0acad1c
BLAKE2b-256 2620e417fa7a8bae5fbc553b4adf30893c3467a2d6bf7c584ac77505ca8c3b77

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for parse_lp-2.6.2-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 a608357e9fe9dba3c56838e47dfcf2cddc898d903006946fb3f161effa36b5d2
MD5 1b3467a57dfafb81182fe69b2db63cb3
BLAKE2b-256 e040c826423870a300a11d3c770c8542e6e3a109c48a1d9b0b46d9617f480004

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-2.6.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6c95ac58bf38369028423e9af24abd9e8219ecb19a98372c46431d847933d5a6
MD5 c4838af497c37d62707e38d1dabb0130
BLAKE2b-256 6da6e9859649e31552f8b849ee7abae16cf3591ceb8e8ebf9d9f0bf4f1a9ce01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-2.6.2-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 2d02359cd0ebdb8f92773883fb22abd738c4828e4d267045cab4a7438a03c630
MD5 5452caeaf83a92c0049fcdfb7a64a4de
BLAKE2b-256 b25d2a4c40933b571797046518354bcbf1a5dbda87ccaaccdf9d4c3ead877227

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-2.6.2-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 0f76307733f98a4d9250aeac69528049dbe7bd9fa90fe78453c2f8ff323764da
MD5 b7fd4f38359e408c0ad114e980396a5e
BLAKE2b-256 206658ac0d8dedea7d2fd05b4c237e24384e3c7b382f0e1e993ebcc35d540142

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-2.6.2-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 351364bacb15454a8f3bf347539181373fe30623f1eda53e8fa0cd39a48d8e7b
MD5 7af39706097ec4a4c30f362b4e53fe03
BLAKE2b-256 17ecb6e166f29494e526793e963e440ffca3a51306d9a1f0849659d86a7f21aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-2.6.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ce6a28851aa94443a147f831415c0453c925285ea7bb9b896b8ed2c86dec9c71
MD5 4d0ee02e5660e935518813c99767789c
BLAKE2b-256 bd7caac2f20ec42b740570c5a142f368e5ac190e3aafb085bb6f211e2c59ed5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-2.6.2-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 4214f3c64cd625e41806e14bb82fede174422b16b96a4e4810354138495848fa
MD5 563f19636e4e1e3227c3aa803ac91791
BLAKE2b-256 c4e5e37783fecb597349bb0b17d6273b669a72e6d56b005951069787ce10ae02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-2.6.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0dc947308a80f4d08c740e9f1eb936d5de3529c8b29c6b4e5edc8b98ecd3391c
MD5 31767ca49b22e51f805e69ec280b87aa
BLAKE2b-256 9ed4e277f59722a62cdde18d2a22618be19855eca19532eba28712c9601b9d3e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-2.6.2-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c74d80e44fb7623fb29816f24402b05887402bc00f80d04ca060a6f8e806fab2
MD5 d2ed437a5c5b79814401c828c8053968
BLAKE2b-256 591c2492059faf1c858160a794b9ab06e3e5b4599fba9e42a437ab6a20eec1a5

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