Spaces:
Sleeping
Sleeping
Merge branch 'main' of https://huggingface.co/spaces/SheSync/meta-hackathon-project
Browse files- my_env/__pycache__/__init__.cpython-314.pyc +0 -0
- my_env/__pycache__/client.cpython-314.pyc +0 -0
- my_env/__pycache__/graders.cpython-314.pyc +0 -0
- my_env/__pycache__/models.cpython-314.pyc +0 -0
- my_env/__pycache__/tasks.cpython-314.pyc +0 -0
- my_env/__pycache__/weather.cpython-314.pyc +0 -0
- my_env/baseline.py +64 -61
- my_env/graders.py +44 -24
- my_env/openenv_my_env.egg-info/PKG-INFO +9 -0
- my_env/openenv_my_env.egg-info/SOURCES.txt +18 -0
- my_env/openenv_my_env.egg-info/dependency_links.txt +1 -0
- my_env/openenv_my_env.egg-info/entry_points.txt +2 -0
- my_env/openenv_my_env.egg-info/requires.txt +5 -0
- my_env/openenv_my_env.egg-info/top_level.txt +1 -0
- my_env/server/Dockerfile +1 -1
- my_env/server/__pycache__/__init__.cpython-314.pyc +0 -0
- my_env/server/__pycache__/app.cpython-314.pyc +0 -0
- my_env/server/__pycache__/my_env_environment.cpython-314.pyc +0 -0
my_env/__pycache__/__init__.cpython-314.pyc
ADDED
|
Binary file (363 Bytes). View file
|
|
|
my_env/__pycache__/client.cpython-314.pyc
ADDED
|
Binary file (3.92 kB). View file
|
|
|
my_env/__pycache__/graders.cpython-314.pyc
ADDED
|
Binary file (5.71 kB). View file
|
|
|
my_env/__pycache__/models.cpython-314.pyc
ADDED
|
Binary file (5.38 kB). View file
|
|
|
my_env/__pycache__/tasks.cpython-314.pyc
ADDED
|
Binary file (2.03 kB). View file
|
|
|
my_env/__pycache__/weather.cpython-314.pyc
ADDED
|
Binary file (8.5 kB). View file
|
|
|
my_env/baseline.py
CHANGED
|
@@ -10,11 +10,10 @@ Baseline Inference Script for Smart Farm Irrigation Environment.
|
|
| 10 |
This script runs a simple rule-based agent through all 3 tasks
|
| 11 |
and produces reproducible baseline scores.
|
| 12 |
|
| 13 |
-
The agent uses
|
| 14 |
-
-
|
| 15 |
-
-
|
| 16 |
-
-
|
| 17 |
-
- Otherwise β no water
|
| 18 |
|
| 19 |
Usage:
|
| 20 |
python baseline.py
|
|
@@ -26,23 +25,26 @@ import argparse
|
|
| 26 |
import sys
|
| 27 |
import os
|
| 28 |
|
| 29 |
-
# Add parent directory to path so we can import our modules
|
| 30 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 31 |
|
| 32 |
from server.my_env_environment import FarmEnvironment
|
| 33 |
from graders import grade_task1, grade_task2, grade_task3
|
| 34 |
from tasks import get_all_tasks
|
| 35 |
|
|
|
|
| 36 |
def simple_agent(observation: dict) -> str:
|
| 37 |
"""
|
| 38 |
-
A
|
| 39 |
-
based on
|
| 40 |
|
| 41 |
Rules:
|
| 42 |
-
-
|
| 43 |
-
-
|
| 44 |
-
-
|
| 45 |
-
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
Args:
|
| 48 |
observation: Current farm state dictionary
|
|
@@ -50,43 +52,56 @@ def simple_agent(observation: dict) -> str:
|
|
| 50 |
Returns:
|
| 51 |
water_amount string: "none"/"low"/"medium"/"high"
|
| 52 |
"""
|
| 53 |
-
moisture
|
| 54 |
-
rainfall
|
|
|
|
| 55 |
|
| 56 |
-
# Don't water if
|
| 57 |
if rainfall > 15:
|
| 58 |
return "none"
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
if
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return "medium"
|
| 65 |
-
elif moisture <
|
| 66 |
return "low"
|
| 67 |
else:
|
| 68 |
return "none"
|
| 69 |
-
|
|
|
|
| 70 |
def run_task(env, task_id: int, city: str = "Indore") -> dict:
|
| 71 |
"""
|
| 72 |
Runs the agent through a single task and returns results.
|
| 73 |
-
|
| 74 |
-
Args:
|
| 75 |
-
env: FarmEnvironment instance
|
| 76 |
-
task_id: 1, 2, or 3
|
| 77 |
-
city: City name for weather data
|
| 78 |
-
|
| 79 |
-
Returns:
|
| 80 |
-
Dictionary with score and episode history
|
| 81 |
"""
|
| 82 |
-
# Task configurations
|
| 83 |
task_config = {
|
| 84 |
1: {"max_steps": 7, "name": "Basic Moisture Control"},
|
| 85 |
2: {"max_steps": 14, "name": "Weather Adaptive Irrigation"},
|
| 86 |
3: {"max_steps": 30, "name": "Full Season Management"},
|
| 87 |
}
|
| 88 |
|
| 89 |
-
config
|
| 90 |
max_steps = config["max_steps"]
|
| 91 |
task_name = config["name"]
|
| 92 |
|
|
@@ -98,25 +113,22 @@ def run_task(env, task_id: int, city: str = "Indore") -> dict:
|
|
| 98 |
# Reset environment
|
| 99 |
obs = env.reset(city=city)
|
| 100 |
|
| 101 |
-
# Convert observation to dict
|
| 102 |
obs_dict = {
|
| 103 |
-
"temperature":
|
| 104 |
-
"rainfall":
|
| 105 |
"soil_moisture": obs.soil_moisture,
|
| 106 |
-
"crop_health":
|
| 107 |
-
"day_number":
|
| 108 |
-
"crop_stage":
|
| 109 |
}
|
| 110 |
|
| 111 |
episode_history = []
|
| 112 |
|
| 113 |
-
# Run for max_steps
|
| 114 |
for step in range(max_steps):
|
| 115 |
|
| 116 |
# Agent decides action
|
| 117 |
water_amount = simple_agent(obs_dict)
|
| 118 |
|
| 119 |
-
# Map action to number for graders
|
| 120 |
action_map = {
|
| 121 |
"none": 0,
|
| 122 |
"low": 1,
|
|
@@ -124,15 +136,15 @@ def run_task(env, task_id: int, city: str = "Indore") -> dict:
|
|
| 124 |
"high": 3
|
| 125 |
}
|
| 126 |
|
| 127 |
-
# Record this step
|
| 128 |
episode_history.append({
|
| 129 |
-
"day":
|
| 130 |
"soil_moisture": obs_dict["soil_moisture"],
|
| 131 |
-
"crop_health":
|
| 132 |
-
"water_action":
|
| 133 |
-
"rainfall":
|
| 134 |
-
"temperature":
|
| 135 |
-
"crop_stage":
|
| 136 |
})
|
| 137 |
|
| 138 |
print(f"Day {obs_dict['day_number']:2d} | "
|
|
@@ -141,7 +153,7 @@ def run_task(env, task_id: int, city: str = "Indore") -> dict:
|
|
| 141 |
f"Rain: {obs_dict['rainfall']:4.1f}mm | "
|
| 142 |
f"Action: {water_amount}")
|
| 143 |
|
| 144 |
-
# Take action
|
| 145 |
from models import FarmAction, WaterAmount
|
| 146 |
action = FarmAction(water_amount=WaterAmount(water_amount))
|
| 147 |
result = env.step(action)
|
|
@@ -150,7 +162,7 @@ def run_task(env, task_id: int, city: str = "Indore") -> dict:
|
|
| 150 |
if result.done:
|
| 151 |
break
|
| 152 |
|
| 153 |
-
# Update observation
|
| 154 |
obs_dict = {
|
| 155 |
"temperature": result.temperature,
|
| 156 |
"rainfall": result.rainfall,
|
|
@@ -177,34 +189,26 @@ def run_task(env, task_id: int, city: str = "Indore") -> dict:
|
|
| 177 |
"episode_history": episode_history,
|
| 178 |
}
|
| 179 |
|
|
|
|
| 180 |
def main(city: str = "Indore", task_id: int = None):
|
| 181 |
"""
|
| 182 |
-
Main function β runs agent through all 3 tasks
|
| 183 |
-
and prints final scores.
|
| 184 |
-
|
| 185 |
-
Args:
|
| 186 |
-
city: City name for weather data
|
| 187 |
-
task_id: Run specific task only (None = run all)
|
| 188 |
"""
|
| 189 |
print("\n" + "="*50)
|
| 190 |
print(" SMART FARM IRRIGATION β BASELINE SCORES")
|
| 191 |
print("="*50)
|
| 192 |
print(f"City: {city}")
|
| 193 |
-
print(f"Agent:
|
| 194 |
|
| 195 |
-
# Create environment
|
| 196 |
env = FarmEnvironment()
|
| 197 |
|
| 198 |
-
# Determine which tasks to run
|
| 199 |
tasks_to_run = [task_id] if task_id else [1, 2, 3]
|
| 200 |
|
| 201 |
-
# Run tasks and collect results
|
| 202 |
results = []
|
| 203 |
for tid in tasks_to_run:
|
| 204 |
result = run_task(env, tid, city=city)
|
| 205 |
results.append(result)
|
| 206 |
|
| 207 |
-
# Print final summary
|
| 208 |
print("\n" + "="*50)
|
| 209 |
print(" FINAL BASELINE SCORES")
|
| 210 |
print("="*50)
|
|
@@ -212,7 +216,6 @@ def main(city: str = "Indore", task_id: int = None):
|
|
| 212 |
print(f"Task {result['task_id']} ({result['task_name']}): "
|
| 213 |
f"{result['score']}")
|
| 214 |
|
| 215 |
-
# Calculate average
|
| 216 |
avg = sum(r["score"] for r in results) / len(results)
|
| 217 |
print(f"\nAverage Score: {round(avg, 2)}")
|
| 218 |
print("="*50)
|
|
@@ -238,4 +241,4 @@ if __name__ == "__main__":
|
|
| 238 |
help="Run specific task only (default: run all tasks)"
|
| 239 |
)
|
| 240 |
args = parser.parse_args()
|
| 241 |
-
main(city=args.city, task_id=args.task)
|
|
|
|
| 10 |
This script runs a simple rule-based agent through all 3 tasks
|
| 11 |
and produces reproducible baseline scores.
|
| 12 |
|
| 13 |
+
The agent uses smart rules considering BOTH moisture AND temperature:
|
| 14 |
+
- Hot day (>=38C) + dry soil β water more
|
| 15 |
+
- Rainy day (>15mm) β don't water
|
| 16 |
+
- Normal rules based on moisture level
|
|
|
|
| 17 |
|
| 18 |
Usage:
|
| 19 |
python baseline.py
|
|
|
|
| 25 |
import sys
|
| 26 |
import os
|
| 27 |
|
|
|
|
| 28 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 29 |
|
| 30 |
from server.my_env_environment import FarmEnvironment
|
| 31 |
from graders import grade_task1, grade_task2, grade_task3
|
| 32 |
from tasks import get_all_tasks
|
| 33 |
|
| 34 |
+
|
| 35 |
def simple_agent(observation: dict) -> str:
|
| 36 |
"""
|
| 37 |
+
A smart rule-based agent that decides how much to water
|
| 38 |
+
based on soil moisture, temperature AND rainfall.
|
| 39 |
|
| 40 |
Rules:
|
| 41 |
+
- Heavy rain (>15mm) β no water (already raining!)
|
| 42 |
+
- Very hot (>=38C) + dry β water high
|
| 43 |
+
- Very hot (>=38C) + okay β water medium
|
| 44 |
+
- Dry (<40%) β water high
|
| 45 |
+
- Slightly dry (<55%) β water medium
|
| 46 |
+
- Slightly moist (<65%) β water low
|
| 47 |
+
- Good moisture (>=65%) β no water
|
| 48 |
|
| 49 |
Args:
|
| 50 |
observation: Current farm state dictionary
|
|
|
|
| 52 |
Returns:
|
| 53 |
water_amount string: "none"/"low"/"medium"/"high"
|
| 54 |
"""
|
| 55 |
+
moisture = observation.get("soil_moisture", 50)
|
| 56 |
+
rainfall = observation.get("rainfall", 0)
|
| 57 |
+
temperature = observation.get("temperature", 30)
|
| 58 |
|
| 59 |
+
# Rule 1 β Don't water if already raining heavily
|
| 60 |
if rainfall > 15:
|
| 61 |
return "none"
|
| 62 |
|
| 63 |
+
# Rule 2 β Very hot day needs more water
|
| 64 |
+
if temperature >= 38:
|
| 65 |
+
if moisture < 45:
|
| 66 |
+
return "high"
|
| 67 |
+
elif moisture < 60:
|
| 68 |
+
return "medium"
|
| 69 |
+
elif moisture < 70:
|
| 70 |
+
return "low"
|
| 71 |
+
else:
|
| 72 |
+
return "none"
|
| 73 |
+
|
| 74 |
+
# Rule 3 β Warm day normal rules
|
| 75 |
+
if temperature >= 30:
|
| 76 |
+
if moisture < 40:
|
| 77 |
+
return "high"
|
| 78 |
+
elif moisture < 55:
|
| 79 |
+
return "medium"
|
| 80 |
+
elif moisture < 65:
|
| 81 |
+
return "low"
|
| 82 |
+
else:
|
| 83 |
+
return "none"
|
| 84 |
+
|
| 85 |
+
# Rule 4 β Cool day needs less water
|
| 86 |
+
if moisture < 35:
|
| 87 |
return "medium"
|
| 88 |
+
elif moisture < 50:
|
| 89 |
return "low"
|
| 90 |
else:
|
| 91 |
return "none"
|
| 92 |
+
|
| 93 |
+
|
| 94 |
def run_task(env, task_id: int, city: str = "Indore") -> dict:
|
| 95 |
"""
|
| 96 |
Runs the agent through a single task and returns results.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
"""
|
|
|
|
| 98 |
task_config = {
|
| 99 |
1: {"max_steps": 7, "name": "Basic Moisture Control"},
|
| 100 |
2: {"max_steps": 14, "name": "Weather Adaptive Irrigation"},
|
| 101 |
3: {"max_steps": 30, "name": "Full Season Management"},
|
| 102 |
}
|
| 103 |
|
| 104 |
+
config = task_config[task_id]
|
| 105 |
max_steps = config["max_steps"]
|
| 106 |
task_name = config["name"]
|
| 107 |
|
|
|
|
| 113 |
# Reset environment
|
| 114 |
obs = env.reset(city=city)
|
| 115 |
|
|
|
|
| 116 |
obs_dict = {
|
| 117 |
+
"temperature": obs.temperature,
|
| 118 |
+
"rainfall": obs.rainfall,
|
| 119 |
"soil_moisture": obs.soil_moisture,
|
| 120 |
+
"crop_health": obs.crop_health.value,
|
| 121 |
+
"day_number": obs.day_number,
|
| 122 |
+
"crop_stage": obs.crop_stage.value,
|
| 123 |
}
|
| 124 |
|
| 125 |
episode_history = []
|
| 126 |
|
|
|
|
| 127 |
for step in range(max_steps):
|
| 128 |
|
| 129 |
# Agent decides action
|
| 130 |
water_amount = simple_agent(obs_dict)
|
| 131 |
|
|
|
|
| 132 |
action_map = {
|
| 133 |
"none": 0,
|
| 134 |
"low": 1,
|
|
|
|
| 136 |
"high": 3
|
| 137 |
}
|
| 138 |
|
| 139 |
+
# Record this step BEFORE taking action
|
| 140 |
episode_history.append({
|
| 141 |
+
"day": obs_dict["day_number"],
|
| 142 |
"soil_moisture": obs_dict["soil_moisture"],
|
| 143 |
+
"crop_health": obs_dict["crop_health"],
|
| 144 |
+
"water_action": action_map[water_amount],
|
| 145 |
+
"rainfall": obs_dict["rainfall"],
|
| 146 |
+
"temperature": obs_dict["temperature"],
|
| 147 |
+
"crop_stage": obs_dict["crop_stage"],
|
| 148 |
})
|
| 149 |
|
| 150 |
print(f"Day {obs_dict['day_number']:2d} | "
|
|
|
|
| 153 |
f"Rain: {obs_dict['rainfall']:4.1f}mm | "
|
| 154 |
f"Action: {water_amount}")
|
| 155 |
|
| 156 |
+
# Take action
|
| 157 |
from models import FarmAction, WaterAmount
|
| 158 |
action = FarmAction(water_amount=WaterAmount(water_amount))
|
| 159 |
result = env.step(action)
|
|
|
|
| 162 |
if result.done:
|
| 163 |
break
|
| 164 |
|
| 165 |
+
# Update observation for next step
|
| 166 |
obs_dict = {
|
| 167 |
"temperature": result.temperature,
|
| 168 |
"rainfall": result.rainfall,
|
|
|
|
| 189 |
"episode_history": episode_history,
|
| 190 |
}
|
| 191 |
|
| 192 |
+
|
| 193 |
def main(city: str = "Indore", task_id: int = None):
|
| 194 |
"""
|
| 195 |
+
Main function β runs agent through all 3 tasks.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
"""
|
| 197 |
print("\n" + "="*50)
|
| 198 |
print(" SMART FARM IRRIGATION β BASELINE SCORES")
|
| 199 |
print("="*50)
|
| 200 |
print(f"City: {city}")
|
| 201 |
+
print(f"Agent: Smart Rule-Based Agent (moisture + temperature)")
|
| 202 |
|
|
|
|
| 203 |
env = FarmEnvironment()
|
| 204 |
|
|
|
|
| 205 |
tasks_to_run = [task_id] if task_id else [1, 2, 3]
|
| 206 |
|
|
|
|
| 207 |
results = []
|
| 208 |
for tid in tasks_to_run:
|
| 209 |
result = run_task(env, tid, city=city)
|
| 210 |
results.append(result)
|
| 211 |
|
|
|
|
| 212 |
print("\n" + "="*50)
|
| 213 |
print(" FINAL BASELINE SCORES")
|
| 214 |
print("="*50)
|
|
|
|
| 216 |
print(f"Task {result['task_id']} ({result['task_name']}): "
|
| 217 |
f"{result['score']}")
|
| 218 |
|
|
|
|
| 219 |
avg = sum(r["score"] for r in results) / len(results)
|
| 220 |
print(f"\nAverage Score: {round(avg, 2)}")
|
| 221 |
print("="*50)
|
|
|
|
| 241 |
help="Run specific task only (default: run all tasks)"
|
| 242 |
)
|
| 243 |
args = parser.parse_args()
|
| 244 |
+
main(city=args.city, task_id=args.task)
|
my_env/graders.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
def health_to_number(crop_health_str: str) -> int:
|
| 6 |
"""
|
| 7 |
Converts crop health string to number for grading.
|
| 8 |
poor=25, fair=50, good=75, excellent=100
|
|
@@ -13,14 +11,15 @@ def health_to_number(crop_health_str: str) -> int:
|
|
| 13 |
"good": 75,
|
| 14 |
"excellent": 100
|
| 15 |
}
|
| 16 |
-
# Handle both string and already-numeric values
|
| 17 |
if isinstance(crop_health_str, (int, float)):
|
| 18 |
-
return crop_health_str
|
| 19 |
return mapping.get(str(crop_health_str).lower(), 50)
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
"""
|
| 23 |
Returns a daily score between -1.0 and 1.0
|
|
|
|
| 24 |
"""
|
| 25 |
reward = 0.0
|
| 26 |
|
|
@@ -33,25 +32,32 @@ def compute_reward(soil_moisture, crop_health, water_action, rainfall):
|
|
| 33 |
reward -= 0.3
|
| 34 |
|
| 35 |
# Crop health check
|
| 36 |
-
|
|
|
|
| 37 |
reward += 0.3
|
| 38 |
-
elif
|
| 39 |
reward += 0.1
|
| 40 |
else:
|
| 41 |
reward -= 0.2
|
| 42 |
|
| 43 |
-
# Penalize watering heavily when
|
| 44 |
-
if rainfall >
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
reward -= 0.2
|
| 46 |
|
| 47 |
-
# Keep between -1.0 and 1.0
|
| 48 |
return round(max(-1.0, min(1.0, reward)), 2)
|
| 49 |
|
|
|
|
|
|
|
| 50 |
# TASK 1 GRADER β Easy (7 days)
|
|
|
|
| 51 |
|
| 52 |
def grade_task1(episode_history):
|
| 53 |
"""
|
| 54 |
-
Score = fraction of days where moisture was between 40
|
| 55 |
Returns 0.0 to 1.0
|
| 56 |
"""
|
| 57 |
if not episode_history:
|
|
@@ -66,7 +72,10 @@ def grade_task1(episode_history):
|
|
| 66 |
score = days_in_range / len(episode_history)
|
| 67 |
return round(score, 2)
|
| 68 |
|
|
|
|
|
|
|
| 69 |
# TASK 2 GRADER β Medium (14 days)
|
|
|
|
| 70 |
|
| 71 |
def grade_task2(episode_history):
|
| 72 |
"""
|
|
@@ -84,14 +93,18 @@ def grade_task2(episode_history):
|
|
| 84 |
record.get("soil_moisture", 0),
|
| 85 |
health_to_number(record.get("crop_health", "fair")),
|
| 86 |
record.get("water_action", 0),
|
| 87 |
-
record.get("rainfall", 0)
|
|
|
|
| 88 |
)
|
| 89 |
total_reward += daily
|
| 90 |
|
| 91 |
score = total_reward / max_possible if max_possible > 0 else 0.0
|
| 92 |
return round(max(0.0, min(1.0, score)), 2)
|
| 93 |
|
|
|
|
|
|
|
| 94 |
# TASK 3 GRADER β Hard (30 days)
|
|
|
|
| 95 |
|
| 96 |
def grade_task3(episode_history):
|
| 97 |
"""
|
|
@@ -108,6 +121,7 @@ def grade_task3(episode_history):
|
|
| 108 |
day = record.get("day", 1)
|
| 109 |
moisture = record.get("soil_moisture", 0)
|
| 110 |
crop_health = health_to_number(record.get("crop_health", "fair"))
|
|
|
|
| 111 |
|
| 112 |
# Each stage needs different moisture
|
| 113 |
if day <= 10: # Seedling
|
|
@@ -117,38 +131,44 @@ def grade_task3(episode_history):
|
|
| 117 |
else: # Mature
|
| 118 |
ideal = 35 <= moisture <= 60
|
| 119 |
|
|
|
|
|
|
|
|
|
|
| 120 |
if ideal and crop_health >= 70:
|
| 121 |
day_scores.append(1.0)
|
| 122 |
elif ideal or crop_health >= 60:
|
| 123 |
day_scores.append(0.5)
|
|
|
|
|
|
|
| 124 |
else:
|
| 125 |
day_scores.append(0.0)
|
| 126 |
|
| 127 |
score = sum(day_scores) / len(day_scores)
|
| 128 |
return round(score, 2)
|
| 129 |
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
-
# Fake data to test all 3 graders
|
| 134 |
fake_7 = [
|
| 135 |
-
{"day": i, "soil_moisture": 55, "crop_health":
|
| 136 |
-
"water_action": 2, "rainfall": 0}
|
| 137 |
for i in range(1, 8)
|
| 138 |
]
|
| 139 |
fake_14 = [
|
| 140 |
-
{"day": i, "soil_moisture": 50 + i, "crop_health":
|
| 141 |
-
"water_action": 1, "rainfall": i % 3}
|
| 142 |
for i in range(1, 15)
|
| 143 |
]
|
| 144 |
fake_30 = [
|
| 145 |
-
{"day": i, "soil_moisture": 55, "crop_health":
|
| 146 |
-
"water_action": 2, "rainfall": i % 5}
|
| 147 |
for i in range(1, 31)
|
| 148 |
]
|
| 149 |
|
| 150 |
print("Task 1 Score:", grade_task1(fake_7))
|
| 151 |
print("Task 2 Score:", grade_task2(fake_14))
|
| 152 |
print("Task 3 Score:", grade_task3(fake_30))
|
| 153 |
-
|
| 154 |
-
print("\nSample daily reward:", compute_reward(55, 78, 2, 0))
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 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 |
+
|
| 54 |
+
# βββββββββββββββββββββββββββββ
|
| 55 |
# TASK 1 GRADER β Easy (7 days)
|
| 56 |
+
# βββββββββββββββββββββββββββββ
|
| 57 |
|
| 58 |
def grade_task1(episode_history):
|
| 59 |
"""
|
| 60 |
+
Score = fraction of days where moisture was between 40-70%
|
| 61 |
Returns 0.0 to 1.0
|
| 62 |
"""
|
| 63 |
if not episode_history:
|
|
|
|
| 72 |
score = days_in_range / len(episode_history)
|
| 73 |
return round(score, 2)
|
| 74 |
|
| 75 |
+
|
| 76 |
+
# βββββββββββββββββββββββββββββ
|
| 77 |
# TASK 2 GRADER β Medium (14 days)
|
| 78 |
+
# βββββββββββββββββββββββββββββ
|
| 79 |
|
| 80 |
def grade_task2(episode_history):
|
| 81 |
"""
|
|
|
|
| 93 |
record.get("soil_moisture", 0),
|
| 94 |
health_to_number(record.get("crop_health", "fair")),
|
| 95 |
record.get("water_action", 0),
|
| 96 |
+
record.get("rainfall", 0),
|
| 97 |
+
record.get("temperature", 30)
|
| 98 |
)
|
| 99 |
total_reward += daily
|
| 100 |
|
| 101 |
score = total_reward / max_possible if max_possible > 0 else 0.0
|
| 102 |
return round(max(0.0, min(1.0, score)), 2)
|
| 103 |
|
| 104 |
+
|
| 105 |
+
# βββββββββββββββββββββββββββββ
|
| 106 |
# TASK 3 GRADER β Hard (30 days)
|
| 107 |
+
# βββββββββββββββββββββββββββββ
|
| 108 |
|
| 109 |
def grade_task3(episode_history):
|
| 110 |
"""
|
|
|
|
| 121 |
day = record.get("day", 1)
|
| 122 |
moisture = record.get("soil_moisture", 0)
|
| 123 |
crop_health = health_to_number(record.get("crop_health", "fair"))
|
| 124 |
+
temperature = record.get("temperature", 30)
|
| 125 |
|
| 126 |
# Each stage needs different moisture
|
| 127 |
if day <= 10: # Seedling
|
|
|
|
| 131 |
else: # Mature
|
| 132 |
ideal = 35 <= moisture <= 60
|
| 133 |
|
| 134 |
+
# Extra credit for managing hot days well
|
| 135 |
+
hot_day_managed = temperature >= 38 and moisture >= 40
|
| 136 |
+
|
| 137 |
if ideal and crop_health >= 70:
|
| 138 |
day_scores.append(1.0)
|
| 139 |
elif ideal or crop_health >= 60:
|
| 140 |
day_scores.append(0.5)
|
| 141 |
+
elif hot_day_managed:
|
| 142 |
+
day_scores.append(0.3)
|
| 143 |
else:
|
| 144 |
day_scores.append(0.0)
|
| 145 |
|
| 146 |
score = sum(day_scores) / len(day_scores)
|
| 147 |
return round(score, 2)
|
| 148 |
|
| 149 |
+
|
| 150 |
+
# βββββββββββββββββββββββββββββ
|
| 151 |
+
# TEST
|
| 152 |
+
# βββββββββββββββββββββββββββββ
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
|
|
|
| 155 |
fake_7 = [
|
| 156 |
+
{"day": i, "soil_moisture": 55, "crop_health": "good",
|
| 157 |
+
"water_action": 2, "rainfall": 0, "temperature": 32}
|
| 158 |
for i in range(1, 8)
|
| 159 |
]
|
| 160 |
fake_14 = [
|
| 161 |
+
{"day": i, "soil_moisture": 50 + i, "crop_health": "fair",
|
| 162 |
+
"water_action": 1, "rainfall": i % 3, "temperature": 30 + i}
|
| 163 |
for i in range(1, 15)
|
| 164 |
]
|
| 165 |
fake_30 = [
|
| 166 |
+
{"day": i, "soil_moisture": 55, "crop_health": "good",
|
| 167 |
+
"water_action": 2, "rainfall": i % 5, "temperature": 35}
|
| 168 |
for i in range(1, 31)
|
| 169 |
]
|
| 170 |
|
| 171 |
print("Task 1 Score:", grade_task1(fake_7))
|
| 172 |
print("Task 2 Score:", grade_task2(fake_14))
|
| 173 |
print("Task 3 Score:", grade_task3(fake_30))
|
| 174 |
+
print("Sample reward:", compute_reward(55, "good", 2, 0, 38))
|
|
|
my_env/openenv_my_env.egg-info/PKG-INFO
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.4
|
| 2 |
+
Name: openenv-my_env
|
| 3 |
+
Version: 0.1.0
|
| 4 |
+
Summary: My Env environment for OpenEnv
|
| 5 |
+
Requires-Python: >=3.10
|
| 6 |
+
Requires-Dist: openenv-core[core]>=0.2.1
|
| 7 |
+
Provides-Extra: dev
|
| 8 |
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
| 9 |
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
my_env/openenv_my_env.egg-info/SOURCES.txt
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
README.md
|
| 2 |
+
pyproject.toml
|
| 3 |
+
./__init__.py
|
| 4 |
+
./baseline.py
|
| 5 |
+
./client.py
|
| 6 |
+
./graders.py
|
| 7 |
+
./models.py
|
| 8 |
+
./tasks.py
|
| 9 |
+
./weather.py
|
| 10 |
+
openenv_my_env.egg-info/PKG-INFO
|
| 11 |
+
openenv_my_env.egg-info/SOURCES.txt
|
| 12 |
+
openenv_my_env.egg-info/dependency_links.txt
|
| 13 |
+
openenv_my_env.egg-info/entry_points.txt
|
| 14 |
+
openenv_my_env.egg-info/requires.txt
|
| 15 |
+
openenv_my_env.egg-info/top_level.txt
|
| 16 |
+
server/__init__.py
|
| 17 |
+
server/app.py
|
| 18 |
+
server/my_env_environment.py
|
my_env/openenv_my_env.egg-info/dependency_links.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
|
my_env/openenv_my_env.egg-info/entry_points.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[console_scripts]
|
| 2 |
+
server = my_env.server.app:main
|
my_env/openenv_my_env.egg-info/requires.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openenv-core[core]>=0.2.1
|
| 2 |
+
|
| 3 |
+
[dev]
|
| 4 |
+
pytest>=8.0.0
|
| 5 |
+
pytest-cov>=4.0.0
|
my_env/openenv_my_env.egg-info/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
my_env
|
my_env/server/Dockerfile
CHANGED
|
@@ -7,7 +7,7 @@
|
|
| 7 |
# Multi-stage build using openenv-base
|
| 8 |
# This Dockerfile is flexible and works for both:
|
| 9 |
# - In-repo environments (with local OpenEnv sources)
|
| 10 |
-
# - Standalone environments (with openenv from PyPI/Git)
|
| 11 |
# The build script (openenv build) handles context detection and sets appropriate build args.
|
| 12 |
|
| 13 |
ARG BASE_IMAGE=ghcr.io/meta-pytorch/openenv-base:latest
|
|
|
|
| 7 |
# Multi-stage build using openenv-base
|
| 8 |
# This Dockerfile is flexible and works for both:
|
| 9 |
# - In-repo environments (with local OpenEnv sources)
|
| 10 |
+
# - Standalone environments (with openenv from PyPI/Git)
|
| 11 |
# The build script (openenv build) handles context detection and sets appropriate build args.
|
| 12 |
|
| 13 |
ARG BASE_IMAGE=ghcr.io/meta-pytorch/openenv-base:latest
|
my_env/server/__pycache__/__init__.cpython-314.pyc
ADDED
|
Binary file (327 Bytes). View file
|
|
|
my_env/server/__pycache__/app.cpython-314.pyc
ADDED
|
Binary file (8.53 kB). View file
|
|
|
my_env/server/__pycache__/my_env_environment.cpython-314.pyc
ADDED
|
Binary file (8.89 kB). View file
|
|
|