Search code examples
pyomo

pyomo: an indexed variable should be linear or integer depending on the variable index


I have a indexed variable New_UnitsBuilt[p] and this variabele should be integer for the index "GasPowerplant" but linear for the index "batterystorage".

new_units_built_set = pyo.Set(initialize=list(params.Installable_units))

    model.New_UnitsBuilt = pyo.Var(new_units_built_set, domain=(pyo.NonNegativeIntegers if p="GasPowerplant" else NonNegativeReals)

Please help me how to do this in pyomo. I am new in pyomo

Best Greetings Gerhard


Solution

  • There are a couple ways you can accomplish this. For the following, I am assuming that your params.Installable_units = ["GasPowerplant", "batterystorage"]:

    If the number of elements in new_units_built_set is small, then you can use a dictionary:

    model.new_units_built_set = pyo.Set(initialize=list(params.Installable_units))
    model.New_UnitsBuilt = pyo.Var(model.new_units_built_set, 
        domain={"GasPowerplant": pyo.NonNegativeIntegers, "batterystorage": pyo.NonNegativeReals})
    

    Or if there are a lot – or there is a simple formula to get the return value – you can use a function (rule):

    model.new_units_built_set = pyo.Set(initialize=list(params.Installable_units))
    def _new_unitsbuilt_domain(m, p):
        return pyo.NonNegativeIntegers if p=="GasPowerplant" else pyo.NonNegativeReals
    model.New_UnitsBuilt = pyo.Var(model.new_units_built_set, domain=_new_unitsbuilt_domain)
    

    Or you can just set everything to one value and override later (assuming you are using a ConcreteModel):

    model.new_units_built_set = pyo.Set(initialize=list(params.Installable_units))
    model.New_UnitsBuilt = pyo.Var(model.new_units_built_set, domain=pyo.NonNegativeReals)
    model.New_UnitsBuilt["GasPowerplant"].domain = pyo.NonNegativeIntegers
    

    All of these will produce:

    >>> model.pprint()
    1 Set Declarations
        new_units_built_set : Size=1, Index=None, Ordered=Insertion
            Key  : Dimen : Domain : Size : Members
            None :     1 :    Any :    2 : {'GasPowerplant', 'batterystorage'}
    
    1 Var Declarations
        New_UnitsBuilt : Size=2, Index=new_units_built_set
            Key            : Lower : Value : Upper : Fixed : Stale : Domain
             GasPowerplant :     0 :  None :  None : False :  True : NonNegativeIntegers
            batterystorage :     0 :  None :  None : False :  True :    NonNegativeReals
    
    2 Declarations: new_units_built_set New_UnitsBuilt