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.2.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.2-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.2-cp39-abi3-win_amd64.whl (329.7 kB view details)

Uploaded CPython 3.9+Windows x86-64

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

Uploaded CPython 3.9+Windows x86

parse_lp-3.0.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (482.5 kB view details)

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

parse_lp-3.0.2-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (519.3 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ s390x

parse_lp-3.0.2-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (602.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ppc64le

parse_lp-3.0.2-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (485.6 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARMv7l

parse_lp-3.0.2-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.2-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl (517.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.5+ i686

parse_lp-3.0.2-cp39-abi3-macosx_11_0_arm64.whl (437.4 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

parse_lp-3.0.2-cp39-abi3-macosx_10_12_x86_64.whl (446.4 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: parse_lp-3.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 d1ab9a4648c2d729f0661cc4c3b6ffd8d86208fe29da809977b5d22964937fb2
MD5 c889719d1d50a197e6b307e4e6ff7ed5
BLAKE2b-256 c77ebc15f5eb24029d3e56df9409c25c6fb1b66749180cb7fdd54234244fe09d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.2-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7ab8dfba7422050ea06d66f1932982b5818ae666ba6439cfddcc332fb5715684
MD5 7c33542be3d7ef27ea7e4f122c2187a3
BLAKE2b-256 2123ef181daf6ffa32a8dd01ef6770bd44a05cf882b6ef4070319565d2bfc6e9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: parse_lp-3.0.2-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 329.7 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.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9b6e54e3057e838dadc7357eef963fc4d983108225b33006a2dd806966509120
MD5 b6c935c27f29ac5a8cc5e84c6ee8bd91
BLAKE2b-256 759d40235320db67a50e8e4e14c0136ebab39b35d601ff775e7df70774b4541c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: parse_lp-3.0.2-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.2-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 f8658067186535f4b95088af352174898350ccc77b605daea287935b5ac10140
MD5 2a3c1cbf0f3c84e056ee7ffeadad6168
BLAKE2b-256 7ef480fe939699083d9c3da8b1a680dd50857f1d1f668cea958d1cbebdd0bbe5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 12686fc7cb494ef30d5133585ab5750730037a2e3b5c8b045ab89c81d3a1e491
MD5 fd9307560a8ecbbe434c05df1c555711
BLAKE2b-256 995bb3a58ee347eea53d87b7d79b497c1847cf0339ce99398bd73252ebf1ebf9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.2-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 872cfae6e09cac30644e0d8c180cc0437c8c61a0f7a2f7b5443af52a85ac886a
MD5 a7076830752b0e205fdf01983ae62d4c
BLAKE2b-256 e4a4d07dca881642b2abe793622dd84a254db76056b98c8bcb796e01a368a64b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.2-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 f6b7ed395905226d4da6c40e88fbe69556c506b29db0e4d5b80048d3dc04f140
MD5 9634d927eb8409e8264ae7f72ad18d92
BLAKE2b-256 8c5b8793542014f07e4b6c853c7b587d7094bced702a958ac1300282d094a0d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.2-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 6ff3a5491392c954acffd314ddf6fbec599b15c1c1cc8e7e17061252ad181717
MD5 2d39aec79c05f15fe8acf8778e49703d
BLAKE2b-256 d3faa7a45da3be6a04cbb5064d98730dc219e3d558ae9615d331b6d34c8129f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 60fbaca8b1bfd13c59c736ce26f3918ded6ea0d6ccd2381605d65e19b98462ce
MD5 108eb5fd622aa0213c92bcf14b7e3a76
BLAKE2b-256 caffaf266b9d43bbea2568fd6c420654841579bbaddcfc003c51739848f201a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.2-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 36c9748dc7f1480a34c0bca44ef3db43179c1a9332b5e876290f2ddeb7499e8e
MD5 9ec81b8ac0a9363c42845389df8e2952
BLAKE2b-256 c1e40d8e8c1e5954d2cd1d200ad92bd675edf17c7558637967b444ce828a417f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bd13fbb39a9b17aec31201725dc3d975074e88ae77b7f6f732e980a0ee68a5bc
MD5 bdeab2e41021532c6a65537cf0c39333
BLAKE2b-256 88bf38c434609b622919f4704f76faf01022b8436d7d0ac89f1502fdd7ac038e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parse_lp-3.0.2-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 449640b49cc54a973d8d51b2e617c8c0d10c3b694c45b4cd58a9870f61016472
MD5 1a27c4ba30c4216efb507aaaaca31c3e
BLAKE2b-256 859cedbd4a11435a5da5e14527c3856e59e9dda318b10351edb12aaf0d97c039

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