Reputation: 21
I want to solve an integer programming problem in which the decision variables are restricted in a specific set.
For example, decision variables xi must be chosen in [2,5,7,10].
For testing, I wrote the python code using Pyomo as follows:
from pyomo.environ import *
model = AbstractModel()
model.X = Set(initialize=[2, 5, 7, 10])
model.x = Var(within=model.X)
model.obj = Objective(expr=model.x+1)
m = model.create_instance()
opt = SolverFactory("glpk")
results = opt.solve(m)
When these codes are executed, I got an error message: "TypeError: Invalid domain type for variable with name 'x'. Variable is not continuous, integer, or binary"
Here are my questions:
Thanks for helping!
Upvotes: 1
Views: 2321
Reputation: 2599
What is the reason of this error?
As the error indicates, mixed-integer linear solvers like GLPK can only handle continuous, binary, and general integer variables. When you specify
model.X = Set(initialize=[2, 5, 7, 10])
model.x = Var(within=model.X)
You are trying to create a discrete categorical variable, which while Pyomo can represent internally, it has no way to pass to that particular solver.
How to deal with it?
There is a standard reformulation using a set of binary variables:
model.X = Set(initialize=[2, 5, 7, 10])
model.select_x = Var(model.X, domain=Binary)
model.x = Var()
def pick_one(m):
return 1 == sum(m.selext_x[i] for i in m.X)
model.pick_one = Constraint(rule=pick_one)
def set_x(m):
return m.x == sum(i*m.select_x[i] for i in m.X)
model.set_x = Constraint(rule=set_x)
Is there any other optimization tools that can handle this type of problems?
While there are some solvers that can handle discrete categorical variables, you are probably better off using the above reformulation.
Upvotes: 3