我有以下型号:
from gurobipy import *
n_units = 1
n_periods = 3
n_ageclasses = 4
units = range(1,n_units+1)
periods = range(1,n_periods+1)
periods_plus1 = periods[:]
periods_plus1.append(max(periods_plus1)+1)
ageclasses = range(1,n_ageclasses+1)
nothickets = ageclasses[1:]
model = Model('MPPM')
HARVEST = model.addVars(units, periods, nothickets, vtype=GRB.INTEGER, name="HARVEST")
FOREST = model.addVars(units, periods_plus1, ageclasses, vtype=GRB.INTEGER, name="FOREST")
model.addConstrs((quicksum(HARVEST[(k+1), (t+1), nothicket] for k in range(n_units) for t in range(n_periods) for nothicket in nothickets) == FOREST[unit, period+1, 1] for unit in units for period in periods if period < max(periods_plus1)), name="A_Thicket")
我在制定约束时遇到问题。我想要为每个单位和每个时期总结变量HARVEST的nothickets部分。具体地说我想要x k = 1,t = 1,2 + x k = 1,t = 1,3 + x k = 1,t = 1 ,4 子> 等等。这应该导致约束矩阵的每行只有三个。但是根据上面的表述我得到了9个。 我尝试在总和之外使用for循环,但这会导致另一个问题:
for k in range(n_units):
for t in range(n_periods):
model.addConstrs((quicksum(HARVEST[(k+1), (t+1), nothicket] for nothicket in nothickets) == FOREST[unit,period+1, 1] for unit in units for period in periods if period < max(periods_plus1)), name="A_Thicket")
通过这个公式,我得到了这个矩阵: constraint matrix
但我想要的是:
row_idx | col_idx | coeff
0 | 0 | 1
0 | 1 | 1
0 | 2 | 1
0 | 13 | -1
1 | 3 | 1
1 | 4 | 1
1 | 5 | 1
1 | 17 | -1
2 | 6 | 1
2 | 7 | 1
2 | 8 | 1
2 | 21 | -1
有人可以帮我重新制定这种约束吗?
答案 0 :(得分:0)
这对我有用:
model.addConstrs((HARVEST.sum(unit, period, '*') == ...