palak563 commited on
Commit
e1936c8
Β·
1 Parent(s): e7729e5

tasks file ai tasks stored and grader file stores ai reward or score

Browse files
Files changed (2) hide show
  1. graders.py +153 -0
  2. tasks.py +67 -0
graders.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # graders.py β€” reward functions and graders for all 3 tasks
2
+
3
+
4
+ # ─────────────────────────────
5
+ # REWARD β€” called every single day
6
+ # ─────────────────────────────
7
+
8
+ def compute_reward(soil_moisture, crop_health, water_action, rainfall):
9
+ """
10
+ Returns a daily score between -1.0 and 1.0
11
+ """
12
+ reward = 0.0
13
+
14
+ # Moisture check β€” ideal is 40% to 70%
15
+ if 40 <= soil_moisture <= 70:
16
+ reward += 0.5
17
+ elif 30 <= soil_moisture < 40 or 70 < soil_moisture <= 80:
18
+ reward += 0.2
19
+ else:
20
+ reward -= 0.3
21
+
22
+ # Crop health check
23
+ if crop_health >= 70:
24
+ reward += 0.3
25
+ elif crop_health >= 50:
26
+ reward += 0.1
27
+ else:
28
+ reward -= 0.2
29
+
30
+ # Penalize watering heavily when it's already raining a lot
31
+ if rainfall > 5 and water_action == 3:
32
+ reward -= 0.2
33
+
34
+ # Keep between -1.0 and 1.0
35
+ return round(max(-1.0, min(1.0, reward)), 2)
36
+
37
+
38
+ # ─────────────────────────────
39
+ # TASK 1 GRADER β€” Easy (7 days)
40
+ # ─────────────────────────────
41
+
42
+ def grade_task1(episode_history):
43
+ """
44
+ Score = fraction of days where moisture was between 40–70%
45
+ Returns 0.0 to 1.0
46
+ """
47
+ if not episode_history:
48
+ return 0.0
49
+
50
+ days_in_range = 0
51
+ for record in episode_history:
52
+ moisture = record.get("soil_moisture", 0)
53
+ if 40 <= moisture <= 70:
54
+ days_in_range += 1
55
+
56
+ score = days_in_range / len(episode_history)
57
+ return round(score, 2)
58
+
59
+
60
+ # ─────────────────────────────
61
+ # TASK 2 GRADER β€” Medium (14 days)
62
+ # ─────────────────────────────
63
+
64
+ def grade_task2(episode_history):
65
+ """
66
+ Score based on daily rewards across 14 days with weather.
67
+ Returns 0.0 to 1.0
68
+ """
69
+ if not episode_history:
70
+ return 0.0
71
+
72
+ total_reward = 0.0
73
+ max_possible = len(episode_history) * 0.8
74
+
75
+ for record in episode_history:
76
+ daily = compute_reward(
77
+ record.get("soil_moisture", 0),
78
+ record.get("crop_health", 0),
79
+ record.get("water_action", 0),
80
+ record.get("rainfall", 0)
81
+ )
82
+ total_reward += daily
83
+
84
+ score = total_reward / max_possible if max_possible > 0 else 0.0
85
+ return round(max(0.0, min(1.0, score)), 2)
86
+
87
+
88
+ # ─────────────────────────────
89
+ # TASK 3 GRADER β€” Hard (30 days)
90
+ # ─────────────────────────────
91
+
92
+ def grade_task3(episode_history):
93
+ """
94
+ Score based on crop stage requirements over 30 days.
95
+ Seedling (days 1-10), Growing (days 11-20), Mature (days 21-30)
96
+ Returns 0.0 to 1.0
97
+ """
98
+ if not episode_history:
99
+ return 0.0
100
+
101
+ day_scores = []
102
+
103
+ for record in episode_history:
104
+ day = record.get("day", 1)
105
+ moisture = record.get("soil_moisture", 0)
106
+ crop_health = record.get("crop_health", 0)
107
+
108
+ # Each stage needs different moisture
109
+ if day <= 10: # Seedling
110
+ ideal = 50 <= moisture <= 75
111
+ elif day <= 20: # Growing
112
+ ideal = 45 <= moisture <= 70
113
+ else: # Mature
114
+ ideal = 35 <= moisture <= 60
115
+
116
+ if ideal and crop_health >= 70:
117
+ day_scores.append(1.0)
118
+ elif ideal or crop_health >= 60:
119
+ day_scores.append(0.5)
120
+ else:
121
+ day_scores.append(0.0)
122
+
123
+ score = sum(day_scores) / len(day_scores)
124
+ return round(score, 2)
125
+
126
+
127
+ # ─────────────────────────────
128
+ # TEST β€” run this file to verify
129
+ # ─────────────────────────────
130
+
131
+ if __name__ == "__main__":
132
+ # Fake data to test all 3 graders
133
+ fake_7 = [
134
+ {"day": i, "soil_moisture": 55, "crop_health": 75,
135
+ "water_action": 2, "rainfall": 0}
136
+ for i in range(1, 8)
137
+ ]
138
+ fake_14 = [
139
+ {"day": i, "soil_moisture": 50 + i, "crop_health": 72,
140
+ "water_action": 1, "rainfall": i % 3}
141
+ for i in range(1, 15)
142
+ ]
143
+ fake_30 = [
144
+ {"day": i, "soil_moisture": 55, "crop_health": 70 + (i % 10),
145
+ "water_action": 2, "rainfall": i % 5}
146
+ for i in range(1, 31)
147
+ ]
148
+
149
+ print("Task 1 Score:", grade_task1(fake_7))
150
+ print("Task 2 Score:", grade_task2(fake_14))
151
+ print("Task 3 Score:", grade_task3(fake_30))
152
+
153
+ print("\nSample daily reward:", compute_reward(55, 78, 2, 0))
tasks.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tasks.py β€” defines all 3 tasks and their action schema
2
+
3
+ TASKS = [
4
+ {
5
+ "task_id": 1,
6
+ "name": "Basic Moisture Control",
7
+ "difficulty": "easy",
8
+ "description": "Keep soil moisture between 40–70% for 7 days.",
9
+ "max_steps": 7,
10
+ "goal": "moisture_in_range"
11
+ },
12
+ {
13
+ "task_id": 2,
14
+ "name": "Weather Adaptive Irrigation",
15
+ "difficulty": "medium",
16
+ "description": "Manage irrigation across 14 days with changing weather.",
17
+ "max_steps": 14,
18
+ "goal": "handle_weather"
19
+ },
20
+ {
21
+ "task_id": 3,
22
+ "name": "Full Season Management",
23
+ "difficulty": "hard",
24
+ "description": "Manage a full 30-day crop season across Seedling, Growing, and Mature stages.",
25
+ "max_steps": 30,
26
+ "goal": "full_season"
27
+ }
28
+ ]
29
+
30
+ ACTION_SCHEMA = {
31
+ "type": "discrete",
32
+ "n": 4,
33
+ "actions": {
34
+ 0: "no_water",
35
+ 1: "low_water",
36
+ 2: "medium_water",
37
+ 3: "high_water"
38
+ },
39
+ "description": "Choose how much water to give the crop each day."
40
+ }
41
+
42
+
43
+ def get_all_tasks():
44
+ """Returns the list of all tasks with action schema."""
45
+ return {
46
+ "tasks": TASKS,
47
+ "action_schema": ACTION_SCHEMA
48
+ }
49
+
50
+
51
+ def get_task_by_id(task_id):
52
+ """Returns a single task by its ID."""
53
+ for task in TASKS:
54
+ if task["task_id"] == task_id:
55
+ return task
56
+ return None
57
+
58
+
59
+ # Quick test
60
+ if __name__ == "__main__":
61
+ print("All tasks:")
62
+ result = get_all_tasks()
63
+ for task in result["tasks"]:
64
+ print(f" Task {task['task_id']}: {task['name']} ({task['difficulty']})")
65
+ print("\nAction schema:")
66
+ for k, v in result["action_schema"]["actions"].items():
67
+ print(f" {k} = {v}")