Linopy
Linopy: Linear optimization with n-dimensional labeled variables - Published in JOSS (2023)
Science Score: 95.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 8 DOI reference(s) in README and JOSS metadata -
○Academic publication links
-
✓Committers with academic emails
7 of 49 committers (14.3%) from academic institutions -
○Institutional organization owner
-
✓JOSS paper metadata
Published in Journal of Open Source Software
Keywords
Keywords from Contributors
Scientific Fields
Repository
Linear optimization with N-D labeled arrays in Python
Basic Info
- Host: GitHub
- Owner: PyPSA
- License: mit
- Language: Python
- Default Branch: master
- Homepage: https://linopy.readthedocs.io
- Size: 3.38 MB
Statistics
- Stars: 208
- Watchers: 5
- Forks: 64
- Open Issues: 61
- Releases: 46
Topics
Metadata Files
README.md
linopy: Optimization with array-like variables and constraints
Linear\ Integer\ Non-linear\ Optimization in\ PYthon
linopy is an open-source python package that facilitates optimization with real world data. It builds a bridge between data analysis packages like xarray & pandas and problem solvers like cbc, gurobi (see the full list below). Linopy supports Linear, Integer, Mixed-Integer and Quadratic Programming while aiming to make linear programming in Python easy, highly-flexible and performant.
Benchmarks
linopy is designed to be fast and efficient. The following benchmark compares the performance of linopy with the alternative popular optimization packages.

Main features
linopy is heavily based on xarray which allows for many flexible data-handling features:
- Define (arrays of) continuous or binary variables with coordinates, e.g. time, consumers, etc.
- Apply arithmetic operations on the variables like adding, substracting, multiplying with all the broadcasting potentials of xarray
- Apply arithmetic operations on the linear expressions (combination of variables)
- Group terms of a linear expression by coordinates
- Get insight into the clear and transparent data model
- Modify and delete assigned variables and constraints on the fly
- Use lazy operations for large linear programs with dask
- Choose from different commercial and non-commercial solvers
- Fast import and export a linear model using xarray's netcdf IO
Installation
So far linopy is available on the PyPI repository
bash
pip install linopy
or on conda-forge
bash
conda install -c conda-forge linopy
In a Nutshell
Linopy aims to make optimization programs transparent and flexible. To illustrate its usage, let's consider a scenario where we aim to minimize the cost of buying apples and bananas over a week, subject to daily and weekly vitamin intake constraints.
```python
import pandas as pd import linopy
m = linopy.Model()
days = pd.Index(["Mon", "Tue", "Wed", "Thu", "Fri"], name="day") apples = m.addvariables(lower=0, name="apples", coords=[days]) bananas = m.addvariables(lower=0, name="bananas", coords=[days]) apples
Variable (day: 5)
[Mon]: apples[Mon] ∈ [0, inf] [Tue]: apples[Tue] ∈ [0, inf] [Wed]: apples[Wed] ∈ [0, inf] [Thu]: apples[Thu] ∈ [0, inf] [Fri]: apples[Fri] ∈ [0, inf] ```
Add daily vitamin constraints
```python
m.addconstraints(3 * apples + 2 * bananas >= 8, name="dailyvitamins")
Constraint
daily_vitamins(day: 5):[Mon]: +3 apples[Mon] + 2 bananas[Mon] ≥ 8 [Tue]: +3 apples[Tue] + 2 bananas[Tue] ≥ 8 [Wed]: +3 apples[Wed] + 2 bananas[Wed] ≥ 8 [Thu]: +3 apples[Thu] + 2 bananas[Thu] ≥ 8 [Fri]: +3 apples[Fri] + 2 bananas[Fri] ≥ 8 ```
Add weekly vitamin constraint
```python
m.addconstraints((3 * apples + 2 * bananas).sum() >= 50, name="weeklyvitamins")
Constraint
weekly_vitamins+3 apples[Mon] + 2 bananas[Mon] + 3 apples[Tue] ... +2 bananas[Thu] + 3 apples[Fri] + 2 bananas[Fri] ≥ 50 ```
Define the prices of apples and bananas and the objective function
```python
appleprice = [1, 1.5, 1, 2, 1] bananaprice = [1, 1, 0.5, 1, 0.5] m.objective = appleprice * apples + bananaprice * bananas ```
Finally, we can solve the problem and get the optimal solution:
```python
m.solve() m.objective.value
17.166 ```
... and display the solution as a pandas DataFrame ```python
m.solution.to_pandas()
apples bananas day Mon 2.667 0 Tue 0 4 Wed 0 9 Thu 0 4 Fri 0 4 ```Supported solvers
linopy supports the following solvers
Note that these do have to be installed by the user separately.
Development Setup
To set up a local development environment for linopy and to run the same tests that are run in the CI, you can run:
sh
python -m venv venv
source venv/bin/activate
pip install uv
uv pip install -e .[dev,solvers]
pytest
The -e flag of the install command installs the linopy package in editable mode, which means that the virtualenv (and thus the tests) will run the code from your local checkout.
Citing Linopy
If you use Linopy in your research, please cite the following paper:
- Hofmann, F., (2023). Linopy: Linear optimization with n-dimensional labeled variables. Journal of Open Source Software, 8(84), 4823, https://doi.org/10.21105/joss.04823
A BibTeX entry for LaTeX users is
latex
@article{Hofmann2023,
doi = {10.21105/joss.04823},
url = {https://doi.org/10.21105/joss.04823},
year = {2023}, publisher = {The Open Journal},
volume = {8},
number = {84},
pages = {4823},
author = {Fabian Hofmann},
title = {Linopy: Linear optimization with n-dimensional labeled variables},
journal = {Journal of Open Source Software}
}
License
Copyright 2021 Fabian Hofmann
This package is published under MIT license. See LICENSE.txt for details.
Owner
- Name: PyPSA
- Login: PyPSA
- Kind: organization
- Website: www.pypsa.org
- Repositories: 29
- Profile: https://github.com/PyPSA
Python for Power System Analysis
JOSS Publication
Linopy: Linear optimization with n-dimensional labeled variables
Authors
Tags
python linear optimizationGitHub Events
Total
- Create event: 55
- Release event: 12
- Issues event: 55
- Watch event: 38
- Delete event: 30
- Member event: 1
- Issue comment event: 143
- Push event: 158
- Pull request review comment event: 33
- Pull request event: 135
- Pull request review event: 68
- Fork event: 13
Last Year
- Create event: 61
- Release event: 12
- Issues event: 56
- Watch event: 38
- Delete event: 34
- Member event: 1
- Issue comment event: 146
- Push event: 170
- Pull request review comment event: 33
- Pull request event: 143
- Pull request review event: 70
- Fork event: 13
Committers
Last synced: 5 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Fabian | f****f@g****e | 639 |
| pre-commit-ci[bot] | 6****] | 79 |
| Lukas Trippe | l****p@p****e | 32 |
| Jonas Hoersch | j****h@c****g | 26 |
| Heinz-Alexander Fuetterer | f****h@p****e | 25 |
| ulfw | u****e@m****m | 19 |
| Fabian Neumann | f****n@o****e | 17 |
| Siddharth Krishna | s****a | 8 |
| Henning Blunck | m****l@h****e | 7 |
| fabulous | f****s@G****x | 7 |
| Irieo | i****n@g****m | 6 |
| Dan Allford | d****d@b****m | 5 |
| Florian Maurer | m****r@f****e | 5 |
| Daniel Müller | a****r@m****g | 4 |
| Felix Hennings | f****x@d****l | 4 |
| Fridolin Glatter | g****r@i****t | 4 |
| Michał Marszal | m****l@i****l | 4 |
| pz-max | m****n@e****k | 4 |
| Oscar Dowson | o****w | 4 |
| ollie-bell | 5****l | 3 |
| Maciej Krakowiak | m****k@i****l | 2 |
| FBumann | 1****n | 2 |
| Daniel Rüdt | 1****t | 2 |
| Daniel Castro Uriegas | d****s@h****m | 2 |
| dependabot[bot] | 4****] | 2 |
| lumbric | l****c@g****m | 2 |
| qheuristics | g****r@g****m | 2 |
| Victor | v****8@g****m | 1 |
| Zlatan Kadragić | a****e@g****m | 1 |
| aszekMosek | m****k@m****m | 1 |
| and 19 more... | ||
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 4 months ago
All Time
- Total issues: 150
- Total pull requests: 450
- Average time to close issues: 3 months
- Average time to close pull requests: 9 days
- Total issue authors: 66
- Total pull request authors: 50
- Average comments per issue: 2.0
- Average comments per pull request: 1.35
- Merged pull requests: 379
- Bot issues: 0
- Bot pull requests: 71
Past Year
- Issues: 44
- Pull requests: 165
- Average time to close issues: about 1 month
- Average time to close pull requests: 7 days
- Issue authors: 31
- Pull request authors: 26
- Average comments per issue: 1.14
- Average comments per pull request: 0.88
- Merged pull requests: 122
- Bot issues: 0
- Bot pull requests: 20
Top Authors
Issue Authors
- FabianHofmann (17)
- FBumann (8)
- fneum (7)
- willu47 (6)
- aurelije (6)
- loongmxbt (6)
- dannyopts (5)
- apfelix (5)
- Cellophil (4)
- KanaBaradei (4)
- tburandt (4)
- leuchtum (4)
- thomgeo (3)
- p-glaum (3)
- BuiMCanmet (2)
Pull Request Authors
- FabianHofmann (177)
- pre-commit-ci[bot] (66)
- lkstrp (32)
- siddharth-krishna (16)
- afuetterer (15)
- coroa (13)
- fneum (9)
- leuchtum (8)
- RobbieKiwi (6)
- ollie-bell (6)
- odow (5)
- dannyopts (5)
- ulfworsoe (5)
- qheuristics (4)
- PeterKlein11 (4)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 2
-
Total downloads:
- pypi 53,820 last-month
- Total docker downloads: 71
-
Total dependent packages: 5
(may contain duplicates) -
Total dependent repositories: 8
(may contain duplicates) - Total versions: 56
- Total maintainers: 2
pypi.org: linopy
Linear optimization with N-D labeled arrays in Python
- Homepage: https://github.com/PyPSA/linopy
- Documentation: https://linopy.readthedocs.io/
- License: MIT License
-
Latest release: 0.5.6
published 4 months ago
Rankings
conda-forge.org: linopy
- Homepage: https://github.com/PyPSA/linopy
- License: MIT
-
Latest release: 0.0.14
published about 3 years ago
Rankings
Dependencies
- bottleneck *
- dask >=0.18.0
- deprecation *
- numexpr *
- numpy *
- scipy *
- toolz *
- tqdm *
- xarray *
- actions/checkout v2 composite
- actions/setup-python v1 composite
- codecov/codecov-action v2 composite
- crazy-max/ghaction-chocolatey v1 composite
- actions/checkout master composite
- actions/setup-python v1 composite
- pypa/gh-action-pypi-publish master composite
- ortools >=9.5
