Advertisement
Mochinov

Untitled

May 18th, 2023
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.98 KB | None | 0 0
  1. import math
  2. import pandas as pd
  3. from pyomo.environ import *
  4.  
  5. from celery import shared_task
  6.  
  7. from forecasts.clickhouse.db import get_session
  8. from .utils import get_replenishment_inputs_file
  9. from .utils import set_replenishment_output_file
  10.  
  11.  
  12.  
  13. # @shared_task(name="replen_optimizer.tasks.default.task_start_parsing_input_replenishment_files")
  14. # def task_start_parsing_input_replenishment_files():
  15.  
  16. # # Считываем данные
  17. # artcodes_data = pd.read_csv(
  18. # get_replenishment_inputs_file('in_artcodes.csv'),
  19. # index_col=['artcode_nm'],
  20. # )
  21.  
  22. # regions_data = pd.read_csv(
  23. # get_replenishment_inputs_file('in_regions.csv'),
  24. # index_col=['region_nm'],
  25. # )
  26.  
  27. # hubs_data = pd.read_csv(
  28. # get_replenishment_inputs_file('in_hubs.csv'),
  29. # index_col=['hub_nm'],
  30. # )
  31.  
  32. # primary_network_data = pd.read_csv(
  33. # get_replenishment_inputs_file('in_network.csv'),
  34. # index_col=['hub_nm', 'region_nm', 'artcode_nm'],
  35. # )
  36.  
  37. # secondary_network_data = pd.read_csv(
  38. # get_replenishment_inputs_file('in_network.csv')
  39. # )
  40.  
  41. # secondary_network_data = secondary_network_data.dropna().set_index(
  42. # ['hub_sec priority', 'region_nm', 'artcode_nm']
  43. # )
  44.  
  45.  
  46. # region_stocks_data = pd.read_csv(
  47. # get_replenishment_inputs_file('in_region_stocks.csv'),
  48. # index_col=['region_nm', 'artcode_nm'],
  49. # )
  50.  
  51. # hub_stocks_data = pd.read_csv(
  52. # get_replenishment_inputs_file('in_hub_stocks.csv'),
  53. # index_col=['hub_nm', 'artcode_nm'],
  54. # )
  55.  
  56. # region_target_data = pd.read_csv(
  57. # get_replenishment_inputs_file('in_target_reg.csv'),
  58. # index_col=['region_nm', 'artcode_nm'],
  59. # )
  60.  
  61. # hub_target_data = pd.read_csv(
  62. # get_replenishment_inputs_file('in_target_hub.csv'),
  63. # index_col=['hub_nm', 'artcode_nm'],
  64. # )
  65.  
  66.  
  67. # ARTCODES = list(artcodes_data.index.values)
  68.  
  69. # BoxQuantity = artcodes_data['box_quantity'].to_dict()
  70. # PalQuantity = artcodes_data['pal_quantity'].to_dict()
  71. # PalRounding = artcodes_data['pal_rounding'].to_dict()
  72. # BoxMpl = artcodes_data['box_mpl'].to_dict()
  73.  
  74. # #КОСТЫЛЬ
  75. # for a in ARTCODES:
  76. # if PalQuantity[a] == 0:
  77. # PalQuantity[a] = 1
  78.  
  79. # if BoxQuantity[a] == 0:
  80. # BoxQuantity[a] = 1
  81.  
  82. # REGIONS = list(regions_data.index.values)
  83.  
  84. # HUBS = list(hubs_data.index.values)
  85.  
  86. # NETWORK = list(primary_network_data.index.values)
  87.  
  88. # SECONDARY_NETWORK = list(secondary_network_data.index.values)
  89.  
  90. # FULL_NETWORK = NETWORK + SECONDARY_NETWORK
  91.  
  92. # RegionDelta = {}
  93. # HubDelta = {}
  94.  
  95. # RegionStock = region_stocks_data['region_stock'].to_dict()
  96. # HubStock = hub_stocks_data['hub_stock'].to_dict()
  97.  
  98. # for a in ARTCODES:
  99. # for h in HUBS:
  100. # if (h, a) not in HubStock.keys():
  101. # HubStock[h, a] = 0
  102.  
  103. # for a in ARTCODES:
  104. # for r in REGIONS:
  105. # if (r, a) not in RegionStock.keys():
  106. # RegionStock[r, a] = 0
  107.  
  108. # RegionTarget = region_target_data['target_stock_reg'].to_dict()
  109.  
  110. # HubTarget = hub_target_data['target_stock_hub'].to_dict()
  111.  
  112. # HubMinTarget = hub_target_data['min_target_stock'].to_dict()
  113.  
  114. # def artcode_filter(a, priority):
  115.  
  116. # for r in REGIONS:
  117. # if (r, a) not in RegionStock.keys():
  118. # RegionStock[r, a] = 0
  119. # if RegionTarget[r,a] < 0:
  120. # RegionTarget[r,a] = 0
  121. # if (r, a) not in RegionTarget.keys() or RegionTarget[r, a] == 0:
  122. # RegionDelta[r, a] = 1
  123. # else:
  124. # RegionDelta[r, a] = RegionStock[r, a] / RegionTarget[r, a]
  125.  
  126. # for h in HUBS:
  127. # if (h, a) not in HubStock.keys():
  128. # HubStock[h, a] = 0
  129. # if (h, a) not in HubTarget.keys() or HubTarget[h, a] == 0:
  130. # HubDelta[h, a] = 1
  131. # else:
  132. # HubDelta[h, a] = HubStock[h, a] / HubTarget[h, a]
  133.  
  134. # # Оставляем только множества с приоритетными хабами
  135. # if priority == 1:
  136. # ARTCODE_NETWORK = [(h, r, a1) for (h, r, a1) in NETWORK
  137. # if a1 == a and HubDelta[h, a1] > RegionDelta[r, a1] and RegionDelta[r,a] < 1]
  138. # ARTCODE_REGIONS = list(
  139. # set([r for (h, r, a1) in NETWORK if a1 == a and (r, a) in RegionTarget.keys()]))
  140. # ARTCODE_HUBS = list(set([h for (h, r, a1) in ARTCODE_NETWORK if a1 == a]))
  141. # else:
  142. # # Оставляем только множества с второстепенными хабами
  143. # ARTCODE_NETWORK = [(h, r, a1)
  144. # for (h, r, a1) in SECONDARY_NETWORK
  145. # if a1 == a and HubDelta[h, a1] > RegionDelta[r, a1] and RegionDelta[r,a] < 1]
  146. # ARTCODE_REGIONS = list(
  147. # set([r for (h, r, a1) in SECONDARY_NETWORK if a1 == a and (r, a) in RegionTarget.keys()]))
  148. # ARTCODE_HUBS = list(
  149. # set([h for (h, r, a1) in ARTCODE_NETWORK if a1 == a]))
  150.  
  151. # QUANTS = {}
  152. # for (h,r,a1) in ARTCODE_NETWORK:
  153. # max_transfer = math.ceil(RegionTarget[r,a1] - RegionStock[r,a1])
  154. # if max_transfer < BoxMpl[a] * BoxQuantity[a]:
  155. # #QUANTS[h,r,a1] = [i for i in range(1, max_transfer + 1, 1)]
  156. # QUANTS[h,r,a1] = [i for i in range(BoxQuantity[a], max_transfer + BoxQuantity[a], BoxQuantity[a])]
  157. # else:
  158. # #QUANTS[h,r,a1] = [i for i in range(1, BoxMpl[a] * BoxQuantity[a], 1)]
  159. # QUANTS[h,r,a1] = [i * BoxQuantity[a] for i in range(1,BoxMpl[a])]
  160. # if max_transfer < round(PalQuantity[a] * BoxQuantity[a] * PalRounding[a]):
  161. # QUANTS[h,r,a1] += [i for i in range(BoxMpl[a] * BoxQuantity[a], max_transfer + BoxMpl[a] * BoxQuantity[a], BoxMpl[a] * BoxQuantity[a])]
  162. # else:
  163. # QUANTS[h,r,a1] += [i for i in range(BoxMpl[a] * BoxQuantity[a], round(PalQuantity[a] * BoxQuantity[a] * PalRounding[a]), BoxMpl[a] * BoxQuantity[a])]
  164. # QUANTS[h,r,a1] += [i for i in range(PalQuantity[a] * BoxQuantity[a], max_transfer + PalQuantity[a] * BoxQuantity[a], PalQuantity[a] * BoxQuantity[a])]
  165.  
  166. # ARTCODE_NETWORK_QUANTS = [(h,r,a,q) for h,r,a in ARTCODE_NETWORK for q in QUANTS[h,r,a]]
  167.  
  168. # return ARTCODE_NETWORK, ARTCODE_REGIONS, ARTCODE_HUBS, ARTCODE_NETWORK_QUANTS
  169.  
  170.  
  171. # def create_output(model, artcode, priority):
  172.  
  173. # a = artcode
  174. # _, ARTCODE_REGIONS, ARTCODE_HUBS, ARTCODE_NETWORK_QUANTS = artcode_filter(artcode, priority)
  175.  
  176.  
  177. # result_list_1= [
  178. # [
  179. # r,
  180. # a,
  181. # RegionTarget[r, a],
  182. # value(model.region_balance[r]),
  183. # value(model.region_balance[r])/RegionTarget[r, a], (RegionStock[r, a] if (r, a) in RegionStock.keys() else 0),
  184. # priority,
  185. # ]
  186. # for r in ARTCODE_REGIONS
  187. # if RegionTarget[r, a] > 0
  188. # ]
  189.  
  190. # result_tmp_1 = pd.DataFrame(
  191. # result_list_1,
  192. # columns=[
  193. # 'region_nm',
  194. # 'artcode_nm',
  195. # 'region_target',
  196. # 'region_balance',
  197. # 'cover',
  198. # 'stock',
  199. # 'priotity',
  200. # ]
  201. # )
  202.  
  203. # result_list_2= [
  204. # [
  205. # a,
  206. # h,
  207. # r,
  208. # q,
  209. # RegionTarget[r, a],
  210. # RegionStock[r,a],
  211. # priority
  212. # ]
  213. # for (h, r, a1, q) in ARTCODE_NETWORK_QUANTS
  214. # if value(model.transfer[h,r,a,q]) > 0.5
  215. # ]
  216.  
  217. # result_tmp_2 = pd.DataFrame(
  218. # result_list_2,
  219. # columns=[
  220. # 'artcode_nm',
  221. # 'hub_nm', 'region_nm',
  222. # 'transfer',
  223. # 'region_target',
  224. # 'region_stock',
  225. # 'priority',
  226. # ]
  227. # )
  228.  
  229. # if value(model.min_cover) < 1 and priority == 1:
  230.  
  231. # for h in ARTCODE_HUBS:
  232. # HubStock[h,a] = value(model.hub_balance[h])
  233.  
  234. # for r in ARTCODE_REGIONS:
  235. # RegionStock[r,a] = value(model.region_balance[r])
  236.  
  237. # return result_tmp_1, result_tmp_2
  238.  
  239.  
  240. # def create_model(artcode, priority):
  241.  
  242. # a = artcode
  243.  
  244. # # Читаем входные данные с фильтром по основному или альтернативному хабу
  245. # ARTCODE_NETWORK, ARTCODE_REGIONS, ARTCODE_HUBS, ARTCODE_NETWORK_QUANTS = artcode_filter(artcode, priority)
  246.  
  247.  
  248.  
  249. # # Оптимизационная модель
  250. # model = ConcreteModel()
  251.  
  252. # # Переменные решения
  253.  
  254. # # Выбор размера партии с хаба на регион
  255. # model.transfer = Var(ARTCODE_NETWORK_QUANTS, domain=Binary, initialize=0)
  256.  
  257. # # Минимальное покрытие среди регионов
  258. # model.min_cover = Var(domain=NonNegativeReals, bounds=(0,1000), initialize=0)
  259.  
  260. # # Неявные переменные решения
  261.  
  262. # # Баланс на хабе после трансфера
  263. # model.hub_balance = {
  264. # h: (HubStock[h, a] if (h, a) in HubStock.keys() else 0) - sum(model.transfer[h, r, a, q] * q
  265. # for (h1, r, a1, q) in ARTCODE_NETWORK_QUANTS
  266. # if h1 == h)
  267. # for h in ARTCODE_HUBS
  268. # }
  269.  
  270. # # Баланс на регионе после трансфера
  271. # model.region_balance = {
  272. # r: (RegionStock[r, a] if (r, a) in RegionStock.keys() else 0) + sum(model.transfer[h1, r1, a1, q] * q
  273. # for (h1, r1, a1, q) in ARTCODE_NETWORK_QUANTS
  274. # if r1 == r)
  275. # for r in ARTCODE_REGIONS
  276. # }
  277.  
  278. # # Ограничения
  279.  
  280. # # Выбор размера партии
  281. # def con_unique_rule(mod, h, r, a):
  282. # return sum(
  283. # mod.transfer[h1, r1, a1, q1]
  284. # for (h1, r1, a1, q1) in ARTCODE_NETWORK_QUANTS
  285. # if h == h1 and r == r1 and a == a1) <= 1
  286.  
  287. # if len(ARTCODE_NETWORK_QUANTS) > 0:
  288. # model.con_unique = Constraint(
  289. # ARTCODE_NETWORK,
  290. # rule=con_unique_rule,
  291. # )
  292.  
  293.  
  294. # # Минимальный таргет на хабе после трансферов
  295. # def con_min_balance_rule(mod, h):
  296. # return mod.hub_balance[h] >= HubMinTarget[h,a]
  297.  
  298. # if len(ARTCODE_NETWORK_QUANTS) > 0:
  299. # model.con_min_balance = Constraint(
  300. # ARTCODE_HUBS,
  301. # rule=con_min_balance_rule,
  302. # )
  303.  
  304. # # Поиск минимального покрытия на регионах после трансферов
  305. # def con_min_cover_rule(mod, r):
  306. # if RegionTarget[r,a] > 0:
  307. # return mod.min_cover * RegionTarget[r, a] <= mod.region_balance[r]
  308. # else:
  309. # return Constraint.Skip
  310.  
  311. # if len(ARTCODE_NETWORK_QUANTS) > 0:
  312. # model.con_min_cover = Constraint(
  313. # ARTCODE_REGIONS, rule=con_min_cover_rule)
  314.  
  315. # # Целевая функция
  316. # model.OBJ = Objective(expr=model.min_cover, sense=maximize)
  317.  
  318. # return model
  319.  
  320.  
  321. # result_1 = []
  322. # result_2 = []
  323.  
  324. # for a in ARTCODES:
  325. # try:
  326. # model = create_model(a, 1)
  327. # Solver = SolverFactory('appsi_highs')
  328. # SolverResults = Solver.solve(model, tee = True)
  329. # result_tmp_1, result_tmp_2 = create_output(model, a, 1)
  330. # result_1.append(result_tmp_1)
  331. # result_2.append(result_tmp_2)
  332. # if value(model.min_cover) < 1:
  333. # model = create_model(a, 0)
  334. # Solver = SolverFactory('appsi_highs')
  335. # SolverResults = Solver.solve(model, tee = True)
  336. # create_output(model, a, 0)
  337. # result_tmp_1, result_tmp_2 = create_output(model, a, 0)
  338. # result_1.append(result_tmp_1)
  339. # result_2.append(result_tmp_2)
  340. # except Exception as e:
  341. # print(e)
  342.  
  343. # result_1 = pd.concat(result_1)
  344. # result_2 = pd.concat(result_2)
  345.  
  346. # result_1.to_csv(
  347. # set_replenishment_output_file('result_1.csv'),
  348. # index=False,
  349. # )
  350. # result_2.to_csv(
  351. # set_replenishment_output_file('result_2.csv'),
  352. # index=False,
  353. # )
  354.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement