使用Gurobi的线性规划最小化优化,不可行模型

时间:2019-07-14 22:17:55

标签: python optimization hash linear-programming gurobi

(该主题已编辑,我更改了方法,并且使用gurobi代替PuLP,CBC求解器) 我有一个线性问题,我想通过最小化目标函数来最大化我的收入。

约束是线性的,因此线性编程适合我的问题。我是python和线性编程的新手,所以我根据网上阅读的内容制定了以下代码。

基本上我想表达的是:

https://imgur.com/PH234gK

代码正常运行。但是,该模型是不可行的。我发现此函数“ opt_model.computeIIS()”可以指出是什么导致这种不可行。但是,我得到的结果没有意义,因为我已经为Pcharge和Pcap设置了界限:

 constraint7_1_0: - 0.0225 Pcharge_1_0 + 0.0277777777777778 Pdis_1_0
   - 0.0186962499419696 Pcap_1_0 + SOC_2_0 + SOC_3_0 + SOC_4_0 + SOC_5_0
   + SOC_6_0 + SOC_7_0 + SOC_8_0 + SOC_9_0 + SOC_10_0 + SOC_11_0 + SOC_12_0
   + SOC_13_0 + SOC_14_0 + SOC_15_0 + SOC_16_0 + SOC_17_0 + SOC_18_0
   + SOC_19_0 + SOC_20_0 + SOC_21_0 + SOC_22_0 + SOC_23_0
   = -20.02450445853112
Bounds
 -infinity <= Pcharge_1_0 <= 10
 -infinity <= Pcap_1_0 <= 10 


代码如下:

import gurobipy as grb
from gurobipy import *



opt_model = grb.Model(name="MIP Model")

import random
n = 23
m = 0
set_I = range(0, n+1)
set_J = range(0, m+1)
energy = {(i,j): random.normalvariate(0.2,0.4) for i in set_I for j in set_J}
gridE = {(i,j): random.normalvariate(-1,1) for i in set_I for j in set_J}
spot = {(i,j): random.normalvariate(0.015,0.25) for i in set_I for j in set_J}
fcr = {(i,j): random.normalvariate(9,19) for i in set_I for j in set_J}
h = {(i,j): float(0.9) for i in set_I for j in set_J}
Q = {(i,j): int(40) for i in set_I for j in set_J}
l = {(i,j): int(0) for i in set_I for j in set_J}
u = {(i,j): int(10) for i in set_I for j in set_J}
l1 = {(i,j): int(0) for i in set_I for j in set_J}
u1 = {(i,j): int(40) for i in set_I for j in set_J}
Pmax = {(i,j): int(10) for i in set_I for j in set_J}
Pmin = {(i,j): int(0) for i in set_I for j in set_J}
SOCmax = {(i,j): int(36) for i in set_I for j in set_J}
SOCmin = {(i,j): int(8) for i in set_I for j in set_J}


#Decision variables

# if Pcharge is Continuous
Pcharge  ={(i,j):opt_model.addVar(vtype=grb.GRB.CONTINUOUS,
                        lb=l[i,j],
                        ub= u[i,j],
                        name="Pcharge_{0}_{1}".format(i,j))
for i in set_I for j in set_J}

# if Pdis is Continuous
Pdis  ={(i,j):opt_model.addVar(vtype=grb.GRB.CONTINUOUS,
                        lb=l[i,j],
                        ub= u[i,j],
                        name="Pdis_{0}_{1}".format(i,j))
for i in set_I for j in set_J}

# if Pcap is Continuous
Pcap  ={(i,j):opt_model.addVar(vtype=grb.GRB.CONTINUOUS,
                        lb=l[i,j],
                        ub= u[i,j],
                        name="Pcap_{0}_{1}".format(i,j))
for i in set_I for j in set_J}


# if SOC is Continuous
SOC  ={(i,j):opt_model.addVar(vtype=grb.GRB.CONTINUOUS,
                        lb=l1[i,j],
                        ub= u1[i,j],
                        name="SOC_{0}_{1}".format(i,j))
for i in set_I for j in set_J}

SOCinit = {(0,0):20}
SOC.update(SOCinit)

# >= constraints
constraints1 = {(i,j) :
opt_model.addConstr(
        lhs=grb.quicksum(Pcharge[i,j] for i in set_I for j in set_J),
        sense=grb.GRB.GREATER_EQUAL,
        rhs=Pmin[i,j],
        name="constraint1_{0}_{1}".format(i,j))
    for i in set_I  for j in set_J}

# >= constraints
constraints2 = {(i,j) :
opt_model.addConstr(
        lhs=grb.quicksum(Pdis[i,j] for i in set_I for j in set_J),
        sense=grb.GRB.GREATER_EQUAL,
        rhs=Pmin[i,j],
        name="constraint2_{0}_{1}".format(i,j))
    for i in set_I  for j in set_J}

# >= constraints
constraints3 = {(i,j) :
opt_model.addConstr(
        lhs=grb.quicksum(Pcap[i,j] for i in set_I for j in set_J),
        sense=grb.GRB.GREATER_EQUAL,
        rhs=Pmin[i,j],
        name="constraint3_{0}_{1}".format(i,j))
    for i in set_I  for j in set_J}

# <= constraints
constraints4 = {(i,j) :
opt_model.addConstr(
        lhs=grb.quicksum(Pcharge[i,j] + Pdis[i,j] + Pcap[i,j] for i in set_I for j in set_J),
        sense=grb.GRB.LESS_EQUAL,
        rhs=Pmax[i,j],
        name="constraint4_{0}_{1}".format(i,j))
    for i in set_I for j in set_J}

# >= constraints
constraints5 = {(i,j) :
opt_model.addConstr(
        lhs=grb.quicksum(SOC[i,j] for i in set_I for j in set_J),
        sense=grb.GRB.GREATER_EQUAL,
        rhs=SOCmin[i,j],
        name="constraint5_{0}_{1}".format(i,j))
    for i in set_I for j in set_J}

# <= constraints
constraints6 = {(i,j) :
opt_model.addConstr(
        lhs=grb.quicksum(SOC[i,j] for i in set_I for j in set_J),
        sense=grb.GRB.LESS_EQUAL,
        rhs=SOCmax[i,j],
        name="constraint6_{0}_{1}".format(i,j))
    for i in set_I for j in set_J}


# == constraints
constraints7 = {(i,j) :
opt_model.addConstr(
        lhs=grb.quicksum(SOC[i,j] for i in set_I for j in set_J),
        sense=grb.GRB.EQUAL,
        rhs=SOC[i,j] + (gridE[i,j] + Pcap[i,j] * energy[i,j] + Pcharge[i,j] * h[i,j] - Pdis[i,j] * 1 / h[i,j]) * 1 / Q[i,j],
        name="constraint7_{0}_{1}".format(i,j))
    for i in set_I for j in set_J}



#objective

objective = grb.quicksum(Pcharge[i,j] * spot[i,j] - Pdis[i,j] * spot[i,j] - Pcap[i,j] * fcr[i,j] for i in set_I for j in set_J)


# for minimization
opt_model.ModelSense = grb.GRB.MINIMIZE
opt_model.setObjective(objective)

# if opt_model.status == GRB.INFEASIBLE:
#     opt_model.feasRelaxS(1, False, False, True)

opt_model.optimize()

opt_model.computeIIS()
opt_model.write("model.ilp")```

1 个答案:

答案 0 :(得分:0)

for i in range(0, 8759):
      SOC[i + 1] = SOC[i] + (gridE[i] + Pcap[i] * energy[i] + Pcharge[i] * h - Pdis[i] * 1 / h) * 1 / Q

在我看来您需要类似的东西

for i in range(0, 8759):
        opt_model += SOC[i + 1] == SOC[i] + (gridE[i] + Pcap[i] * energy[i] + Pcharge[i] * h - Pdis[i] * 1 / h) * 1 / Q

这实际上将对模型添加约束