suhaas-code commited on
Commit
ea7aba8
·
1 Parent(s): 76acce0

phase2-graders-3

Browse files
Files changed (1) hide show
  1. tasks/graders.py +62 -42
tasks/graders.py CHANGED
@@ -1,68 +1,88 @@
1
- from __future__ import annotations
2
-
3
- from typing import Dict, List
4
 
5
- MIN_REWARD_PER_STEP = -1.75
6
- MAX_REWARD_PER_STEP = 1.40
7
- MAX_CHEMICAL_PER_STEP = 30.0
8
 
 
9
 
10
- def clamp_01(value: float) -> float:
11
- return max(0.0, min(1.0, float(value)))
12
 
13
 
14
- def normalize(value: float, low: float, high: float) -> float:
15
- if high <= low:
 
16
  return 0.0
17
- return clamp_01((value - low) / (high - low))
 
 
18
 
19
 
20
- def grade_yield_performance(total_reward: float, total_steps: int) -> Dict[str, float]:
21
- min_total = MIN_REWARD_PER_STEP * total_steps
22
- max_total = MAX_REWARD_PER_STEP * total_steps
23
- score = normalize(total_reward, min_total, max_total)
24
- return {"task_id": "task_easy_yield", "score": score}
 
 
 
 
 
 
 
 
 
25
 
26
 
27
  def grade_chemical_efficiency(
28
- total_fertilizer: float,
29
- total_pesticide: float,
30
- total_steps: int,
31
- ) -> Dict[str, float]:
32
- total_chemical_use = total_fertilizer + total_pesticide
33
- max_chemical_use = MAX_CHEMICAL_PER_STEP * total_steps
34
- score = 1.0 - normalize(total_chemical_use, 0.0, max_chemical_use)
35
- return {"task_id": "task_medium_chemical_efficiency", "score": clamp_01(score)}
 
 
 
 
36
 
37
 
38
  def grade_sustainability_balance(
39
- total_yield: float,
40
- total_fertilizer: float,
41
- total_pesticide: float,
42
- ) -> Dict[str, float]:
 
43
  ratio = total_yield / (total_fertilizer + total_pesticide + 1.0)
44
- score = ratio / (ratio + 1.0)
45
- return {"task_id": "task_hard_sustainability_balance", "score": clamp_01(score)}
 
 
 
46
 
47
 
48
  def grade_all(
49
- total_reward: float,
50
- total_yield: float,
51
- total_fertilizer: float,
52
- total_pesticide: float,
53
- total_steps: int,
54
- ) -> List[Dict[str, float]]:
55
- return [
56
- grade_yield_performance(
57
- total_reward=total_reward, total_steps=total_steps),
58
- grade_chemical_efficiency(
 
 
 
59
  total_fertilizer=total_fertilizer,
60
  total_pesticide=total_pesticide,
61
  total_steps=total_steps,
62
  ),
63
- grade_sustainability_balance(
64
  total_yield=total_yield,
65
  total_fertilizer=total_fertilizer,
66
  total_pesticide=total_pesticide,
67
  ),
68
- ]
 
1
+ """Task grading functions for FarmRL OpenEnv submission.
 
 
2
 
3
+ Each grader returns a normalized score in [0, 1].
4
+ """
 
5
 
6
+ from __future__ import annotations
7
 
8
+ from typing import Dict
 
9
 
10
 
11
+ def _clamp01(value: float) -> float:
12
+ """Clamp a numeric value to the [0, 1] interval."""
13
+ if value < 0.0:
14
  return 0.0
15
+ if value > 1.0:
16
+ return 1.0
17
+ return value
18
 
19
 
20
+ def grade_yield_performance(
21
+ total_reward: float = 0.0,
22
+ total_steps: int = 0,
23
+ ) -> Dict[str, float | str]:
24
+ """Grade yield performance using average reward per step.
25
+
26
+ Scale factor keeps scores in a useful range while staying normalized.
27
+ """
28
+ avg_reward = total_reward / total_steps if total_steps > 0 else 0.0
29
+ score = _clamp01(avg_reward / 10.0)
30
+ return {
31
+ "task_id": "task_easy_yield",
32
+ "score": score,
33
+ }
34
 
35
 
36
  def grade_chemical_efficiency(
37
+ total_fertilizer: float = 0.0,
38
+ total_pesticide: float = 0.0,
39
+ total_steps: int = 0,
40
+ ) -> Dict[str, float | str]:
41
+ """Grade chemical efficiency by penalizing chemical use per step."""
42
+ steps = total_steps if total_steps > 0 else 1
43
+ chemical_per_step = (total_fertilizer + total_pesticide) / steps
44
+ score = 1.0 - _clamp01(chemical_per_step / 10.0)
45
+ return {
46
+ "task_id": "task_medium_chemical_efficiency",
47
+ "score": score,
48
+ }
49
 
50
 
51
  def grade_sustainability_balance(
52
+ total_yield: float = 0.0,
53
+ total_fertilizer: float = 0.0,
54
+ total_pesticide: float = 0.0,
55
+ ) -> Dict[str, float | str]:
56
+ """Grade sustainability using yield-to-chemical-input ratio."""
57
  ratio = total_yield / (total_fertilizer + total_pesticide + 1.0)
58
+ score = _clamp01(ratio / 5.0)
59
+ return {
60
+ "task_id": "task_hard_sustainability_balance",
61
+ "score": score,
62
+ }
63
 
64
 
65
  def grade_all(
66
+ total_reward: float = 0.0,
67
+ total_yield: float = 0.0,
68
+ total_fertilizer: float = 0.0,
69
+ total_pesticide: float = 0.0,
70
+ total_steps: int = 0,
71
+ ) -> Dict[str, Dict[str, float | str]]:
72
+ """Return scores for all tasks in one call."""
73
+ return {
74
+ "task_easy_yield": grade_yield_performance(
75
+ total_reward=total_reward,
76
+ total_steps=total_steps,
77
+ ),
78
+ "task_medium_chemical_efficiency": grade_chemical_efficiency(
79
  total_fertilizer=total_fertilizer,
80
  total_pesticide=total_pesticide,
81
  total_steps=total_steps,
82
  ),
83
+ "task_hard_sustainability_balance": grade_sustainability_balance(
84
  total_yield=total_yield,
85
  total_fertilizer=total_fertilizer,
86
  total_pesticide=total_pesticide,
87
  ),
88
+ }