|
| 1 | +""" |
| 2 | +FrozenLake Environment Adapter |
| 3 | +
|
| 4 | +This adapter implements the EnvironmentAdapter interface for FrozenLake environments, |
| 5 | +enabling integration with the MCP-Gym framework. |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import Any, Dict, Optional, Tuple |
| 9 | + |
| 10 | +from gymnasium.envs.toy_text.frozen_lake import FrozenLakeEnv, generate_random_map |
| 11 | + |
| 12 | +from eval_protocol.mcp.adapter import EnvironmentAdapter |
| 13 | + |
| 14 | + |
| 15 | +class FrozenLakeAdapter(EnvironmentAdapter): |
| 16 | + """FrozenLake adapter for MCP-Gym framework.""" |
| 17 | + |
| 18 | + ACTION_NAMES = ["LEFT", "DOWN", "RIGHT", "UP"] |
| 19 | + |
| 20 | + def create_environment(self, config: Optional[Dict[str, Any]] = None) -> FrozenLakeEnv: |
| 21 | + """ |
| 22 | + Create FrozenLake environment. |
| 23 | +
|
| 24 | + Args: |
| 25 | + config: Configuration dictionary with optional 'map_name' and 'seed' |
| 26 | +
|
| 27 | + Returns: |
| 28 | + FrozenLake environment instance |
| 29 | + """ |
| 30 | + print(f"🔍 FrozenLakeAdapter.create_environment: config: {config}") |
| 31 | + config = config or {} |
| 32 | + |
| 33 | + # Determine grid size from config |
| 34 | + grid_size = 4 |
| 35 | + if "map_name" in config: |
| 36 | + if "8x8" in config["map_name"]: |
| 37 | + grid_size = 8 |
| 38 | + |
| 39 | + # Generate random map if seed is provided |
| 40 | + seed = config.get("seed") |
| 41 | + print(f"🔍 FrozenLakeAdapter.create_environment: extracted seed: {seed} (type: {type(seed)})") |
| 42 | + print(f"🔍 FrozenLakeAdapter.create_environment: grid_size: {grid_size}") |
| 43 | + |
| 44 | + if seed is not None: |
| 45 | + print(f"🔍 FrozenLakeAdapter.create_environment: Generating map with seed {seed}") |
| 46 | + desc = generate_random_map(size=grid_size, p=0.8, seed=seed) |
| 47 | + print(f"🔍 FrozenLakeAdapter.create_environment: Generated map desc: {desc}") |
| 48 | + else: |
| 49 | + print("🔍 FrozenLakeAdapter.create_environment: Generating map without seed") |
| 50 | + desc = generate_random_map(size=grid_size, p=0.8) |
| 51 | + print(f"🔍 FrozenLakeAdapter.create_environment: Generated map desc: {desc}") |
| 52 | + |
| 53 | + env = FrozenLakeEnv(desc=desc, is_slippery=False, render_mode="ansi") |
| 54 | + print("🔍 FrozenLakeAdapter.create_environment: Created FrozenLakeEnv") |
| 55 | + return env |
| 56 | + |
| 57 | + def create_environment_with_seed( |
| 58 | + self, config: Optional[Dict[str, Any]] = None, seed: Optional[int] = None |
| 59 | + ) -> Tuple[FrozenLakeEnv, int, Dict[str, Any]]: |
| 60 | + """ |
| 61 | + Create FrozenLake environment with seed and return initial state. |
| 62 | +
|
| 63 | + Args: |
| 64 | + config: Configuration dictionary |
| 65 | + seed: Seed for reproducible environments |
| 66 | +
|
| 67 | + Returns: |
| 68 | + Tuple of (environment, initial_observation, initial_info) |
| 69 | + """ |
| 70 | + print(f"🔍 FrozenLakeAdapter.create_environment_with_seed: config: {config}, seed: {seed}") |
| 71 | + config = config or {} |
| 72 | + |
| 73 | + # Add seed to config for environment creation |
| 74 | + env_config = {**config, "seed": seed} |
| 75 | + print(f"🔍 FrozenLakeAdapter.create_environment_with_seed: env_config: {env_config}") |
| 76 | + |
| 77 | + env = self.create_environment(env_config) |
| 78 | + print(f"🔍 FrozenLakeAdapter.create_environment_with_seed: created env, calling reset with seed: {seed}") |
| 79 | + obs, info = env.reset(seed=seed) |
| 80 | + print(f"🔍 FrozenLakeAdapter.create_environment_with_seed: reset returned obs: {obs}, info: {info}") |
| 81 | + |
| 82 | + return env, obs, info |
| 83 | + |
| 84 | + def reset_environment(self, env: FrozenLakeEnv, seed: Optional[int] = None) -> Tuple[int, Dict[str, Any]]: |
| 85 | + """ |
| 86 | + Reset environment. |
| 87 | +
|
| 88 | + Args: |
| 89 | + env: Environment instance |
| 90 | + seed: Optional seed for reset |
| 91 | +
|
| 92 | + Returns: |
| 93 | + Tuple of (observation, info) |
| 94 | + """ |
| 95 | + return env.reset(seed=seed) |
| 96 | + |
| 97 | + def step_environment(self, env: FrozenLakeEnv, action: int) -> Tuple[int, float, bool, bool, Dict[str, Any]]: |
| 98 | + """ |
| 99 | + Execute environment step. |
| 100 | +
|
| 101 | + Args: |
| 102 | + env: Environment instance |
| 103 | + action: Action index |
| 104 | +
|
| 105 | + Returns: |
| 106 | + Tuple of (observation, reward, terminated, truncated, info) |
| 107 | + """ |
| 108 | + return env.step(action) |
| 109 | + |
| 110 | + def close_environment(self, env: FrozenLakeEnv) -> None: |
| 111 | + """ |
| 112 | + Close environment. |
| 113 | +
|
| 114 | + Args: |
| 115 | + env: Environment instance |
| 116 | + """ |
| 117 | + # FrozenLake doesn't need explicit cleanup |
| 118 | + pass |
| 119 | + |
| 120 | + def parse_action(self, action_str: str) -> int: |
| 121 | + """ |
| 122 | + Parse action string to integer. |
| 123 | +
|
| 124 | + Args: |
| 125 | + action_str: Action string (LEFT, DOWN, RIGHT, UP) |
| 126 | +
|
| 127 | + Returns: |
| 128 | + Action index |
| 129 | +
|
| 130 | + Raises: |
| 131 | + ValueError: If action is invalid |
| 132 | + """ |
| 133 | + action_str = action_str.strip().upper() |
| 134 | + if action_str not in self.ACTION_NAMES: |
| 135 | + raise ValueError(f"Invalid action '{action_str}'. Valid actions: {self.ACTION_NAMES}") |
| 136 | + return self.ACTION_NAMES.index(action_str) |
| 137 | + |
| 138 | + def format_observation(self, observation: int) -> int: |
| 139 | + """ |
| 140 | + Format observation for JSON serialization. |
| 141 | +
|
| 142 | + Args: |
| 143 | + observation: Raw observation from environment |
| 144 | +
|
| 145 | + Returns: |
| 146 | + Formatted observation |
| 147 | + """ |
| 148 | + return int(observation) |
| 149 | + |
| 150 | + def get_default_config(self) -> Dict[str, Any]: |
| 151 | + """ |
| 152 | + Get default configuration. |
| 153 | +
|
| 154 | + Returns: |
| 155 | + Default configuration dictionary |
| 156 | + """ |
| 157 | + return { |
| 158 | + "map_name": "4x4", |
| 159 | + "is_slippery": False, |
| 160 | + } |
0 commit comments