File size: 2,212 Bytes
a6b3f26
 
 
00c1f31
eb42730
00c1f31
ba52031
2058884
00c1f31
2058884
 
 
 
a6b3f26
 
 
2058884
a6b3f26
 
 
 
00c1f31
a6b3f26
 
2058884
a6b3f26
 
 
 
00c1f31
a6b3f26
 
00c1f31
2058884
 
 
 
 
 
 
 
 
 
 
 
00c1f31
2058884
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00c1f31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import ast
import difflib

def _clamp(val: float) -> float:
    return float(round(min(max(val, 0.001), 0.999), 4))

def grade(pass_rate: float) -> float:
    """Baseline reward — direct pass rate (used as fallback)."""
    return _clamp(pass_rate)


def grade_by_comparison(submitted: str, reference: str) -> float:
    """
    Grades submitted code against reference code, prioritizing semantic exactness.
    - First attempts AST parsing: if both codes produce identical ASTs, returns 1.0.
    - If AST parsing fails or differs, falls back to difflib SequenceMatcher on sanitized lines.
    """
    try:
        sub_ast = ast.unparse(ast.parse(submitted))
        ref_ast = ast.unparse(ast.parse(reference))
        if sub_ast == ref_ast:
            return 0.9999
    except Exception:
        pass  # Fall back to token comparison if syntax is invalid

    sub_lines = [line.strip() for line in submitted.splitlines() if line.strip()]
    ref_lines = [line.strip() for line in reference.splitlines() if line.strip()]
    
    if not ref_lines:
        return 0.0001 if sub_lines else 0.9999
        
    matcher = difflib.SequenceMatcher(None, sub_lines, ref_lines)
    return _clamp(matcher.ratio())


def grade_with_steps(pass_rate: float, step_count: int, max_steps: int = 40) -> float:
    """
    Shaped reward that incentivises efficiency.

    - Partial credit: linear pass_rate contribution
    - Step penalty: -0.01 per step after the first 3 (discourages thrashing), capped at -0.3
    - Completion bonus: +0.1 flat for reaching pass_rate == 1.0
    - Efficiency bonus: up to +0.2 for solving early (only on full solve)
    """
    if pass_rate == 0.0:
        return 0.0001

    base = float(pass_rate)

    # Step penalty: starts after step 3, max -0.3
    penalty = min(max(0.0, (step_count - 3) * 0.01), 0.3)

    # Completion bonus
    completion_bonus = 0.1 if pass_rate == 1.0 else 0.0

    # Efficiency bonus: only on full solve, scales with how early
    efficiency_bonus = 0.0
    if pass_rate == 1.0 and max_steps > 0:
        efficiency_bonus = 0.2 * max(0.0, 1.0 - step_count / max_steps)

    reward = base - penalty + completion_bonus + efficiency_bonus
    return _clamp(reward)