124 lines
6.7 KiB
Python
124 lines
6.7 KiB
Python
import random
|
|
|
|
import numpy as np
|
|
|
|
from dataloader import *
|
|
from lineopt_genetic import line_optimizer_genetic
|
|
from lineopt_heuristic import line_optimizer_heuristic
|
|
from lineopt_reconfiguration import line_optimizer_reconfiguration
|
|
from lineopt_hyperheuristic import line_optimizer_hyperheuristic
|
|
from lineopt_model import line_optimizer_model
|
|
|
|
from base_optimizer.optimizer_interface import *
|
|
|
|
|
|
def optimizer(pcb_data, component_data, line_optimizer, machine_optimizer, machine_number):
|
|
assembly_info = []
|
|
if line_optimizer == 'hyper-heuristic' or line_optimizer == 'heuristic' or line_optimizer == 'genetic' or \
|
|
line_optimizer == 'reconfiguration':
|
|
if machine_number > 1:
|
|
if line_optimizer == 'hyper-heuristic':
|
|
assignment_result = line_optimizer_hyperheuristic(component_data, pcb_data, machine_number)
|
|
elif line_optimizer == "heuristic":
|
|
assignment_result = line_optimizer_heuristic(component_data, machine_number)
|
|
elif line_optimizer == "genetic":
|
|
assignment_result = line_optimizer_genetic(component_data, machine_number)
|
|
else:
|
|
assignment_result = line_optimizer_reconfiguration(component_data, pcb_data, machine_number)
|
|
else:
|
|
assignment_result = [[]]
|
|
for _, data in component_data.iterrows():
|
|
assignment_result[-1].append(data.points)
|
|
partial_pcb_data, partial_component_data = convert_line_assigment(pcb_data, component_data, assignment_result)
|
|
|
|
for machine_index in range(machine_number):
|
|
assembly_info.append(
|
|
base_optimizer(machine_index + 1, partial_pcb_data[machine_index], partial_component_data[machine_index],
|
|
feeder_data=pd.DataFrame(columns=['slot', 'part', 'arg']), method=machine_optimizer,
|
|
hinter=True))
|
|
elif line_optimizer == 'model':
|
|
assembly_info = line_optimizer_model(component_data, pcb_data, machine_number)
|
|
else:
|
|
raise 'line optimizer method is not existed'
|
|
|
|
return assembly_info
|
|
|
|
|
|
@timer_wrapper
|
|
def main():
|
|
warnings.simplefilter(action='ignore', category=FutureWarning)
|
|
# 参数解析
|
|
parser = argparse.ArgumentParser(description='assembly line optimizer implementation')
|
|
parser.add_argument('--mode', default=1, type=int, help='mode: 0 -directly load pcb data without optimization '
|
|
'for data analysis, 1 -optimize pcb data')
|
|
parser.add_argument('--filename', default='PCB.txt', type=str, help='load pcb data')
|
|
parser.add_argument('--comp_register', default=1, type=int, help='register the component according the pcb data')
|
|
parser.add_argument('--machine_number', default=3, type=int, help='the number of machine in the assembly line')
|
|
parser.add_argument('--machine_optimizer', default='feeder-scan', type=str, help='optimizer for single machine')
|
|
parser.add_argument('--line_optimizer', default='hyper-heuristic', type=str, help='optimizer for PCB assembly line')
|
|
# parser.add_argument('--line_optimizer', default='genetic', type=str, help='optimizer for PCB assembly line')
|
|
parser.add_argument('--feeder_limit', default=1, type=int, help='the upper feeder limit for each type of component')
|
|
params = parser.parse_args()
|
|
|
|
# 结果输出显示所有行和列
|
|
pd.set_option('display.max_columns', None)
|
|
pd.set_option('display.max_rows', None)
|
|
if params.mode == 0:
|
|
partial_pcb_data, partial_component_data, _ = load_data(params.filename)
|
|
assembly_info = []
|
|
for machine_index in range(len(partial_pcb_data)):
|
|
component_result, cycle_result, feeder_slot_result, placement_result, head_sequence = \
|
|
convert_pcbdata_to_result(partial_pcb_data[machine_index], partial_component_data[machine_index])
|
|
print('----- Placement machine ' + str(machine_index) + ' ----- ')
|
|
info = placement_info_evaluation(partial_component_data[machine_index], partial_pcb_data[machine_index],
|
|
component_result, cycle_result, feeder_slot_result, placement_result,
|
|
head_sequence)
|
|
assembly_info.append(info)
|
|
optimization_assign_result(partial_component_data[machine_index], partial_pcb_data[machine_index],
|
|
component_result, cycle_result, feeder_slot_result, nozzle_hinter=True,
|
|
component_hinter=True, feeder_hinter=True)
|
|
info.print()
|
|
print('------------------------------ ')
|
|
else:
|
|
# 加载PCB数据
|
|
partial_pcb_data, partial_component_data, _ = load_data(params.filename)
|
|
pcb_data, component_data = merge_data(partial_pcb_data, partial_component_data)
|
|
|
|
assembly_info = optimizer(pcb_data, component_data, params.line_optimizer, params.machine_optimizer,
|
|
params.machine_number)
|
|
|
|
# index_list, part_list = [5, 6, 7, 8, 9, 10, 11, 12, 13], []
|
|
# for idx in index_list:
|
|
# part_list.append(component_data.iloc[idx].part)
|
|
# pcb_data = pcb_data[pcb_data['part'].isin(part_list)].reset_index(drop=True)
|
|
# component_data = component_data.iloc[index_list].reset_index(drop=True)
|
|
#
|
|
# from lineopt_hyperheuristic import DataMgr, Net
|
|
# data_mgr = DataMgr()
|
|
#
|
|
# cp_points, cp_nozzle = defaultdict(int), defaultdict(str)
|
|
# for _, data in component_data.iterrows():
|
|
# cp_points[data.part], cp_nozzle[data.part] = data.points, data.nz
|
|
#
|
|
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
# net = Net(input_size=data_mgr.get_feature(), output_size=1).to(device)
|
|
#
|
|
# net.load_state_dict(torch.load('model/net_model.pth'))
|
|
# board_width, board_height = pcb_data['x'].max() - pcb_data['x'].min(), pcb_data['y'].max() - pcb_data['y'].min()
|
|
# encoding = np.array(data_mgr.encode(cp_points, cp_nozzle, board_width, board_height))
|
|
# encoding = torch.from_numpy(encoding.reshape((-1, np.shape(encoding)[0]))).float().to("cuda")
|
|
# print(f'net pred time: {net(encoding)[0, 0].item():.3f}')
|
|
|
|
for machine_idx, info in enumerate(assembly_info):
|
|
print(f'assembly time for machine {machine_idx + 1: d}: {info.total_time: .3f} s, total placement: '
|
|
f'{info.total_points}, total component types {info.total_components: d}')
|
|
|
|
print(f'finial assembly time: {max(info.total_time for info in assembly_info): .3f} s, '
|
|
f'standard deviation: {np.std([info.total_time for info in assembly_info]): .3f}')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|
|
|