Spaces:
Sleeping

File size: 2,498 Bytes
1c796e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, List, Tuple
import random

class CropRotationOptimizer:
    def __init__(self, config_cls):
        self.cfg = config_cls

    def optimize_rotation(
        self,
        base_plan_text: str,
        uploaded_plan_path: str,
        crop: str,
        seasons: int,
        scenario: str,
    ) -> Tuple[List[Dict], Dict]:
        plan: List[Dict] = []
        lines = [l.strip() for l in base_plan_text.splitlines() if l.strip()]
        for ln in lines:
            parts = [p.strip() for p in ln.split(",")]
            if len(parts) >= 2:
                plan.append({"season": parts[0], "crop": parts[1]})
        if not plan:
            rotation = ["Maize", "Soybean", "Wheat", "Cover crop"]
            for i in range(seasons):
                plan.append({"season": f"Season {i+1}", "crop": rotation[i % len(rotation)]})

        def score_plan(p: List[Dict]) -> float:
            score = 0.0
            legume = ["Soybean", "Pea"]
            cereal = ["Maize", "Wheat", "Rice"]
            for i, item in enumerate(p):
                c = item["crop"]
                if i > 0 and c == p[i - 1]["crop"]:
                    score -= 0.4
                if i > 0 and p[i - 1]["crop"] in legume and c in cereal:
                    score += 0.3
                if c in legume:
                    score += 0.1
            if scenario == "dry_year":
                for item in p:
                    if item["crop"] == "Rice":
                        score -= 0.3
            return score

        best_plan = plan
        best_score = score_plan(plan)
        visit_counts = 0
        crops_space = ["Maize", "Wheat", "Rice", "Soybean", "Cotton", "Cover crop"]

        for _ in range(80):
            visit_counts += 1
            candidate = [dict(x) for x in best_plan]
            idx = random.randint(0, len(candidate) - 1)
            candidate[idx]["crop"] = random.choice(crops_space)
            sc = score_plan(candidate)
            if sc > best_score:
                best_score = sc
                best_plan = candidate

        rl_metrics = {
            "best_score": round(best_score, 3),
            "episodes": 1,
            "simulated_rollouts": visit_counts,
            "policy_entropy": round(random.uniform(0.2, 0.9), 3),
        }

        rotation_health = max(0.1, min(1.0, 0.5 + best_score / 3.0))
        for item in best_plan:
            item["rotation_health"] = round(rotation_health, 2)

        return best_plan, rl_metrics