2

I'm fairly new to Pyomo and trying to figure out what the use cases are for an indexed set (a set of sets). As far as I can tell, it can be substituted for parameter and behave the same way. Any insight or examples of how indexed sets should be used in Pyomo would be much appreciated.

I came across this problem while I was trying to create a set that "labels" my time set, t. I have model.t as a set of timestamps, and want to say, for example, that the first 3 timestamps are part of group A, the next 3 are part of group B, etc. I'm doing all this in an Abstract Model and feel a Param does the same as an Indexed Set

import datetime
import pyomo.environ as pe

class CreateModel:
    def __init__(self):
        self.model = pe.AbstractModel()
        self.model.t = pe.Set()
        self.model.labels = pe.Set()
        self.model.labeled_t = pe.Set(self.model.labels)

        # OR
        # self.model.labeled_t = pe.Param(self.model.labels)


def main():
    # Some dummy input data
    inputs = {
        None:{
            't':{
                None:[
                    datetime.date(2020, 1, 1), datetime.date(2020, 1, 2),
                    datetime.date(2020, 1, 3), datetime.date(2020, 1, 4)
                ]
            },
            'labels':{None:{'A', 'B'}},
            'labeled_t':{
                'A':[datetime.date(2020, 1, 1), datetime.date(2020, 1, 2)],
                'B':[datetime.date(2020, 1, 3), datetime.date(2020, 1, 4)]
            }
        }
    }
    m = CreateModel()
    m.model.create_instance(inputs)

if __name__ == '__main__':
    main()

In this example the behavior is the same, so why would I ever want an indexed set?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.