This commit is contained in:
Yofuria 2023-07-25 21:32:08 +08:00
parent 49020043db
commit 1229d9e3c2
15 changed files with 249 additions and 191 deletions

View File

@ -2,7 +2,10 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.10 (2)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="C:\Users\Yofuria\miniconda3" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="PyDocumentationSettings">
<option name="renderExternalDocumentation" value="true" />
</component>
</module> </module>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (2)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="C:\Users\Yofuria\miniconda3" project-jdk-type="Python SDK" />
</project> </project>

6
.idea/other.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PySciProjectComponent">
<option name="PY_SCI_VIEW_SUGGESTED" value="true" />
</component>
</project>

View File

@ -18,7 +18,7 @@ class FMSEnv(ap.Model):
# xv_int_max_order: int # xv_int_max_order: int
# ev_n_order_created: int # ev_n_order_created: int
the_firm: Firm the_firm: Firm # Firm类
# record data, define below # record data, define below
# op_os_n_total_order: int # op_os_n_total_order: int
@ -28,7 +28,7 @@ class FMSEnv(ap.Model):
# op_is_flt_material_room_left: float # op_is_flt_material_room_left: float
# op_is_flt_product_room_left: float # op_is_flt_product_room_left: float
op_ps_str_status: str op_os_int_status: int
op_os_to_dlv: np.ndarray op_os_to_dlv: np.ndarray
op_is_current_product: np.ndarray op_is_current_product: np.ndarray
op_is_current_material: np.ndarray op_is_current_material: np.ndarray
@ -39,6 +39,7 @@ class FMSEnv(ap.Model):
op_ip_prd_s: np.ndarray op_ip_prd_s: np.ndarray
op_ip_prd_big_s: np.ndarray op_ip_prd_big_s: np.ndarray
op_ip_prd_est_pfm: int op_ip_prd_est_pfm: int
op_os_n_total_order: int
def __init__(self, dct_all_para, _run_id=None): def __init__(self, dct_all_para, _run_id=None):
super().__init__() super().__init__()
@ -47,11 +48,13 @@ class FMSEnv(ap.Model):
self.the_firm = Firm(env=self, dct_all_para=dct_all_para) self.the_firm = Firm(env=self, dct_all_para=dct_all_para)
# get the input parameter values from the dictionary # get the input parameter values from the dictionary
self.int_stop_time = int(dct_all_para['time']) self.int_stop_time = int(dct_all_para['time']) # 停止接单时间
# self.xv_int_max_order = int(dct_all_para['xv_int_max_order']) # self.xv_int_max_order = int(dct_all_para['xv_int_max_order'])
self.xv_dlv_product_para = np.asarray(dct_all_para['xv_dlv_product_para']) # self.xv_dlv_product_para = np.asarray(dct_all_para['xv_dlv_product_para'])
# self.xv_int_dlv_period_lam = int(dct_all_para['xv_int_dlv_period_lam']) # self.xv_int_dlv_period_lam = int(dct_all_para['xv_int_dlv_period_lam'])
# self.ev_n_order_created = 0 # self.ev_n_order_created = 0
self.op_os_n_total_order = 0
self.op_os_int_status = 0
self.running = True self.running = True
self.t = 0 self.t = 0
@ -62,17 +65,16 @@ class FMSEnv(ap.Model):
xv_int_create_order_num = 1 xv_int_create_order_num = 1
# xv_int_create_order_num = random.poisson(lam=xv_int_create_order_lam, size=None) # xv_int_create_order_num = random.poisson(lam=xv_int_create_order_lam, size=None)
# if self.ev_n_order_created < xv_int_max_order: # if self.ev_n_order_created < xv_int_max_order:
for i in range(xv_int_create_order_num): # for i in range(xv_int_create_order_num):
new_order = Order(model=self, time_created=self.t, xv_dlv_product_para=self.xv_dlv_product_para, new_order = Order(model=self, time_created=self.t)
leadtime=max(self.the_firm.xv_ary_lead_time))
return new_order return new_order
return None # return None
# Execute the interactions of each time step in the simulation. # Execute the interactions of each time step in the simulation.
def step(self): def step(self):
# organize the interactions of agents in each time step here # organize the interactions of agents in each time step here
new_order = self.create_order() new_order = self.create_order() # 接收创建的订单
self.the_firm.the_os.accept_order(new_order) self.the_firm.the_os.accept_order(new_order=new_order)
self.the_firm.operating() self.the_firm.operating()
self.update() self.update()
@ -82,16 +84,17 @@ class FMSEnv(ap.Model):
self.stop() self.stop()
else: else:
print(f"running the {self.t} step") print(f"running the {self.t} step")
print(self.the_firm.the_os.ev_ave_delay_time)
# Record data after each simulation # Record data after each simulation
def update(self): def update(self): # ?
self.op_os_n_total_order = len(self.the_firm.the_os.a_lst_order) self.op_os_n_total_order = len(self.the_firm.the_os.a_lst_order) # 订单个数
# self.op_os_n_total_order_delayed = len([e for e in self.the_firm.the_os.a_lst_order if e.xv_dlv_t < self.t]) # self.op_os_n_total_order_delayed = len([e for e in self.the_firm.the_os.a_lst_order if e.xv_dlv_t < self.t])
self.op_os_to_dlv = self.the_firm.the_os.ev_ary_to_dlv self.op_os_to_dlv = self.the_firm.the_os.ev_ary_to_dlv # 当期及之前未满足需求总和
# self.op_os_all_delay_time = self.the_firm.the_os.ev_lst_all_delay_time # self.op_os_all_delay_time = self.the_firm.the_os.ev_lst_all_delay_time
self.op_ps_produced_num = self.the_firm.the_ps.ev_ary_produced_num self.op_ps_produced_num = self.the_firm.the_ps.ev_ary_produced_num # 当期产品生产数量
self.op_ps_str_status = self.the_firm.the_ps.ev_str_status self.op_ps_str_status = self.the_firm.the_os.ev_int_produce_type # 当期生产状态
# self.op_is_current_product = self.model.the_firm.the_is.ev_ary_current_product # self.op_is_current_product = self.model.the_firm.the_is.ev_ary_current_product
# #
@ -101,13 +104,14 @@ class FMSEnv(ap.Model):
self.op_ps_back_trans_material = self.model.the_firm.the_ps.ev_lst_backtrans_material self.op_ps_back_trans_material = self.model.the_firm.the_ps.ev_lst_backtrans_material
self.record([att for att in self.__dict__.keys() if att.startswith('op_')]) self.record([att for att in self.__dict__.keys() if att.startswith('op_')]) # ?
pass
# pass
if __name__ == '__main__': if __name__ == '__main__':
dct_para = { dct_para = {
'time': 60, 'time': 60, # 进行总时间数
# 'xv_int_max_order': random.randint(30, 50), # 'xv_int_max_order': random.randint(30, 50),
# 'xv_dlv_product_para': tuple([(30, 100), (30, 50)]), # 'xv_dlv_product_para': tuple([(30, 100), (30, 50)]),
# 'xv_dlv_product_para': tuple([30,40,30,20]), # 读取生产率 np.read. # 'xv_dlv_product_para': tuple([30,40,30,20]), # 读取生产率 np.read.
@ -117,11 +121,20 @@ if __name__ == '__main__':
# 'xv_ary_cost_material_per': tuple([0.1,0.1,0.2,0.4]), # 'xv_ary_cost_material_per': tuple([0.1,0.1,0.2,0.4]),
# 'xv_ary_volume_material': tuple([1.0, 1.5]), # 'xv_ary_volume_material': tuple([1.0, 1.5]),
# 'xv_ary_volume_product': tuple([3.0, 5.0]), # 'xv_ary_volume_product': tuple([3.0, 5.0]),
'xv_array_lead_time': 2, # 读取原材料表格 np.read.,暂时不读 # 'xv_array_lead_time': 2, # 读取原材料表格 np.read, 暂时不读 变量代表的含义
# 'xv_int_lead_time_c': 3, # 'xv_int_lead_time_c': 3,
# 'xv_int_lead_time_d': 1, # 'xv_int_lead_time_d': 1,
'xv_ary_initial_product_num': pd.read_excel("initial_product.xlsx").to_numpy(), 'xv_ary_product_id': pd.read_excel("initial_product.xlsx").to_numpy()[:, 0], # 产成品id顺序
'xv_ary_initial_material_num': pd.read_excel("initial_material.xlsx").to_numpy(), # 应读取遗传算法中随机生成的s暂写为'1' 创建两个excel分别存储产品和原材料的库存 每个excel中存系统代码和库存 'xv_ary_material_id': pd.read_excel("initial_material.xlsx").to_numpy()[:, 0], # 原材料id顺序
'xv_product_num': len(pd.read_excel("initial_product.xlsx").to_numpy()), # 产成品个数
'xv_material_num': len(pd.read_excel("initial_material.xlsx").to_numpy()), # 原材料个数
'xv_ary_initial_product_num': pd.read_excel("initial_product.xlsx").to_numpy(), # 初始产成品库存 23x2
'xv_ary_initial_material_num': pd.read_excel("initial_material.xlsx").to_numpy(), # 初始原材料库存 115x2
'xv_ary_bom': pd.read_excel("bom23.xlsx").to_numpy(), # bom表
'xv_ary_plan': pd.read_excel("plan.xlsx").to_numpy(), # plan表
'xv_ary_s': pd.read_excel("rawmaterial - s.xlsx").to_numpy(), # s
'xv_ary_S': pd.read_excel("rawmaterialS.xlsx").to_numpy(), # S
# 应读取遗传算法中随机生成的s暂写为'1' 创建两个excel分别存储产品和原材料的库存 每个excel中存系统代码和库存
# 'xv_flt_initial_cash': 50000.0, # 'xv_flt_initial_cash': 50000.0,
# 'dct_status_info': json.dumps({ #需要引入生产状态表 # 'dct_status_info': json.dumps({ #需要引入生产状态表
# "0": {"xv_flt_produce_rate": tuple([0.0, 0.0]), # "0": {"xv_flt_produce_rate": tuple([0.0, 0.0]),

44
Firm.py
View File

@ -5,6 +5,7 @@ from InventorySystem import InventorySystem
from OrderSystem import OrderSystem from OrderSystem import OrderSystem
from ProduceSystem import ProduceSystem from ProduceSystem import ProduceSystem
# Create Company Instance # Create Company Instance
class Firm: class Firm:
the_os: OrderSystem the_os: OrderSystem
@ -15,23 +16,46 @@ class Firm:
self.env = env self.env = env
# get the parameters here # get the parameters here
self.xv_ary_initial_product_num = np.asarray(dct_all_para['xv_ary_initial_product_num']) self.xv_ary_initial_product_num = np.asarray(dct_all_para['xv_ary_initial_product_num']) # 初始产成品库存
self.xv_ary_initial_material_num = np.asarray(dct_all_para['xv_ary_initial_material_num']) self.xv_ary_initial_material_num = np.asarray(dct_all_para['xv_ary_initial_material_num']) # 初始原材料库存
self.dct_status_info = json.loads(dct_all_para['dct_status_info']) self.xv_product_num = int(dct_all_para['xv_product_num']) # 产成品个数
self.xv_ary_lead_time = np.array([dct_all_para['xv_int_lead_time_a'], dct_all_para['xv_int_lead_time_b']]) self.xv_material_num = int(dct_all_para['xv_material_num']) # 原材料个数
self.xv_ary_plan = np.asarray(dct_all_para['xy_ary_plan'])
self.xv_ary_bom = np.asarray(dct_all_para['xv_ary_bom'])
self.xv_ary_product_id = np.asarray(dct_all_para['xv_ary_product_id'])
self.xv_ary_material_id = np.asarray(dct_all_para['xv_ary_material_id'])
self.xv_ary_s = np.asarray(dct_all_para['xv_ary_s'])
self.xv_ary_S = np.asarray(dct_all_para['xv_ary_S'])
# self.dct_status_info = json.loads(dct_all_para['dct_status_info']) # 生产状态表
# self.xv_ary_lead_time = np.array([dct_all_para['xv_int_lead_time_a'], dct_all_para['xv_int_lead_time_b']])
# create agents here # create agents here
self.the_os = OrderSystem(env) self.the_os = OrderSystem(env, xv_produt_num=self.xv_product_num, xv_ary_plan=self.xv_ary_plan,
xv_ary_product_id=self.xv_ary_product_id) # 实例Oss
self.the_is = InventorySystem(env, xv_ary_initial_material_num=self.xv_ary_initial_material_num, self.the_is = InventorySystem(env, xv_ary_initial_material_num=self.xv_ary_initial_material_num,
xv_ary_initial_product_num=self.xv_ary_initial_product_num, xv_ary_initial_product_num=self.xv_ary_initial_product_num,
xv_ary_lead_time=self.xv_ary_lead_time) xv_product_num=self.xv_product_num,
self.the_ps = ProduceSystem(env, dct_status_info=self.dct_status_info) xv_material_num=self.xv_material_num,
xv_ary_bom=self.xv_ary_bom, xv_ary_pan=self.xv_ary_plan,
xv_ary_material_id=self.xv_ary_material_id) # 实例Iss
self.the_ps = ProduceSystem(env, xv_ary_bom=self.xv_ary_bom, xv_ary_plan=self.xv_ary_plan,
xv_product_num=self.xv_product_num,
xv_ary_product_id=self.xv_ary_product_id,
xv_ary_material_id=self.xv_ary_material_id) # 实例Pss
# self.the_fs = FinancialSystem(env, xv_flt_initial_cash=self.xv_flt_initial_cash) # self.the_fs = FinancialSystem(env, xv_flt_initial_cash=self.xv_flt_initial_cash)
def operating(self): def operating(self): # 按步骤进行
self.the_os.rank_order() self.the_os.rank_order()
self.the_os.produce_status()
self.the_is.material_state_to_use()
self.the_is.material_check()
self.the_ps.change_status() self.the_ps.change_status()
self.the_ps.run_produce() self.the_ps.run_produce(ev_lst_trans_quan_material=self.the_is.ev_lst_trans_quan_material,
self.the_os.do_shipment() ev_ary_product_to_produce=self.the_os.ev_ary_product_to_produce)
self.the_is.consume_and_store(ev_ary_produced_num=self.the_ps.ev_ary_produce_number,
ev_changed_product=self.the_os.ev_changed_product,
ev_lst_backtrans_material=self.the_ps.ev_lst_backtrans_material)
self.the_is.material_replenishment()
self.the_os.do_shipment(ev_ary_current_product=self.the_is.ev_ary_current_product)
# self.the_is.inventory_cost() # self.the_is.inventory_cost()
# self.the_fs.financial_calculating() # self.the_fs.financial_calculating()

View File

@ -1,135 +1,131 @@
import agentpy as ap import agentpy as ap
import numpy as np import numpy as np
import pandas as pd
from collections import deque from collections import deque
class InventorySystem(ap.Agent): class InventorySystem(ap.Agent):
xv_ary_initial_material_num: np.ndarray # 初始原材料库存
xv_ary_initial_material_num: np.ndarray xv_ary_initial_product_num: np.ndarray # 初始产成品库存
xv_ary_initial_product_num: np.ndarray ev_ary_current_material: np.ndarray # 当期原材料库存
ev_ary_current_material: np.ndarray #期末库存 ev_ary_current_product: np.ndarray # 当期产成品库存
ev_ary_current_product: np.ndarray #期末库存
ev_lst_trans_quan_material: np.ndarray # Iss传递给Pss的原材料数量
ev_lst_backtrans_quan_material: np.ndarray # Pss退回的原材料数量
ev_ary_material_state_to_use: np.ndarray # 当期预期使用的原材料 ev_ary_material_state_to_use: np.ndarray # 当期预期使用的原材料
ev_ary_material_to_use: np.ndarray # 当期实际消耗的原材料 ev_ary_material_to_use: np.ndarray # 当期实际消耗的原材料
xv_array_dlv_product: np.ndarray # 产品交付
ev_ary_is_order_material: np.ndarray # 是否订购产品 ev_ary_is_order_material: np.ndarray # 是否订购产品
ev_ary_num_material_to_order: np.ndarray # 订购数量 ev_ary_num_material_to_order: np.ndarray # 订购数量
ev_ary_num_material_order_done: np.ndarray # 当期送到的数量 ev_ary_num_material_order_done: np.ndarray # 当期送到的数量
# set the lead time of material replenishment # set the lead time of material replenishment
xv_ary_lead_time: np.ndarray xv_ary_lead_time: np.ndarray # 原材料订货的等待时间
# the list of materials in transit # the list of materials in transit
ev_lst_trans_material: list # Iss交给Pss的原材料 ev_lst_trans_quan_material: list # Iss传递给Pss的原材料数量
ev_list_back_trans_material: list # Pss返回的原材料 ev_lst_backtrans_quan_material: list # Pss退回的原材料数量
ev_lst_material_inventory: list
ev_lst_product_inventory: list
# Material list # Material list
xv_lst_material: np.ndarray xv_ary_material: np.ndarray
xv_ary_bom: np.ndarray xv_ary_bom: np.ndarray
# 原材料订购的开始时间和到达时间 # 原材料订购的开始时间和到达时间
ev_int_order_time: np.ndarray ev_ary_order_time: np.ndarray
ev_int_arrive_time: np.ndarray ev_ary_arrive_time: np.ndarray
ev_ary_num_material_order_done: np.ndarray # 当期原材料到货量
def setup(self, xv_ary_initial_material_num, xv_product_num: int
xv_ary_initial_product_num, xv_ary_lead_time): xv_material_num: int
xv_ary_plan: np.ndarray
def setup(self, xv_ary_initial_material_num, xv_ary_initial_product_num, xv_product_num, xv_material_num,
xv_ary_bom, xv_ary_plan, xv_ary_material_id):
# Set up the initial inventory quantity # Set up the initial inventory quantity
self.xv_ary_initial_material_num = xv_ary_initial_material_num self.xv_ary_initial_material_num = xv_ary_initial_material_num
self.xv_ary_initial_product_num = xv_ary_initial_product_num self.xv_ary_initial_product_num = xv_ary_initial_product_num
self.ev_ary_current_material = xv_ary_initial_material_num # 115*2 self.ev_ary_current_material = xv_ary_initial_material_num # 115*2
self.ev_ary_current_product = xv_ary_initial_product_num #23*2 self.ev_ary_current_product = xv_ary_initial_product_num # 23*2
self.ev_ary_num_material_order_done = np.zeros((115, )) self.xv_product_num = xv_product_num # 产成品个数
self.xv_material_num = xv_material_num # 原材料个数
self.ev_ary_num_material_order_done = np.zeros((xv_material_num,)) # 当期原材料送到的数量 115x1
# set the lead time of materials # set the lead time of materials
# 读取各个原材料的历史平均交货周期 bom table # 读取各个原材料的历史平均交货周期 bom table
self.xv_ary_lead_time = pd.read_excel("bom23.xlsx").iloc[:, 4].to_numpy() self.xv_ary_lead_time = xv_ary_bom[:, (1, 4)] # 每一种原材料的到货时间(有重复)
# 切换成原材料 # 切换成原材料
self.ev_ary_is_order_material = np.array([False for i in range(115)]) self.ev_ary_is_order_material = np.array([False for i in range(xv_material_num)]) # 是否订购原材料
self.ev_ary_num_material_to_order = np.zeros((115,)) # 初始化 self.ev_ary_num_material_to_order = np.zeros((xv_material_num,)) # 原材料订货数量
self.ev_ary_material_state_to_use = np.zeros((115,)) self.ev_ary_material_state_to_use = np.zeros((xv_material_num,)) # 一个周期内按照设定生产状态完全生产 原材料消耗数量 计划
self.ev_ary_material_to_use = np.zeros((xv_material_num,)) # 当期实际消耗的原材料数量
self.ev_lst_material_inventory = [] self.xv_ary_material = xv_ary_material_id # 原材料顺序
self.ev_lst_product_inventory = []
self.xv_lst_material = pd.read_excel("rawmaterial.xlsx").to_numpy() self.xv_ary_bom = xv_ary_bom # bom表
self.xv_ary_plan = xv_ary_plan # plan表
self.xv_ary_bom = pd.read_excel("bom23.xlsx").to_numpy()
self.ev_lst_trans_quan_material = [] # Iss传递给Pss的原材料数量 self.ev_lst_trans_quan_material = [] # Iss传递给Pss的原材料数量
self.ev_lst_backtrans_quan_material = [] # Pss退回的原材料数量 self.ev_lst_backtrans_quan_material = [] # Pss退回的原材料数量
self.ev_int_order_time = np.zeros((115,)) self.ev_ary_order_time = np.zeros((xv_material_num,)) # 原材料订货时间戳
self.ev_int_arrive_time = np.zeros((115,)) self.ev_ary_arrive_time = np.zeros((xv_material_num,)) # 原材料到货时间戳
<<<<<<< HEAD # def material_state_to_use(self, ev_int_produce_type, xv_plan_excel):
def material_state_to_use(self, ev_int_produce_type, xv_plan_excel): # # 读取Oss决定的生产状态根据生产状态所需的原材料进行汇总
# # self. ev_ary_material_state_to_use = np.multiply(self.xv_array_dlv_product, 原材料表 )
# produce_state = ev_int_produce_type # 获取生产状态
def material_state_to_use(self): # 根据生产状态 看生产一个周期需要多少原材料
# 读取Oss决定的生产状态根据生产状态所需的原材料进行汇总 # 读取Oss决定的生产状态根据生产状态所需的原材料进行汇总
# self. ev_ary_material_state_to_use = np.multiply(self.xv_array_dlv_product, 原材料表 ) # self. ev_ary_material_state_to_use = np.multiply(self.xv_array_dlv_product, 原材料表 )
produce_state = ev_int_produce_type # 获取生产状态 produce_state = self.model.the_firm.the_os.ev_int_produce_type # 获取生产状态
======= produce_plan = self.xv_ary_plan[self.xv_ary_plan[:, 0] == produce_state] # 该生产状态下各产品的生产计划
def material_state_to_use(self, xv_ary_dlv_product, ev_int_produce_type): for product_plan in produce_plan:
# 读取Oss决定的生产状态根据生产状态所需的原材料进行汇总
# self. ev_ary_material_state_to_use = np.multiply(self.xv_array_dlv_product, 原材料表 )
produce_state = 1 # 获取生产状态
>>>>>>> 6027857189fa76bd175eb941904972de305532a4
produce_plan = xv_plan_excel[xv_plan_excel[:, 0] == produce_state]
for plan in produce_plan:
for bom in self.xv_ary_bom: for bom in self.xv_ary_bom:
if plan[1] == bom[0]: if product_plan[1] == bom[0]:
self.ev_ary_material_state_to_use[np.where(self.xv_lst_material == bom[1])] += plan[3] * bom[ self.ev_ary_material_state_to_use[np.where(self.xv_ary_material == bom[1])] += product_plan[3] * \
bom[
2] # 某原材料的生产数量加等产品生产量乘单位原材料消耗量 2] # 某原材料的生产数量加等产品生产量乘单位原材料消耗量
def material_check(self, ev_ary_material_state_to_use): def material_check(self): # 检查库存是否充足
# 根据Iss决策核对库存是否充足并将拥有的原材料交给Pss # 根据Iss决策核对库存是否充足并将拥有的原材料交给Pss
# 可能需要一部生成原材料列表在调用 # 可能需要一部生成原材料列表在调用
# Check whether materials are enough and transfer material # Check whether materials are enough and transfer material
index_mapping = {value: idx for idx, value in enumerate(self.ev_ary_current_material[:, 0])} for i in range(self.xv_material_num):
sorted_indices = [index_mapping[value] for value in self.xv_lst_material] self.ev_lst_trans_quan_material[i] = min(self.ev_ary_current_material[i, 1],
self.ev_ary_current_material = self.ev_ary_current_material[sorted_indices] # 按照原材料制定顺序进行重排 self.ev_ary_material_state_to_use[i]) # 需要数量和库存两者间的较小值
for i in range(115): # if self.ev_ary_current_material[i, 1] >= ev_ary_material_state_to_use[i]: # 库存足够按照计划生产
if self.ev_ary_current_material[i, 1] >= ev_ary_material_state_to_use[i]: # self.ev_lst_trans_quan_material[i] = ev_ary_material_state_to_use[i]
self.ev_lst_trans_quan_material[i] = ev_ary_material_state_to_use[i] # else:
# return (self.ev_ary_current_material >= ev_ary_material_to_use).all() # self.ev_lst_trans_quan_material[i] = self.ev_ary_current_material[i]
else:
self.ev_lst_trans_quan_material[i] = self.ev_ary_current_material[i]
def consume_and_store(self, ev_ary_material_to_use, ev_ary_produced_num,ev_changed_product, def consume_and_store(self, ev_ary_produced_num, ev_changed_product,
ev_lst_backtrans_material): ev_lst_backtrans_material):
self.ev_lst_backtrans_quan_material = ev_lst_backtrans_material self.ev_lst_backtrans_quan_material = ev_lst_backtrans_material # Pss传回的原材料数量
# Update the inventory after production # Update the inventory after production
for i in range(115): for i in range(self.xv_material_num):
self.ev_ary_material_to_use[i] = self.ev_lst_trans_quan_material[i] - \ self.ev_ary_material_to_use[i] = self.ev_lst_trans_quan_material[i] - \
self.ev_lst_backtrans_quan_material[i] self.ev_lst_backtrans_quan_material[i]
if self.ev_int_arrive_time == self.model.t: # 判断材料是否到达 if self.ev_ary_arrive_time[i] == self.model.t: # 判断材料是否到达
self.ev_ary_num_material_order_done[i] += self.ev_ary_num_material_to_order[i] self.ev_ary_current_material[i, 1] = self.ev_ary_current_material[i, 1] - self.ev_ary_material_to_use[i] \
+ self.ev_ary_num_material_to_order[i] # 减消耗量 加订购量
self.ev_ary_num_material_to_order[i] = 0 self.ev_ary_num_material_to_order[i] = 0
self.ev_ary_is_order_material[i] = False self.ev_ary_is_order_material[i] = False
self.ev_ary_current_material = self.ev_ary_current_material - ev_ary_material_to_use \ self.ev_ary_current_product[:, 1] = ev_changed_product + ev_ary_produced_num[:, 1] # 上期期末加本期生产
+ self.ev_ary_num_material_order_done
self.ev_ary_current_product = ev_changed_product + ev_ary_produced_num
return self.ev_ary_current_material, self.ev_ary_current_product
def material_replenishment(self): # return self.ev_ary_current_material, self.ev_ary_current_product
def material_replenishment(self, xv_ary_s, xv_ary_S):
# Check the inventory of material to decide whether to replenish # Check the inventory of material to decide whether to replenish
# 核对原材料数量是否低于s # 核对原材料数量是否低于s
# 带时间戳 # 带时间戳
for i in range(115): for i in range(self.xv_material_num):
if self.ev_ary_current_material[i] <= s: if self.ev_ary_current_material[i, 1] <= xv_ary_s[i, 1] and self.ev_ary_is_order_material[i] == False:
self.ev_ary_is_order_material[i] = True self.ev_ary_is_order_material[i] = True
self.ev_ary_num_material_to_order[i] = S - self.ev_ary_current_material[i] self.ev_ary_num_material_to_order[i] = xv_ary_S[i, 1] - self.ev_ary_current_material[i, 1]
self.ev_int_order_time[i] = self.model.t self.ev_ary_order_time[i] = self.model.t
self.ev_int_arrive_time[i] = self.ev_int_order_time[i] + \ self.ev_ary_arrive_time[i] = self.ev_int_order_time[i] + self.xv_ary_lead_time[
self.xv_ary_bom[self.xv_ary_bom[:, 1] == self.xv_lst_material[i]][0, 4] self.xv_ary_lead_time[:, 0] == self.ev_ary_current_material[i, 0]][0, 1]
else: else:
self.ev_ary_num_material_to_order[i] = 0 self.ev_ary_num_material_to_order[i] = 0

View File

@ -6,7 +6,6 @@ import pandas as pd
class Order(ap.Agent): class Order(ap.Agent):
xv_time_created: int # 订单创建时间 xv_time_created: int # 订单创建时间
xv_time_circle: int # 随机生成的交货周期 xv_time_circle: int # 随机生成的交货周期
xv_dlv_t: int # 客户希望的交货时间 xv_dlv_t: int # 客户希望的交货时间
@ -14,22 +13,20 @@ class Order(ap.Agent):
ev_is_delivered: bool # 订单是否已交付 ev_is_delivered: bool # 订单是否已交付
ev_is_accepted: bool # 订单是否被接受 ev_is_accepted: bool # 订单是否被接受
ev_int_delay_time: int # total delay time ev_int_delay_time: int # 订单延迟交付时间
xv_ary_dlv_product: np.ndarray xv_ary_dlv_product: np.ndarray
ev_ary_dlv_product: np.ndarray ev_ary_dlv_product: np.ndarray
def setup(self, time_created): def setup(self, time_created):
self.xv_time_created = time_created self.xv_time_created = time_created # 订单创建时间
# read the demand of 23 productions # read the demand of 23 productions
df = pd.read_excel("demand23.xlsx") self.xv_ary_dlv_product = pd.read_excel("demand23.xlsx").to_numpy() # 接神经网络结果
self.xv_ary_dlv_product = df.to_numpy() self.ev_ary_dlv_product = pd.read_excel("demand23.xlsx").to_numpy()[:, 1] # 23x1 把产品的顺序按照xv_ary_product_id重新排列!!!
df = df.iloc[:, 1]
self.ev_ary_dlv_product = df.to_numpy()
self.xv_time_circle = np.random.randint(7, 11, 1) self.xv_time_circle = np.random.randint(7, 11, 1)
self.xv_dlv_t = self.xv_time_created + self.xv_time_circle self.xv_dlv_t = self.xv_time_created + self.xv_time_circle # 随机生成期望交付时间
self.ev_actual_dlv_t = self.xv_dlv_t self.ev_actual_dlv_t = self.xv_dlv_t
self.ev_int_delay_time = self.ev_actual_dlv_t - self.xv_dlv_t self.ev_int_delay_time = 0
# Set the initial status of order to be undelivered, accepted # Set the initial status of order to be undelivered, accepted
self.ev_is_delivered = False self.ev_is_delivered = False

View File

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

View File

@ -4,57 +4,74 @@ import random
class ProduceSystem(ap.Agent): class ProduceSystem(ap.Agent):
ev_str_status: str # 0,6,8,10 pp_id ev_str_status: int # 0,6,8,10 pp_id
ev_lst_backtrans_material: list # 退回Iss的原材料 ev_lst_backtrans_material: list # 退回Iss的原材料
ev_ary_produce_number: np.ndarray ev_ary_produce_number: np.ndarray
def setup(self, dct_status_info): xv_ary_bom: np.ndarray
self.ev_str_status = 0 xv_ary_plan: np.ndarray
xv_product_num: int
ev_ary_product: np.ndarray
xv_ary_material: np.ndarray
def setup(self, xv_ary_bom, xv_ary_plan, xv_product_num, xv_ary_product_id, xv_ary_material_id):
self.ev_str_status = 0 # 设置初始生产状态
self.ev_lst_backtrans_material = [] self.ev_lst_backtrans_material = []
self.ev_ary_produce_number = np.zeros((xv_product_num, 2))
self.xv_ary_bom = xv_ary_bom
self.xv_ary_plan = xv_ary_plan
self.xv_product_num = xv_product_num
self.ev_ary_product = xv_ary_product_id
self.xv_ary_material = xv_ary_material_id
def change_status(self): def change_status(self):
self.ev_str_status = self.model.the_firm.the_os.produce_status() self.ev_str_status = self.model.the_firm.the_os.ev_int_produce_type
def run_produce(self, ev_lst_trans_quan_material,
def run_produce(self, ev_lst_material, ev_lst_trans_quan_material, ev_ary_product_to_produce, xv_plan_excel, ev_ary_product_to_produce): # ev_ary_product_to_produce 是 需求和库存的gap 23x1
xv_ary_bom): # ev_ary_product_to_produce 是 需求和库存的gap 23x2
# 生产状态由Iss确定这个函数需要计算生产数量和原材料消耗量。 # 生产状态由Iss确定这个函数需要计算生产数量和原材料消耗量。
# 如果原材料不足就按照production gap(ev_ary_product_to_produce) 大小进行生产如果没有gap的产品了就按照库存水平由少到多进行生产。 # 如果原材料不足就按照production gap(ev_ary_product_to_produce) 大小进行生产如果没有gap的产品了就按照库存水平由少到多进行生产。
# 输出生产结果和原材料消耗结果 # 输出生产结果和原材料消耗结果
produce_plan = xv_plan_excel[xv_plan_excel[:, 0] == self.ev_str_status] produce_plan = self.xv_ary_plan[self.xv_ary_plan[:, 0] == self.ev_str_status] # 当前生产状态下的产品生产速度
sorted_indices = np.argsort(ev_ary_product_to_produce[:, 1])[::-1] sorted_indices = np.argsort(ev_ary_product_to_produce)[::-1]
sorted_data = ev_ary_product_to_produce[sorted_indices] # 对gap进行从大到小排序 sorted_data = ev_ary_product_to_produce[sorted_indices] # 对gap进行从大到小排序
self.ev_ary_product = self.ev_ary_product[sorted_indices]
sorted_indices_product = np.argsort(self.model.the_firm.the_is.ev_ary_current_product[:, 1]) # 对库存进行从小到大排序 sorted_indices_product = np.argsort(self.model.the_firm.the_is.ev_ary_current_product[:, 1]) # 对库存进行从小到大排序
sorted_data_product = self.model.the_firm.the_is.ev_ary_current_product[sorted_indices_product] sorted_data_product = self.model.the_firm.the_is.ev_ary_current_product[:, sorted_indices_product]
self.ev_lst_backtrans_material = ev_lst_trans_quan_material self.ev_lst_backtrans_material = ev_lst_trans_quan_material # 存储退回给Iss的原材料
self.ev_ary_produce_number = [] self.ev_ary_produce_number = np.zeros((self.xv_product_num, 2))
for product in sorted_data: produce_number_lst = []
if product[1] > 0: # gap存在 for i in range(self.xv_product_num):
product_material = xv_ary_bom[xv_ary_bom[:, 0] == product[0]] if sorted_data[i] > 0: # gap存在
produce_number = produce_plan[produce_plan[:, 1] == product[0]][3] product_material = self.xv_ary_bom[self.xv_ary_bom[:, 0] == self.ev_ary_product[i]]
produce_number = produce_plan[produce_plan[:, 1] == self.ev_ary_product[i]][3]
for material in product_material: for material in product_material:
produce_number = min(produce_number, int( produce_number = min(produce_number, int(
self.ev_lst_backtrans_material[np.where(ev_lst_material == material[1])] / material[ self.ev_lst_backtrans_material[np.where(self.xv_ary_material == material[1])] / material[
2])) # 取能生产的最小个数 2])) # 取能生产的最小个数
for material in product_material: for material in product_material:
self.ev_lst_backtrans_material[np.where(ev_lst_material == material[1])] -= produce_number * material[ self.ev_lst_backtrans_material[np.where(self.xv_ary_material == material[1])] -= produce_number * \
material[
2] # 更新原材料消耗情况 2] # 更新原材料消耗情况
self.ev_ary_produce_number.append([product[0], produce_number]) produce_number_lst.append([self.ev_ary_product[i], produce_number])
sorted_data_product = sorted_data_product[sorted_data_product[:, 0] != product[0]] sorted_data_product = sorted_data_product[sorted_data_product[:, 0] != self.ev_ary_product[i]]
else: else:
for current_product in sorted_data_product: for current_product in sorted_data_product:
product_material = xv_ary_bom[xv_ary_bom[:, 0] == current_product[0]] product_material = self.xv_ary_bom[self.xv_ary_bom[:, 0] == current_product[0]]
produce_number = produce_plan[produce_plan[:, 1] == current_product[0]][3] produce_number = produce_plan[produce_plan[:, 1] == current_product[0]][3]
for material in product_material: for material in product_material:
produce_number = min(produce_number, int( produce_number = min(produce_number, int(
self.ev_lst_backtrans_material[np.where(ev_lst_material == material[1])] / material[ self.ev_lst_backtrans_material[np.where(self.xv_ary_material == material[1])] / material[
2])) # 取能生产的最小个数 2])) # 取能生产的最小个数
for material in product_material: for material in product_material:
self.ev_lst_backtrans_material[np.where(ev_lst_material == material[1])] -= produce_number * \ self.ev_lst_backtrans_material[
material[2] # 更新原材料消耗情况 np.where(self.xv_ary_material == material[1])] -= produce_number * \
self.ev_ary_produce_number.append([current_product[0], produce_number]) material[
2] # 更新原材料消耗情况
produce_number_lst.append([current_product[0], produce_number])
break break
self.ev_ary_produce_number = np.array(self.ev_ary_produce_number) self.ev_ary_produce_number = np.array(produce_number_lst)
return self.ev_ary_produce_number, self.ev_lst_backtrans_material # return self.ev_ary_produce_number, self.ev_lst_backtrans_material

Binary file not shown.

BIN
initial_material.xlsx Normal file

Binary file not shown.

BIN
initial_product.xlsx Normal file

Binary file not shown.

BIN
rawmaterial - s.xlsx Normal file

Binary file not shown.

Binary file not shown.

BIN
rawmaterialS.xlsx Normal file

Binary file not shown.