I am working on a pyomo model, and need to create a few different sets of variables that are dependent on a solution from a master model. I need the variables to be indexed variables however the conventional method of using "Var(indexSet1, indexSet2, indexSet3, etc.)" is not applicable because "indexSet2, indexSet3, etc." are dependent upon the current index of "indexSet1". So as an example if I have an index set called crews = [0,1,2], then I will have a two dimensional array of jobs where the first index of the array is the crew and the second index of the set is job one of the jobs assigned to that crew. So jobs would look like [[0,3,7], [1,2,6],[4,5]]. Then I would like to create a variable "x_ij" using the first index set and the second two dimensional array. I tried x_ij= Var(crews, jobs[crews]) but this obviously didn't work. I also tried the add_component method which does add the correct variables to the model, but I tried to store them in a python dict with the keys being their index tuple (i,j), however, add_component does not have a return value so my dict has the form dict = {(i,j): none}. Any help on how to do this would be greatly appreciated.


1 Answer 1


I am preparing a list of tuple of index based on the conditions, after then giving to index parameter to "Var". For your problem, if I understand it correctly, this will be enough:

index_list = [(0,1),(0,3), (0,7), (1,1), (1,2), (1,6), (2,4), (2,5)]
x_ij = Var(index_list) 

You can prepare this list any way, such as:

index_list = [] 
for crew in crews:
   for job in jobs[crew]:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.