Spaces:
Sleeping
Sleeping
suhaas-code commited on
Commit ·
ea7aba8
1
Parent(s): 76acce0
phase2-graders-3
Browse files- tasks/graders.py +62 -42
tasks/graders.py
CHANGED
|
@@ -1,68 +1,88 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from typing import Dict, List
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
MAX_CHEMICAL_PER_STEP = 30.0
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
return max(0.0, min(1.0, float(value)))
|
| 12 |
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
|
|
|
| 16 |
return 0.0
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
-
def grade_yield_performance(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
def grade_chemical_efficiency(
|
| 28 |
-
total_fertilizer: float,
|
| 29 |
-
total_pesticide: float,
|
| 30 |
-
total_steps: int,
|
| 31 |
-
) -> Dict[str, float]:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 /
|
| 45 |
-
return {
|
|
|
|
|
|
|
|
|
|
| 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 |
-
) ->
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
+
}
|