def __init__(self, group, task_type, mode='FIFO', cncs_type='00000000', fault=False, print_info=False): ''' group: cnc机器类型组别 task_type: 1/2,1表示一道工序模式,2表示两道工序模式。 cncs_type: 八位二进制0/1字符串,0表示处理第一道工序,1表示处理第二道工序。 ''' self.group = group self.group_1 = [20, 33, 46, 560, 400, 378, 28, 31, 25] self.group_2 = [23, 41, 59, 580, 280, 500, 30, 35, 30] self.group_3 = [18, 32, 46, 545, 455, 182, 27, 32, 25] self.groups_para = [self.group_1, self.group_2, self.group_3] # 各组cnc,rgv机器参数 self.task_type = task_type # self.T = 8 * 60 * 60 # 一次加工总时间,单位:秒 self.time = 0 # 计时器 self.cnt = 0 # 时间 self.cncs_type = cncs_type if self.task_type == 1: self.cnc_ls = [ CNC(i + 1, group, self.task_type - 1, self.groups_para, fault) for i in range(8) ] # task 1 else: self.cnc_ls = [ CNC( int(i) + 1, group, int(cncs_type[i]) + 1, self.groups_para, fault) for i in range(8) ] # task 2 self.rgv = RGV(0, group, self.task_type, self.groups_para) # 初始化rgv self.wait_queue = [] # 任务等待队列,按照任务先后顺序 self.wait_queue_task1 = [] # 第一阶段等待队列 self.wait_queue_task2 = [] # 第二阶段等待队列 self.wait_set = set() # 任务等待集合 self.wait_set_task1 = set() # 第一阶段等待集合 self.wait_set_task2 = set() # 第二阶段等待集合 self.mode = mode self.select_ls = [] self.print_info = print_info
class TaskSys: def __init__(self, group, task_type, mode='FIFO', cncs_type='00000000', fault=False, print_info=False): ''' group: cnc机器类型组别 task_type: 1/2,1表示一道工序模式,2表示两道工序模式。 cncs_type: 八位二进制0/1字符串,0表示处理第一道工序,1表示处理第二道工序。 ''' self.group = group self.group_1 = [20, 33, 46, 560, 400, 378, 28, 31, 25] self.group_2 = [23, 41, 59, 580, 280, 500, 30, 35, 30] self.group_3 = [18, 32, 46, 545, 455, 182, 27, 32, 25] self.groups_para = [self.group_1, self.group_2, self.group_3] # 各组cnc,rgv机器参数 self.task_type = task_type # self.T = 8 * 60 * 60 # 一次加工总时间,单位:秒 self.time = 0 # 计时器 self.cnt = 0 # 时间 self.cncs_type = cncs_type if self.task_type == 1: self.cnc_ls = [ CNC(i + 1, group, self.task_type - 1, self.groups_para, fault) for i in range(8) ] # task 1 else: self.cnc_ls = [ CNC( int(i) + 1, group, int(cncs_type[i]) + 1, self.groups_para, fault) for i in range(8) ] # task 2 self.rgv = RGV(0, group, self.task_type, self.groups_para) # 初始化rgv self.wait_queue = [] # 任务等待队列,按照任务先后顺序 self.wait_queue_task1 = [] # 第一阶段等待队列 self.wait_queue_task2 = [] # 第二阶段等待队列 self.wait_set = set() # 任务等待集合 self.wait_set_task1 = set() # 第一阶段等待集合 self.wait_set_task2 = set() # 第二阶段等待集合 self.mode = mode self.select_ls = [] self.print_info = print_info def get_up_thres(self): if self.task_type == 1: return self.T / self.groups_para[self.group - 1][3] * len( self.cnc_ls) n_0 = 0 n_1 = 0 for t in self.cncs_type: if t == '0': n_0 += 1 if t == '1': n_1 += 1 t_0 = self.groups_para[self.group - 1][4] t_1 = self.groups_para[self.group - 1][5] up_thres = self.T / max(t_0 / n_0, t_1 / n_0) return up_thres def stats(self, filename=None): # rgv num_result = self.rgv.get_result(self.T) # 完成任务的数量 + if num_result != 0: mean_sys_do_task_time = self.T / num_result # 完成任务的平均时间 + else: mean_sys_do_task_time = 0 mean_cnc_do_task_time = mean_sys_do_task_time * len(self.cnc_ls) # cnc cnc_sum_wait_time = self.rgv.get_sum_wait_time( self.cnc_ls) # cnc 完成所有任务的总等待时间 + cnc_mean_wait_time = self.rgv.get_mean_wait_time( self.cnc_ls) # cnc 每完成一个任务的平均等待时间 + cnc_std_wait_time = self.rgv.get_std_wait_time( self.cnc_ls) # cnc 完成每个任务的等待时间的标准差 + pre_cnc_wait_time = cnc_sum_wait_time / self.T # cnc闲置占比 + up_thres = self.get_up_thres() pre_task_in_up_thres = num_result / up_thres #完成任务占上界的比 return [ num_result, mean_cnc_do_task_time, mean_sys_do_task_time, cnc_sum_wait_time, cnc_mean_wait_time, cnc_std_wait_time, pre_cnc_wait_time, pre_task_in_up_thres, up_thres ] def write_stats_info(self): pass def print_stats(self): print('mode: %s, group: %d' % (self.mode, self.group)) if self.cncs_type != None: print('cncs_type: %s' % self.cncs_type) rgv_sum = self.rgv.idle_time + self.rgv.move_time + self.rgv.updown_time + self.rgv.wash_time print('rgv, idle:%d, move:%d, updown:%d, wash:%d, sum:%d ' % (self.rgv.idle_time, self.rgv.move_time, self.rgv.updown_time, self.rgv.wash_time, rgv_sum)) print('mv_dist:', self.rgv.sum_move_distance) for i, cnc in enumerate(self.cnc_ls): cnc.stats() print('cnc_type: %d cnc: %d, wc_t: %d, idle_t: %d, mean_wt: %s' % (cnc.get_type(), i + 1, cnc.wait_call_time, cnc.idle_time, str(cnc.mean_wait_time))) # print(self.select_ls) def run(self): self.cnt = self.T while True: self.time = self.T - self.cnt self.update_cnn_condition(self.time) self.update_rgv_condition(self.time) self.cnt -= 1 if self.cnt == 0: break if self.print_info: self.print_stats() self.stats() def update_cnn_condition(self, time): for cnc in self.cnc_ls: # 处于等待响应队列的cnc设备,0:初始化状态;2:完成任务状态 status = cnc.get_status() if status == 0 or status == 2: num = cnc.get_num() if num not in self.wait_set: self.wait_queue.append(num) self.wait_set.add(num) # 如果cnc加工的是第一道工序 if cnc.type == 1: self.wait_queue_task1.append(num) self.wait_set_task1.add(num) # 如果cnc加工的是第二道工序 if cnc.type == 2: self.wait_queue_task2.append(num) self.wait_set_task2.add(num) cnc.wait_call() # 处于加工状态的cnc设备 if status == 1: cnc.do_task(time) # 检查任务是否已经完成,完成则添加到等待队列中 if cnc.get_status() == 2: num = cnc.get_num() if num not in self.wait_set: self.wait_queue.append(num) self.wait_set.add(num) # 如果cnc加工的是第一道工序 if cnc.type == 1: self.wait_queue_task1.append(num) self.wait_set_task1.add(num) # 如果cnc加工的是第二道工序 if cnc.type == 2: self.wait_queue_task2.append(num) self.wait_set_task2.add(num) # 处于被rgv响应的设备,但是还没有开始任务 if status == 3: cnc.idle() # 设备处于故障状态 if status == -1: cnc.start_maintain(time) if status == 4: cnc.maintain(time) def update_rgv_condition(self, time): status = self.rgv.get_status() task_time = self.rgv.get_task_time() # rgv 处于闲置状态 if status == 0: # 检查是否有cnc发出请求 或者 使用的是STSPW算法 if self.wait_queue != [] or self.mode == 'STSPW': wait_queue_ls = [ self.wait_queue, self.wait_queue_task1, self.wait_queue_task2 ] wait_set_ls = [ self.wait_set, self.wait_set_task1, self.wait_set_task2 ] # 寻找目标 k = self.rgv.update_status_move(wait_queue_ls, wait_set_ls, self.cnc_ls, self.mode, time) if k == -1: self.rgv.idle() else: self.select_ls.append(k) # 如果cnc发出了响应请求,则通知cnc.num = k , 请求已被响应,并且整理等待队列信息 cnc_status = self.cnc_ls[k - 1].get_status() if cnc_status == 2 or cnc_status == 0: self.cnc_ls[k - 1].reply() if k in self.wait_set: self.wait_queue.remove(k) self.wait_set.remove(k) if self.task_type == 2: if k in self.wait_set_task1: self.wait_queue_task1.remove(k) self.wait_set_task1.remove(k) else: self.wait_queue_task2.remove(k) self.wait_set_task2.remove(k) else: self.rgv.idle() # rgv 处于移动状态 elif status == 1: if task_time == 0: self.rgv.update_status_updown_material(self.cnc_ls, time) else: self.rgv.move() # rgv 处于上下料状态 elif status == 2: if task_time == 0: # self.rgv.change_hand_status(self.rgv.num_sever_cnc, self.cnc_ls) self.cnc_ls[self.rgv.num_sever_cnc - 1].start_task(time) # 如果两只机械手上存在3(两道加工后的料) if self.rgv.get_hand_1()[0] == 3 or self.rgv.get_hand_2( )[0] == 3: self.rgv.update_status_wash_material() else: # 两种情况,一种取到一道加工后的料2,一种,什么也没取到 self.rgv.set_status(0) self.update_rgv_condition(time) else: self.rgv.updown_material() # rgv 处于清洗材料状态 elif status == 3: if task_time == 0: # 释放手上的成料 self.rgv.release_hand() self.rgv.update_status_idle() else: self.rgv.wash_material() # 考虑边界 status = self.rgv.get_status() if status == 0: self.update_rgv_condition(time) def save_stats_result(self): pass def output_result_task1(self): return self.rgv.get_result(self.T) def get_result_task1(self): return self.rgv.get_result_ls_task1(self.T) def get_result_task2(self): return self.rgv.get_result_ls_task2(self.T) def get_use_material_result_ls(self): return self.rgv.get_used_material_ls(self.T) def output_result_task2(self): return self.rgv.get_result(self.T)
from CNC import CNC from RGV import RGV time = 0 goodsnum = 0 relativeloca = [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000] relativeloca2 = [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000] rgv = RGV([0, 18, 32, 46], 27, 32, 25, 1) workingtime = [0, 455, 182] cnclist = [] fcnc = [0] scnc = [0] sequenceall = [0] sequence1 = [0] sequence2 = [0] uptsequence1 = [0] uptsequence2 = [0] updowntsequence1 = [0] updowntsequence2 = [0] genome = [-1, 0, 1, 1, 0, 0, 1, 0, 0] def express(): global cnclist global fcnc global scnc for n in range(1, 8 + 1): if genome[n] == 1: cnclist[n].whethersec = True scnc.append(cnclist[n].num) else: fcnc.append(cnclist[n].num)
from CNC import CNC from RGV import RGV import random time = 0 goodsnum = 0 relativeloca = [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000] relativeloca2 = [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000] rgv = RGV([0, 20, 33, 46], 28, 31, 25, 1) workingtime = [0, 400, 378] cnclist = [] fcnc = [0] scnc = [0] sequenceall = [0] sequence1 = [0] sequence2 = [0] uptsequence1 = [0] uptsequence2 = [0] updowntsequence1 = [0] updowntsequence2 = [0] genome = [-1, 0, 1, 0, 1, 0, 1, 0, 1] def express(): global cnclist global fcnc global scnc for n in range(1, 8 + 1): if genome[n] == 1: cnclist[n].whethersec = True scnc.append(cnclist[n].num)
from CNC import CNC from RGV import RGV time = 0 goodsnum = 0 relativeloca = [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000] relativeloca2 = [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000] rgv = RGV([0, 23, 41, 59], 30, 35, 30, 1) workingtime = [0, 280, 500] cnclist = [] fcnc = [0] scnc = [0] sequenceall = [0] sequence1 = [0] sequence2 = [0] uptsequence1 = [0] uptsequence2 = [0] updowntsequence1 = [0] updowntsequence2 = [0] genome = [-1, 0, 1, 0, 1, 1, 1, 0, 1] def express(): global cnclist global fcnc global scnc for n in range(1, 8 + 1): if genome[n] == 1: cnclist[n].whethersec = True scnc.append(cnclist[n].num) else: fcnc.append(cnclist[n].num)