Scalar linear expressions and constraints in linopy

336 views
Skip to first unread message

Jack Simpson

unread,
Sep 30, 2022, 2:41:40 AM9/30/22
to pypsa
Hi, do you know if there were plans to support ScalarLinearExpression when creating the objective function and AnonymousScalarConstraint when adding constraints (and if it would be possible to pass an array of AnonymousScalarConstraint to add many constraints at the same time)? 

I've been experimenting with formulating a model with the variables stored in a pandas dataframe (so for example I can do things like do a groupby sum to create a column of type ScalarLinearExpression combining multiple values of type ScalarVariable). I managed to find a workaround by using tuples in my dataframe to build a LinearExpression to create the objective function, but haven't been able to find a workaround to add constraints from the AnonymousScalarConstraint values I've been generating.

Thanks so much, Jack

Jack Simpson

unread,
Sep 30, 2022, 2:57:16 AM9/30/22
to pypsa
I'm using pandas mainly because I was really struggling to deal with xarray's support for groupby with multiple coordinates at the same time, whereas with pandas its so much more straight forward to use. I find it much easier to map the linopy variables into a pandas column and use them from there (I created a workaround to do the mapping as the Variable object is not technically callable).

Sorry to bother you with all this - I'm really interested and impressed with linopy and am keen to try to use it on projects, so I hope you can forgive me messaging with a bunch of questions and requests like this. Let me know if there's ways I can contribute to the project as well if it helps at all.

Fabian Hofmann

unread,
Sep 30, 2022, 3:29:03 AM9/30/22
to py...@googlegroups.com

Hey Jack,


no worries, happy to help. Before trying to get down the pandas road, have tried out the LinearExpression.groupby_sum function? Ideally you only have to make sure that passed groups have a the correct dimension name. For example


from linopy import Model
import pandas as pd

m = Model()

index = pd.Index(range(10), name='my-dim')
groups = pd.Series([1,1,1,1,1,2,2,2,2,2], index=index)

x = m.add_variables(0, 10, coords=[index])
expr = 2 * x

expr = expr.groupby_sum(groups.to_xarray())


`index` and `groups` are pandas objects, and only have to be converted to xarray when needed. The only thing important really is that the dimensions have explicit names. Does that make things easier with xarray?


As for the integer variables, it is on the todo-list :)


Best

Fabian

--
You received this message because you are subscribed to the Google Groups "pypsa" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pypsa+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/pypsa/805f72fa-2832-4e77-9ece-d589c89a86c6n%40googlegroups.com.
-- 
Fabian Hofmann 

Postdoctoral Researcher
Institute of Energy Technology
Technische Universität Berlin
http://fabianhofmann.org/

Group website: https://tub-ensys.github.io/

Jack Simpson

unread,
Sep 30, 2022, 7:02:42 AM9/30/22
to pypsa
Thanks so much Fabian, I really appreciate the example - I tested it out and this works really, really well! I can definitely work like this without fighting the library to get everything into a dataframe. Thanks so much!

Jack Simpson

unread,
Sep 30, 2022, 7:09:16 AM9/30/22
to pypsa
Thanks so much too for letting me know about integers being on the road map - do you know if there will also be the ability to solve a maximise model too (in additon to minimise, or have I missed that functionality)? Thanks again!
Reply all
Reply to author
Forward
0 new messages