12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import json
- from pathlib import Path
- def read_config(file_path):
- with open(file_path, 'r', encoding='utf-8') as file:
- config = json.load(file)
- return config
- def calculate_hydraulic_fault_weights(faults, config):
- # 初始化总权值
- total_weight = 0
- if '液压故障' in faults:
- total_weight += config['液压故障']['weight']
- else:
- # 检查左液压子系统
- left_hydraulic_subsystem = config['左液压子系统']
- if any(fault in faults for fault in left_hydraulic_subsystem['faults']):
- total_weight += left_hydraulic_subsystem['weight']
- else:
- subsystem_total_weight = 0
- for subsystem_name, subsystem_data in left_hydraulic_subsystem['subsystems'].items():
- relevant_fault_indices = [i for i, fault in enumerate(subsystem_data['faults']) if fault in faults]
- if relevant_fault_indices:
- if subsystem_data.get('max_weight', False):
- max_weight = max(subsystem_data['weights'][i] for i in relevant_fault_indices)
- subsystem_total_weight = max(subsystem_total_weight, max_weight)
- else:
- # 特殊处理液压泵子系统的权重计算
- if '左液压泵超压' in faults:
- subsystem_total_weight += 0.2
- else:
- pump1_faults = ['液压泵1低压', '液压泵1传感器故障']
- pump3_faults = ['液压泵3低压', '液压泵3传感器故障']
- pump1_occurred = any(fault in faults for fault in pump1_faults)
- pump3_occurred = any(fault in faults for fault in pump3_faults)
- if pump1_occurred:
- subsystem_total_weight += 0.1
- if pump3_occurred:
- subsystem_total_weight += 0.1
- total_weight += subsystem_total_weight
- # 检查右液压子系统
- right_hydraulic_subsystem = config['右液压子系统']
- if any(fault in faults for fault in right_hydraulic_subsystem['faults']):
- total_weight += right_hydraulic_subsystem['weight']
- else:
- subsystem_total_weight = 0
- for subsystem_name, subsystem_data in right_hydraulic_subsystem['subsystems'].items():
- relevant_fault_indices = [i for i, fault in enumerate(subsystem_data['faults']) if fault in faults]
- if relevant_fault_indices:
- if subsystem_data.get('max_weight', False):
- max_weight = max(subsystem_data['weights'][i] for i in relevant_fault_indices)
- subsystem_total_weight = max(subsystem_total_weight, max_weight)
- else:
- subsystem_total_weight += sum(subsystem_data['weights'][i] for i in relevant_fault_indices)
- total_weight += subsystem_total_weight
- return total_weight
- # 示例用法
- if __name__ == "__main__":
- current_dir = Path(__file__).parent
- json_path = current_dir.parent / "config" / "score_config.json"
- config = read_config(json_path)
- faults_occurred = ["液压泵1低压", "液压泵1传感器故障"]
- total_weight = calculate_hydraulic_fault_weights(faults_occurred, config)
- print(f"Total Weight: {total_weight}")
|