我正在寻找一种节省内存的方法。正如您在下面看到的,我创建了很多列表来设计我自己的概率分布(F)。当T = 2时,这意味着我创建了2个离散概率分布。一个仅用于第一个时期,一个用于两个时期。直到11个周期(T = 11),它工作正常,但如果我选择T = 12,我在sc_prob上有一个memoryError。我使用64位版本,所以这不是问题。有没有办法避免这种memoryError?
#number of periods
T=2
# problem data
k=100
b=9
h=1
def multiply(n):
temp = 1
for i in range(len(n)):
temp *= n[i]
return temp
minimum=[2, 11, 13, 20, 1, 0, 11, 9, 18, 20, 18, 20, 6, 20, 10, 17, 18, 18, 11, 9, 3, 16, 0, 20, 9]
mean=[23.5, 16.5, 43.0, 43.5, 8.0, 5.0, 40.0, 18.0, 41.5, 51.5, 46.0, 31.5, 53.5, 26.5, 21.5, 23.0, 49.5, 54.0, 20.5, 37.0, 20.5, 23.5, 17.0, 39.5, 55.5]
maximum=[45, 22, 73, 67, 15, 10, 69, 27, 65, 83, 74, 43, 101, 33, 33, 29, 81, 90, 30, 65, 38, 31, 34, 59, 102]
mad=[18, 5, 30, 23, 6, 3, 29, 9, 23, 31, 28, 11, 42, 6, 11, 6, 31, 36, 9, 28, 15, 7, 13, 19, 43]
#scenarios
scenario=list(product([0,1,2],repeat=T))
#print "scenarios are:",scenario
#Assigning probability to every 0,1,and 2
probability= [[[] for n in range(3)] for s in range(T)]
for n in range(T):
probability[n][0]=mad[n]/float(2*(mean[n]-minimum[n]))
probability[n][1]=1-(mad[n]/float(2*(mean[n]-minimum[n])))-(mad[n]/float(2*(maximum[n]-mean[n])))
probability[n][2]=mad[n]/float(2*(maximum[n]-mean[n]))
#print "probabilities are:", probability
# Create same list as 'scenarios', but now 0,1,2 are replaced with its probability
sc_prob=[[[] for n in range(len(scenario[s]))] for s in range(len(scenario))]
for s in range(len(scenario)):
for n in range(len(scenario[s])):
if scenario[s][n]==0:
sc_prob[s][n]=probability[n][0]
if scenario[s][n]==1:
sc_prob[s][n]=probability[n][1]
if scenario[s][n]==2:
sc_prob[s][n]=probability[n][2]
#print "sc_prob per period per scenario are:",sc_prob
# all probabilities within 1 scenario are multiplied to calculate probability of whole scenario
sc_prob_2=range(len(scenario))
for s in range(len(scenario)):
sc_prob_2[s]=multiply(sc_prob[s])
#print "sc_prob_2", sc_prob_2
# create same list as scenarios, but now 0,1,2 are replaced with its demand
demand=[[[] for n in range(len(scenario[s]))] for s in range(len(scenario))]
for s in range(len(scenario)):
for n in range(len(scenario[s])):
if scenario[s][n]==0:
demand[s][n]=minimum[n]
if scenario[s][n]==1:
demand[s][n]=mean[n]
if scenario[s][n]==2:
demand[s][n]=maximum[n]
#print "demands are: ", demand
# creating list where demand and probability are merged
# to get sum probability equal to 1, probability is divided by (3**(T-(t+1)))
prob_C_Demand=range(T)
for t in range(T):
prob_C_Demand[t]=list((sum(demand[s][:t+1]),multiply(sc_prob[s][:t+1])/(3**(T-(t+1)))) for s in range(len(scenario)))
#print "prob_C_Demand:", prob_C_Demand
#merge demand with probability when demand is the same
#create sorted dict
F=range(T)
for t in range(T):
F[t]= {x:0 for x,_ in prob_C_Demand[t]}
for name,num in prob_C_Demand[t]:
F[t][name] += num
F[t]=sorted(F[t].iteritems())
#print "F:",F
#to calculate estimated demand 'D'
probxDemand=[[[] for s in range(len(scenario))] for t in range(T)]
for t in range(T):
for s in range(len(scenario)):
probxDemand[t][s]=multiply(prob_C_Demand[t][s])
print "probxDemand:", probxDemand
D=range(T)
for t in range(T):
D[t]=sum(probxDemand[t])
print "D:", D