Commit e3f3cb42 authored by Allvey's avatar Allvey

分流配比改进

parent a9d69a45
......@@ -375,24 +375,77 @@ class ExpectedTime(AlgorithmBase):
class DistributionRatio(object):
"""
@date:2022/7/19 8:49
@author:maqc
@para:
truck_id:分流配比模式下,对应的卡车id
excavator_id:该卡车对应的电铲id
@return:{truck_id:unload_area_id}
@desc:计算分流配比模式下,卡车与卸载区的对应关系
class for distributed ratio alg.
"""
# TODO: 统一与预期等待时间的接口
def __init__(self, group, truck, excavator_id):
super().__init__()
def __init__(self, exactor_id, truck):
self.group = group
self.truck = truck
self.exactor_id = exactor_id
self.exactor_truck_num = DispatchInfo.get_exactor_truck_nums(exactor_id) # 获取该电铲下的卡车数量
self.exactor_unload_match = DispatchInfo.get_exactor_unload_match(exactor_id) # 获取电铲对应的所有卸载区id
self.unload_ratio = {}
self.logger = get_logger("zxt.DistributionRatio")
self.excavator_id = excavator_id
self.logger = get_logger("zxt.algorithm.distributed")
def solve(self):
# 获取一段时间时间内累计产量
recent_unload_area_output = self.get_cumulative_production()
# 获取当前正在前往卸载车辆情况
current_heading_to_unload_area_trucks = self.get_current_heading_to_unload_area_truck()
# 计算逻辑累计产量
logic_cumulative_output = {k: recent_unload_area_output[k] + current_heading_to_unload_area_trucks[k]
for k in recent_unload_area_output}
# 获取分流比例
DispatchInfo.get_exactor_unload_match(self.excavator_id)
unload_ratio = {}
for item in self.group.group_unload_areas:
unload_ratio[item] = DispatchInfo.unload_rate_dict[item]
# 逻辑累计产量与分流比例比值
logic_cumulative_output = {k: float(logic_cumulative_output[k]) / unload_ratio[k] for k in logic_cumulative_output}
# 获取比值最小者为目标卸载点
target_unload_area = min(logic_cumulative_output, key=logic_cumulative_output.get)
return target_unload_area
def get_cumulative_production(self):
"""
Get unloading point yield in the past hour.
:return: recent_unload_area_output
"""
recent_unload_area_output = {}
recent_duration = datetime.now() - timedelta(hours=1)
for unload_area_id in self.group.group_unload_areas:
recent_unload_area_output[unload_area_id] = \
len(session_mysql.query(RecordTruckOutput).filter_by(unload_area_id=unload_area_id).all())
return recent_unload_area_output
def get_current_heading_to_unload_area_truck(self):
"""
Get the number of vehicles currently travelling to the unloading point.
"""
current_heading_to_unload_area_trucks = {}
for truck_info in self.group.truck_info_list:
trip = truck_info.get_trip()
if trip in [3, 4]:
heading_unload_area = truck_info.get_combined_unload_area()
current_heading_to_unload_area_trucks[heading_unload_area] += 1
return current_heading_to_unload_area_trucks
def get_unload_ratio(self):
"""
......@@ -521,3 +574,153 @@ class DistributionRatio(object):
self.logger.error("分流配比主函数异常")
self.logger.error(es)
return next_unload_area_id
# class DistributionRatio(object):
# """
# @date:2022/7/19 8:49
# @author:maqc
# @para:
# truck_id:分流配比模式下,对应的卡车id
# excavator_id:该卡车对应的电铲id
# @return:{truck_id:unload_area_id}
# @desc:计算分流配比模式下,卡车与卸载区的对应关系
# """
#
# # TODO: 统一与预期等待时间的接口
#
# def __init__(self, exactor_id, truck):
# self.truck = truck
# self.exactor_id = exactor_id
# self.exactor_truck_num = DispatchInfo.get_exactor_truck_nums(exactor_id) # 获取该电铲下的卡车数量
# self.exactor_unload_match = DispatchInfo.get_exactor_unload_match(exactor_id) # 获取电铲对应的所有卸载区id
# self.unload_ratio = {}
# self.logger = get_logger("zxt.DistributionRatio")
#
# def get_unload_ratio(self):
# """
# @date:2022/7/19 15:21
# @author:maqc
# @desc:初始化卸载区的比值
# """
# try:
# ratio = 0
# for value in self.exactor_unload_match:
# ratio += DispatchInfo.unload_rate_dict[value]
# for item in self.exactor_unload_match:
# self.unload_ratio[item] = DispatchInfo.unload_rate_dict[item] / ratio
#
# except Exception as es:
# self.logger.error("初始化卸载区的比值异常")
# self.logger.error(es)
#
# def get_unload_truck_total_num(self, exactor_id):
# """
# @date:2022/7/19 16:12
# @author:maqc
# @desc:计算正在驶往全部卸载区的卡车数量
# """
#
# unload_area_truck_num = 0
#
# for item in session_mysql.query(EquipmentPair).filter_by(exactor_id=exactor_id, isdeleted=0).all():
# try:
# truck_id = item.truck_id
# task = self.truck.get_truck_current_task()[truck_id]
# if task in [3, 4]:
# unload_area_truck_num = unload_area_truck_num + 1
# except Exception as es:
# self.logger.error("获取驶往卸载点全部车辆数量异常")
# self.logger.error(es)
#
# # item = session_mysql.query(EquipmentPair).filter_by(unload_area_id=unload_area_id, isdeleted=0).all()
# return unload_area_truck_num
#
# def get_unload_truck_num(self, unload_area_id):
# """
# @date:2022/7/19 16:12
# @author:maqc
# @desc:计算正在派往当前卸载区的卡车数量
# """
#
# unload_area_truck_num = 0
#
# for item in session_mysql.query(EquipmentPair).filter_by(unload_area_id=unload_area_id, isdeleted=0).all():
# try:
# truck_id = item.truck_id
# task = self.truck.get_truck_current_task()[truck_id]
# if task in [3, 4]:
# unload_area_truck_num = unload_area_truck_num + 1
# except Exception as es:
# self.logger.error(f'获取驶往卸载点 {item.unload_area_id} 车辆异常')
# self.logger.error(es)
#
# # item = session_mysql.query(EquipmentPair).filter_by(unload_area_id=unload_area_id, isdeleted=0).all()
# return unload_area_truck_num
#
# def get_next_unload_id(self):
# """
# @date:2022/7/19 15:39
# @author:maqc
# @desc:计算卡车与下一个卸载区的对应关系
# """
#
# next_unload_id = None
#
# try:
#
# self.logger.info("卸载设置分流配比")
# self.logger.info(self.unload_ratio)
# # 无论当前卡车是否在备停区,下一个卸载区选择比值最大值对应的区域
# for dump_id in self.unload_ratio.keys():
# self.unload_ratio[dump_id] -= ((self.get_unload_truck_num(dump_id) + 0.001) /
# (self.get_unload_truck_total_num(self.exactor_id) + 0.001))
#
# self.logger.info(dump_id)
# self.logger.info(
# f'unload_truck_total_num {self.get_unload_truck_total_num(self.exactor_id)}')
# self.logger.info(f'unload_truck_num {self.get_unload_truck_num(dump_id)}')
#
# self.logger.info("卸载实时分流配比")
# self.logger.info(self.unload_ratio)
#
# temp = list(self.unload_ratio.values())
# next_unload_id = list(self.unload_ratio.keys())[temp.index(max(temp))]
#
# self.logger.info(self.unload_ratio.keys())
# self.logger.info(temp)
# self.logger.info(f'下一个卸载区对应的id {next_unload_id}')
#
# except Exception as es:
# self.logger.error("卡车与下一个卸载区的配对关系异常")
# self.logger.error(es)
#
# # try:
# # temp = list(self.unload_ratio.values())
# # next_unload_id = list(self.unload_ratio.keys())[temp.index(max(temp))]
# # # 更新卸载区对应的比例值
# # self.logger.info(f"下一个卸载区对应的id:{next_unload_id}")
# # self.unload_ratio[next_unload_id] -= self.get_unload_truck_num(
# # next_unload_id) / self.exactor_truck_num
# # self.logger.info(f"当前卸载区对应的比值:{self.unload_ratio[next_unload_id]}")
# # except Exception as es:
# # self.logger.error("卡车与下一个卸载区的配对关系异常")
# # self.logger.error(es)
# return next_unload_id
#
# def ratio_main(self):
# """
# @date:2022/7/19 9:10
# @author:maqc
# @desc:分流配比模式类函数主入口
# """
# try:
# self.get_unload_ratio()
# self.logger.info("卸载区分流比例")
# self.logger.info(self.unload_ratio)
# next_unload_area_id = self.get_next_unload_id()
# self.logger.info(f'当前卡车对应的卸载区:{next_unload_area_id}')
# except Exception as es:
# self.logger.error("分流配比主函数异常")
# self.logger.error(es)
# return next_unload_area_id
......@@ -31,10 +31,10 @@ class Dispatcher:
self.request_mode = request_mode
self.logger = get_logger("zxt.dispatcher")
self.redispatch_active = active
if self.redispatch_active:
self.topo = Topo()
self.topo.generate_topo_graph()
else:
# if self.redispatch_active:
# self.topo = Topo()
# self.topo.generate_topo_graph()
# else:
self.topo = None
self.submission = DispatchSubmission(dump, excavator, truck, self.topo)
......
......@@ -443,11 +443,10 @@ class GroupDispatcher:
except Exception as es:
self.logger.error("固定派车-计算异常")
self.logger.error(es)
# 分流配比模式
elif self.group.group_mode == 4:
self.logger.info("调度模式:分流配比模式调度")
self.ratio_mode(truck_id, truck_dispatch, current_truck.get_task())
self.ratio_mode(truck_id, truck_dispatch, current_truck)
self.logger.info(
f'============================= 车辆调度结束 {current_truck.get_name()} =============================')
......@@ -461,11 +460,18 @@ class GroupDispatcher:
return truck_dispatch
def ratio_mode(self, i, truck_dispatch, truck_task):
next_exactor_id = DispatchInfo.get_truck_exactor(i) # 获取该卡车对应的exactor_id
def ratio_mode(self, i, truck_dispatch, truck_info):
solver = DistributionRatio(self.group, truck_info, truck_info.get_combined_excavator())
next_exactor_id = truck_info.get_combined_excavator()
# next_exactor_id = DispatchInfo.get_truck_exactor(i) # 获取该卡车对应的exactor_id
self.logger.info(f"分流配比模式,对应的卡车:{i}")
self.logger.info(f"矿卡对应的铲车:{next_exactor_id}")
next_unload_area_id = None
truck_task = truck_info.get_trip()
if truck_task == -2:
next_unload_area_id = "Park"
......@@ -480,12 +486,13 @@ class GroupDispatcher:
next_unload_area_id = dump_uuid_to_unload_area_uuid_dict[self.group.truck.truck_dump_bind[i]]
else:
next_unload_area_id = DistributionRatio(next_exactor_id, self.group.truck).ratio_main()
# next_unload_area_id = DistributionRatio(next_exactor_id, self.group.truck).ratio_main()
next_unload_area_id = solver.solve()
except Exception as es:
self.logger.error("分流配比模式-->>空载车辆计算异常")
self.logger.error(es)
# 重载模式下,按照固定派车进行计算
......
......@@ -141,10 +141,6 @@ class DumpInfo(WalkManage):
try:
unload_area_index = self.dump_index_to_unload_area_index_dict[self.dump_uuid_to_index_dict[dump_id]]
unload_area_id = unload_area_index_to_uuid_dict[unload_area_index]
self.logger.debug("update_dump_priority-unload_area_index")
self.logger.debug(unload_area_index)
self.logger.debug("update_dump_priority-unload_area_id")
self.logger.debug(unload_area_id)
item = session_postgre.query(DumpArea).filter_by(Id=unload_area_id).first()
self.dump_priority_coefficient[self.dump_uuid_to_index_dict[dump_id]] += item.Priority
except Exception as es:
......@@ -152,6 +148,9 @@ class DumpInfo(WalkManage):
self.logger.error(es)
session_postgre.rollback()
self.logger.info('卸载点优先级更新')
self.logger.info(self.dump_priority_coefficient)
def update_unload_ability(self):
unload_area_index_to_uuid_dict = get_value("unload_area_index_to_uuid_dict")
try:
......
......@@ -149,30 +149,33 @@ class ExcavatorInfo(WalkManage):
for excavator_id in get_value("dynamic_excavator_set"):
try:
item = session_mysql.query(Equipment).filter_by(id=excavator_id).first()
# self.logger.info("excavator_priority_coefficient")
# self.logger.info(self.excavator_priority_coefficient)
# self.excavator_priority_coefficient[self.excavator_uuid_to_index_dict[excavator_id]] = item.priority + 1
# 物料优先级控制
rule = 2
rule7 = session_mysql.query(DispatchRule).filter_by(id=7).first()
material_priority_use = rule7.disabled
if material_priority_use == 0:
rule = rule7.rule_weight
if rule == 3:
if self.excavator_material[excavator_id] == 'e56eb559-a746-4cc4-8ada-ebf9819fbe27':
self.excavator_material_priority[self.excavator_uuid_to_index_dict[excavator_id]] = 5
elif rule == 1:
if self.excavator_material[excavator_id] == '81bb175d-50fe-4be3-937e-6791ac4d6fec':
self.excavator_material_priority[self.excavator_uuid_to_index_dict[excavator_id]] = 5
self.logger.info("excavator_priority_coefficient")
self.logger.info(self.excavator_priority_coefficient)
self.excavator_priority_coefficient[self.excavator_uuid_to_index_dict[excavator_id]] = item.priority + 1
# # 物料优先级控制
# rule = 2
# rule7 = session_mysql.query(DispatchRule).filter_by(id=7).first()
#
# material_priority_use = rule7.disabled
# if material_priority_use == 0:
# rule = rule7.rule_weight
#
# if rule == 3:
# if self.excavator_material[excavator_id] == 'e56eb559-a746-4cc4-8ada-ebf9819fbe27':
# self.excavator_material_priority[self.excavator_uuid_to_index_dict[excavator_id]] = 5
# elif rule == 1:
# if self.excavator_material[excavator_id] == '81bb175d-50fe-4be3-937e-6791ac4d6fec':
# self.excavator_material_priority[self.excavator_uuid_to_index_dict[excavator_id]] = 5
except Exception as es:
self.logger.error("物料更新异常")
self.logger.error("挖机优先级更新异常")
self.logger.error(es)
session_postgre.rollback()
session_mysql.rollback()
self.logger.info('挖机优先级更新')
self.logger.info(self.excavator_priority_coefficient)
def update_mining_ability(self):
try:
try:
......@@ -229,13 +232,12 @@ class ExcavatorInfo(WalkManage):
try:
rule7 = session_mysql.query(DispatchRule).filter_by(id=7).first().disabled
self.logger.info("物料优先级规则")
self.logger.info(rule7)
except Exception as es:
session_postgre.rollback()
session_mysql.rollback()
self.logger.info("物料优先级规则")
self.logger.info(rule7)
# # 用于动态调度的挖机设备
# self.dynamic_excavator_set = set(update_autodisp_excavator())
#
......
......@@ -485,6 +485,7 @@ class DispatchMatch(Base):
self.exactor_id = exactor_id
self.unload_area_id = unload_area_id
class DispatchEquipment(Base):
__tablename__ = 'sys_dispatch_equipment'
id = Column(VARCHAR(36), primary_key=True)
......@@ -501,3 +502,17 @@ class DispatchEquipment(Base):
self.match_code = match_code
self.isdeleted = isdeleted
self.equipment_id = equipment_id
class RecordTruckOutput(Base):
__tablename__ = 'record_truck_output'
end_dump_time = Column(DateTime)
id = Column(Integer, primary_key=True)
unload_area_id = Column(VARCHAR(36))
equipment_id = Column(VARCHAR(36))
def __init__(self, id, unload_area_id, equipment_id, end_dump_time):
self.id = id
self.end_dump_time = end_dump_time
self.unload_area_id = unload_area_id
self.equipment_id = equipment_id
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment