Skip to content
Advertisement

Creating Pyomo’s ConstraintList with a list of constraints rather than adding them individually?

I’m currently storing my Pyomo variables in a Pandas dataframe, and have been using that to generate arrays of constraints.

Is there a way I can add them to my model (e.g. by initialising a ConstraintList with it) rather than having to loop through them all and add them individually? I don’t think I’m able to use a rule to create the constraints because I’m indexing based on my Pandas dataframe.

This is how I’m storing my variables – I’m using Pandas because I find it really easy to index by values in my dataframe:

JavaScript

And I want to do something like this which does not work:

JavaScript

It works if I add them individually like this:

JavaScript

Thanks so much.

Advertisement

Answer

I think you are trying too hard to push pyomo into a pandas box. :). But that is just an opinion. I don’t see much advantage to doing this because you are not “vectorizing” any operations here as pyomo constraint construction isn’t vectorized.

I would suggest (others may differ) just doing the optimization in pyomo without putting any components into the df, but pulling constants out as needed for constraints. Jury is out on whether I’d put the index set you are using into a pyomo Set, which I think makes things easier to T/S, and pyomo is going to make a virtual set(s) internally anyhow that you can see in the first example below, but that is a side story.

Here are 2 cuts at your construct that I think are workable and disentangle a bit from pandas. This assumes that the datetime you have in your df is unique and can be made into the index for simplicity. (Actually it has to be unique as you are already using it as an indexing set…answered my own question)

JavaScript

Generates:

JavaScript

model2 separated for clarity…

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement