我有一些使用PuLP模块进行线性编程的代码,我想按升序对输出进行打印。我尝试使用sorted()方法,但是不幸的是在这种情况下它不起作用,因为输出不是列表。另外,输出(如下所述)是一些变量名和变量值的组合,我想将它们一起打印,并用“ =”符号分隔。
import time
start_time = time.time()
from pulp import *
from openpyxl import load_workbook
import pandas as pd
from pulp import solvers
import xlrd
import os
vName = list()
vValue = list()
wName = list()
wValue = list()
PriceData=open('C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Otherdata\pricedata.txt', 'r')
line1= PriceData.readlines()
wb=load_workbook(filename="C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Price Data\\"+ line1[0].strip())
ws=wb['Sheet1']
OC_Data=open('C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Otherdata\OC_Data.txt', 'r')
line2= OC_Data.readlines()
OC=open('C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Operating Characteristics\\'+ line2[0].strip())
line3= OC.readlines()
Horizon=int(line1[1][10:])
pieces=int(line3[4][29:])
T=list(range(Horizon))
L=list(range(pieces))
p=[0 for j in range(Horizon)]
UB=[0 for l in range(pieces)]
LB=[0 for l in range(pieces)]
f=[0 for l in range(pieces)]
g=[0 for l in range(pieces)]
for j in range(Horizon):
p[j]=ws.cell(row=967, column=j+2).value
UB[0]=float(line3[6][5:])
UB[1]=float(line3[10][5:])
UB[2]=float(line3[14][5:])
LB[0]=float(line3[5][5:])
LB[1]=float(line3[9][5:])
LB[2]=float(line3[13][5:])
f[0]=float(line3[7][6:])*30.5
f[1]=float(line3[11][6:])*30.5
f[2]=float(line3[15][6:])*30.5
g[0]=float(line3[8][6:])*30.5
g[1]=float(line3[12][6:])*30.5
g[2]=float(line3[16][6:])*30.5
Cap=float(line3[3][10:])
M=Cap
prob= LpProblem("EL-NR", LpMaximize)
v = LpVariable.matrix("v", (T),0, None, LpContinuous)
w = LpVariable.matrix("w", (T),0, None, LpContinuous)
I = LpVariable.matrix("I", (T),0, None, LpInteger)
z = LpVariable.matrix("z", (T), 0, 1, LpBinary)
lb= LpVariable.matrix("lambda", (T,L,L), 0, 1, LpContinuous)
mu= LpVariable.matrix("mu", (T,L,L), 0, 1, LpContinuous)
r= LpVariable.matrix("r", (T,L), 0 ,1, LpBinary)
zz= LpVariable.matrix("zz", (T,L,L),0, 1,LpBinary)
xx= LpVariable.matrix("xx", (T,L,L),0,1,LpBinary)
prob+= (lpSum([p[t]*(-v[t]+w[t]) for t in T]))
for t in T:
prob+= w[t]<=I[t]
prob+= w[t]<=M*z[t]
prob+= v[t]<=M*(1-z[t])
prob+= I[t]<=Cap
prob+= lpSum([r[t][l] for l in L])==1
def constraint_1(t,L):
value = 0
for l in L:
for k in L:
if k>=l:
value+= lb[t][l][k]*f[k]
return value
def constraint_2(t,L):
value = 0
for l in L:
for k in L:
if k<=l:
value+= mu[t][l][k]*g[k]
return value
for t in T:
prob+=v[t]<= constraint_1(t,L)
prob+=w[t]<= constraint_2(t,L)
for t in T:
for l in L[:pieces-1]:
for k in L:
if k==l:
prob+= lb[t][l][k]<=((UB[l]-I[t])/f[l])+M*(1-r[t][l])
for l in L:
for k in L:
if k>l:
prob+= lb[t][l][k]<=((UB[k]-UB[k-1])/f[k])+M*(1-r[t][l])
for l in L:
for k in L:
if k>=l:
prob+= lpSum([lb[t][l][k] for k in L])==r[t][l]
for l in L[1:]:
for k in L:
if k==l:
prob+=mu[t][l][k]<=((I[t]-LB[l])/g[l])+M*(1-r[t][l])
for l in L:
for k in L:
if k<l:
prob+=mu[t][l][k]<=((LB[k+1]-LB[k])/g[k])+M*(1-r[t][l])
if k<=l:
prob+= lpSum([mu[t][l][k] for k in L])==r[t][l]
if k>=l:
prob+=lb[t][l][k]<=zz[t][l][k]
if k<=l:
prob+=mu[t][l][k]<=xx[t][l][k]
for kr in L:
if k>=l:
if kr>k:
prob+=zz[t][l][k]>=zz[t][l][kr]
for kr in L:
if k<=l:
if kr<k:
prob+=xx[t][l][k]>=xx[t][l][kr]
prob+=lb[t][l][k]>=0
prob+=mu[t][l][k]>=0
if t>0:
b=t-1
prob+= I[t]==I[b]+v[b]-w[b]
prob+= I[0]==float(line3[1][25:])
prob.solve(GUROBI())
for v in prob.variables():
if v.name[:1]=="v":
a=v.name[1:]
vName.append(a)
b=v.varValue
vValue.append(b)
print(v.name[2:]+'='+ str(b))
This is what I am getting:
0=0.0
1=-0.0
10=-0.0
11=0.0
12=0.0
13=0.0
14=0.0
15=274000.0
16=189500.0
17=183000.0
18=0.0
19=0.0
2=0.0
20=251140.00000618323
21=46707.99999497511
22=0.0
23=0.0
3=0.0
4=274000.0
5=189500.0
6=170500.0
7=183000.0
8=183000.0
9=0.0
And this is what I need:
0=0.0
1=-0.0
2=0.0
3=0.0
4=274000.0
5=189500.0
6=170500.0
7=183000.0
8=183000.0
9=0.0
10=-0.0
11=0.0
12=0.0
13=0.0
14=0.0
15=274000.0
16=189500.0
17=183000.0
18=0.0
19=0.0
20=251140.00000618323
21=46707.99999497511
22=0.0
23=0.0
如果有人能回答这个问题,我将不胜感激。 TIA:)
答案 0 :(得分:1)
这对您有用吗?...
替换
for v in prob.variables():
if v.name[:1]=="v":
a=v.name[1:]
vName.append(a)
b=v.varValue
vValue.append(b)
print(v.name[2:]+'='+ str(b))
使用:
for t in T:
print(str(t) + '=' + str(v[t].varValue))