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.1.0.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-3.1.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (477.9 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

parse_lp-3.1.0-cp39-abi3-win_amd64.whl (344.5 kB view details)

Uploaded CPython 3.9+Windows x86-64

parse_lp-3.1.0-cp39-abi3-win32.whl (324.8 kB view details)

Uploaded CPython 3.9+Windows x86

parse_lp-3.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (494.9 kB view details)

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

parse_lp-3.1.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (522.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ s390x

parse_lp-3.1.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (621.0 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ppc64le

parse_lp-3.1.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (491.2 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARMv7l

parse_lp-3.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (484.3 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

parse_lp-3.1.0-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl (535.0 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.5+ i686

parse_lp-3.1.0-cp39-abi3-macosx_11_0_arm64.whl (448.5 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

parse_lp-3.1.0-cp39-abi3-macosx_10_12_x86_64.whl (456.4 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for parse_lp-3.1.0.tar.gz
Algorithm Hash digest
SHA256 724efdb1a50969fcff505231fd3d59bb9018441f0904421473a96d4337c586ed
MD5 277256c0a8ded8b66765d86209949915
BLAKE2b-256 006ed1e28c4fd8bb88dd0f6ffdac7783127996fe32f5d7e33fd58dfb52aecb37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.1.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fbacb9e4a55eee83ad39c6247cd899458f8e3d99c2f40009fbf60c89acb91448
MD5 c4d80afa001bf291745ccf91f66bf259
BLAKE2b-256 c1c923eaebc890e93932d4de1ac9f20da03e7c9918c0b986552df2cdc9fa7340

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 50fc98fc3379b205468f08718a90c8c0baa2bd3d165fe51f1c158b357bafc5ca
MD5 702795b5e6b0dc8c23f6691b8adab104
BLAKE2b-256 4e52b6069ed0ba58fef555e0d5414b7f4be41767eaa36e224415e2b1a506eb15

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 90167c734aef7e9d0cf3ad3a9c6ed1d78b4b3b77e12347c51afac50b633c81cf
MD5 263b0b8ff52a3c3ff41da868d9e03945
BLAKE2b-256 1d53d9376ff899616d38c4de86b4f510d6ff164aa71a98fa4648f13e8789a8fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5260348e9352b147a391f39ebd4411f190cc147613a2a642c3a9b8a710cdaf6c
MD5 759c3ee609b6ddc6cb1ff0dc94f7a595
BLAKE2b-256 939304dc91997f2bdda9e05ce0cbc2a4cb80a79e980cce1a9dcb907efa33eaee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 0aa84133a0f5bdef0352878e4728cb18f2c25688588eeb9e9035361868b4ead2
MD5 7eb6fb859ab1be09e8e0081f5acb3ced
BLAKE2b-256 59c31d5c20d6835813bb41f9a5f1bb8f55596dcf2ee0316ac1aa1afa87f91655

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 d10f68989f567d0f0457df80e0ffc47fd694fe727fd038a1e98a84f82a7b1370
MD5 bd97c50aab309207d53bbe48ca35e176
BLAKE2b-256 731b9dddf70f8b33a4a0987815005e11b0aee779332e881cc132723491a40679

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 fc6ff910d51dc32582fdf803542ed99ed9c84c7cd9d386e23e62d232a76c86fe
MD5 a156a809dd7f09c884f21f55fd86fa83
BLAKE2b-256 6ecba25f09384dfa2959c84289b3b1f2b5d9be675f368d98f08c18d33298228e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5211aaf3b9485dc23205e07de9d62cc2dd0151530ce154151751d136011f6323
MD5 8ca45b215b5e68af0a5f688ed95bdf39
BLAKE2b-256 f3ad7ddd810b8205b0d5839b9d6117730c16fc5ee61f9a00d4cf8c8edd6f7a02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 2e416dec7ece3b494db83eacda76e2e9fd931bba623bbdb6efb78f5c5c42822a
MD5 4414957764de6ca02e84f041e9c94552
BLAKE2b-256 ab0774cf2d8d3b46a96fc09206d3b53ec662f8c58a3246ad1df31796e79d5d52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8f42e63e30bf4b67a3b1bd5d15c05c610d9ae029c0fdb6582c593dd00bcb1f94
MD5 11ca385302458369d8000922b76f8e45
BLAKE2b-256 6f76b4a14a5d960c456177b282cc00a16d02839003fa100433cee13913e18f6a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b8a90929b8e8edf91da6308f6b5f42a4f1cecaa7cb6bd80335003762ba6f0936
MD5 7d294898a5c3eb54c42e35c2cdc62b8e
BLAKE2b-256 8c43653843b700dc0cea6a4d0e97d0c162ebf302b2c3acb5cd8d6feeec03ae65

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