Skip to main content

Add your description here

Project description

polORs - Operation Research with polars

This package is heavily inspired by gurobipy-pandas but uses polars as dataframe backend.

Basic example

import polors.gurobi as pg
import gurobipy as gp

model = gp.Model()

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],
    }
)

df = (
    df
    .pipe(pg.add_vars, model, name="x", ub="u", obj = "obj", indices = ["i", "j"], vtype = gp.GRB.CONTINUOUS)
)
# shape: (5, 6)
# ┌─────┬─────┬─────┬─────┬─────┬─────────────────────┐
# │ i   ┆ j   ┆ u   ┆ c   ┆ obj ┆ x                   │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ ---                 │
# │ i64 ┆ i64 ┆ f64 ┆ f64 ┆ f64 ┆ object              │
# ╞═════╪═════╪═════╪═════╪═════╪═════════════════════╡
# │ 0   ┆ 1   ┆ 0.3 ┆ 1.3 ┆ 2.5 ┆ <gurobi.Var x[0,1]> │
# │ 0   ┆ 2   ┆ 1.2 ┆ 1.7 ┆ 2.7 ┆ <gurobi.Var x[0,2]> │
# │ 1   ┆ 0   ┆ 0.7 ┆ 1.4 ┆ 1.2 ┆ <gurobi.Var x[1,0]> │
# │ 2   ┆ 0   ┆ 0.9 ┆ 1.1 ┆ 1.7 ┆ <gurobi.Var x[2,0]> │
# │ 2   ┆ 1   ┆ 1.2 ┆ 0.9 ┆ 3.9 ┆ <gurobi.Var x[2,1]> │
# └─────┴─────┴─────┴─────┴─────┴─────────────────────┘

(
    df
    .pipe(pg.apply_eval, "2 * x - c", name = "y")
    .group_by("i").agg(pg.quicksum("y"), pl.col("c").min())
    .pipe(pg.add_constrs, model, "y <= c", name="constr")
)
# shape: (3, 4)
# ┌─────┬────────────────────────────────┬─────┬────────────────────────┐
# │ i   ┆ y                              ┆ c   ┆ constr                 │
# │ --- ┆ ---                            ┆ --- ┆ ---                    │
# │ i64 ┆ object                         ┆ f64 ┆ object                 │
# ╞═════╪════════════════════════════════╪═════╪════════════════════════╡
# │ 1   ┆ -1.4 + 2.0 x[1,0]              ┆ 1.4 ┆ <gurobi.Constr constr> │
# │ 0   ┆ -3.0 + 2.0 x[0,1] + 2.0 x[0,2] ┆ 1.3 ┆ <gurobi.Constr constr> │
# │ 2   ┆ -2.0 + 2.0 x[2,0] + 2.0 x[2,1] ┆ 0.9 ┆ <gurobi.Constr constr> │
# └─────┴────────────────────────────────┴─────┴────────────────────────┘

model.optimize()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

polors-0.2.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file polors-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: polors-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.19

File hashes

Hashes for polors-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d9c1d5324620efe45999f4680a532459d6eee4679dc1120af915cc567124e956
MD5 97f7e1ae8aeeea26beb0478f7d40da16
BLAKE2b-256 3c98511f5624cbc0de91851ac012456af6e6b3da51be16a464a6f6482006fe6e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page