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