-
Notifications
You must be signed in to change notification settings - Fork 0
/
costmodel.py
205 lines (173 loc) · 5.99 KB
/
costmodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
# Implementation of extended learning curve model - ELCM
# Project thesis, NTNU 2011
# Author: Audun Follegg
####
# if line.find('Dataset') == 0:
# setname = line.split(':')[1].strip()
# sets[setname] = []
from math import log, e, floor, ceil
from lcurvemodel import learningCurve, collectionCurve, readConfig as lcurveConf
import csv
import xlwt
def readConfig():
# Holder of datasets {i: (q,c)}
components = []
# Get the Data
file = "costdata.conf"
infile = open(file,"r")
line = infile.readline()
while line:
line = infile.readline()
if line.find('#') == 0 or line == '\n' or line == '':
continue
else:
item = line.split(':')
name, q, c, e, l = item
components.append(( name, int(q), float(c), float(e),int(l)))
infile.close()
return components
# Fixed component purchase cost
def fixedCostSum(components):
costs = [ q*c for name, q, c, _, _ in components]
return sum(costs)
def fixedCost(components):
return [(name,q*c) for name,q,c,_,_ in components]
# Components costs(t)
def variableCostSum(components,t):
# Name, q, e for each component. If c==0: calc cost from learning curve
var_comp = [ (name,q,e) for name, q, c,e,_ in components if c == 0]
# Collections of components modelled usin ELC
lcurveComps = lcurveConf()
costs = []
for name,q,e in var_comp:
colname = lcurveComps[name]
cost_t = collectionCurve(colname, t)
costs.append((cost_t,q))
return sum([c*q for c,q in costs])
def variableCostList(components,t):
var_comp = [ (name,q,e,l) for name, q, c,e,l in components if c == 0]
# Collections of components modelled usin ELC
lcurveComps = lcurveConf()
costs = {}
for name,q,e,l in var_comp:
costs[name] =[]
colname = lcurveComps[name]
for n in range(t):
cost_n = collectionCurve(colname, n)
costs[name].append((n,cost_n,q,l))
return costs
# Returns list[(name,e_cost)]
def energyCost(components,hrs,e_price):
e_costs = [(name,q*e*hrs*e_price) for name,q,c,e,_ in components]
return e_costs
# Returns dict[name] = [(hour,quantity,cost)]
def maintenenceCost(components,hrs):
m_data = [(name,q,c,l) for name,q,c,e,l in components]
lcurveComps = lcurveConf()
result = []
for name,q,c,l in m_data:
ltmp = l
h=0
print "-- > "+name+"< --"
print "component length: " + str(len(components))
for h in range(hrs):
if h>=l:
if c==0:
colname = lcurveComps[name]
c = collectionCurve(colname,hoursInYears(h))
y = hoursInYears(h)
print y
result.append((name,y,h,q,c))
l += ltmp
return result
def yearsInHours(t):
return int(round(t*365*24))
def hoursInYears(t):
return int(round(t/8765,0))
def daysInWeeks(t):
return int((round(t/7),0))
def hoursInDays(t):
return int(round(t/24,0))
"""
END MODEL FUNCTIONS
"""
### Energy Costs ###
#__________________________________#
# hrs = hours of operation #
# e_price = average price of 1 KWh #
# ->> returns list of costs #
def getCostModelResults(t):
components = readConfig()
curveComponents = lcurveConf()
f_costs = fixedCostSum(components)
m_costs = []
e_costs = []
c_sum = []
# ADD PURCHASE COST
i=0
init_cost = float(f_costs) + float(variableCostSum(components,0))
e_cost_sum = 0
m_cost_sum = 0
total_sum = 0
m = maintenenceCost(components,yearsInHours(t))
for i in range(t):
e = energyCost(components,yearsInHours(1),0.24106)
e_unit_sum = sum([c for name,c in e])
e_cost_sum += e_unit_sum
e_costs.append(e_cost_sum)
# print "Ecost for t="+str(i)
# print e_costs
m_unit_sum = sum([q*c for name,y,h,q,c in m if y==i])
# print "unit m cost sum"
# print m_unit_sum
m_cost_sum += m_unit_sum
m_costs.append(m_cost_sum)
# print "Mcost for t="+str(i)
# print m_costs
if i ==0:
total_sum += e_unit_sum + m_unit_sum + init_cost
c_sum.append(total_sum)
# c_sum.append(e_costs[i] + m_costs[i] + init_cost)
else:
total_sum += e_unit_sum + m_unit_sum
c_sum.append(total_sum)
# c_sum.append(e_costs[i] + m_costs[i])
# print "Csum , ecost , mcost"
# print c_sum,e_costs[i], m_costs[i],init_cost
result = {'Sum':c_sum,'Energy':e_costs,'Maintenence':m_costs}
return result
def readXLS():
book = xlrd.open_workbook("results.xls")
print "The number of worksheets is", book.nsheets
print "Worksheet name(s):", book.sheet_names()
sh = book.sheet_by_index(0)
def writeXLS(results,t,sheet):
book = xlwt.Workbook()
cmodel_sheet = book.add_sheet('Cost Model Results')
lmodel_sheet = book.add_sheet('Learning Curve Results')
cmodel_sheet.write(0,0,'t')
cmodel_sheet.write(0,1,'Fixed purchase cost')
cmodel_sheet.write(0,2,'LCurve purchase cost')
cmodel_sheet.write(0,3,'Energy')
cmodel_sheet.write(0,4,'Maintenence')
cmodel_sheet.write(0,5,'Total cost')
e = results["Energy"]
m = results["Maintenence"]
s = results["Sum"]
for i in range(t):
print i
print e[i]
print m[i]
print s[i]
for i in range(t):
cmodel_sheet.write(i+1,0,i)
cmodel_sheet.write(i+1,3,e[i])
cmodel_sheet.write(i+1,4,m[i])
cmodel_sheet.write(i+1,5,s[i])
book.save('coool.xls')
if __name__ == '__main__':
t=15
x = getCostModelResults(t)
writeXLS(x,t,0)
print "20000 timer i aar"+ str(hoursInYears(70000))