• Stars
    star
    122
  • Rank 282,630 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created about 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Linear optimization with N-D labeled arrays in Python

linopy: Linear optimization with N-dimensional labeled variables

PyPI CI License doc codecov

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.

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

pip install linopy

or on conda-forge

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.

>>> import pandas as pd
>>> import linopy

>>> m = linopy.Model()

>>> days = pd.Index(['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], name='day')
>>> apples = m.add_variables(lower=0, name='apples', coords=[days])
>>> bananas = m.add_variables(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

>>> m.add_constraints(3 * apples + 2 * bananas >= 8, name='daily_vitamins')
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

>>> m.add_constraints((3 * apples + 2 * bananas).sum() >= 50, name='weekly_vitamins')
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

>>> apple_price = [1, 1.5, 1, 2, 1]
>>> banana_price = [1, 1, 0.5, 1, 0.5]
>>> m.objective = apple_price * apples + banana_price * bananas

Finally, we can solve the problem and get the optimal solution:

>>> m.solve()
>>> m.objective.value
17.166

... and display the solution as a pandas DataFrame

>>> 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.

Citing Linopy

If you use Linopy in your research, please cite the following paper:

A BibTeX entry for LaTeX users is

@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.

More Repositories

1

PyPSA

PyPSA: Python for Power System Analysis
Python
1,036
star
2

pypsa-eur

PyPSA-Eur: A Sector-Coupled Open Optimisation Model of the European Energy System
Python
288
star
3

atlite

Atlite: A Lightweight Python Package for Calculating Renewable Power Potentials and Time Series
Python
238
star
4

pypsa-eur-sec

PyPSA-Eur-Sec: A Sector-Coupled Open Optimisation Model of the European Energy System
Python
86
star
5

technology-data

Compiles assumptions on energy system technologies (e.g. costs and efficiencies) for various years.
TeX
45
star
6

WHOBS

Optimal Wind+Hydrogen+Other+Battery+Solar (WHOBS) electricity systems for European countries
Jupyter Notebook
36
star
7

whobs-server

Online optimisation tool for wind+solar+storage systems
Python
34
star
8

247-cfe

Explore the impacts of 24/7 Carbon-Free Energy PPAs
Python
25
star
9

pypsa-usa

PyPSA-USA: An Open-Source Energy System Optimization Model for the United States
Python
20
star
10

EnergyModels.jl

Julia
17
star
11

PyPSA-animation

Tool to convert PyPSA results to interactive web animations
JavaScript
13
star
12

pypsa-za

PyPSA Model of the South African Energy System
Python
9
star
13

PSA.jl

Partial implementation of PyPSA in Julia with main focus on fast optimal power flow calculation
Julia
9
star
14

pypsa-eur-mga

Snakemake workflow for modelling-to-generate-alternatives with PyPSA-Eur
Python
7
star
15

nomopyomo

Python
7
star
16

pypsa-server

A web interface for running PyPSA scenarios using the snakemake workflow
Python
6
star
17

efuels-server

Online optimisation of efuels delivery
Python
4
star
18

condynet-2-wp-1

Code for topology-based approximations for N-1 contingency constraints
Jupyter Notebook
2
star