Search code examples
optimizationpyomomixed-integer-programming

Restricting one variable in my variable vector to integer in Pyomo


I have a minimization problem that currently has only continuous variables

min Cx
s.t. Ax <= b
lb <= x <= ub

Where C is my cost vector, A is my coefficient matrix and b is my fixed vector. X is my variable vector of continuous variables.

A = 24x29, x = 29x1, b = 24x1

I want to force one of the x variables to be an integer, how can that be done in Pyomo?

I am new to the package, any help is appreciated


Solution

  • Pyomo offers a way to set the domain of your variables. Let me prove it to you in this example that you can entirely run in a Python console with copy/paste.

    Suppose you want to change x[1] to integers, you can use (1 being part of set S = {1,2,3}):

    from pyomo.environ import ConcreteModel, Set, Var, Integers
    # Create your model here (set params, vars, constraint...)
    model = ConcreteModel()
    model.S = Set(initialize={1,2,3})
    model.x = Var(model.S)
    

    Let's pause the example here and type model.x.display() in the Python console. You should see that the domain of all elements in model.x is set by default to Real. Let's continue.

    # Here, we suppose your model is created.
    # You can change the domain of one lement of your var using this line:
    model.x[1].domain = Integers
    

    Now, type model.x.display() in your Python console and you should see this:

    x : Size=3, Index=S
    Key : Lower : Value : Upper : Fixed : Stale : Domain
      1 :  None :  None :  None : False :  True : Integers
      2 :  None :  None :  None : False :  True :    Reals
      3 :  None :  None :  None : False :  True :    Reals
    

    So, only x[1] is integer.