-
Notifications
You must be signed in to change notification settings - Fork 0
/
projected_demand.py
177 lines (164 loc) · 6.4 KB
/
projected_demand.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
import os
from datetime import date
from sqlalchemy import *
from sqlalchemy.orm import *
from classes import *
from sqlalchemy import func
from sqlalchemy.dialects.mysql import insert
engine = create_engine('mysql+mysqlconnector://'+
os.environ.get('MYSQL_USER', 'root')+':'+
os.environ.get('MYSQL_PASSWORD','')+'@'+
os.environ.get('MYSQL_HOST','127.0.0.1')+'/dw', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
def populate_qtde():
real_demand = session.query(
Order.IDPROD,
case([
(func.month(Order.DATAPED) < 4, 1),
(func.month(Order.DATAPED) < 7, 2),
(func.month(Order.DATAPED) < 10, 3)
],
else_=4),
func.year(Order.DATAPED),
func.sum(Order.QTDE)
).group_by(
case([
(func.month(Order.DATAPED) < 4, 1),
(func.month(Order.DATAPED) < 7, 2),
(func.month(Order.DATAPED) < 10, 3)
],
else_=4),
func.year(Order.DATAPED),
Order.IDPROD
).all()
for demand in real_demand:
time = session.query(TimeDimension).filter(TimeDimension.TRIMESTRE == demand[1], TimeDimension.ANO == demand[2]).first()
if (time is None):
time = TimeDimension(TRIMESTRE = demand[1], ANO = demand[2])
session.add(time)
session.commit()
time = session.query(TimeDimension).filter(TimeDimension.TRIMESTRE == time.TRIMESTRE, TimeDimension.ANO == time.ANO).first()
order = engine.execute(insert(ProjectedDemandFact).values(IDPROD = demand[0], IDTEMPO = time.ID, QTDE = float(demand[3])).on_duplicate_key_update(QTDE = float(demand[3])))
session.commit()
def projected_demand():
product_list = session.query(func.distinct(ProjectedDemandFact.IDPROD)).all()
dp_mm_list = []
dp_ae_list = []
dp_rl_list = []
for product in product_list:
best = [0]*3
for year in range(2016,2019):
num = 0
for trim in range (1, 5):
if (year == 2018 and trim>1):
dp_ae_list.pop(0)
break
elif (not(year == 2016 and trim == 1)):
dp_mm_list.append(dp_mm(product[0], year, trim))
dp_ae_list.append(dp_ae(product[0], year, trim, dp_ae_list[-1][3]))
num +=1
dp_rl_list.append(dp_rl(product[0], year, trim, num))
best = better_between_projects(dp_mm_list[-1], dp_ae_list[-1], dp_rl_list[-1], best)
else:
actual_demand = real_retroactive_demand(product[0], year, trim, 0)
[xy, cont, x, y, x2, y2] = [1.0 * actual_demand, 1, 1.0, actual_demand, 1.0, actual_demand * actual_demand]
dp_ae_list.append([product[0], year, trim, actual_demand])
def real_retroactive_demand(product, year, trimester, num):
demand = None
if ((trimester - num)<1):
year = year - 1
num = 4 + (trimester - num)
else:
num = trimester - num
subquery = session.query(TimeDimension.ID).filter(TimeDimension.ANO == year, TimeDimension.TRIMESTRE == num).subquery()
demand = session.query(ProjectedDemandFact.QTDE).filter(ProjectedDemandFact.IDTEMPO == subquery, ProjectedDemandFact.IDPROD == product).first()
if demand is None:
return int(0)
elif demand[0] is None:
return int(0)
else:
return int(demand[0])
def dp_mm(product, year, trim):
cont = 0
dpmm = 0
for i in range(1,5):
demand_on_trimester = real_retroactive_demand(product,year,trim,i)
if demand_on_trimester > 0:
cont += 1
dpmm += demand_on_trimester
return [product, year, trim, dpmm/float(cont)]
def dp_ae(product, year, trimester, dp_minus_one):
alpha = 0.5
real_demand = real_retroactive_demand(product,year,trimester,1)
return [product, year, trimester, alpha * real_demand + (1 - alpha) * dp_minus_one]
def dp_rl(product, year, trim, num):
xy = 0
cont = 0
x = 0
y = 0
x2 = 0
y2 = 0
if (num>4):
num = 4
for i in range(1,num+1):
actual_y = real_retroactive_demand(product, year, trim, (num+1)-i)
if (actual_y != 0):
cont += i
x += cont
y += actual_y
xy += cont * actual_y
x2 += cont * cont
y2 += actual_y * actual_y
try:
beta = (cont * xy - x*y) / float(cont*x2 - x*x)
alpha = (y/cont) - (x/cont) * beta
dp_rl_plus_one = [product, year, trim, alpha + beta * cont]
return dp_rl_plus_one
except ZeroDivisionError:
return [product, year, trim, 0]
def better_between_projects(dp_mm, dp_ae, dp_rl, best):
real_demand = real_retroactive_demand(dp_mm[0],dp_mm[1], dp_mm[2],0)
insert_dp = 0
if (real_demand != 0):
dp = [None, None]*3
dp[0] = [dp_mm[3] - real_demand, dp_mm[3]]
dp[1] = [dp_ae[3] - real_demand, dp_ae[3]]
dp[2] = [dp_rl[3] - real_demand, dp_rl[3]]
if (dp[2][1] != 0 ):
for i in range(0,3):
best[i] += dp[i][0]
better = best[0]
insert_dp = dp[0][1]
for i in range(1,3):
if (best[i] < better):
better = best[i]
insert_dp = dp[i][1]
else:
if (dp[0][0] < dp[1][0]):
insert_dp = dp[0][1]
else:
insert_dp = dp[1][1]
else:
dp = [0]*3
dp[0] = dp_mm[3]
dp[1] = dp_ae[3]
dp[2] = dp_rl[3]
smaller = best[0]
insert_dp = dp[0]
for i in range(1,3):
if (best[i] < smaller):
smaller = best[i]
insert_dp = dp[i]
time = session.query(TimeDimension).filter(TimeDimension.TRIMESTRE == dp_mm[2], TimeDimension.ANO == dp_mm[1]).first()
if (time is None):
time = TimeDimension(TRIMESTRE = dp_mm[2], ANO = dp_mm[1])
session.add(time)
session.commit()
time = session.query(TimeDimension).filter(TimeDimension.TRIMESTRE == dp_mm[2], TimeDimension.ANO == dp_mm[1]).first()
statement = insert(ProjectedDemandFact).values(IDPROD = dp_mm[0], IDTEMPO = time.ID, DEMANDA_PROJETADA = insert_dp).on_duplicate_key_update(DEMANDA_PROJETADA = insert_dp)
engine.execute(statement)
session.commit()
return best
populate_qtde()
projected_demand()