Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import pandas as pd
- from pyomo.environ import *
- from celery import shared_task
- from forecasts.clickhouse.db import get_session
- from .utils import get_replenishment_inputs_file
- from .utils import set_replenishment_output_file
- # @shared_task(name="replen_optimizer.tasks.default.task_start_parsing_input_replenishment_files")
- # def task_start_parsing_input_replenishment_files():
- # # Считываем данные
- # artcodes_data = pd.read_csv(
- # get_replenishment_inputs_file('in_artcodes.csv'),
- # index_col=['artcode_nm'],
- # )
- # regions_data = pd.read_csv(
- # get_replenishment_inputs_file('in_regions.csv'),
- # index_col=['region_nm'],
- # )
- # hubs_data = pd.read_csv(
- # get_replenishment_inputs_file('in_hubs.csv'),
- # index_col=['hub_nm'],
- # )
- # primary_network_data = pd.read_csv(
- # get_replenishment_inputs_file('in_network.csv'),
- # index_col=['hub_nm', 'region_nm', 'artcode_nm'],
- # )
- # secondary_network_data = pd.read_csv(
- # get_replenishment_inputs_file('in_network.csv')
- # )
- # secondary_network_data = secondary_network_data.dropna().set_index(
- # ['hub_sec priority', 'region_nm', 'artcode_nm']
- # )
- # region_stocks_data = pd.read_csv(
- # get_replenishment_inputs_file('in_region_stocks.csv'),
- # index_col=['region_nm', 'artcode_nm'],
- # )
- # hub_stocks_data = pd.read_csv(
- # get_replenishment_inputs_file('in_hub_stocks.csv'),
- # index_col=['hub_nm', 'artcode_nm'],
- # )
- # region_target_data = pd.read_csv(
- # get_replenishment_inputs_file('in_target_reg.csv'),
- # index_col=['region_nm', 'artcode_nm'],
- # )
- # hub_target_data = pd.read_csv(
- # get_replenishment_inputs_file('in_target_hub.csv'),
- # index_col=['hub_nm', 'artcode_nm'],
- # )
- # ARTCODES = list(artcodes_data.index.values)
- # BoxQuantity = artcodes_data['box_quantity'].to_dict()
- # PalQuantity = artcodes_data['pal_quantity'].to_dict()
- # PalRounding = artcodes_data['pal_rounding'].to_dict()
- # BoxMpl = artcodes_data['box_mpl'].to_dict()
- # #КОСТЫЛЬ
- # for a in ARTCODES:
- # if PalQuantity[a] == 0:
- # PalQuantity[a] = 1
- # if BoxQuantity[a] == 0:
- # BoxQuantity[a] = 1
- # REGIONS = list(regions_data.index.values)
- # HUBS = list(hubs_data.index.values)
- # NETWORK = list(primary_network_data.index.values)
- # SECONDARY_NETWORK = list(secondary_network_data.index.values)
- # FULL_NETWORK = NETWORK + SECONDARY_NETWORK
- # RegionDelta = {}
- # HubDelta = {}
- # RegionStock = region_stocks_data['region_stock'].to_dict()
- # HubStock = hub_stocks_data['hub_stock'].to_dict()
- # for a in ARTCODES:
- # for h in HUBS:
- # if (h, a) not in HubStock.keys():
- # HubStock[h, a] = 0
- # for a in ARTCODES:
- # for r in REGIONS:
- # if (r, a) not in RegionStock.keys():
- # RegionStock[r, a] = 0
- # RegionTarget = region_target_data['target_stock_reg'].to_dict()
- # HubTarget = hub_target_data['target_stock_hub'].to_dict()
- # HubMinTarget = hub_target_data['min_target_stock'].to_dict()
- # def artcode_filter(a, priority):
- # for r in REGIONS:
- # if (r, a) not in RegionStock.keys():
- # RegionStock[r, a] = 0
- # if RegionTarget[r,a] < 0:
- # RegionTarget[r,a] = 0
- # if (r, a) not in RegionTarget.keys() or RegionTarget[r, a] == 0:
- # RegionDelta[r, a] = 1
- # else:
- # RegionDelta[r, a] = RegionStock[r, a] / RegionTarget[r, a]
- # for h in HUBS:
- # if (h, a) not in HubStock.keys():
- # HubStock[h, a] = 0
- # if (h, a) not in HubTarget.keys() or HubTarget[h, a] == 0:
- # HubDelta[h, a] = 1
- # else:
- # HubDelta[h, a] = HubStock[h, a] / HubTarget[h, a]
- # # Оставляем только множества с приоритетными хабами
- # if priority == 1:
- # ARTCODE_NETWORK = [(h, r, a1) for (h, r, a1) in NETWORK
- # if a1 == a and HubDelta[h, a1] > RegionDelta[r, a1] and RegionDelta[r,a] < 1]
- # ARTCODE_REGIONS = list(
- # set([r for (h, r, a1) in NETWORK if a1 == a and (r, a) in RegionTarget.keys()]))
- # ARTCODE_HUBS = list(set([h for (h, r, a1) in ARTCODE_NETWORK if a1 == a]))
- # else:
- # # Оставляем только множества с второстепенными хабами
- # ARTCODE_NETWORK = [(h, r, a1)
- # for (h, r, a1) in SECONDARY_NETWORK
- # if a1 == a and HubDelta[h, a1] > RegionDelta[r, a1] and RegionDelta[r,a] < 1]
- # ARTCODE_REGIONS = list(
- # set([r for (h, r, a1) in SECONDARY_NETWORK if a1 == a and (r, a) in RegionTarget.keys()]))
- # ARTCODE_HUBS = list(
- # set([h for (h, r, a1) in ARTCODE_NETWORK if a1 == a]))
- # QUANTS = {}
- # for (h,r,a1) in ARTCODE_NETWORK:
- # max_transfer = math.ceil(RegionTarget[r,a1] - RegionStock[r,a1])
- # if max_transfer < BoxMpl[a] * BoxQuantity[a]:
- # #QUANTS[h,r,a1] = [i for i in range(1, max_transfer + 1, 1)]
- # QUANTS[h,r,a1] = [i for i in range(BoxQuantity[a], max_transfer + BoxQuantity[a], BoxQuantity[a])]
- # else:
- # #QUANTS[h,r,a1] = [i for i in range(1, BoxMpl[a] * BoxQuantity[a], 1)]
- # QUANTS[h,r,a1] = [i * BoxQuantity[a] for i in range(1,BoxMpl[a])]
- # if max_transfer < round(PalQuantity[a] * BoxQuantity[a] * PalRounding[a]):
- # QUANTS[h,r,a1] += [i for i in range(BoxMpl[a] * BoxQuantity[a], max_transfer + BoxMpl[a] * BoxQuantity[a], BoxMpl[a] * BoxQuantity[a])]
- # else:
- # QUANTS[h,r,a1] += [i for i in range(BoxMpl[a] * BoxQuantity[a], round(PalQuantity[a] * BoxQuantity[a] * PalRounding[a]), BoxMpl[a] * BoxQuantity[a])]
- # QUANTS[h,r,a1] += [i for i in range(PalQuantity[a] * BoxQuantity[a], max_transfer + PalQuantity[a] * BoxQuantity[a], PalQuantity[a] * BoxQuantity[a])]
- # ARTCODE_NETWORK_QUANTS = [(h,r,a,q) for h,r,a in ARTCODE_NETWORK for q in QUANTS[h,r,a]]
- # return ARTCODE_NETWORK, ARTCODE_REGIONS, ARTCODE_HUBS, ARTCODE_NETWORK_QUANTS
- # def create_output(model, artcode, priority):
- # a = artcode
- # _, ARTCODE_REGIONS, ARTCODE_HUBS, ARTCODE_NETWORK_QUANTS = artcode_filter(artcode, priority)
- # result_list_1= [
- # [
- # r,
- # a,
- # RegionTarget[r, a],
- # value(model.region_balance[r]),
- # value(model.region_balance[r])/RegionTarget[r, a], (RegionStock[r, a] if (r, a) in RegionStock.keys() else 0),
- # priority,
- # ]
- # for r in ARTCODE_REGIONS
- # if RegionTarget[r, a] > 0
- # ]
- # result_tmp_1 = pd.DataFrame(
- # result_list_1,
- # columns=[
- # 'region_nm',
- # 'artcode_nm',
- # 'region_target',
- # 'region_balance',
- # 'cover',
- # 'stock',
- # 'priotity',
- # ]
- # )
- # result_list_2= [
- # [
- # a,
- # h,
- # r,
- # q,
- # RegionTarget[r, a],
- # RegionStock[r,a],
- # priority
- # ]
- # for (h, r, a1, q) in ARTCODE_NETWORK_QUANTS
- # if value(model.transfer[h,r,a,q]) > 0.5
- # ]
- # result_tmp_2 = pd.DataFrame(
- # result_list_2,
- # columns=[
- # 'artcode_nm',
- # 'hub_nm', 'region_nm',
- # 'transfer',
- # 'region_target',
- # 'region_stock',
- # 'priority',
- # ]
- # )
- # if value(model.min_cover) < 1 and priority == 1:
- # for h in ARTCODE_HUBS:
- # HubStock[h,a] = value(model.hub_balance[h])
- # for r in ARTCODE_REGIONS:
- # RegionStock[r,a] = value(model.region_balance[r])
- # return result_tmp_1, result_tmp_2
- # def create_model(artcode, priority):
- # a = artcode
- # # Читаем входные данные с фильтром по основному или альтернативному хабу
- # ARTCODE_NETWORK, ARTCODE_REGIONS, ARTCODE_HUBS, ARTCODE_NETWORK_QUANTS = artcode_filter(artcode, priority)
- # # Оптимизационная модель
- # model = ConcreteModel()
- # # Переменные решения
- # # Выбор размера партии с хаба на регион
- # model.transfer = Var(ARTCODE_NETWORK_QUANTS, domain=Binary, initialize=0)
- # # Минимальное покрытие среди регионов
- # model.min_cover = Var(domain=NonNegativeReals, bounds=(0,1000), initialize=0)
- # # Неявные переменные решения
- # # Баланс на хабе после трансфера
- # model.hub_balance = {
- # h: (HubStock[h, a] if (h, a) in HubStock.keys() else 0) - sum(model.transfer[h, r, a, q] * q
- # for (h1, r, a1, q) in ARTCODE_NETWORK_QUANTS
- # if h1 == h)
- # for h in ARTCODE_HUBS
- # }
- # # Баланс на регионе после трансфера
- # model.region_balance = {
- # r: (RegionStock[r, a] if (r, a) in RegionStock.keys() else 0) + sum(model.transfer[h1, r1, a1, q] * q
- # for (h1, r1, a1, q) in ARTCODE_NETWORK_QUANTS
- # if r1 == r)
- # for r in ARTCODE_REGIONS
- # }
- # # Ограничения
- # # Выбор размера партии
- # def con_unique_rule(mod, h, r, a):
- # return sum(
- # mod.transfer[h1, r1, a1, q1]
- # for (h1, r1, a1, q1) in ARTCODE_NETWORK_QUANTS
- # if h == h1 and r == r1 and a == a1) <= 1
- # if len(ARTCODE_NETWORK_QUANTS) > 0:
- # model.con_unique = Constraint(
- # ARTCODE_NETWORK,
- # rule=con_unique_rule,
- # )
- # # Минимальный таргет на хабе после трансферов
- # def con_min_balance_rule(mod, h):
- # return mod.hub_balance[h] >= HubMinTarget[h,a]
- # if len(ARTCODE_NETWORK_QUANTS) > 0:
- # model.con_min_balance = Constraint(
- # ARTCODE_HUBS,
- # rule=con_min_balance_rule,
- # )
- # # Поиск минимального покрытия на регионах после трансферов
- # def con_min_cover_rule(mod, r):
- # if RegionTarget[r,a] > 0:
- # return mod.min_cover * RegionTarget[r, a] <= mod.region_balance[r]
- # else:
- # return Constraint.Skip
- # if len(ARTCODE_NETWORK_QUANTS) > 0:
- # model.con_min_cover = Constraint(
- # ARTCODE_REGIONS, rule=con_min_cover_rule)
- # # Целевая функция
- # model.OBJ = Objective(expr=model.min_cover, sense=maximize)
- # return model
- # result_1 = []
- # result_2 = []
- # for a in ARTCODES:
- # try:
- # model = create_model(a, 1)
- # Solver = SolverFactory('appsi_highs')
- # SolverResults = Solver.solve(model, tee = True)
- # result_tmp_1, result_tmp_2 = create_output(model, a, 1)
- # result_1.append(result_tmp_1)
- # result_2.append(result_tmp_2)
- # if value(model.min_cover) < 1:
- # model = create_model(a, 0)
- # Solver = SolverFactory('appsi_highs')
- # SolverResults = Solver.solve(model, tee = True)
- # create_output(model, a, 0)
- # result_tmp_1, result_tmp_2 = create_output(model, a, 0)
- # result_1.append(result_tmp_1)
- # result_2.append(result_tmp_2)
- # except Exception as e:
- # print(e)
- # result_1 = pd.concat(result_1)
- # result_2 = pd.concat(result_2)
- # result_1.to_csv(
- # set_replenishment_output_file('result_1.csv'),
- # index=False,
- # )
- # result_2.to_csv(
- # set_replenishment_output_file('result_2.csv'),
- # index=False,
- # )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement