import gurobipy as gp import numpy as np # 定义参数 c_p = 22 tau = 135000 C_re = 20 C_se = 30 h = 3.25 q = 0.2 e = 18.98 S = 10 T = 20 expr2_1 = (C_re*q+C_se)*e m = np.full((1, T), 3) z = np.around((1-1*np.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(3*a[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"y有什么问题{var.varName}: {round(var.X, 3)}")
时间: 2023-11-12 16:05:31 浏览: 159
这是一个线性规划问题的Python代码,使用了Gurobi库进行求解。变量x、y、k和a、b分别对应模型中的决策变量和辅助变量。约束条件包括三个方面:第一个约束限制了某个表达式不超过给定的值;第二个约束限制了某个表达式大于等于另外两个表达式之和;第三个约束限制了某个表达式的总和不超过一个给定的值。目标函数是最小化某个变量的值。最后使用model.optimize()函数进行求解,并输出最优目标函数值和每个变量的取值。
相关问题
import gurobipy as gp import numpy as np # 定义参数 c_p = 225 tau = 13500 C_re = 20 C_se = 30 h = 3.25 q = 0.2 e = 18.98 S = 10 T = 20 expr2_1 = (C_re*q+C_se)*e m = np.full((1, T), 3) z = np.around((1-1*np.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="C") # 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(3*a[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)) model.addConstr(expc <= k, name=f'c3_{s}') # 创建目标 model.setObjective(k, gp.GRB.MINIMIZE) # 求解 model.optimize() # 输出结果 print(f'最优解: {model.objVal}')遇到了expc = gp.quicksum(abs(expr2_1-a[t,s]+b[t,s]) for t in range (T)) TypeError: bad operand type for abs(): 'gurobipy.LinExpr'
这个错误是因为 `abs()` 函数不能直接作用于 `gurobipy.LinExpr` 类型,需要将其转化为可计算的类型。可以使用 `gurobipy.quicksum()` 函数将 `a[t,s]` 和 `b[t,s]` 的差值转化为可计算的表达式:
```
expc = gp.quicksum(gp.abs_(expr2_1-a[t,s]+b[t,s]) for t in range (T))
```
这样就可以避免这个错误了。
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_re*q+C_se)*e m = np.full((1, T), 3) z = np.around((1-1*np.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(3*a[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
根据你提供的代码,变量 x 的下限被设置为 0,而目标函数又是最小化变量 k,所以 Gurobi 会尝试让 x 的值为 0 以最小化 k。
如果你想让 x 的值为非零值,你可以将其下限改为一个正数或者添加其他约束条件来限制 x 的取值范围。比如,你可以添加一个 x 的上限约束,或者添加一个与 x 相关的目标函数项来引导 Gurobi 让 x 取非零值。
阅读全文