-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrate_tier.py
95 lines (93 loc) · 3.39 KB
/
rate_tier.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
import sys
import random
from numpy import loadtxt
class RateTier:
def __init__(self, low, high, rate):
self.low_bound = low
self.high_bound = high
self.rate = rate
self.count = 0
self.total_volume = 0
self.tier_volume = 0
self.revenue = 0
self.elasticity = 1.0
def dump(self):
print('{0} - {1} rate = {2}, count = {3}, total_volume = {4}, tier_volume = {5}, revenue = {6}'.format(self.low_bound, self.high_bound, self.rate, self.count, self.total_volume, self.tier_volume, self.revenue))
def log(self, logger):
logger.log('{0} - {1} rate = {2}, count = {3}, total_volume = {4}, tier_volume = {5}, revenue = {6}'.format(self.low_bound, self.high_bound, self.rate, self.count, self.total_volume, self.tier_volume, self.revenue))
def account(self, volume):
return
class TierSystem:
def __init__(self):
self.tiers = []
def add_tier(self, tier):
self.tiers.append(tier)
def set_elasticity(self, multipliers):
for i in range(0, len(self.tiers)):
self.tiers[i].elasticity = multipliers[i]
#n = len(multipliers)
#idx = 0
#for i in range(len(self.tiers) - n, len(self.tiers)):
# self.tiers[i].elasticity = multipliers[idx]
# idx += 1
def get_tiers(self):
return self.tiers
def get_tier_index(self, volume):
idx = 0
for tier in self.tiers:
v = min(volume, tier.high_bound) - tier.low_bound - 1
if volume <= tier.high_bound:
return idx
idx += 1
return idx
def get_tier(self, volume):
idx = self.get_tier_index(volume)
return self.tiers[idx]
def account(self, volume):
if random.random() > 0.5:
volume = volume * self.get_tier(volume).elasticity
for tier in self.tiers:
v = min(volume, tier.high_bound) - tier.low_bound
tier.revenue += v * tier.rate
tier.total_volume += v
if volume <= tier.high_bound:
tier.tier_volume += volume
tier.count += 1
break
def clear_account(self):
for t in self.tiers:
t.revenue = 0
t.total_volume = 0
t.tier_volume = 0
t.count = 0
def total_revenue(self):
total = 0.0;
for t in self.tiers:
total += t.revenue;
return total
def total_volume(self):
total = 0.0;
for t in self.tiers:
total += t.total_volume;
return total
def dump(self):
for tier in self.tiers:
tier.dump()
print('total volume = {0}'.format(self.total_volume()))
print('total revenue = {0}'.format(self.total_revenue()))
def export(self, filename):
f = open(filename, 'w')
idx = 2
f.write('"Usage (HCF)","Count","Volume (HCF)","Rate ($)","Revenue ($)"\n')
for t in self.tiers:
f.write('"{0} - {1}",{2},{3},{4},=(C{5}*D{5})\n'.format(t.low_bound, t.high_bound, t.count, t.total_volume, t.rate, idx))
idx += 1
f.write('"","","","",=SUM(E2:E{0})\n'.format(len(self.tiers) + 1))
f.close()
@staticmethod
def load_from_file(filename):
lines = loadtxt(filename)
ts = TierSystem()
for l in lines:
ts.add_tier(RateTier(l[0], l[1], l[2]))
return ts