import gurobipy as gp import numpy as np # 定义参数 c_p = 225 tau = 135000 C_re = 20 C_se = 30 h = 3.25 q = 0.2 e = 18.98 S = 10 T = 20 expr2_1 = (C_req+C_se)e m = np.full((1, T), 3) z = np.around((1-1np.random.rand(T, S)) * 3) # 创建模型 model = gp.Model() # 创建变量 x = model.addVar(lb=0, name='x') k = model.addVar(lb=0, name='k') y = model.addVars(S, lb=0, name='y') a = model.addVars(T,S, vtype=gp.GRB.BINARY, name="a") # a = model.addVars(T, S, lb=0, name='a') b = model.addVars(T, S, lb=0, name='b') # 创建约束 for s in range(S): expa = c_p * x + e * h * x + (1/S) * gp.quicksum(y[t] for t in range(S)) model.addConstr(expa <= tau, name=f'c1_{s}') expb1=gp.quicksum(3a[t,s] for t in range(T)) expb2=gp.quicksum((expr2_1-a[t,s]+b[t,s])*z[t,s] for t in range (T)) expb = expb1+expb2 model.addConstr(y[s] >= expb, name=f'c2_{s}') # expc = gp.quicksum(abs(expr2_1-a[t,s]+b[t,s]) for t in range (T)) expc = gp.quicksum((expr2_1 - a[t, s] + b[t, s]) for t in range(T)) model.addConstr(expc <= k, name=f'c3_{s}') # 创建目标 model.setObjective(k, gp.GRB.MINIMIZE) # 求解 model.optimize() print("Optimal Objective Value", model.objVal) for var in model.getVars(): print(f"{var.varName}: {round(var.X, 3)}")为什么x的值是0
时间: 2023-07-15 15:13:25 浏览: 84
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
在您提供的代码中,变量x没有被任何约束限制,它的上下界分别为0和正无穷,因此,模型可能会找到x=0的最优解。您需要添加一个适当的约束来限制变量x的值。如果x应该是非零的,可以将其下界设置为一个小正数,例如0.001。如果x应该是正整数,可以将其类型更改为整数类型(vtype=gp.GRB.INTEGER)。请根据您的问题设置适当的约束来限制变量x的值。
阅读全文