image.domain\self_evolve\smart_repair.mbt

90.7% · 49 / 54 points · 166 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 智能修复 self_evolve/smart_repair
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 智能修复策略 — 多策略修复(保守/激进/最优)+ 质量评分
  7. 7// Smart repair: multi-strategy repair + quality scoring
  8. 8//
  9. 9// Phase 6 v287: 将退化修复从"单一策略"推向"多策略对比 + 最优选择"。
  10. 10
  11. 11///|
  12. 12/// 修复策略。
  13. 13/// 描述 Repair strategy.
  14. 14/// `#non_exhaustive` 表示未来可能添加新构造器
  15. 15#derive(Eq, @debug.Debug)
  16. 16#non_exhaustive
  17. 17pub(all) enum RepairStrategy {
  18. 18 Conservative // 保守:仅修复 Fatal 级退化
  19. 19 Aggressive // 激进:修复全部退化
  20. 20 Optimal // 最优:选择质量最高
  21. 21}
  22. 22
  23. 23///|
  24. 24pub extend RepairStrategy with @debug.Debug::{to_repr}
  25. 25
  26. 26///|
  27. 27pub extend RepairStrategy with Eq::{not_equal, equal}
  28. 28
  29. 29///|
  30. 30/// 单策略修复结果。
  31. 31/// 描述 Single strategy repair result.
  32. 32#derive(Eq, @debug.Debug)
  33. 33pub(all) struct StrategyRepairResult {
  34. 34 strategy : RepairStrategy
  35. 35 op_tag : String
  36. 36 degs_before : Int32
  37. 37 degs_after : Int32
  38. 38 quality_score : Int32
  39. 39 repair_desc : String
  40. 40}
  41. 41
  42. 42///|
  43. 43pub extend StrategyRepairResult with @debug.Debug::{to_repr}
  44. 44
  45. 45///|
  46. 46pub extend StrategyRepairResult with Eq::{not_equal, equal}
  47. 47
  48. 48// 保守修复:仅修复 Fatal 级退化(Resize/Crop 零尺寸、GaussianBlur sigma=0)
  49. 49
  50. 50///|
  51. 51fn repair_op_conservative(op : @imgir.IROp) -> @imgir.IROp {
  52. 52● match op {
  53. 53● Resize(w, h) => {
  54. 54● let nw = if w == 0 { 1 } else { w }
  55. 55● let nh = if h == 0 { 1 } else { h }
  56. 56 @imgir.Resize(nw, nh)
  57. 57 }
  58. 58● Crop(x, y, w, h) => {
  59. 59◐ let nw = if w == 0 { 1 } else { w }
  60. 60◐ let nh = if h == 0 { 1 } else { h }
  61. 61 @imgir.Crop(x, y, nw, nh)
  62. 62 }
  63. 63● GaussianBlur(r, sigma) => {
  64. 64◐ let nsigma = if sigma == 0.0F { 1.0F } else { sigma }
  65. 65 @imgir.GaussianBlur(r, nsigma)
  66. 66 }
  67. 67● _ => op
  68. 68 }
  69. 69}
  70. 70
  71. 71// 质量评分:修复后退化越少分越高(0-100)
  72. 72
  73. 73///|
  74. 74fn quality_score(op : @imgir.IROp) -> Int32 {
  75. 75● let degs = detect_degradation(op)
  76. 76● let score = 100 - degs.length() * 20
  77. 77 if score < 0 {
  78. 78○ 0
  79. 79 } else {
  80. 80● score
  81. 81 }
  82. 82}
  83. 83
  84. 84///|
  85. 85/// 按策略修复算子。
  86. 86/// 描述 Repair op with a given strategy.
  87. 87pub fn repair_op_strategy(
  88. 88 op : @imgir.IROp,
  89. 89 strategy : RepairStrategy,
  90. 90) -> @imgir.IROp {
  91. 91● match strategy {
  92. 92● Conservative => repair_op_conservative(op)
  93. 93● Aggressive => repair_op(op)
  94. 94● Optimal => repair_op(op)
  95. 95 }
  96. 96}
  97. 97
  98. 98///|
  99. 99/// 三策略对比修复。
  100. 100/// 比较 Compare all three strategies.
  101. 101pub fn repair_with_strategies(op : @imgir.IROp) -> Array[StrategyRepairResult] {
  102. 102● let strategies = [Conservative, Aggressive, Optimal]
  103. 103 let results : Array[StrategyRepairResult] = []
  104. 104● let degs_before = detect_degradation(op).length()
  105. 105 for s in strategies {
  106. 106● let repaired = repair_op_strategy(op, s)
  107. 107● let degs_after = detect_degradation(repaired).length()
  108. 108● let score = quality_score(repaired)
  109. 109● let desc = match s {
  110. 110 Conservative =>
  111. 111● "保守修复: " + degs_after.to_string() + " 退化剩余"
  112. 112● Aggressive => "激进修复: " + degs_after.to_string() + " 退化剩余"
  113. 113● Optimal => "最优修复: " + degs_after.to_string() + " 退化剩余"
  114. 114 }
  115. 115● results.push({
  116. 116 strategy: s,
  117. 117● op_tag: op.tag(),
  118. 118 degs_before,
  119. 119 degs_after,
  120. 120 quality_score: score,
  121. 121 repair_desc: desc,
  122. 122 })
  123. 123 }
  124. 124 results
  125. 125}
  126. 126
  127. 127///|
  128. 128/// 选择最优修复(质量分最高)。
  129. 129/// 选择 Select best repair (highest quality score).
  130. 130pub fn best_repair(op : @imgir.IROp) -> StrategyRepairResult {
  131. 131● let results = repair_with_strategies(op)
  132. 132 let mut best = results[0]
  133. 133 for r in results {
  134. 134● if r.quality_score > best.quality_score {
  135. 135● best = r
  136. 136 }
  137. 137 }
  138. 138 best
  139. 139}
  140. 140
  141. 141///|
  142. 142/// 对全部 19 种算子选择最优修复。
  143. 143/// 描述 Best repair for all 19 op variants.
  144. 144pub fn best_repair_all() -> Array[StrategyRepairResult] {
  145. 145● let results : Array[StrategyRepairResult] = []
  146. 146● for op in all_ops() {
  147. 147● results.push(best_repair(op))
  148. 148 }
  149. 149 results
  150. 150}
  151. 151
  152. 152///|
  153. 153/// 修复是否完全(退化清零)。
  154. 154/// 描述 Whether repair is complete (zero degradation).
  155. 155pub fn StrategyRepairResult::is_complete(self : StrategyRepairResult) -> Bool {
  156. 156● self.degs_after == 0
  157. 157}
  158. 158
  159. 159///|
  160. 160/// 修复是否改善了质量。
  161. 161/// 描述 Whether repair improved quality.
  162. 162pub fn StrategyRepairResult::is_improvement(
  163. 163 self : StrategyRepairResult,
  164. 164) -> Bool {
  165. 165○ self.degs_after < self.degs_before
  166. 166}