Package: linopy
linopy: Linear optimization with N-dimensional labeled variableslinopyis an open-source python package that facilitatesoptimizationwithreal world data. It builds a bridge between data analysis packages likexarray&pandasand problem solvers likecbc,gurobi(see the full list below).LinopysupportsLinear, Integer, Mixed-Integer and Quadratic Programmingwhile aiming to make linear programming in Python easy, highly-flexible and performant.Main featureslinopyis heavily based onxarraywhich allows for many flexible data-handling features:Define (arrays of) continuous or binary variables withcoordinates, e.g. time, consumers, etc.Applyarithmetic operationson the variables like adding, substracting, multiplying with all thebroadcastingpotentials of xarrayApplyarithmetic operationson thelinear expressions(combination of variables)Group termsof a linear expression by coordinatesGet insight into theclear and transparent data modelModifyanddeleteassigned variables and constraints on the flyUselazy operationsfor large linear programs  withdaskChoose fromdifferent commercial and non-commercial solversFastimport and exporta linear model using xarray's netcdf IOInstallationSo farlinopyis available on the PyPI repositorypipinstalllinopyor on conda-forgecondainstall-cconda-forgelinopyIn a NutshellLinopy 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.>>>importpandasaspd>>>importlinopy>>>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])>>>applesVariable (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] ≥ 8Add 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] ≥ 50Define 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*bananasFinally, we can solve the problem and get the optimal solution:>>>m.solve()>>>m.objective.value17.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          4Supported solverslinopysupports the following solversCbcGLPKHiGHSGurobiXpressCplexMOSEKCOPTNote that these do have to be installed by the user separately.Citing LinopyIf 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.04823A 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}}LicenseCopyright 2021 Fabian HofmannThis package is published under MIT license. SeeLICENSE.txtfor details.