import agentpy as ap import numpy as np import random class ProduceSystem(ap.Agent): ev_str_status: int # 0,6,8,10 pp_id ev_lst_backtrans_material: list # 退回Iss的原材料 ev_ary_produce_number: np.ndarray xv_ary_bom: np.ndarray 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_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): self.ev_str_status = self.model.the_firm.the_os.ev_int_produce_type def run_produce(self, ev_lst_trans_quan_material, ev_ary_product_to_produce): # ev_ary_product_to_produce 是 需求和库存的gap 23x1 # 生产状态由Iss确定,这个函数需要计算生产数量和原材料消耗量。 # 如果原材料不足,就按照production gap(ev_ary_product_to_produce) 大小进行生产;如果没有gap的产品了,就按照库存水平由少到多进行生产。 # 输出生产结果和原材料消耗结果 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] 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_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 # 存储退回给Iss的原材料 self.ev_ary_produce_number = np.zeros((self.xv_product_num, 2)) produce_number_lst = [] for i in range(self.xv_product_num): if sorted_data[i] > 0: # gap存在 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: produce_number = min(produce_number, int( self.ev_lst_backtrans_material[np.where(self.xv_ary_material == material[1])] / material[ 2])) # 取能生产的最小个数 for material in product_material: self.ev_lst_backtrans_material[np.where(self.xv_ary_material == material[1])] -= produce_number * \ material[ 2] # 更新原材料消耗情况 produce_number_lst.append([self.ev_ary_product[i], produce_number]) sorted_data_product = sorted_data_product[sorted_data_product[:, 0] != self.ev_ary_product[i]] else: for current_product in sorted_data_product: 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] for material in product_material: produce_number = min(produce_number, int( self.ev_lst_backtrans_material[np.where(self.xv_ary_material == material[1])] / material[ 2])) # 取能生产的最小个数 for material in product_material: self.ev_lst_backtrans_material[ np.where(self.xv_ary_material == material[1])] -= produce_number * \ material[ 2] # 更新原材料消耗情况 produce_number_lst.append([current_product[0], produce_number]) break self.ev_ary_produce_number = np.array(produce_number_lst) # return self.ev_ary_produce_number, self.ev_lst_backtrans_material