score_relu.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import json
  2. from pathlib import Path
  3. def read_config(file_path):
  4. with open(file_path, 'r', encoding='utf-8') as file:
  5. config = json.load(file)
  6. return config
  7. def calculate_hydraulic_fault_weights(faults, config):
  8. # 初始化总权值
  9. total_weight = 0
  10. if '液压故障' in faults:
  11. total_weight += config['液压故障']['weight']
  12. else:
  13. # 检查左液压子系统
  14. left_hydraulic_subsystem = config['左液压子系统']
  15. if any(fault in faults for fault in left_hydraulic_subsystem['faults']):
  16. total_weight += left_hydraulic_subsystem['weight']
  17. else:
  18. subsystem_total_weight = 0
  19. for subsystem_name, subsystem_data in left_hydraulic_subsystem['subsystems'].items():
  20. relevant_fault_indices = [i for i, fault in enumerate(subsystem_data['faults']) if fault in faults]
  21. if relevant_fault_indices:
  22. if subsystem_data.get('max_weight', False):
  23. max_weight = max(subsystem_data['weights'][i] for i in relevant_fault_indices)
  24. subsystem_total_weight = max(subsystem_total_weight, max_weight)
  25. elif subsystem_data.get('special_rules'):
  26. applied_special_rule = False
  27. for rule in subsystem_data['special_rules']:
  28. if all(condition in faults for condition in rule['condition']):
  29. subsystem_total_weight += rule['weight']
  30. applied_special_rule = True
  31. break
  32. if not applied_special_rule:
  33. subsystem_total_weight += sum(subsystem_data['weights'][i] for i in relevant_fault_indices)
  34. else:
  35. subsystem_total_weight += sum(subsystem_data['weights'][i] for i in relevant_fault_indices)
  36. total_weight += subsystem_total_weight
  37. # 检查右液压子系统
  38. right_hydraulic_subsystem = config['右液压子系统']
  39. if any(fault in faults for fault in right_hydraulic_subsystem['faults']):
  40. total_weight += right_hydraulic_subsystem['weight']
  41. else:
  42. subsystem_total_weight = 0
  43. for subsystem_name, subsystem_data in right_hydraulic_subsystem['subsystems'].items():
  44. relevant_fault_indices = [i for i, fault in enumerate(subsystem_data['faults']) if fault in faults]
  45. if relevant_fault_indices:
  46. if subsystem_data.get('max_weight', False):
  47. max_weight = max(subsystem_data['weights'][i] for i in relevant_fault_indices)
  48. subsystem_total_weight = max(subsystem_total_weight, max_weight)
  49. elif subsystem_data.get('special_rules'):
  50. applied_special_rule = False
  51. for rule in subsystem_data['special_rules']:
  52. if all(condition in faults for condition in rule['condition']):
  53. subsystem_total_weight += rule['weight']
  54. applied_special_rule = True
  55. break
  56. if not applied_special_rule:
  57. subsystem_total_weight += sum(subsystem_data['weights'][i] for i in relevant_fault_indices)
  58. else:
  59. subsystem_total_weight += sum(subsystem_data['weights'][i] for i in relevant_fault_indices)
  60. total_weight += subsystem_total_weight
  61. return total_weight
  62. # 示例用法
  63. if __name__ == "__main__":
  64. current_dir = Path(__file__).parent
  65. json_path = current_dir.parent / "config" / "score_" \
  66. "config.json"
  67. config = read_config(json_path)
  68. faults_occurred = ['液压泵1低压', '液压泵3传感器故障']
  69. total_weight = calculate_hydraulic_fault_weights(faults_occurred, config)
  70. print(f"Total Weight: {total_weight}")