File size: 13,359 Bytes
a8d4cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""
qlearning.py — Tabular Q-Learning for the Garbage Collecting Robot.

Training runs directly against GarbageRobotEnv (no HTTP server needed).
The Q-table is persisted to disk as JSON and loaded by inference.py at startup.

State representation:
    (robot_x, robot_y, sorted_garbage_tuple)
    e.g. (2, 3, ((1,1),(4,4)))   — compact, hashable, fully describes the relevant world

Actions:
    0=UP  1=DOWN  2=LEFT  3=RIGHT  4=COLLECT

Usage:
    # Train all tasks and save
    python3 qlearning.py --train --episodes 8000

    # Evaluate silently (uses saved Q-table)
    python3 qlearning.py --eval

Fix applied:
    - load() previously had two separate key-reconstruction passes, where the
      first pass result (variable `k`) was computed but then immediately discarded.
      The second pass also misidentified the garbage sub-list when it had exactly
      2 integer elements (treating [gx, gy] pairs as flat coords instead of a
      tuple-of-tuples). Replaced both passes with a single, unambiguous decode:
        parsed = [rx, ry, [[gx1,gy1],[gx2,gy2],...]]
      where the third element is always the nested garbage list.
"""

import os
import json
import random
import argparse
from collections import defaultdict
from environment import GarbageRobotEnv
from scenarios import SCENARIOS

# ── Constants ──────────────────────────────────────────────────────────────

ACTIONS      = ["UP", "DOWN", "LEFT", "RIGHT", "COLLECT"]
ACTION_IDX   = {a: i for i, a in enumerate(ACTIONS)}
Q_TABLE_PATH = os.environ.get("Q_TABLE_PATH", "qtable.json")

# ── Hyperparameters ─────────────────────────────────────────────────────────

ALPHA         = 0.15
GAMMA         = 0.97
EPSILON_START = 1.0
EPSILON_END   = 0.05
EPSILON_DECAY = 0.9995


# ── State Encoding ──────────────────────────────────────────────────────────

def encode_state(obs: dict) -> tuple:
    """
    Convert a raw observation dict into a hashable tuple suitable as a Q-table key.

    Key structure: (robot_x, robot_y, ((gx1,gy1),(gx2,gy2),...))
    Garbage positions are sorted so order doesn't create phantom new states.
    """
    rx, ry = obs["robot_position"]
    garbage = tuple(sorted((int(g[0]), int(g[1])) for g in obs["garbage_positions"]))
    return (int(rx), int(ry), garbage)


# ── Q-Table ─────────────────────────────────────────────────────────────────

class QTable:
    """
    Dictionary-backed Q-table with defaultdict initialisation.
    Values default to a small optimistic initial value to encourage exploration.
    """

    def __init__(self, optimistic_init: float = 0.5):
        self.optimistic_init = optimistic_init
        self._q: dict = {}

    def _ensure(self, state: tuple):
        if state not in self._q:
            self._q[state] = [self.optimistic_init] * len(ACTIONS)

    def get(self, state: tuple, action_idx: int) -> float:
        self._ensure(state)
        return self._q[state][action_idx]

    def update(self, state: tuple, action_idx: int, value: float):
        self._ensure(state)
        self._q[state][action_idx] = value

    def best_action(self, state: tuple) -> int:
        """Return the index of the greedy best action."""
        self._ensure(state)
        return int(max(range(len(ACTIONS)), key=lambda i: self._q[state][i]))

    def best_q(self, state: tuple) -> float:
        self._ensure(state)
        return max(self._q[state])

    # ── Persistence ─────────────────────────────────────────────────────────

    def save(self, path: str = Q_TABLE_PATH):
        """
        Serialise Q-table to JSON.

        Key format saved to disk:
            [rx, ry, [[gx1,gy1], [gx2,gy2], ...]]
        This is unambiguous: element 0 and 1 are ints, element 2 is always a
        list-of-lists, even when there is only one garbage piece.
        """
        serialisable = {}
        for (rx, ry, garbage), v in self._q.items():
            key = json.dumps([rx, ry, [list(g) for g in garbage]])
            serialisable[key] = v
        with open(path, "w") as f:
            json.dump(serialisable, f)
        print(f"[Q-Table] Saved {len(self._q):,} states → {path}")

    def load(self, path: str = Q_TABLE_PATH) -> bool:
        """
        Load Q-table from JSON.

        FIX: The previous implementation had two redundant key-reconstruction
        loops. The first built variable `k` which was immediately discarded;
        the second pass misclassified [gx, gy] pairs (lists of 2 ints) as flat
        coordinates rather than garbage-position tuples, corrupting multi-garbage
        states.

        New single-pass decode relies on the unambiguous 3-element structure:
            parsed[0] = rx  (int)
            parsed[1] = ry  (int)
            parsed[2] = [[gx1,gy1], ...]  (always a list-of-lists)
        """
        if not os.path.exists(path):
            return False
        with open(path, "r") as f:
            raw = json.load(f)
        self._q = {}
        for k_str, v in raw.items():
            parsed = json.loads(k_str)
            # Robustly handle both new format [rx, ry, [[gx,gy],...]]
            # and old format [rx, ry, [gx, gy]] (single garbage, flat list).
            rx, ry = int(parsed[0]), int(parsed[1])
            raw_garbage = parsed[2]
            if raw_garbage and isinstance(raw_garbage[0], list):
                # New / multi-garbage format: [[gx1,gy1],[gx2,gy2],...]
                garbage = tuple(tuple(p) for p in raw_garbage)
            elif raw_garbage and isinstance(raw_garbage[0], int):
                # Old single-garbage flat format: [gx, gy]
                garbage = (tuple(raw_garbage),)
            else:
                garbage = ()
            self._q[(rx, ry, garbage)] = v
        print(f"[Q-Table] Loaded {len(self._q):,} states ← {path}")
        return True

    def __len__(self):
        return len(self._q)


# ── Observation Helper ───────────────────────────────────────────────────────

def _obs_from_env(env) -> dict:
    """Build an obs dict directly from GarbageRobotEnv fields."""
    obs_obj = env.get_observation()
    return {
        "robot_position":     obs_obj.robot_position,
        "garbage_positions":  list(obs_obj.garbage_positions),
        "obstacle_positions": list(obs_obj.obstacle_positions),
        "grid_size":          obs_obj.grid_size,
        "battery_level":      obs_obj.battery_level,
        "inventory_count":    obs_obj.inventory_count,
        "message":            obs_obj.message,
        "robot_mode":         obs_obj.robot_mode,
        "home_position":      obs_obj.home_position,
        "unload_station":     obs_obj.unload_station,
        "current_storage_load": obs_obj.current_storage_load,
        "storage_capacity":   obs_obj.storage_capacity,
        "distance_from_home": obs_obj.distance_from_home,
    }


# ── Training ─────────────────────────────────────────────────────────────────

def train(
    task_ids=None,
    episodes: int = 8000,
    qtable: QTable = None,
    verbose: bool = True,
) -> QTable:
    """
    Run Q-learning over the given task_ids for `episodes` total episodes.
    Tasks are sampled uniformly so the agent generalises across difficulties.
    """
    if task_ids is None:
        task_ids = list(SCENARIOS.keys())

    if qtable is None:
        qtable = QTable()

    env     = GarbageRobotEnv()
    epsilon = EPSILON_START

    best_scores: dict = {t: 0.0 for t in task_ids}

    for ep in range(1, episodes + 1):
        task_id = random.choice(task_ids)
        env.reset(task_id)
        obs       = _obs_from_env(env)
        state     = encode_state(obs)

        total_reward = 0.0
        done         = False

        while not done:
            if random.random() < epsilon:
                action_idx = random.randrange(len(ACTIONS))
            else:
                action_idx = qtable.best_action(state)

            action     = ACTIONS[action_idx]
            result     = env.step(action)
            next_obs   = result["observation"]
            reward     = result["reward"]
            done       = result["done"]

            next_state = encode_state(next_obs)

            # Bellman update
            old_q     = qtable.get(state, action_idx)
            td_target = reward + (0.0 if done else GAMMA * qtable.best_q(next_state))
            new_q     = old_q + ALPHA * (td_target - old_q)
            qtable.update(state, action_idx, new_q)

            state = next_state
            obs   = next_obs
            total_reward += reward

        score = env.grade(task_id)
        if score > best_scores[task_id]:
            best_scores[task_id] = score

        epsilon = max(EPSILON_END, epsilon * EPSILON_DECAY)

        if verbose and ep % 500 == 0:
            avg_best = sum(best_scores.values()) / len(best_scores)
            print(
                f"  Ep {ep:5d}/{episodes}  ε={epsilon:.4f}  "
                f"states={len(qtable):,}  "
                f"best_scores={best_scores}  avg={avg_best:.2f}"
            )

    return qtable


# ── Inference Helper (used by inference.py) ──────────────────────────────────

class QLearningAgent:
    """
    Thin wrapper around a loaded Q-table for use by inference.py.
    Falls through (returns None) when the state has never been seen during training.
    """

    def __init__(self, path: str = Q_TABLE_PATH):
        self.qtable = QTable()
        self.loaded = self.qtable.load(path)

    def get_action(self, obs: dict) -> str | None:
        if not self.loaded:
            return None
        state = encode_state(obs)
        if state not in self.qtable._q:
            return None
        return ACTIONS[self.qtable.best_action(state)]


# ── Evaluation ───────────────────────────────────────────────────────────────

def evaluate(qtable: QTable, task_ids=None, runs: int = 5) -> dict:
    """Run `runs` greedy episodes per task and return average scores."""
    if task_ids is None:
        task_ids = list(SCENARIOS.keys())

    env     = GarbageRobotEnv()
    results = {}

    for task_id in task_ids:
        scores = []
        for _ in range(runs):
            env.reset(task_id)
            obs  = _obs_from_env(env)
            done = False
            while not done:
                state      = encode_state(obs)
                action_idx = qtable.best_action(state)
                result     = env.step(ACTIONS[action_idx])
                obs        = result["observation"]
                done       = result["done"]
            scores.append(env.grade(task_id))
        avg = sum(scores) / len(scores)
        results[task_id] = round(avg, 3)
        print(f"  {task_id:12s}  avg score = {avg:.3f}  ({scores})")

    return results


# ── CLI Entry Point ───────────────────────────────────────────────────────────

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Q-Learning for Garbage Robot")
    parser.add_argument("--train",    action="store_true", help="Run training")
    parser.add_argument("--eval",     action="store_true", help="Run evaluation only")
    parser.add_argument("--episodes", type=int, default=8000)
    parser.add_argument("--tasks",    nargs="+", default=None)
    parser.add_argument("--output",   default=Q_TABLE_PATH)
    args = parser.parse_args()

    if args.train:
        print("=" * 55)
        print("  Q-Learning Training — Garbage Collecting Robot")
        print("=" * 55)
        task_ids = args.tasks or list(SCENARIOS.keys())
        print(f"  Tasks    : {task_ids}")
        print(f"  Episodes : {args.episodes}")
        print(f"  α={ALPHA}  γ={GAMMA}  ε {EPSILON_START}{EPSILON_END}  decay={EPSILON_DECAY}")
        print()

        qt = train(task_ids=task_ids, episodes=args.episodes, verbose=True)
        qt.save(args.output)

        print("\n  — Evaluation on greedy policy —")
        evaluate(qt, task_ids)

    elif args.eval:
        print("=" * 55)
        print("  Q-Learning Evaluation")
        print("=" * 55)
        qt = QTable()
        if not qt.load(args.output):
            print(f"[ERROR] No Q-table found at {args.output}. Run with --train first.")
        else:
            evaluate(qt)
    else:
        parser.print_help()