My model sets are defined as,
graph = { 1 : [2],
2 : [3, 4],
3 : [4, 5, 6],
4 : [7, 8, 9],
5 : [],
6 : [],
7 : [],
8 : [],
9 : [10],
10 : [],
11 : [12],
12 : [4, 8]
}
connections = [(s, t) for s in graph.keys() for t in graph[s]]
model.N = pyo.Set(initialize=graph.keys()) ## Set of all nodes
model.P = pyo.Set(within=model.N, initialize= [1, 11]) ## Set of production nodes
model.connection = pyo.Set(model.N, within=model.N, initialize=graph) ## Set of all connections, indexed by node
model.E = pyo.Set(within=model.N * model.N, initialize=connections) ## Set of all edges (arcs)
model.T = pyo.Set(initialize=range(1,8)) ## Set of time period
model.S = pyo.Set(initialize= [1, 2, 3, 4])
model.Prod = pyo.Var(model.P, model.T, model.S, within=NonNegativeReals)
The realization values used in the non anticipativity constraints is defined as,
Omega = {
1: {
1: [1, 2, 3, 4],
2: [1, 2],
3: [1, 2],
4: [1, 2],
5: [1],
6: [1],
7: [1],
},
2: {
1: [1, 2, 3, 4],
2: [1, 2],
3: [1, 2],
4: [1, 2],
5: [2],
6: [2],
7: [2],
},
3: {
1: [1, 2, 3, 4],
2: [3, 4],
3: [3, 4],
4: [3, 4],
5: [3],
6: [3],
7: [3],
},
4: {
1: [1, 2, 3, 4],
2: [3, 4],
3: [3, 4],
4: [3, 4],
5: [4],
6: [4],
7: [4],
}
}
Omega is well defined.
model.AA1 = ConstraintList()
for P in model.Prod:
for T in model.T:
for S in model.S:
for Omega in Omega[S][T]:
model.AA1.add(model.Prod[P, T, S] == model.Prod[P, T, Omega])
model.pprint()
The above constraint is giving an index error as,
KeyError: "Index '(1, 1, 1, 1, 1)' is not valid for indexed component 'Prod'"
This following one works very well.
W = Set(initialize=range(1, 4))
S = Set(initialize=range(1, 5))
TH = Set(initialize=range(1, 3))
model_parcel.z = Var(W, TH, S, within=NonNegativeIntegers)
Omega = {
1: {1: [1, 2, 3, 4], 2: [1, 2], 3: [1]},
2: {1: [1, 2, 3, 4], 2: [1, 2], 3: [2]},
3: {1: [1, 2, 3, 4], 2: [3, 4], 3: [3]},
4: {1: [1, 2, 3, 4], 2: [3, 4], 3: [4]},
}
model_parcel.non_anticipativity_z = ConstraintList()
for w in W:
for t in TH:
for s in S:
for omega in Omega[s][t]:
model_parcel.non_anticipativity_z.add(model_parcel.z[w, t, s] == model_parcel.z[w, t, omega])
Where am i doing the mistake?
Found out that I should be using this constraint,
model.AA1 = ConstraintList()
for P in model.P:
for T in model.T:
for S in model.S:
for j in Omega[S][T]:
model.AA1.add(model.Prod[P, T, S] == model.Prod[P, T, j])
model.pprint()
I was doing a silly mistake by mentioning the model.Prod variable in the for loop instead of the model.P set.