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

Uploaded PyPymanylinux: glibc 2.17+ ARM64

parse_lp-3.0.0-cp39-abi3-win_amd64.whl (331.0 kB view details)

Uploaded CPython 3.9+Windows x86-64

parse_lp-3.0.0-cp39-abi3-win32.whl (313.8 kB view details)

Uploaded CPython 3.9+Windows x86

parse_lp-3.0.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (484.6 kB view details)

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

parse_lp-3.0.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (519.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ s390x

parse_lp-3.0.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (603.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ppc64le

parse_lp-3.0.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (485.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARMv7l

parse_lp-3.0.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (477.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

parse_lp-3.0.0-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl (517.7 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.5+ i686

parse_lp-3.0.0-cp39-abi3-macosx_11_0_arm64.whl (437.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

parse_lp-3.0.0-cp39-abi3-macosx_10_12_x86_64.whl (448.3 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: parse_lp-3.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 df16de89216da7d9dacebe91db93b1eeb47ed6385ff60503c3353a1c4baff0a7
MD5 d464dc14a193c5e92e4939c18fed3564
BLAKE2b-256 1e3343c48972e39f255c6ac684a898ea0fb5d658efda77f3ea1101b07d95a754

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 35f14cc50420618a395c41fc0de1c98e550960ec730a5c26afed3d95f2a2ada6
MD5 76162b3cea46c1674b52a8190159b92b
BLAKE2b-256 f879eaf75a8b773759082520849f01f81dcf872496abe35533cbd4f92e629bac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: parse_lp-3.0.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 331.0 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.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b963aaef1baee56874ffe338e4ecbdf64d5f9e08cf35e0326e10e7a49c6c3b60
MD5 00ca601f02c45f50dcb06e6a1ec03bdc
BLAKE2b-256 abb21e0be3bfd4da8aedb81ee49f8b70797565f7b823cbb13dbd55fc024489cc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: parse_lp-3.0.0-cp39-abi3-win32.whl
  • Upload date:
  • Size: 313.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.0-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 5c74e8499522f16ee125c95e6e505e8887c5769bf1ec26088a26a060c6e1924b
MD5 726a11530333aba234a2e8f9eb747371
BLAKE2b-256 cc9295245b55315be4a525de5addb2bc78cc300378d4d559f10bcee603d7c3b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f7dc2bcae56b781b2c26a0f5bf469afb21c95559aadc8bcb5873e2461d17640d
MD5 814f6ef1d8844b7c2789e5653ca82ead
BLAKE2b-256 e4dcc82066994a04e7e44b79831867ea6104bbc21dc2cf56e6fad25a172103e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 32b525138e32969396f89f37df443d867f73786caf677467b6e4d5bab50a82b6
MD5 ec28f0506459702403fed0a58058d07f
BLAKE2b-256 faa74aaa58c0733d234cf5ae5f2a5bc1fe2d54da79e70b49c182dd294ad5d7c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 677d61beea3b8ea9e847935ff02d76a622011841ada678875c40bf0853d63e24
MD5 16f10059ab19c566f4604856829cd3f4
BLAKE2b-256 d7d5cbf968a5faab886ae0b9002bbea3a6f9428d857677d064186689e474af98

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 e1f761958b00cd5ce99ed6e70ace4eb18b6cc683e121675bade94f8cfbd72243
MD5 f257b19c53d671018b81a361733c9c1f
BLAKE2b-256 1bca6721590b53642043adf389937c04e37f9cd32b7d85bcd500dce1d91efb6d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a0d7004067e7c80f4cccb8b5daa70f97d778827d6953660fe8d7366492dd34c0
MD5 1aac987b66c046cc855adeefbb763044
BLAKE2b-256 6155d31e07d9ef871844980f701e55d8abcaede83c0c5978a29ee3c4b283231c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.0-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 17741fce8db6ebfec4900cb429c9bdfda6eb16a1ea27c1f3c2345bde1a02e268
MD5 656a73f9b84261bb6698441671535e40
BLAKE2b-256 3de1b9c9447fdfcdd73bc077a464e06f6ca60753c6dc003831133a7312210086

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 562529e6356c09dffabd70399333be8c2306a2fe366d73a560bfe3bf6cc410fd
MD5 70d50964a3d5f6dc5c2084cb38d2f6f9
BLAKE2b-256 75f080672d7366a1f7a495e2c2ba0e3cb9eef3fd54071d33bfab4f4849efc7cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 954f2f0fad84067db641ab67cbc717451f3e2088f819be5296334bba8ec5a929
MD5 08d7da9309d4291fa9b5e7e1711658f9
BLAKE2b-256 604055ae1c737246f09b82bda41ee879e4dc52e6ab53e3a6906780856969fe06

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