| title | Precision Crop Irrigation RL Agent | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| emoji | 🌾 | ||||||||
| colorFrom | green | ||||||||
| colorTo | blue | ||||||||
| sdk | gradio | ||||||||
| sdk_version | 6.11.0 | ||||||||
| app_file | app.py | ||||||||
| pinned | false | ||||||||
| license | mit | ||||||||
| tags |
|
||||||||
| short_description | RL agent for smart irrigation — OpenEnv Hackathon |
A complete, from-scratch Reinforcement Learning system for smart irrigation of Punjab rice farms. Built for the OpenEnv Hackathon — zero external ML dependencies.
"Train an agent to save groundwater for 140M Indian farmers."
| Phase | Status | Description |
|---|---|---|
| 1 | ✅ | Core Gymnasium env · 7 unit tests · baseline agents |
| 2 | ✅ | POMDP v2 · crop stages · farm grid · flash floods · IMD API client |
| 3 | ✅ | Pure-NumPy PPO (Adam, GAE, clipped surrogate) · 100k steps |
| 4 | ✅ | Mock IMD HTTP API (/forecast /summary /health) |
| 5 | ✅ | Dashboard · policy heatmap · episode renders · ablations |
git clone https://github.com/SamanPandey-in/irrigation-agent.git
cd irrigation-agent
pip install -r requirements.txt
# Generate weather data
python scripts/generate_data.py
# Run Phase 1 tests
python tests/test_env.py
# Train PPO + all baselines + ablations (100k steps, ~2 min CPU)
python scripts/train_and_eval.py
# Generate full dashboard
python viz/dashboard.py
# Start mock IMD weather API
python api/mock_imd_server.py # → http://localhost:8765
# Run OpenEnv Inference (LLM-driven baseline)
# Requires: API_BASE_URL, MODEL_NAME, HF_TOKEN
# Optional: LOCAL_URL, TASKS, BENCHMARK, MAX_STEPS
python inference.pydocker build -t irrigation-agent .
docker run irrigation-agentpython scripts/evaluate_cli.py --mode batch \
--actions "2,3,2,3,2,2,3" \
--preset drought \
--rain-mult 0.1 \
--sensor-noise 0.08 \
--api-key sk-ant-...
python scripts/evaluate_cli.py --preset hard --dump-config > hard.json
python scripts/evaluate_cli.py --mode interactive --config-file hard.json
📁 envs/
precision_irrigation.py Phase 1 — Gymnasium v0 env
precision_irrigation_v2.py Phase 2 — POMDP + farm grid
precision_irrigation_v3.py Phase 3 — Calibrated RL-ready env ← used for training
📁 rl/
ppo.py Pure NumPy PPO (Adam, GAE, clipped surrogate)
obs_normalizer.py Hand-coded obs normalisation
📁 api/
mock_imd_server.py Built-in HTTP server, no fastapi needed
📁 viz/
dashboard.py Phase 5 matplotlib dashboard
📁 scripts/
generate_data.py 6yr Punjab IMD-style weather (2021-2026)
train_and_eval.py Full pipeline: train → eval → ablations
📁 server/
app.py Multi-mode OpenEnv entrypoint (server:main)
📄 openenv.yaml Full environment & task specification
📄 inference.py LLM-driven inference script with [START]/[STEP]/[END] logging
📄 validator.py API compliance verification script
📁 data/
rainfall.csv 918 rows: 2021–2026, Jun–Oct season
📁 models/
ppo_irrigation.pkl Trained policy (pickle, ~290 KB)
📁 outputs/
dashboard.png Full dashboard image
episode_render_ppo.png PPO agent episode render
episode_render_rb.png Rule-Based agent episode render
comparison_render.png Side-by-side comparison
metrics.json Full eval results + training log
| Key | Shape | Description |
|---|---|---|
soil_moisture_obs |
(1,) | Noisy sensor reading [0,1] |
crop_growth |
(1,) | Growth fraction [0,1] |
crop_stage |
(1,) | 0=seedling 1=vegetative 2=reproductive 3=maturity |
weather_forecast |
(3,2) | 3-day noisy [[rain_mm, temp_C], …] |
water_tank |
(1,) | Normalised tank level |
power_status |
(1,) | 1=pump available |
day_of_season |
(1,) | Current day [0,90] |
grid_moisture |
(4,4) | Sub-field heterogeneous moisture |
| # | Label | Volume |
|---|---|---|
| 0 | No water | 0 L |
| 1 | Low | ~1 000 L |
| 2 | Medium | ~3 000 L |
| 3 | High | ~5 000 L |
| 4 | Drain | remove excess |
Note: grid_moisture is an observational signal for heterogeneity/risk awareness, while actions are intentionally field-wide pump controls (single actuator). The policy uses grid spread to choose a balanced uniform action.
- Task 1 (Normal): 0.98 (easy — agent succeeds reliably)
- Task 2 (Drought): 0.53 (medium — requires strategic irrigation)
- Task 3 (Hard): 0.16 (hard — crop fails under adversarial conditions)
| Agent | Reward | Yield t/ha | Water kL | Efficiency |
|---|---|---|---|---|
| Random | +11.68 | 5.06 | 92.9 | 0.054 |
| Rule-Based | +14.56 | 6.22 | 92.6 | 0.067 |
| Greedy | +15.28 | 6.31 | 95.3 | 0.066 |
| Smart Forecast | +12.75 | 5.67 | 76.3 | 0.074 |
| PPO (trained) ★ | +15.37 | 6.33 | 95.3 | 0.066 |
| Scenario | Reward | Yield |
|---|---|---|
| PPO — no forecast | +15.46 | 6.35 t |
| PPO — drought (−75% rain) | +13.06 | 5.54 t |
| Forecast — drought | +11.57 | 5.24 t |
| Rule-Based — drought | +12.41 | 5.45 t |
PPO shows drought robustness (+5.5% yield over Smart Forecast in drought).
No PyTorch. No TensorFlow. No gymnasium required.
ActorCritic— shared MLP trunk (256×256 tanh) + actor/critic headsAdamoptimiser — stateful per-parameter m/v momentsGAE— generalised advantage estimation (γ=0.99, λ=0.95)Clipped surrogate— PPO-clip (ε=0.2)Entropy bonus— annealed 0.05→0.005Reward clipping— [-10, 10] during training- ~870 steps/sec on a single CPU core
The application exposes a standard FastAPI endpoint under / for automated evaluation.
| Endpoint | Method | Description |
|---|---|---|
/reset |
POST |
Reset env with {"task_id": "task_1", "seed": 42} |
/step |
POST |
Take action {"action": 2}. Returns score in [0.0, 1.0] |
/state |
GET |
Get current observation dict |
/health |
GET |
Check server status and version |
This repo is multi-mode compatible and passes openenv validate.
pyproject.tomlincludes[project.scripts] server = "server.app:main"server/app.pyprovides a callablemain()entrypointuv.lockis generated viauv lock
Run locally with:
python server/app.pyValidate:
openenv validatepython api/mock_imd_server.py
curl "http://localhost:8765/forecast?year=2023&day=15&days=3"
curl "http://localhost:8765/summary?year=2022"
curl "http://localhost:8765/health"- Phase 6: Multi-agent MARL — neighbourhood farms sharing canal resources
- Real IoT: Raspberry Pi soil sensors → live obs
- Mobile deploy: Export policy → TFLite → Flutter Kisan app
- Research: Hierarchical RL, weather-LLM hybrid forecasts