生产组件依赖关系下列表计算逻辑错误修正及正确输出实现
生产流程组件总执行时间计算代码修正
问题概述
现有Python代码用于计算生产流程中组件的总执行时间,需考虑组件间的层级依赖关系,但当前输出存在计算错误,核心问题是未正确递归处理多层依赖分支的乘积累加。
错误输出
['CT', 'X', 'Z'] [100, 1.0583, 1.0633] [200, 3.012, 5.873600000000001] [300, 1.79, 2.5220000000000002] ['Total', 0, 0]
错误原因
以Z列CT=200的计算为例,现有代码仅计算了直接依赖的乘积,未递归处理深层依赖(如G依赖F/E,而F/E依赖Z)。正确计算需包含所有分支的乘积项:
0.4804 * 1 * 3 + 0.351 * 1 * 2 + 0.77 * 1 * 4 + 0.2168 * 1 * 4 * 2 + 0.2168 * 1 * 3 * 1 = 7.608
预期正确输出
capacity_list = [ ['CT', 'X', 'Z'], [100, 1.0583 * 1, 1.0633 * 1], [200, 0.351 * 1 * 2 + 0.77 * 1 * 3, 0.4804 * 1 * 3 + 0.351 * 1 * 2 + 0.77 * 1 * 4 + 0.2168 * 1 * 4 * 2 + 0.2168 * 1 * 3 * 1], [300, 0.895 * 1 * 2, 0.895 * 1 * 2 + 0.244 * 1 * 3], ['Total', 4.8583, 11.1933] ]
现有代码
初始化数据
# Initial data routing_info_list = [ ['Art.', 'CT', 'Batch_Size', 'Setup_Time', 'Unit_Run_Time'], ['X', 100, 30, 0.0083, 1.0583], ['Z', 100, 30, 0.0033, 1.0633], ['D', 200, 40, 0.011, 0.351], ['D', 300, 30, 0.015, 0.895], ['E', 200, 10, 0.0, 0.77], ['F', 200, 25, 0.0304, 0.4804], ['F', 300, 50, 0.004, 0.244], ['G', 200, 25, 0.0068, 0.2168] ] capacity_list = [ ['CT', 'X', 'Z'], [100], [200], [300], ['Total'] ] # Represents the component relationship scheme in first image list_components_and_hierarchical_relationships = [ [ ['X', ['PRODUCT', 1]], ['D', [['X', 2]]], ['E', [['X', 3]]] ], [ ['Z', ['PRODUCT', 1]], ['F', [['Z', 3]]], ['D', [['Z', 2]]], ['E', [['Z', 4]]], ['G', [['F', 1]]], ['G', [['E', 2]]] ] ]
核心逻辑代码
# Function to search for Unit_Run_Time in routing_info_list def search_execution_time_func(articulo, ct): for row in routing_info_list[1:]: if row[0] == articulo and row[1] == ct: return row[4] return None # Function to process a product def process_product_info_func(components, ct): total_time = 0 for component, dependencies in components: run_time = search_execution_time_func(component, ct) if run_time is not None: # Multiply by the dependencies total_quantity = 1 for dep in dependencies: if isinstance(dep, list) and dep[0] != 'PRODUCT': total_quantity *= dep[1] print( str(run_time) + " hrs * " + str(total_quantity) ) total_time += run_time * total_quantity return total_time # Process each CT row from the product capacity list for each column for row in capacity_list[1:]: ct = row[0] times = [] for product_idx, product in enumerate(list_components_and_hierarchical_relationships): total_time = process_product_info_func(product, ct) times.append(total_time) row.extend(times) # Calculate the 'Total' row # Get the number of columns, excluding 'CT' num_columns = len(capacity_list[0]) - 1 # Sum the values of each column, ignoring the header row and the 'CT' column for col in range(1, num_columns + 1): total_sum = sum(row[col] for row in capacity_list[1:-1]) capacity_list[-1][col] = total_sum # Display the final result for row in capacity_list: print(row)
修正后的完整代码
# Initial data routing_info_list = [ ['Art.', 'CT', 'Batch_Size', 'Setup_Time', 'Unit_Run_Time'], ['X', 100, 30, 0.0083, 1.0583], ['Z', 100, 30, 0.0033, 1.0633], ['D', 200, 40, 0.011, 0.351], ['D', 300, 30, 0.015, 0.895], ['E', 200, 10, 0.0, 0.77], ['F', 200, 25, 0.0304, 0.4804], ['F', 300, 50, 0.004, 0.244], ['G', 200, 25, 0.0068, 0.2168] ] capacity_list = [ ['CT', 'X', 'Z'], [100], [200], [300], ['Total', 0, 0] # 初始化Total行元素,避免索引错误 ] # Represents the component relationship scheme list_components_and_hierarchical_relationships = [ [ ['X', ['PRODUCT', 1]], ['D', [['X', 2]]], ['E', [['X', 3]]] ], [ ['Z', ['PRODUCT', 1]], ['F', [['Z', 3]]], ['D', [['Z', 2]]], ['E', [['Z', 4]]], ['G', [['F', 1]]], ['G', [['E', 2]]] ] ] # Function to search for Unit_Run_Time in routing_info_list def search_execution_time_func(articulo, ct): for row in routing_info_list[1:]: if row[0] == articulo and row[1] == ct: return row[4] return None # 递归计算单个组件的总依赖乘积 def calculate_component_total(component, ct, components_map): run_time = search_execution_time_func(component, ct) if run_time is None: return 0 # 获取当前组件的依赖关系 dependencies = components_map.get(component, []) total = 0 for dep in dependencies: if dep[0] == 'PRODUCT': # 直接依赖产品,乘积为dep[1] total += run_time * dep[1] else: # 递归计算依赖组件的总乘积,再乘以当前依赖数量 dep_total = calculate_component_total(dep[0], ct, components_map) if dep_total != 0: total += run_time * dep[1] * (dep_total / search_execution_time_func(dep[0], ct)) return total if total != 0 else run_time * 1 # 处理无依赖的情况 # Function to process a product def process_product_info_func(components, ct): # 把组件列表转成字典,方便快速查找依赖 components_map = {} for comp, deps in components: if comp not in components_map: components_map[comp] = [] # 统一依赖格式为列表 components_map[comp].extend(deps if isinstance(deps[0], list) else [deps]) total_time = 0 for component in components_map: # 计算每个组件的完整依赖链总时间 component_total = calculate_component_total(component, ct, components_map) total_time += component_total return total_time # Process each CT row from the product capacity list for each column for row in capacity_list[1:-1]: # 排除Total行 ct = row[0] times = [] for product_idx, product in enumerate(list_components_and_hierarchical_relationships): total_time = process_product_info_func(product, ct) times.append(total_time) row.extend(times) # Calculate the 'Total' row num_columns = len(capacity_list[0]) - 1 for col in range(1, num_columns + 1): total_sum = sum(row[col] for row in capacity_list[1:-1]) capacity_list[-1][col] = round(total_sum, 4) # 保留四位小数,优化浮点精度 # Display the final result for row in capacity_list: print(row)
关键修正点
- 递归处理依赖链:新增
calculate_component_total递归函数,遍历每个组件的完整依赖分支,计算所有层级的乘积项,确保深层依赖(如G→F→Z)被正确累加。 - 组件依赖映射:将组件列表转换为字典,快速查找每个组件的依赖关系,提升处理效率。
- Total行初始化修复:提前初始化Total行的元素,避免赋值时出现索引越界错误。
- 浮点精度处理:对Total行的求和结果保留四位小数,避免不必要的浮点误差。
内容的提问来源于stack exchange,提问作者Elektvocal95
相关产品推荐
相关产品推荐

