Vidhiii1711 commited on
Commit
acbbeb5
·
1 Parent(s): e77e322

fixing ranges b/w 0 to 1

Browse files
my_env/graders.py CHANGED
@@ -1,10 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # graders.py — reward functions and graders for all 3 tasks
2
 
3
  def health_to_number(crop_health_str) -> int:
4
- """
5
- Converts crop health string to number for grading.
6
- poor=25, fair=50, good=75, excellent=100
7
- """
8
  mapping = {
9
  "poor": 25,
10
  "fair": 50,
@@ -17,13 +177,8 @@ def health_to_number(crop_health_str) -> int:
17
 
18
 
19
  def compute_reward(soil_moisture, crop_health, water_action, rainfall, temperature=30):
20
- """
21
- Returns a daily score between -1.0 and 1.0
22
- Now considers temperature too!
23
- """
24
  reward = 0.0
25
 
26
- # Moisture check — ideal is 40% to 70%
27
  if 40 <= soil_moisture <= 70:
28
  reward += 0.5
29
  elif 30 <= soil_moisture < 40 or 70 < soil_moisture <= 80:
@@ -31,7 +186,6 @@ def compute_reward(soil_moisture, crop_health, water_action, rainfall, temperatu
31
  else:
32
  reward -= 0.3
33
 
34
- # Crop health check
35
  health_num = health_to_number(crop_health)
36
  if health_num >= 70:
37
  reward += 0.3
@@ -40,25 +194,32 @@ def compute_reward(soil_moisture, crop_health, water_action, rainfall, temperatu
40
  else:
41
  reward -= 0.2
42
 
43
- # Penalize watering heavily when raining a lot
44
  if rainfall > 15 and water_action == 3:
45
  reward -= 0.2
46
 
47
- # Penalize not watering enough on very hot days
48
  if temperature >= 38 and soil_moisture < 40 and water_action == 0:
49
  reward -= 0.2
50
 
51
  return round(max(-1.0, min(1.0, reward)), 2)
52
 
 
 
 
 
 
 
 
 
 
53
  # TASK 1 GRADER — Easy (7 days)
54
 
55
  def grade_task1(episode_history):
56
  """
57
  Score = fraction of days where moisture was between 40-70%
58
- Returns 0.0 to 1.0
59
  """
60
  if not episode_history:
61
- return 0.0
62
 
63
  days_in_range = 0
64
  for record in episode_history:
@@ -66,18 +227,19 @@ def grade_task1(episode_history):
66
  if 40 <= moisture <= 70:
67
  days_in_range += 1
68
 
69
- score = days_in_range / len(episode_history)
70
- return round(score, 2)
 
71
 
72
  # TASK 2 GRADER — Medium (14 days)
73
 
74
  def grade_task2(episode_history):
75
  """
76
  Score based on daily rewards across 14 days with weather.
77
- Returns 0.0 to 1.0
78
  """
79
  if not episode_history:
80
- return 0.0
81
 
82
  total_reward = 0.0
83
  max_possible = len(episode_history) * 0.8
@@ -92,8 +254,8 @@ def grade_task2(episode_history):
92
  )
93
  total_reward += daily
94
 
95
- score = total_reward / max_possible if max_possible > 0 else 0.0
96
- return round(max(0.0, min(1.0, score)), 2)
97
 
98
 
99
  # TASK 3 GRADER — Hard (30 days)
@@ -101,44 +263,40 @@ def grade_task2(episode_history):
101
  def grade_task3(episode_history):
102
  """
103
  Score based on crop stage requirements over 30 days.
104
- Seedling (days 1-10), Growing (days 11-20), Mature (days 21-30)
105
- Returns 0.0 to 1.0
106
  """
107
  if not episode_history:
108
- return 0.0
109
 
110
  day_scores = []
111
 
112
  for record in episode_history:
113
- day = record.get("day") or record.get("day_number", 1)
114
- moisture = record.get("soil_moisture", 0)
115
  crop_health = health_to_number(record.get("crop_health", "fair"))
116
  temperature = record.get("temperature", 30)
117
 
118
- # Each stage needs different moisture
119
- if day <= 10: # Seedling
120
  ideal = 50 <= moisture <= 75
121
- elif day <= 20: # Growing
122
  ideal = 45 <= moisture <= 70
123
- else: # Mature
124
  ideal = 35 <= moisture <= 60
125
 
126
- # Extra credit for managing hot days well
127
  hot_day_managed = temperature >= 38 and moisture >= 40
128
 
129
  if ideal and crop_health >= 70:
130
- day_scores.append(1.0)
131
  elif ideal or crop_health >= 60:
132
  day_scores.append(0.5)
133
  elif hot_day_managed:
134
  day_scores.append(0.3)
135
  else:
136
- day_scores.append(0.0)
137
 
138
- score = sum(day_scores) / len(day_scores)
139
- return round(score, 2)
140
 
141
- # TEST
142
 
143
  if __name__ == "__main__":
144
  fake_7 = [
 
1
+ # # graders.py — reward functions and graders for all 3 tasks
2
+
3
+ # def health_to_number(crop_health_str) -> int:
4
+ # """
5
+ # Converts crop health string to number for grading.
6
+ # poor=25, fair=50, good=75, excellent=100
7
+ # """
8
+ # mapping = {
9
+ # "poor": 25,
10
+ # "fair": 50,
11
+ # "good": 75,
12
+ # "excellent": 100
13
+ # }
14
+ # if isinstance(crop_health_str, (int, float)):
15
+ # return int(crop_health_str)
16
+ # return mapping.get(str(crop_health_str).lower(), 50)
17
+
18
+
19
+ # def compute_reward(soil_moisture, crop_health, water_action, rainfall, temperature=30):
20
+ # """
21
+ # Returns a daily score between -1.0 and 1.0
22
+ # Now considers temperature too!
23
+ # """
24
+ # reward = 0.0
25
+
26
+ # # Moisture check — ideal is 40% to 70%
27
+ # if 40 <= soil_moisture <= 70:
28
+ # reward += 0.5
29
+ # elif 30 <= soil_moisture < 40 or 70 < soil_moisture <= 80:
30
+ # reward += 0.2
31
+ # else:
32
+ # reward -= 0.3
33
+
34
+ # # Crop health check
35
+ # health_num = health_to_number(crop_health)
36
+ # if health_num >= 70:
37
+ # reward += 0.3
38
+ # elif health_num >= 50:
39
+ # reward += 0.1
40
+ # else:
41
+ # reward -= 0.2
42
+
43
+ # # Penalize watering heavily when raining a lot
44
+ # if rainfall > 15 and water_action == 3:
45
+ # reward -= 0.2
46
+
47
+ # # Penalize not watering enough on very hot days
48
+ # if temperature >= 38 and soil_moisture < 40 and water_action == 0:
49
+ # reward -= 0.2
50
+
51
+ # return round(max(-1.0, min(1.0, reward)), 2)
52
+
53
+ # # TASK 1 GRADER — Easy (7 days)
54
+
55
+ # def grade_task1(episode_history):
56
+ # """
57
+ # Score = fraction of days where moisture was between 40-70%
58
+ # Returns 0.0 to 1.0
59
+ # """
60
+ # if not episode_history:
61
+ # return 0.0
62
+
63
+ # days_in_range = 0
64
+ # for record in episode_history:
65
+ # moisture = record.get("soil_moisture", 0)
66
+ # if 40 <= moisture <= 70:
67
+ # days_in_range += 1
68
+
69
+ # score = days_in_range / len(episode_history)
70
+ # return round(score, 2)
71
+
72
+ # # TASK 2 GRADER — Medium (14 days)
73
+
74
+ # def grade_task2(episode_history):
75
+ # """
76
+ # Score based on daily rewards across 14 days with weather.
77
+ # Returns 0.0 to 1.0
78
+ # """
79
+ # if not episode_history:
80
+ # return 0.0
81
+
82
+ # total_reward = 0.0
83
+ # max_possible = len(episode_history) * 0.8
84
+
85
+ # for record in episode_history:
86
+ # daily = compute_reward(
87
+ # record.get("soil_moisture", 0),
88
+ # health_to_number(record.get("crop_health", "fair")),
89
+ # record.get("water_action", 0),
90
+ # record.get("rainfall", 0),
91
+ # record.get("temperature", 30)
92
+ # )
93
+ # total_reward += daily
94
+
95
+ # score = total_reward / max_possible if max_possible > 0 else 0.0
96
+ # return round(max(0.0, min(1.0, score)), 2)
97
+
98
+
99
+ # # TASK 3 GRADER — Hard (30 days)
100
+
101
+ # def grade_task3(episode_history):
102
+ # """
103
+ # Score based on crop stage requirements over 30 days.
104
+ # Seedling (days 1-10), Growing (days 11-20), Mature (days 21-30)
105
+ # Returns 0.0 to 1.0
106
+ # """
107
+ # if not episode_history:
108
+ # return 0.0
109
+
110
+ # day_scores = []
111
+
112
+ # for record in episode_history:
113
+ # day = record.get("day") or record.get("day_number", 1)
114
+ # moisture = record.get("soil_moisture", 0)
115
+ # crop_health = health_to_number(record.get("crop_health", "fair"))
116
+ # temperature = record.get("temperature", 30)
117
+
118
+ # # Each stage needs different moisture
119
+ # if day <= 10: # Seedling
120
+ # ideal = 50 <= moisture <= 75
121
+ # elif day <= 20: # Growing
122
+ # ideal = 45 <= moisture <= 70
123
+ # else: # Mature
124
+ # ideal = 35 <= moisture <= 60
125
+
126
+ # # Extra credit for managing hot days well
127
+ # hot_day_managed = temperature >= 38 and moisture >= 40
128
+
129
+ # if ideal and crop_health >= 70:
130
+ # day_scores.append(1.0)
131
+ # elif ideal or crop_health >= 60:
132
+ # day_scores.append(0.5)
133
+ # elif hot_day_managed:
134
+ # day_scores.append(0.3)
135
+ # else:
136
+ # day_scores.append(0.0)
137
+
138
+ # score = sum(day_scores) / len(day_scores)
139
+ # return round(score, 2)
140
+
141
+ # # TEST
142
+
143
+ # if __name__ == "__main__":
144
+ # fake_7 = [
145
+ # {"day": i, "soil_moisture": 55, "crop_health": "good",
146
+ # "water_action": 2, "rainfall": 0, "temperature": 32}
147
+ # for i in range(1, 8)
148
+ # ]
149
+ # fake_14 = [
150
+ # {"day": i, "soil_moisture": 50 + i, "crop_health": "fair",
151
+ # "water_action": 1, "rainfall": i % 3, "temperature": 30 + i}
152
+ # for i in range(1, 15)
153
+ # ]
154
+ # fake_30 = [
155
+ # {"day": i, "soil_moisture": 55, "crop_health": "good",
156
+ # "water_action": 2, "rainfall": i % 5, "temperature": 35}
157
+ # for i in range(1, 31)
158
+ # ]
159
+
160
+ # print("Task 1 Score:", grade_task1(fake_7))
161
+ # print("Task 2 Score:", grade_task2(fake_14))
162
+ # print("Task 3 Score:", grade_task3(fake_30))
163
+ # print("Sample reward:", compute_reward(55, "good", 2, 0, 38))
164
+
165
  # graders.py — reward functions and graders for all 3 tasks
166
 
167
  def health_to_number(crop_health_str) -> int:
 
 
 
 
168
  mapping = {
169
  "poor": 25,
170
  "fair": 50,
 
177
 
178
 
179
  def compute_reward(soil_moisture, crop_health, water_action, rainfall, temperature=30):
 
 
 
 
180
  reward = 0.0
181
 
 
182
  if 40 <= soil_moisture <= 70:
183
  reward += 0.5
184
  elif 30 <= soil_moisture < 40 or 70 < soil_moisture <= 80:
 
186
  else:
187
  reward -= 0.3
188
 
 
189
  health_num = health_to_number(crop_health)
190
  if health_num >= 70:
191
  reward += 0.3
 
194
  else:
195
  reward -= 0.2
196
 
 
197
  if rainfall > 15 and water_action == 3:
198
  reward -= 0.2
199
 
 
200
  if temperature >= 38 and soil_moisture < 40 and water_action == 0:
201
  reward -= 0.2
202
 
203
  return round(max(-1.0, min(1.0, reward)), 2)
204
 
205
+
206
+ def _clamp(score: float) -> float:
207
+ """
208
+ Clamp score to strictly between 0.0 and 1.0
209
+ as required by hackathon validator (0.0 and 1.0 not allowed).
210
+ """
211
+ return round(max(0.01, min(0.99, score)), 4)
212
+
213
+
214
  # TASK 1 GRADER — Easy (7 days)
215
 
216
  def grade_task1(episode_history):
217
  """
218
  Score = fraction of days where moisture was between 40-70%
219
+ Returns strictly between 0.01 and 0.99
220
  """
221
  if not episode_history:
222
+ return 0.01
223
 
224
  days_in_range = 0
225
  for record in episode_history:
 
227
  if 40 <= moisture <= 70:
228
  days_in_range += 1
229
 
230
+ raw_score = days_in_range / len(episode_history)
231
+ return _clamp(raw_score)
232
+
233
 
234
  # TASK 2 GRADER — Medium (14 days)
235
 
236
  def grade_task2(episode_history):
237
  """
238
  Score based on daily rewards across 14 days with weather.
239
+ Returns strictly between 0.01 and 0.99
240
  """
241
  if not episode_history:
242
+ return 0.01
243
 
244
  total_reward = 0.0
245
  max_possible = len(episode_history) * 0.8
 
254
  )
255
  total_reward += daily
256
 
257
+ raw_score = total_reward / max_possible if max_possible > 0 else 0.0
258
+ return _clamp(raw_score)
259
 
260
 
261
  # TASK 3 GRADER — Hard (30 days)
 
263
  def grade_task3(episode_history):
264
  """
265
  Score based on crop stage requirements over 30 days.
266
+ Returns strictly between 0.01 and 0.99
 
267
  """
268
  if not episode_history:
269
+ return 0.01
270
 
271
  day_scores = []
272
 
273
  for record in episode_history:
274
+ day = record.get("day", record.get("day_number", 1))
275
+ moisture = record.get("soil_moisture", 0)
276
  crop_health = health_to_number(record.get("crop_health", "fair"))
277
  temperature = record.get("temperature", 30)
278
 
279
+ if day <= 10: # Seedling
 
280
  ideal = 50 <= moisture <= 75
281
+ elif day <= 20: # Growing
282
  ideal = 45 <= moisture <= 70
283
+ else: # Mature
284
  ideal = 35 <= moisture <= 60
285
 
 
286
  hot_day_managed = temperature >= 38 and moisture >= 40
287
 
288
  if ideal and crop_health >= 70:
289
+ day_scores.append(0.98)
290
  elif ideal or crop_health >= 60:
291
  day_scores.append(0.5)
292
  elif hot_day_managed:
293
  day_scores.append(0.3)
294
  else:
295
+ day_scores.append(0.02)
296
 
297
+ raw_score = sum(day_scores) / len(day_scores)
298
+ return _clamp(raw_score)
299
 
 
300
 
301
  if __name__ == "__main__":
302
  fake_7 = [
server/__pycache__/app.cpython-313.pyc CHANGED
Binary files a/server/__pycache__/app.cpython-313.pyc and b/server/__pycache__/app.cpython-313.pyc differ
 
server/__pycache__/my_env_environment.cpython-313.pyc CHANGED
Binary files a/server/__pycache__/my_env_environment.cpython-313.pyc and b/server/__pycache__/my_env_environment.cpython-313.pyc differ
 
server/app.py CHANGED
@@ -155,14 +155,24 @@ async def grade_episode(request: Request):
155
  status_code=400
156
  )
157
 
158
- score = graders[task_id](episode_history)
 
 
 
 
 
 
 
 
 
 
159
 
160
  return JSONResponse(content={
161
- "task_id": task_id,
162
- "score": score,
163
- "min_score": 0.0,
164
- "max_score": 1.0,
165
- })
166
 
167
  except Exception as e:
168
  return JSONResponse(
 
155
  status_code=400
156
  )
157
 
158
+ # score = graders[task_id](episode_history)
159
+
160
+ # return JSONResponse(content={
161
+ # "task_id": task_id,
162
+ # "score": score,
163
+ # "min_score": 0.0,
164
+ # "max_score": 1.0,
165
+ # })
166
+ raw_score = graders[task_id](episode_history)
167
+ # Clamp strictly between 0 and 1
168
+ score = round(max(0.01, min(0.99, float(raw_score))), 4)
169
 
170
  return JSONResponse(content={
171
+ "task_id": task_id,
172
+ "score": score,
173
+ "min_score": 0.01,
174
+ "max_score": 0.99,
175
+ })
176
 
177
  except Exception as e:
178
  return JSONResponse(
server/my_env_environment.py CHANGED
@@ -400,20 +400,20 @@ class FarmEnvironment(Environment):
400
  low, high = ideal_ranges[self._crop_stage]
401
 
402
  if low <= self._soil_moisture <= high:
403
- reward = 1.0
404
  elif (low - 10) <= self._soil_moisture <= (high + 10):
405
  reward = 0.5
406
  elif self._soil_moisture < 20:
407
- reward = -1.0
408
  elif self._soil_moisture > 90:
409
  reward = -0.5
410
  else:
411
- reward = 0.0
412
 
413
  if self._rainfall > 15 and water_added > 0:
414
  reward -= 0.3
415
 
416
- return round(max(-1.0, min(1.0, reward)), 2)
417
 
418
  @property
419
  def state(self) -> State:
 
400
  low, high = ideal_ranges[self._crop_stage]
401
 
402
  if low <= self._soil_moisture <= high:
403
+ reward = 0.98
404
  elif (low - 10) <= self._soil_moisture <= (high + 10):
405
  reward = 0.5
406
  elif self._soil_moisture < 20:
407
+ reward = -0.98
408
  elif self._soil_moisture > 90:
409
  reward = -0.5
410
  else:
411
+ reward = 0.02
412
 
413
  if self._rainfall > 15 and water_added > 0:
414
  reward -= 0.3
415
 
416
+ return round(max(-0.99, min(0.99, reward)), 4)
417
 
418
  @property
419
  def state(self) -> State: