我正在尝试在python中使用gurobi为优化模型建模。当我向模型中添加一组变量时,该模型变得不可行,即使没有与这些变量相关的约束也是如此。有问题的代码是'queue_length = model.addVars(number_of_movememts,total_time + 1,name ='queue_length')'。
from gurobipy import *
order_of_miu = 3
movement_to_phase_dict = {0:[0],1:[1]}
saturated_flow_rate = [1, 2/3]
number_of_phase = 2
number_of_movememts = 2
C_min = 60
C_max = 180
lost_time_per_phase = 4
lost_time_per_cycle = lost_time_per_phase * number_of_phase
total_time = 1500
def OptimizationModel():
model = Model('Signal Optimization')
gr_min = (C_min - lost_time_per_cycle) / C_min
gr_max = (C_max - lost_time_per_cycle) / C_max
gr_coeff = model.addVars(number_of_phase,order_of_miu + 1, lb = -1 * GRB.INFINITY, name = 'gr_coeff')
queue_length = model.addVars(number_of_movememts,total_time + 1, name = 'queue_length')
model.addConstrs((sum([gr_coeff[p,k]*t**k for p in range(number_of_phase)
for k in range(order_of_miu + 1)]) >= gr_min for t in range(total_time + 1)), "C7-1")
model.addConstrs((sum([gr_coeff[p,k]*t**k for p in range(number_of_phase)
for k in range(order_of_miu + 1)]) <= gr_max for t in range(total_time + 1)), "C7-2")
model.optimize()
if model.status == GRB.Status.OPTIMAL:
solution = model.getAttr('x',gr_coeff)
coeffs_of_all_phase = [[solution[p,k] for k in range(order_of_miu + 1)] for p in range(number_of_phase)]
return coeffs_of_all_phase
else:
return -1
if __name__ == "__main__":
coeffs_of_all_phase = OptimizationModel()