HTSim/OrderSystem.py

105 lines
5.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import agentpy as ap
import numpy as np
from Order import Order
import pandas as pd
# 输出一个生产状态指令,一个交付情况,一个延期时间计算
class OrderSystem(ap.Agent):
a_lst_order: ap.AgentList[Order]
ev_int_produce_type: int
ev_ary_to_dlv: np.ndarray # 当前order list内所有产品的综合
ev_ary_product_to_produce: np.ndarray # 用于计算 production gap
ev_ave_delay_time: float
xv_ary_plan: np.ndarray
ev_changed_product: np.ndarray
xv_product_num: int
xv_ary_product: np.ndarray
# ev_ary_dlv_product: np.ndarray??????
def setup(self, xv_product_num, xv_ary_plan, xv_ary_product_id):
# Create a list of order
self.a_lst_order = ap.AgentList(self, []) #
self.ev_ary_to_dlv = np.zeros((xv_product_num,))
self.ev_int_produce_type = 0 # 生产状态(方案)
self.ev_ary_product_to_produce = np.zeros((xv_product_num,))
self.ev_ave_delay_time = 0
# self.ev_ary_dlv_product = np.zeros((23,)) ????
self.ev_changed_product = np.zeros((xv_product_num,))
self.xv_ary_plan = xv_ary_plan
self.xv_product_num = xv_product_num
self.xv_ary_product = xv_ary_product_id
def accept_order(self, new_order):
# Determine whether the order is received and all are currently received
if new_order is not None:
new_order.ev_is_accepted = True
self.a_lst_order.append(new_order)
def rank_order(self):
# Sort order
self.a_lst_order = self.a_lst_order.sort('xv_dlv_t', reverse=False) # 按预期交付时间排序
def produce_status(self):
# 计算a_lst_order内所有订单包含的产品总量与当期库存的gap排序最大缺口量选定生产状态
self.ev_ary_to_dlv = np.zeros((self.xv_product_num,)) # 产品需求总和
for order in self.a_lst_order:
self.ev_ary_to_dlv += order.ev_ary_dlv_product # 当前天之前所有的还未满足的需求求和
self.ev_ary_product_to_produce = self.ev_ary_to_dlv - self.model.the_firm.the_is.ev_ary_current_product[:, 1]
# self.ev_ary_product_to_produce = self.model.the_firm.the_is.ev_ary_current_product - self.ev_ary_to_dlv
# self.ev_ary_product_to_produce > 0:
# 选出这些产品按照数值大小进行分类数值最大的产品对应的能带来最大生产率的状态则选定为ev_int_produce_type
# 如果均<=0, 按照产品库存ev_ary_current_material进行排序选择能给库存最低的产品带来最高生产率的状态
sorted_indices = np.argsort(self.ev_ary_product_to_produce)[::-1]
sorted_data = self.xv_ary_product[sorted_indices] # gap从大到小的产品id
gap_sorted = self.ev_ary_product_to_produce[sorted_indices]
if gap_sorted[0] > 0: # 判断是否存在库存不足
pass
else:
sorted_indices = np.argsort(self.model.the_firm.the_is.ev_ary_current_product[:, 1]) # 对库存进行从小到大排序
sorted_data = self.model.the_firm.the_is.ev_ary_current_product[:, sorted_indices][:, 0] # 库存从小到大的产品id
option = self.xv_ary_plan[self.xv_ary_plan[:, 1] == sorted_data[0]] # 检索最大值的产品的四种方案或者最小库存产品的四种方案
sorted_indices = np.argsort(option[:, 3])[::-1]
sorted_data = option[sorted_indices]
self.ev_int_produce_type = sorted_data[0, 0]
# return self.ev_int_produce_type
def do_shipment(self, ev_ary_current_product):
# Make shipments based on ranked order list
# 交付两次第一次未交付完成的订单要标记交货的时候先便利delay的订单如果能够满足全部剩余量就交货如果不能
# 就继续在列表中存在
# 只有完全交付的订单才计算delay time = order.ev_int_delay_time * 第二次交付的订单内的各类产品之和
# 需要更新 order.ev_ary_dlv_product
# Make shipments based on ranked order list
self.ev_ave_delay_time = 0
self.ev_changed_product = ev_ary_current_product[:, 1] # 23x1 ndarray 存储本次库存的改变
for order in self.a_lst_order:
if order.xv_dlv_t == self.model.t: # 第一次交付
# Check and make shipment
order.ev_is_delivered = True
for i in range(self.xv_product_num):
if order.ev_ary_dlv_product[i] <= self.ev_changed_product[i]:
self.ev_changed_product[i] -= order.ev_ary_dlv_product[i]
order.ev_ary_dlv_product[i] = 0
else:
order.ev_is_delivered = False
elif order.xv_dlv_t < self.model.t and order.ev_is_delivered == False: # 第二次交付
order.ev_is_delivered = True
for i in range(self.xv_product_num):
if order.ev_ary_dlv_product[i] > self.ev_changed_product[i]: # 先判断能不能一次性交付
order.ev_is_delivered = False
if order.ev_is_delivered: # 如果一次性交付
delay_num = np.sum(order.ev_ary_dlv_product)
self.ev_changed_product = self.ev_changed_product - order.ev_ary_dlv_product
order.ev_ary_dlv_product = np.zeros((self.xv_product_num,))
order.ev_actual_dlv_t = self.model.t
order.ev_int_delay_time = order.ev_actual_dlv_t - order.xv_dlv_t
self.ev_ave_delay_time += order.ev_int_delay_time * delay_num / 10000
# return self.ev_changed_product, self.ev_ave_delay_time