something problems in Python code
import gurobipy
from gurobipy import *
m = Model("mip1")
# 创建常量
pbuy = 0.27
psold = 0.13
gsold = 0.2
gbuy = 0.4
pload = 56.079715030976004
hload = 40.37739482230272
gload = 43.768518510060744
ppv = 0
pwt = 23.264563195
pp2g = 63.27525490012397
gchp1 = 0
gchp2 = 0
pess = -24.078819315855416
hess = 0
peb = 44.73949564798085
# 创建变量
pp2g_delta = m.addVar(vtype=GRB.CONTINUOUS, name="pp2g_delta")
gchp1_delta = m.addVar(vtype=GRB.CONTINUOUS, name="gchp1_delta")
pess_delta = m.addVar(vtype=GRB.CONTINUOUS, name="pess_delta")
gchp2_delta = 0.8 * gchp1_delta
# 定义辅助变量
part1_switch = m.addVar(vtype=GRB.BINARY, name="part1_switch")
part2_switch = m.addVar(vtype=GRB.BINARY, name="part2_switch")
part3_switch = m.addVar(vtype=GRB.BINARY, name="part3_switch")
part4_switch = m.addVar(vtype=GRB.BINARY, name="part4_switch")
gas = gload - (pp2g + pp2g_delta) * 0.83 + gchp1 + gchp2
power = pload - ppv - pwt + pp2g + hload / (0.95 * 0.95) + pess_delta
# 目标函数
part1 = -gbuy * (gload - (pp2g + pp2g_delta) * 0.83) * part1_switch
part2 = gsold * (gload - (pp2g + pp2g_delta) * 0.83) * part2_switch
part3 = -pbuy * (pload - pwt + pp2g + hload / (0.95 * 0.95) + pess_delta) * part3_switch
part4 = psold * (pload - pwt + pp2g + hload / (0.95 * 0.95) + pess_delta) * part4_switch
#Q1:I want to use the absolute value of this variable in the formula, but I get an error
TypeError: unsupported operand type(s) for *: 'float' and 'GenExpr'
part6 = -0.01 * abs_(pess_delta)
part5 = -0.01 * abs_(pp2g + pp2g_delta)
# 设置目标函数为各个部分的线性组合
m.setObjective(part1 + part2 + part3 + part4 + part5 + part6, GRB.MAXIMIZE)
# 约束 m.addConstr(pp2g + pp2g_delta >= 0, "c4")
m.addConstr(pp2g + pp2g_delta<= 150, "c5")
m.addConstr(pess + pess_delta >= -1000, "c6")
m.addConstr(pess + pess_delta <= 1000, "c7")
m.addConstr(hess >= -800, "c8")
m.addConstr(pp2g_delta <= 60, "c12")
m.addConstr(pp2g_delta >= 0, "c13")
# Q2:The logic here goes like this:
there are 2 variables in “gas” and “power” - “pp2g_delta” and “pess_delta”.
if gas >= 0: r1 = -gbuy * gas else: r1 = gsold * gas
if power >= 0: r2 = -pbuy * power else: r2 = psold * power
But the program runs and tells me that this is wrong, and an error is reported
——gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
So, I want to use the 0/1 variable to circumvent the problem, means:
r1= part1 * 1 + part2 * 0 another part1 * 0 + part2 * 1 r2= part3 * 1 + part4 * 0 another part3 * 0 + part2 * 4
but there is still an error
——gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
m.addConstr(part1_switch == gurobipy.LinExpr(1 if gas >= 0 else 0), name="part1_switch_constraint")
m.addConstr(part2_switch == gurobipy.LinExpr(1 if gas < 0 else 0), name="part2_switch_constraint")
m.addConstr(part3_switch == gurobipy.LinExpr(1 if power >= 0 else 0), name="part3_switch_constraint")
m.addConstr(part4_switch == gurobipy.LinExpr(1 if power < 0 else 0), name="part4_switch_constraint")
m.optimize() for v in m.getVars(): print('%s %g' % (v.varName, v.x)) print('Obj: %g' % m.objVal)
0
Please sign in to leave a comment.
Comments
0 comments