Created
April 2, 2017 13:35
-
-
Save aragaer/c354b22b90ac07b415d730b0ffb19223 to your computer and use it in GitHub Desktop.
MosEnergoSbyt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from datetime import date | |
class tariff(): | |
def __init__(self, costs, start_date, end_date): | |
self.costs = costs | |
self.start_date = start_date | |
self.end_date = end_date | |
class measurement(): | |
def __init__(self, day, values): | |
self.day = day | |
self.values = values | |
measurements = [ | |
measurement(date(2014, 2, 11), (3706, 2546, 3986)), | |
measurement(date(2014, 8, 25), (4044, 2894, 4391)), | |
] | |
tariffs = [ | |
tariff((4.54, 1.16, 3.76), date(2014, 1, 1), date(2014, 6, 30)), | |
tariff((4.54, 1.16, 3.76), date(2014, 7, 1), date(2014, 10, 31)), | |
tariff((4.92, 1.26, 4.08), date(2014, 11, 1), date(2014, 12, 31)), | |
tariff((4.92, 1.26, 4.08), date(2015, 1, 1), date(2015, 6, 30)), | |
tariff((5.58, 1.43, 4.63), date(2015, 7, 1), date(2015, 12, 31)), | |
tariff((5.58, 1.43, 4.63), date(2016, 1, 1), date(2016, 6, 30)), | |
tariff((6.41, 1.64, 5.32), date(2016, 7, 1), date(2016, 12, 31)), | |
tariff((6.41, 1.64, 5.32), date(2017, 1, 1), date(2017, 6, 30)), | |
] | |
def interval_to_cost(start_values, end_values, costs): | |
return sum((v1-v0)*c for v0, v1, c in zip(start_values, end_values, costs)) | |
def values_at_point(measurement0, measurement1, day): | |
mid_days = (day-measurement0.day).days+1 | |
all_days = (measurement1.day-measurement0.day).days+1 | |
return tuple(v0+int((v1-v0)*mid_days/all_days) | |
for v0, v1 in zip(measurement0.values, measurement1.values)) | |
def calc(measurement0, measurement1): | |
days = (measurement1.day - measurement0.day).days + 1 | |
result = 0 | |
for tariff in tariffs: | |
if tariff.end_date < measurement0.day or tariff.start_date > measurement1.day: | |
continue | |
#print("Tariff",tariff.start_date,tariff.end_date,tariff.costs, | |
# "is in interval",measurement0.day,"to",measurement1.day) | |
start = max(tariff.start_date, measurement0.day) | |
end = min(measurement1.day, tariff.end_date) | |
values_start = values_at_point(measurement0, measurement1, start) | |
values_end = values_at_point(measurement0, measurement1, end) | |
cost = interval_to_cost(values_start, values_end, tariff.costs) | |
print(values_start, values_end, cost) | |
result += cost | |
days -= (end-start).days+1 | |
assert(days == 0) | |
print("Interval",measurement0.day,"to",measurement1.day,"result is",result) | |
return result | |
if __name__ == '__main__': | |
print(sum(calc(m0, m1) for m0, m1 in zip(measurements, measurements[1:]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment