我刚开始使用Python和Gurobi进行优化。我试图编码约束
f_{s}^{E}=x_{i}\sum_{1}^{i-1}ALW_{js},\forall s\in S,\forall i={1,2,...,\frac{c(c-1)}{2}}
其中A_ {s} ^ {E}和x_ {i}是变量。 要计算ALW_ {js},我们需要读取距离矩阵的上三角,然后对i,j \ inC和j> i的距离d_ {ij}进行降序排序。排序结果可以表示为:
d_{1} \geqslant d_{2} \geqslant ... \geqslant d_{\frac{c(c-1)}{2}}
其中d_ {1} =max{d_ {ij}}和d _ {\ frac {c(c-1)} {2} = min {d_ {ij}}。 上面的每个已排序距离(表示d_ {1},d_ {2},...,d _ {\ frac {c(c-1)} {2}})在方案s中具有对应的值
ALN_{ijs}=(1-0.05)(1-0.72) \max \left \{ AN_{is},AN{js} \right \}, \forall s\in S,\forall i,j\in C and j>i
从文件中读取AN_ {is}的位置。可以写ALN_ {ijs}
ALW_{js}, \forall s\in S,\forall i={1,2,...,\frac{c(c-1)}{2}}
我的代码如下。 Gurobi提供了解决方案,但它说了约束
f_{s}^{E}=x_{i}\sum_{1}^{i-1}ALW_{js},\forall s\in S,\forall i={1,2,...,\frac{c(c-1)}{2}}
是平方的,不是真的。如果有人可以指导我,我将不胜感激。
x={}
fE={}
for s in range (S):
fE[s]=Z.addVar(lb=0, vtype=GRB.CONTINUOUS, name='fE%s'%(s))
DtN={}
with open('Distances.csv', 'rU') as file:
table = [row for row in csv.reader(file)]
for i in range (C):
for j in range (C):
if j>i:
DtN[i,j]=round(-1*float(table[i][j]),2)
SortDis=[]
SortDisKey=[]
for key, value in sorted(DtN.iteritems(), key=lambda (k,v): (v,k)):
SortDis.append(abs(value))
SortDisKey.append(key)
for i in range (len(DtN)):
x[i]=Z.addVar(lb=0, vtype=GRB.BINARY, name='x%s'%(i))
with open('Feed1.txt', 'r') as Fee:
for i in range(C):
Feed= round(float(Fee.readline()),3)
for s in L11:
AN[i,s]=round(Feed/10**6,9)
for s in L12:
AN[i,s] = round(Feed*1.28/10**6,9)
for s in L13:
AN[i,s] = round(Feed*0.95/10**6,9)
ALN={}
for s in range (S):
ALN[s]={}
for s in range(S):
for i in range(C):
for j in range(C):
if j>i:
ALN[s][i,j]=max((1-0.05)*(1-0.72)*AN[i,s],(1-0.05)*(1-0.72)*AN[j,s])
ALW={}
for s in range (S):
ALW[s]=[]
for s in range (S):
for j in SortDisKey:
ALW[s].append(ALN[s][j])
for s in range (S):
for i in range (len(DtN)):
Z.addConstr(fE[s]>=(x[i]*(quicksum(ALW[s][j] for j in range (0,i-1)))), name='N3%s%s'%(s,i))