I am building a routing optimization model using pyomo on python.
I have solved my model but I am trying to extract the decision variable information for my model. My model is binary, and the values I am looking for are values of my model.z
decision variable that equal to 1.
When I write instance.pprint()
I get the following sample of output. I therefore want to code something that gives me only the decision variables that are equal to 1 such as z(1,4).
Sample of my code is shown below:
model.I = RangeSet(5)
model.J = RangeSet(5)
model.z = Var(model.I, model.J, domain = Binary)
def constraint (model,i):
return sum(model.z[i,j] - model.z[j,i] for j in model.J if i != j) == 0
model.constraint = Constraint(model.I, rule=constraint)
print()
z_values = pd.Series(model.z[i,j].extract_values(), name = model.z.name)
print(z_values)
I have tried the above code but as some of my values are 0 (because they have not being visited), I have been getting the following error message.
ValueError: Error retrieving component z[5,4]: The component has not been constructed.
Ideally the output should be something like this:
(0,3) -- 1
(1,2) -- 1
(2,4) -- 1
(3,1) -- 1
(4,5) -- 1
(5,0) -- 1
Any ideas?