Spaces:
Sleeping
Sleeping
Commit ·
4a4dc2f
1
Parent(s): ccdcbde
Docker file resolved
Browse files- Dockerfile +10 -2
- server/my_env_environment.py +311 -133
Dockerfile
CHANGED
|
@@ -69,7 +69,7 @@
|
|
| 69 |
# ENV PATH="/app/.venv/bin:$PATH"
|
| 70 |
|
| 71 |
# # Set PYTHONPATH so imports work correctly
|
| 72 |
-
# ENV PYTHONPATH="/app/
|
| 73 |
|
| 74 |
# # Required LLM credentials for inference.py
|
| 75 |
# ENV API_BASE_URL="https://router.huggingface.co/v1"
|
|
@@ -92,4 +92,12 @@ COPY . .
|
|
| 92 |
|
| 93 |
RUN pip install --no-cache-dir -r server/requirements.txt
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# ENV PATH="/app/.venv/bin:$PATH"
|
| 70 |
|
| 71 |
# # Set PYTHONPATH so imports work correctly
|
| 72 |
+
# ENV PYTHONPATH="/app:/app/my_env:$PYTHONPATH"
|
| 73 |
|
| 74 |
# # Required LLM credentials for inference.py
|
| 75 |
# ENV API_BASE_URL="https://router.huggingface.co/v1"
|
|
|
|
| 92 |
|
| 93 |
RUN pip install --no-cache-dir -r server/requirements.txt
|
| 94 |
|
| 95 |
+
ENV PYTHONPATH="/app:/app/my_env:$PYTHONPATH"
|
| 96 |
+
|
| 97 |
+
# Install openenv-core explicitly
|
| 98 |
+
RUN /app/.venv/bin/pip install openenv-core || \
|
| 99 |
+
/app/.venv/bin/python -m pip install openenv-core || \
|
| 100 |
+
pip install openenv-core
|
| 101 |
+
|
| 102 |
+
# CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "8000"]
|
| 103 |
+
CMD ["sh", "-c", "cd /app && uvicorn server.app:app --host 0.0.0.0 --port 8000"]
|
server/my_env_environment.py
CHANGED
|
@@ -1,23 +1,249 @@
|
|
| 1 |
-
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
-
# All rights reserved.
|
| 3 |
-
#
|
| 4 |
-
# This source code is licensed under the BSD-style license found in the
|
| 5 |
-
# LICENSE file in the root directory of this source tree.
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
Smart Farm Irrigation Environment Implementation.
|
| 9 |
-
|
| 10 |
-
A real-world environment where an AI agent manages crop irrigation
|
| 11 |
-
by deciding how much water to give crops each day based on:
|
| 12 |
-
- Weather conditions (temperature, rainfall) from real Weather API
|
| 13 |
-
- Soil moisture (from sensor or auto-calculated)
|
| 14 |
-
- Crop health (user input)
|
| 15 |
-
- Crop stage (auto-managed by code)
|
| 16 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
from uuid import uuid4
|
| 19 |
|
| 20 |
-
from openenv import Environment
|
| 21 |
from openenv.core.env_server.types import State
|
| 22 |
|
| 23 |
try:
|
|
@@ -29,25 +255,14 @@ except ImportError:
|
|
| 29 |
|
| 30 |
|
| 31 |
class FarmEnvironment(Environment):
|
| 32 |
-
"""
|
| 33 |
-
Smart Farm Irrigation Environment.
|
| 34 |
-
|
| 35 |
-
The AI agent decides how much to water crops every day.
|
| 36 |
-
The environment simulates soil moisture, crop health,
|
| 37 |
-
weather conditions, and crop growth stages.
|
| 38 |
-
"""
|
| 39 |
|
| 40 |
SUPPORTS_CONCURRENT_SESSIONS: bool = True
|
| 41 |
|
| 42 |
def __init__(self):
|
| 43 |
-
"""Initialize the farm environment with default values."""
|
| 44 |
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 45 |
-
|
| 46 |
-
# --- Weather Service ---
|
| 47 |
self._weather_service = WeatherService()
|
| 48 |
self._city = "Indore"
|
| 49 |
-
|
| 50 |
-
# --- Farm State Variables ---
|
| 51 |
self._day = 1
|
| 52 |
self._soil_moisture = 30.0
|
| 53 |
self._crop_health = CropHealthStatus.FAIR
|
|
@@ -62,18 +277,8 @@ class FarmEnvironment(Environment):
|
|
| 62 |
sensor_moisture: float = None,
|
| 63 |
crop_health: str = "fair"
|
| 64 |
) -> FarmObservation:
|
| 65 |
-
"""
|
| 66 |
-
Reset the farm to Day 1 fresh state.
|
| 67 |
-
Fetches real weather data for the given city.
|
| 68 |
-
|
| 69 |
-
Args:
|
| 70 |
-
city: City name for weather data
|
| 71 |
-
sensor_moisture: Soil moisture from sensor (optional)
|
| 72 |
-
crop_health: User selected crop health status
|
| 73 |
-
"""
|
| 74 |
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 75 |
|
| 76 |
-
# --- Fetch Real Weather Data ---
|
| 77 |
self._city = city
|
| 78 |
weather = self._weather_service.get_weather(city)
|
| 79 |
self._temperature = weather["temperature"]
|
|
@@ -81,13 +286,10 @@ class FarmEnvironment(Environment):
|
|
| 81 |
|
| 82 |
print(f"Weather for {city}: {self._temperature}C, {self._rainfall}mm rain")
|
| 83 |
|
| 84 |
-
# --- Soil Moisture ---
|
| 85 |
if sensor_moisture is not None:
|
| 86 |
-
# User has a sensor — use their reading
|
| 87 |
self._soil_moisture = sensor_moisture
|
| 88 |
print(f"Soil moisture from sensor: {self._soil_moisture}%")
|
| 89 |
else:
|
| 90 |
-
# No sensor — calculate from weather
|
| 91 |
self._soil_moisture = self._weather_service.calculate_soil_moisture(
|
| 92 |
current_moisture=50.0,
|
| 93 |
temperature=self._temperature,
|
|
@@ -95,13 +297,11 @@ class FarmEnvironment(Environment):
|
|
| 95 |
)
|
| 96 |
print(f"Soil moisture calculated: {self._soil_moisture}%")
|
| 97 |
|
| 98 |
-
# --- Crop Health from User ---
|
| 99 |
try:
|
| 100 |
self._crop_health = CropHealthStatus(crop_health)
|
| 101 |
except ValueError:
|
| 102 |
self._crop_health = CropHealthStatus.FAIR
|
| 103 |
|
| 104 |
-
# --- Reset Other Values ---
|
| 105 |
self._day = 1
|
| 106 |
self._crop_stage = CropStage.SEEDLING
|
| 107 |
|
|
@@ -117,92 +317,80 @@ class FarmEnvironment(Environment):
|
|
| 117 |
)
|
| 118 |
|
| 119 |
def step(self, action: FarmAction) -> FarmObservation:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
# --- Use real historical weather for each day ---
|
| 136 |
-
weather = self._weather_service.get_historical_weather(
|
| 137 |
-
self._city, self._day
|
| 138 |
-
)
|
| 139 |
-
self._temperature = weather["temperature"]
|
| 140 |
-
self._rainfall = weather["rainfall"]
|
| 141 |
-
|
| 142 |
-
# --- Update Soil Moisture ---
|
| 143 |
-
if self._sensor_moisture is not None:
|
| 144 |
-
# sensor user — just add water and subtract evaporation
|
| 145 |
-
evaporation = (self._temperature / 100) * 10
|
| 146 |
-
self._soil_moisture = self._soil_moisture + water_added - evaporation
|
| 147 |
-
else:
|
| 148 |
-
# no sensor — use weather calculator
|
| 149 |
-
self._soil_moisture = self._weather_service.calculate_soil_moisture(
|
| 150 |
-
current_moisture=self._soil_moisture + water_added,
|
| 151 |
-
temperature=self._temperature,
|
| 152 |
-
rainfall=self._rainfall
|
| 153 |
)
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
current_idx = health_order.index(self._crop_health.value)
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
self._crop_health = CropHealthStatus(health_order[new_idx])
|
| 166 |
-
elif 40 <= self._soil_moisture <= 75:
|
| 167 |
-
# Good moisture — health improves
|
| 168 |
-
new_idx = min(3, current_idx + 1)
|
| 169 |
-
self._crop_health = CropHealthStatus(health_order[new_idx])
|
| 170 |
-
# else: moisture okay but not ideal — health stays same
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
self.
|
| 180 |
-
|
| 181 |
-
self.
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
def _calculate_reward(self, water_added: float) -> float:
|
| 201 |
-
"""
|
| 202 |
-
Calculate reward based on soil moisture and water usage.
|
| 203 |
-
Returns value between -1.0 and 1.0
|
| 204 |
-
"""
|
| 205 |
-
# Ideal moisture ranges per crop stage
|
| 206 |
ideal_ranges = {
|
| 207 |
CropStage.SEEDLING: (40, 70),
|
| 208 |
CropStage.GROWING: (45, 70),
|
|
@@ -211,26 +399,17 @@ class FarmEnvironment(Environment):
|
|
| 211 |
|
| 212 |
low, high = ideal_ranges[self._crop_stage]
|
| 213 |
|
| 214 |
-
# Perfect moisture range
|
| 215 |
if low <= self._soil_moisture <= high:
|
| 216 |
reward = 1.0
|
| 217 |
-
|
| 218 |
-
# Acceptable but not ideal
|
| 219 |
elif (low - 10) <= self._soil_moisture <= (high + 10):
|
| 220 |
reward = 0.5
|
| 221 |
-
|
| 222 |
-
# Too dry
|
| 223 |
elif self._soil_moisture < 20:
|
| 224 |
reward = -1.0
|
| 225 |
-
|
| 226 |
-
# Too wet
|
| 227 |
elif self._soil_moisture > 90:
|
| 228 |
reward = -0.5
|
| 229 |
-
|
| 230 |
else:
|
| 231 |
reward = 0.0
|
| 232 |
|
| 233 |
-
# Penalize watering on rainy days (water waste)
|
| 234 |
if self._rainfall > 15 and water_added > 0:
|
| 235 |
reward -= 0.3
|
| 236 |
|
|
@@ -238,5 +417,4 @@ class FarmEnvironment(Environment):
|
|
| 238 |
|
| 239 |
@property
|
| 240 |
def state(self) -> State:
|
| 241 |
-
|
| 242 |
-
return self._state
|
|
|
|
| 1 |
+
# # Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# # All rights reserved.
|
| 3 |
+
# #
|
| 4 |
+
# # This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# # LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
# """
|
| 8 |
+
# Smart Farm Irrigation Environment Implementation.
|
| 9 |
+
|
| 10 |
+
# A real-world environment where an AI agent manages crop irrigation
|
| 11 |
+
# by deciding how much water to give crops each day based on:
|
| 12 |
+
# - Weather conditions (temperature, rainfall) from real Weather API
|
| 13 |
+
# - Soil moisture (from sensor or auto-calculated)
|
| 14 |
+
# - Crop health (user input)
|
| 15 |
+
# - Crop stage (auto-managed by code)
|
| 16 |
+
# """
|
| 17 |
+
|
| 18 |
+
# from uuid import uuid4
|
| 19 |
+
|
| 20 |
+
# from openenv.core.env_server.interfaces import Environment
|
| 21 |
+
# from openenv.core.env_server.types import State
|
| 22 |
+
|
| 23 |
+
# try:
|
| 24 |
+
# from ..models import FarmAction, FarmObservation, CropStage, CropHealthStatus
|
| 25 |
+
# from ..weather import WeatherService
|
| 26 |
+
# except ImportError:
|
| 27 |
+
# from models import FarmAction, FarmObservation, CropStage, CropHealthStatus
|
| 28 |
+
# from weather import WeatherService
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# class FarmEnvironment(Environment):
|
| 32 |
+
# """
|
| 33 |
+
# Smart Farm Irrigation Environment.
|
| 34 |
+
|
| 35 |
+
# The AI agent decides how much to water crops every day.
|
| 36 |
+
# The environment simulates soil moisture, crop health,
|
| 37 |
+
# weather conditions, and crop growth stages.
|
| 38 |
+
# """
|
| 39 |
+
|
| 40 |
+
# SUPPORTS_CONCURRENT_SESSIONS: bool = True
|
| 41 |
+
|
| 42 |
+
# def __init__(self):
|
| 43 |
+
# """Initialize the farm environment with default values."""
|
| 44 |
+
# self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 45 |
+
|
| 46 |
+
# # --- Weather Service ---
|
| 47 |
+
# self._weather_service = WeatherService()
|
| 48 |
+
# self._city = "Indore"
|
| 49 |
+
|
| 50 |
+
# # --- Farm State Variables ---
|
| 51 |
+
# self._day = 1
|
| 52 |
+
# self._soil_moisture = 30.0
|
| 53 |
+
# self._crop_health = CropHealthStatus.FAIR
|
| 54 |
+
# self._temperature = 30.0
|
| 55 |
+
# self._rainfall = 0.0
|
| 56 |
+
# self._crop_stage = CropStage.SEEDLING
|
| 57 |
+
# self._sensor_moisture = None
|
| 58 |
+
|
| 59 |
+
# def reset(
|
| 60 |
+
# self,
|
| 61 |
+
# city: str = "Indore",
|
| 62 |
+
# sensor_moisture: float = None,
|
| 63 |
+
# crop_health: str = "fair"
|
| 64 |
+
# ) -> FarmObservation:
|
| 65 |
+
# """
|
| 66 |
+
# Reset the farm to Day 1 fresh state.
|
| 67 |
+
# Fetches real weather data for the given city.
|
| 68 |
+
|
| 69 |
+
# Args:
|
| 70 |
+
# city: City name for weather data
|
| 71 |
+
# sensor_moisture: Soil moisture from sensor (optional)
|
| 72 |
+
# crop_health: User selected crop health status
|
| 73 |
+
# """
|
| 74 |
+
# self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 75 |
+
|
| 76 |
+
# # --- Fetch Real Weather Data ---
|
| 77 |
+
# self._city = city
|
| 78 |
+
# weather = self._weather_service.get_weather(city)
|
| 79 |
+
# self._temperature = weather["temperature"]
|
| 80 |
+
# self._rainfall = weather["rainfall"]
|
| 81 |
+
|
| 82 |
+
# print(f"Weather for {city}: {self._temperature}C, {self._rainfall}mm rain")
|
| 83 |
+
|
| 84 |
+
# # --- Soil Moisture ---
|
| 85 |
+
# if sensor_moisture is not None:
|
| 86 |
+
# # User has a sensor — use their reading
|
| 87 |
+
# self._soil_moisture = sensor_moisture
|
| 88 |
+
# print(f"Soil moisture from sensor: {self._soil_moisture}%")
|
| 89 |
+
# else:
|
| 90 |
+
# # No sensor — calculate from weather
|
| 91 |
+
# self._soil_moisture = self._weather_service.calculate_soil_moisture(
|
| 92 |
+
# current_moisture=50.0,
|
| 93 |
+
# temperature=self._temperature,
|
| 94 |
+
# rainfall=self._rainfall
|
| 95 |
+
# )
|
| 96 |
+
# print(f"Soil moisture calculated: {self._soil_moisture}%")
|
| 97 |
+
|
| 98 |
+
# # --- Crop Health from User ---
|
| 99 |
+
# try:
|
| 100 |
+
# self._crop_health = CropHealthStatus(crop_health)
|
| 101 |
+
# except ValueError:
|
| 102 |
+
# self._crop_health = CropHealthStatus.FAIR
|
| 103 |
+
|
| 104 |
+
# # --- Reset Other Values ---
|
| 105 |
+
# self._day = 1
|
| 106 |
+
# self._crop_stage = CropStage.SEEDLING
|
| 107 |
+
|
| 108 |
+
# return FarmObservation(
|
| 109 |
+
# temperature=self._temperature,
|
| 110 |
+
# rainfall=self._rainfall,
|
| 111 |
+
# soil_moisture=self._soil_moisture,
|
| 112 |
+
# crop_health=self._crop_health,
|
| 113 |
+
# day_number=self._day,
|
| 114 |
+
# crop_stage=self._crop_stage,
|
| 115 |
+
# done=False,
|
| 116 |
+
# reward=0.0,
|
| 117 |
+
# )
|
| 118 |
+
|
| 119 |
+
# def step(self, action: FarmAction) -> FarmObservation:
|
| 120 |
+
# """
|
| 121 |
+
# Execute one day of farming.
|
| 122 |
+
# AI provides water_amount, environment updates accordingly.
|
| 123 |
+
# """
|
| 124 |
+
# self._state.step_count += 1
|
| 125 |
+
|
| 126 |
+
# # --- Water Amount Mapping ---
|
| 127 |
+
# water_map = {
|
| 128 |
+
# "none": 0,
|
| 129 |
+
# "low": 10,
|
| 130 |
+
# "medium": 25,
|
| 131 |
+
# "high": 50,
|
| 132 |
+
# }
|
| 133 |
+
# water_added = water_map.get(action.water_amount.value, 0)
|
| 134 |
+
|
| 135 |
+
# # --- Use real historical weather for each day ---
|
| 136 |
+
# weather = self._weather_service.get_historical_weather(
|
| 137 |
+
# self._city, self._day
|
| 138 |
+
# )
|
| 139 |
+
# self._temperature = weather["temperature"]
|
| 140 |
+
# self._rainfall = weather["rainfall"]
|
| 141 |
+
|
| 142 |
+
# # --- Update Soil Moisture ---
|
| 143 |
+
# if self._sensor_moisture is not None:
|
| 144 |
+
# # sensor user — just add water and subtract evaporation
|
| 145 |
+
# evaporation = (self._temperature / 100) * 10
|
| 146 |
+
# self._soil_moisture = self._soil_moisture + water_added - evaporation
|
| 147 |
+
# else:
|
| 148 |
+
# # no sensor — use weather calculator
|
| 149 |
+
# self._soil_moisture = self._weather_service.calculate_soil_moisture(
|
| 150 |
+
# current_moisture=self._soil_moisture + water_added,
|
| 151 |
+
# temperature=self._temperature,
|
| 152 |
+
# rainfall=self._rainfall
|
| 153 |
+
# )
|
| 154 |
+
|
| 155 |
+
# # Keep moisture in valid range 0-100
|
| 156 |
+
# self._soil_moisture = max(0.0, min(100.0, self._soil_moisture))
|
| 157 |
+
|
| 158 |
+
# # --- Update Crop Health based on moisture ---
|
| 159 |
+
# health_order = ["poor", "fair", "good", "excellent"]
|
| 160 |
+
# current_idx = health_order.index(self._crop_health.value)
|
| 161 |
+
|
| 162 |
+
# if self._soil_moisture < 20 or self._soil_moisture > 90:
|
| 163 |
+
# # Very wrong moisture — health degrades
|
| 164 |
+
# new_idx = max(0, current_idx - 1)
|
| 165 |
+
# self._crop_health = CropHealthStatus(health_order[new_idx])
|
| 166 |
+
# elif 40 <= self._soil_moisture <= 75:
|
| 167 |
+
# # Good moisture — health improves
|
| 168 |
+
# new_idx = min(3, current_idx + 1)
|
| 169 |
+
# self._crop_health = CropHealthStatus(health_order[new_idx])
|
| 170 |
+
# # else: moisture okay but not ideal — health stays same
|
| 171 |
+
|
| 172 |
+
# # --- Advance Day ---
|
| 173 |
+
# self._day += 1
|
| 174 |
+
|
| 175 |
+
# # --- Auto Update Crop Stage based on day ---
|
| 176 |
+
# if self._day <= 10:
|
| 177 |
+
# self._crop_stage = CropStage.SEEDLING
|
| 178 |
+
# elif self._day <= 20:
|
| 179 |
+
# self._crop_stage = CropStage.GROWING
|
| 180 |
+
# else:
|
| 181 |
+
# self._crop_stage = CropStage.MATURE
|
| 182 |
+
|
| 183 |
+
# # --- Calculate Reward ---
|
| 184 |
+
# reward = self._calculate_reward(water_added)
|
| 185 |
+
|
| 186 |
+
# # --- Is episode done? ---
|
| 187 |
+
# done = self._day > 30
|
| 188 |
+
|
| 189 |
+
# return FarmObservation(
|
| 190 |
+
# temperature=self._temperature,
|
| 191 |
+
# rainfall=self._rainfall,
|
| 192 |
+
# soil_moisture=self._soil_moisture,
|
| 193 |
+
# crop_health=self._crop_health,
|
| 194 |
+
# day_number=min(self._day, 30),
|
| 195 |
+
# crop_stage=self._crop_stage,
|
| 196 |
+
# done=done,
|
| 197 |
+
# reward=reward,
|
| 198 |
+
# )
|
| 199 |
+
|
| 200 |
+
# def _calculate_reward(self, water_added: float) -> float:
|
| 201 |
+
# """
|
| 202 |
+
# Calculate reward based on soil moisture and water usage.
|
| 203 |
+
# Returns value between -1.0 and 1.0
|
| 204 |
+
# """
|
| 205 |
+
# # Ideal moisture ranges per crop stage
|
| 206 |
+
# ideal_ranges = {
|
| 207 |
+
# CropStage.SEEDLING: (40, 70),
|
| 208 |
+
# CropStage.GROWING: (45, 70),
|
| 209 |
+
# CropStage.MATURE: (35, 60),
|
| 210 |
+
# }
|
| 211 |
+
|
| 212 |
+
# low, high = ideal_ranges[self._crop_stage]
|
| 213 |
+
|
| 214 |
+
# # Perfect moisture range
|
| 215 |
+
# if low <= self._soil_moisture <= high:
|
| 216 |
+
# reward = 1.0
|
| 217 |
+
|
| 218 |
+
# # Acceptable but not ideal
|
| 219 |
+
# elif (low - 10) <= self._soil_moisture <= (high + 10):
|
| 220 |
+
# reward = 0.5
|
| 221 |
+
|
| 222 |
+
# # Too dry
|
| 223 |
+
# elif self._soil_moisture < 20:
|
| 224 |
+
# reward = -1.0
|
| 225 |
+
|
| 226 |
+
# # Too wet
|
| 227 |
+
# elif self._soil_moisture > 90:
|
| 228 |
+
# reward = -0.5
|
| 229 |
+
|
| 230 |
+
# else:
|
| 231 |
+
# reward = 0.0
|
| 232 |
+
|
| 233 |
+
# # Penalize watering on rainy days (water waste)
|
| 234 |
+
# if self._rainfall > 15 and water_added > 0:
|
| 235 |
+
# reward -= 0.3
|
| 236 |
+
|
| 237 |
+
# return round(max(-1.0, min(1.0, reward)), 2)
|
| 238 |
+
|
| 239 |
+
# @property
|
| 240 |
+
# def state(self) -> State:
|
| 241 |
+
# """Return current environment state."""
|
| 242 |
+
# return self._state
|
| 243 |
|
| 244 |
from uuid import uuid4
|
| 245 |
|
| 246 |
+
from openenv.core.env_server.interfaces import Environment
|
| 247 |
from openenv.core.env_server.types import State
|
| 248 |
|
| 249 |
try:
|
|
|
|
| 255 |
|
| 256 |
|
| 257 |
class FarmEnvironment(Environment):
|
| 258 |
+
"""Smart Farm Irrigation Environment."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
SUPPORTS_CONCURRENT_SESSIONS: bool = True
|
| 261 |
|
| 262 |
def __init__(self):
|
|
|
|
| 263 |
self._state = State(episode_id=str(uuid4()), step_count=0)
|
|
|
|
|
|
|
| 264 |
self._weather_service = WeatherService()
|
| 265 |
self._city = "Indore"
|
|
|
|
|
|
|
| 266 |
self._day = 1
|
| 267 |
self._soil_moisture = 30.0
|
| 268 |
self._crop_health = CropHealthStatus.FAIR
|
|
|
|
| 277 |
sensor_moisture: float = None,
|
| 278 |
crop_health: str = "fair"
|
| 279 |
) -> FarmObservation:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 281 |
|
|
|
|
| 282 |
self._city = city
|
| 283 |
weather = self._weather_service.get_weather(city)
|
| 284 |
self._temperature = weather["temperature"]
|
|
|
|
| 286 |
|
| 287 |
print(f"Weather for {city}: {self._temperature}C, {self._rainfall}mm rain")
|
| 288 |
|
|
|
|
| 289 |
if sensor_moisture is not None:
|
|
|
|
| 290 |
self._soil_moisture = sensor_moisture
|
| 291 |
print(f"Soil moisture from sensor: {self._soil_moisture}%")
|
| 292 |
else:
|
|
|
|
| 293 |
self._soil_moisture = self._weather_service.calculate_soil_moisture(
|
| 294 |
current_moisture=50.0,
|
| 295 |
temperature=self._temperature,
|
|
|
|
| 297 |
)
|
| 298 |
print(f"Soil moisture calculated: {self._soil_moisture}%")
|
| 299 |
|
|
|
|
| 300 |
try:
|
| 301 |
self._crop_health = CropHealthStatus(crop_health)
|
| 302 |
except ValueError:
|
| 303 |
self._crop_health = CropHealthStatus.FAIR
|
| 304 |
|
|
|
|
| 305 |
self._day = 1
|
| 306 |
self._crop_stage = CropStage.SEEDLING
|
| 307 |
|
|
|
|
| 317 |
)
|
| 318 |
|
| 319 |
def step(self, action: FarmAction) -> FarmObservation:
|
| 320 |
+
"""Execute one day of farming."""
|
| 321 |
+
self._state.step_count += 1
|
| 322 |
+
|
| 323 |
+
# Water Amount Mapping
|
| 324 |
+
water_map = {
|
| 325 |
+
"none": 0,
|
| 326 |
+
"low": 10,
|
| 327 |
+
"medium": 25,
|
| 328 |
+
"high": 50,
|
| 329 |
+
}
|
| 330 |
+
water_added = water_map.get(action.water_amount.value, 0)
|
| 331 |
+
|
| 332 |
+
# Use real historical weather for each day
|
| 333 |
+
weather = self._weather_service.get_historical_weather(
|
| 334 |
+
self._city, self._day
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
)
|
| 336 |
+
self._temperature = weather["temperature"]
|
| 337 |
+
self._rainfall = weather["rainfall"]
|
| 338 |
|
| 339 |
+
# Update Soil Moisture
|
| 340 |
+
if self._sensor_moisture is not None:
|
| 341 |
+
evaporation = (self._temperature / 100) * 10
|
| 342 |
+
self._soil_moisture = self._soil_moisture + water_added - evaporation
|
| 343 |
+
else:
|
| 344 |
+
self._soil_moisture = self._weather_service.calculate_soil_moisture(
|
| 345 |
+
current_moisture=self._soil_moisture + water_added,
|
| 346 |
+
temperature=self._temperature,
|
| 347 |
+
rainfall=self._rainfall
|
| 348 |
+
)
|
| 349 |
|
| 350 |
+
# Keep moisture in valid range 0-100
|
| 351 |
+
self._soil_moisture = max(0.0, min(100.0, self._soil_moisture))
|
|
|
|
| 352 |
|
| 353 |
+
# Update Crop Health based on moisture
|
| 354 |
+
health_order = ["poor", "fair", "good", "excellent"]
|
| 355 |
+
current_idx = health_order.index(self._crop_health.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
+
if self._soil_moisture < 20 or self._soil_moisture > 90:
|
| 358 |
+
new_idx = max(0, current_idx - 1)
|
| 359 |
+
self._crop_health = CropHealthStatus(health_order[new_idx])
|
| 360 |
+
elif 40 <= self._soil_moisture <= 75:
|
| 361 |
+
new_idx = min(3, current_idx + 1)
|
| 362 |
+
self._crop_health = CropHealthStatus(health_order[new_idx])
|
| 363 |
|
| 364 |
+
# Advance Day
|
| 365 |
+
self._day += 1
|
| 366 |
+
|
| 367 |
+
# Auto Update Crop Stage based on day
|
| 368 |
+
if self._day <= 10:
|
| 369 |
+
self._crop_stage = CropStage.SEEDLING
|
| 370 |
+
elif self._day <= 20:
|
| 371 |
+
self._crop_stage = CropStage.GROWING
|
| 372 |
+
else:
|
| 373 |
+
self._crop_stage = CropStage.MATURE
|
| 374 |
+
|
| 375 |
+
# Calculate Reward
|
| 376 |
+
reward = self._calculate_reward(water_added)
|
| 377 |
+
|
| 378 |
+
# Is episode done?
|
| 379 |
+
done = self._day > 30
|
| 380 |
+
|
| 381 |
+
return FarmObservation(
|
| 382 |
+
temperature=self._temperature,
|
| 383 |
+
rainfall=self._rainfall,
|
| 384 |
+
soil_moisture=self._soil_moisture,
|
| 385 |
+
crop_health=self._crop_health,
|
| 386 |
+
day_number=min(self._day, 30),
|
| 387 |
+
crop_stage=self._crop_stage,
|
| 388 |
+
done=done,
|
| 389 |
+
reward=reward,
|
| 390 |
+
)
|
| 391 |
|
| 392 |
def _calculate_reward(self, water_added: float) -> float:
|
| 393 |
+
"""Calculate reward. Returns value between -1.0 and 1.0"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
ideal_ranges = {
|
| 395 |
CropStage.SEEDLING: (40, 70),
|
| 396 |
CropStage.GROWING: (45, 70),
|
|
|
|
| 399 |
|
| 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 |
|
|
|
|
| 417 |
|
| 418 |
@property
|
| 419 |
def state(self) -> State:
|
| 420 |
+
return self._state
|
|
|