File size: 4,882 Bytes
df055d6
f63f438
 
 
df055d6
 
 
 
 
f63f438
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96478ce
 
 
 
f63f438
 
 
 
 
 
 
 
96478ce
 
 
 
 
 
 
f63f438
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
title: FarmRL OpenEnv Submission
emoji: 🌾
colorFrom: green
colorTo: blue
sdk: docker
pinned: false
---

# FarmRL OpenEnv Submission

A reinforcement learning environment and agent for optimizing farm management decisions using the OpenEnv framework.

## Overview

FarmRL trains an intelligent agent to manage crop farming decisions by controlling irrigation, fertilizer application, and pesticide use. The agent learns from a real agricultural dataset and aims to maximize crop yield while maintaining sustainability.

**Key Features:**
- OpenEnv-compliant REST API for environment interaction
- LLM-based inference via OpenAI-compatible endpoints
- Tabular RL training with preprocessing pipeline
- Grading and evaluation framework

## Quick Start

### Prerequisites
- Python 3.11+
- `uv` package manager (recommended) or pip
- Environment variables configured (see Configuration section)

### Installation

```bash
uv pip install -e .
```

Or with pip:
```bash
pip install -r requirements.txt
```

### Configuration

Create a `.env` file in the project root with required credentials:

```env
# API Configuration (defaults provided for base URL and model)
API_BASE_URL=https://api.openai.com/v1
MODEL_NAME=gpt-4o-mini
TASK_NAME=farm-yield-optimization
BENCHMARK=farmrl
PORT=7860

# Required API Credentials (no defaults)
# Submission/Judge environments inject API_KEY.
API_KEY=your_api_key_here
# Optional local compatibility fallback:
# OPENAI_API_KEY=your_api_key_here
```

**Environment Variables:**
- `API_BASE_URL`: LLM endpoint URL (default: `https://api.openai.com/v1`)
- `MODEL_NAME`: Model identifier (default: `gpt-4o-mini`)
- `TASK_NAME`: Task identifier (default: `farm-yield-optimization`)
- `BENCHMARK`: Benchmark name (default: `farmrl`)
- `PORT`: Server port (default: `7860`)
- `API_KEY`: Primary API key for submission validation (required, no default)
- `OPENAI_API_KEY`: Optional local compatibility fallback when `API_KEY` is not set

**Hackathon submission note:**
- Use only the injected `API_BASE_URL` and `API_KEY` values provided by the validator.
- Do not hardcode API keys, alternate providers, or alternate base URLs.
- Inference is configured to fail fast if proxy-compatible LLM settings are missing.

### Running the API Server

Start the OpenEnv API server on your configured port:

```bash
uv run python -m server.app
```

The server will be available at `http://localhost:7860`

### Running Inference

Execute the full inference pipeline:

```bash
uv run python inference.py
```

This runs the agent against the environment, logging results to stdout in the standard format:
- `[START]` - Episode initialization
- `[STEP]` - Individual step results
- `[END]` - Episode completion with score

## Project Structure

```
.
β”œβ”€β”€ api/              # REST API endpoints
β”œβ”€β”€ env/              # FarmRL environment implementation
β”œβ”€β”€ server/           # API server setup
β”œβ”€β”€ tasks/            # Grading and evaluation
β”œβ”€β”€ scripts/          # Data preprocessing utilities
β”œβ”€β”€ reference-material/  # Documentation and examples
β”œβ”€β”€ inference.py      # Main inference script
β”œβ”€β”€ openenv.yaml      # Environment schema definition
β”œβ”€β”€ requirements.txt  # Python dependencies
└── farmer_advisor_dataset.csv  # Agricultural training data
```

## Environment Specification

The environment state and action spaces are defined in `openenv.yaml`:

**Observations:**
- `soil_moisture`: Soil water availability (0-100%)
- `soil_ph`: Soil acidity (4-9)
- `temperature`: Environmental temperature
- `rainfall`: Precipitation amount (mm)
- `crop_stage`: Current crop growth stage
- `day`: Days since planting

**Actions:**
- `water`: Irrigation amount (0-50 mm)
- `fertilizer`: Fertilizer application
- `pesticide`: Pesticide application

## API Endpoints

- `POST /reset` - Reset environment (optional seed parameter)
- `POST /step` - Execute action and get next state
- `GET /state` - Get current environment state
- `GET /health` - Health check

## Data Pipeline

The project includes a preprocessing script (`scripts/add_water_variable.py`) that:
1. Adds a `Water_mm` column representing agent-controllable irrigation
2. Adjusts `Rainfall_mm` to maintain water-balance invariance

Run preprocessing:
```bash
uv run python scripts/add_water_variable.py farmer_advisor_dataset.csv
```

## Evaluation

The grading system (`tasks/graders.py`) evaluates agent performance based on:
- Crop yield optimization
- Sustainability metrics
- Action validity

## Docker

The project includes a Dockerfile for containerized deployment:

```bash
docker build -t farmrl-openenv .
docker run -p 7860:7860 farmrl-openenv
```

## References

- [OpenEnv Framework](https://github.com/openenv-ai/openenv)
- [FarmGym Simulation](https://github.com/farm-gym)
- Dataset: Real agricultural data with crop yield observations