Search code examples
python-3.xoptimizationpyomo

Pyomo - is it possible to define conditional domains on a variable set?


I want to define an array of variables x, where some will be integer variables and some real (continuous) variables. For instance, I have three sets:

model    = pyo.AbstractModel()
model.N  = pyo.Set()
model.NL = pyo.Set()
model.NN = pyo.Set()

NL and NN are mutually exclusive sets whose union is N.

I would like to define the following variables:

model.x = pyo.Var(model.N, within = pyo.Integers) # if x in NL
model.x = pyo.Var(model.N, within = pyo.Reals)    # if x in NN

I can of course rename xL and xN, but is it possible to have a single variable set x with subset dependent domains?

Thank you very much.


Solution

  • Yes. There are several ways to accomplish this:

    1. The domain (or within) argument can take a rule:

      def x_domain(m, i):
          if i in m.NL:
              return pyo.Integers
         else:
              return pyo.Reals
      model.x = pyo.Var(model.N, within=x_domain)
      
    2. You can set the Var to one domain and then update the domain after the fact:

      model.x = pyo.Var(model.N, within=Reals)
      for i in model.NL:
          model.x[i].domain = Integers