A unified framework for Operation Research modeling with polars
Project description
xplor: A Modern DataFrame-Centric Optimization Framework
xplor provides a unified framework for building Operation Research models using polars DataFrames. By leveraging polars' performance and ergonomic API, xplor makes mathematical optimization more intuitive and maintainable.
Features
- ๐ Polars Integration: Built on top of polars for high-performance data operations
- ๐งฉ Solver Agnostic: Designed to support multiple solvers (currently Gurobi, more coming soon)
- ๐ Intuitive API: Natural expression syntax for constraints and objectives
- โก Vectorized Operations: Efficient model building with DataFrame operations
- ๐ Type Hints: Full typing support for better IDE integration
Installation
pip install xplor
For Gurobi support, make sure you have Gurobi installed and licensed:
pip install gurobipy
Quick Start
Here's a simple example showing how to build and solve an optimization model using xplor:
>>> import xplor.gurobi as xpg
>>> import polars as pl
>>> import gurobipy as gp
# Wrap your model with XplorGurobi
>>> model = gp.Model()
>>> xmodel = xpg.XplorGurobi(model, deterministic=True, auto_update=True)
# Create sample data
>>> df = pl.DataFrame({
... "i": [0, 0, 1, 2, 2],
... "j": [1, 2, 0, 0, 1],
... "u": [0.3, 1.2, 0.7, 0.9, 1.2],
... "c": [1.3, 1.7, 1.4, 1.1, 0.9],
... "obj": [2.5, 2.7, 1.2, 1.7, 3.9],
... })
# Add variables
>>> df = (
... df
... .pipe(xmodel.add_vars, name="x", ub="u", obj="obj", indices=["i", "j"])
... .pipe(xpg.apply_eval, "y = 2 * x - c")
... )
>>> df
shape: (5, 7)
โโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโ
โ i โ j โ u โ c โ obj โ x โ y โ
โ --- โ --- โ --- โ --- โ --- โ --- โ --- โ
โ i64 โ i64 โ f64 โ f64 โ f64 โ object โ object โ
โโโโโโโชโโโโโโชโโโโโโชโโโโโโชโโโโโโชโโโโโโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโโโโก
โ 0 โ 1 โ 0.3 โ 1.3 โ 2.5 โ <gurobi.Var x[0,1]> โ -1.3 + 2.0 x[0,1] โ
โ 0 โ 2 โ 1.2 โ 1.7 โ 2.7 โ <gurobi.Var x[0,2]> โ -1.7 + 2.0 x[0,2] โ
โ 1 โ 0 โ 0.7 โ 1.4 โ 1.2 โ <gurobi.Var x[1,0]> โ -1.4 + 2.0 x[1,0] โ
โ 2 โ 0 โ 0.9 โ 1.1 โ 1.7 โ <gurobi.Var x[2,0]> โ -1.1 + 2.0 x[2,0] โ
โ 2 โ 1 โ 1.2 โ 0.9 โ 3.9 โ <gurobi.Var x[2,1]> โ -0.9 + 2.0 x[2,1] โ
โโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโ
# Add constraints using grouped operations
>>> (
... df
... .group_by("i")
... .agg(xpg.quicksum("y"), pl.col("c").min())
... .pipe(xmodel.add_constrs, "y <= c", name="sum_on_j", indices=["i"])
... )
shape: (3, 4)
โโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ i โ y โ c โ sum_on_j โ
โ --- โ --- โ --- โ --- โ
โ i64 โ object โ f64 โ object โ
โโโโโโโชโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโชโโโโโโชโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโก
โ 1 โ -1.4 + 2.0 x[1,0] โ 1.4 โ <gurobi.Constr sum_on_j[1]> โ
โ 0 โ -3.0 + 2.0 x[0,1] + 2.0 x[0,2] โ 1.3 โ <gurobi.Constr sum_on_j[0]> โ
โ 2 โ -2.0 + 2.0 x[2,0] + 2.0 x[2,1] โ 0.9 โ <gurobi.Constr sum_on_j[2]> โ
โโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# Solve the model
>>> model.optimize()
# Extract solution
>>> df.with_columns(xpg.read_value("x"))
shape: (5, 7)
โโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโโโโโโโโโโโโโโโ
โ i โ j โ u โ c โ obj โ x โ y โ
โ --- โ --- โ --- โ --- โ --- โ --- โ --- โ
โ i64 โ i64 โ f64 โ f64 โ f64 โ f64 โ object โ
โโโโโโโชโโโโโโชโโโโโโชโโโโโโชโโโโโโชโโโโโโชโโโโโโโโโโโโโโโโโโโโก
โ 0 โ 1 โ 0.3 โ 1.3 โ 2.5 โ 0.0 โ -1.3 + 2.0 x[0,1] โ
โ 0 โ 2 โ 1.2 โ 1.7 โ 2.7 โ 0.0 โ -1.7 + 2.0 x[0,2] โ
โ 1 โ 0 โ 0.7 โ 1.4 โ 1.2 โ 0.0 โ -1.4 + 2.0 x[1,0] โ
โ 2 โ 0 โ 0.9 โ 1.1 โ 1.7 โ 0.0 โ -1.1 + 2.0 x[2,0] โ
โ 2 โ 1 โ 1.2 โ 0.9 โ 3.9 โ 0.0 โ -0.9 + 2.0 x[2,1] โ
โโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโโโโโโโโโโโโโโโ
Current Status
xplor is in active development. Currently supported:
- โ Gurobi backend
- โ Basic model building operations
- โ Variable and constraint creation
- โ Expression evaluation
- โ Solution reading
Planned features:
- ๐ง Support for additional solvers (CPLEX, CBC, SCIP)
- ๐ง Extended modeling capabilities
- ๐ง Performance optimizations
- ๐ง More utility functions
Why xplor?
xplor aims to modernize the Operation Research workflow by:
- Using polars instead of pandas for better performance and memory usage
- Providing a consistent API across different solvers
- Making model building more intuitive with DataFrame operations
- Enabling better code organization and maintenance
Comparison with Other Tools
xplor is heavily inspired by gurobipy-pandas but differs in these key aspects:
- Uses polars instead of pandas for better performance
- Designed to be solver-agnostic from the ground up
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
# Clone the repository
git clone https://github.com/gab23r/xplor.git
cd xplor
# Install development dependencies
uv sync --all-extras
# Run tests
pytest
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- gurobipy-pandas for inspiration
- polars for the amazing DataFrame library
- Gurobi for the optimization solver
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xplor-0.2.3.tar.gz.
File metadata
- Download URL: xplor-0.2.3.tar.gz
- Upload date:
- Size: 23.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2a1157345e65c31ec0cb217256484a3276213a055ee2c3220ed16ebcccb0d46
|
|
| MD5 |
dd3ea3e41ed7219c5b3e48cd61148d9a
|
|
| BLAKE2b-256 |
66b50e9f5bff36ca401e1e0aff5430d738c554f9a782b2cd248452d06a543b6d
|
File details
Details for the file xplor-0.2.3-py3-none-any.whl.
File metadata
- Download URL: xplor-0.2.3-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1773d60469acbd16afd5b4cb42f7a3cc6bf2bc53d7a0e31d1990c81d1b8957c2
|
|
| MD5 |
a5fb2cda75689cf4229b414c0f8a8794
|
|
| BLAKE2b-256 |
42d7b7da6594750b8fc453a224c79e9de72a6d02f98ee36bc98229e4516ca872
|