Update environment.py
Browse files- environment.py +29 -51
environment.py
CHANGED
|
@@ -11,11 +11,13 @@ class DocumentClassificationEnv(gym.Env):
|
|
| 11 |
"render_modes": ["human"],
|
| 12 |
"name": "DocumentClassification-v1"
|
| 13 |
}
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
CATEGORY_MAPS = {
|
| 16 |
"easy": {
|
| 17 |
0: "General",
|
| 18 |
-
1: "Billing",
|
| 19 |
2: "Support",
|
| 20 |
3: "Technical",
|
| 21 |
4: "HR"
|
|
@@ -32,34 +34,28 @@ class DocumentClassificationEnv(gym.Env):
|
|
| 32 |
8: "Legal",
|
| 33 |
9: "Executive"
|
| 34 |
},
|
| 35 |
-
# ✅ FIXED: only 20 categories
|
| 36 |
"hard": {
|
| 37 |
0: "General", 1: "Billing", 2: "Billing-Dispute", 3: "Billing-Refund",
|
| 38 |
4: "Support-Urgent", 5: "Support-Normal", 6: "Technical", 7: "Technical-Bug",
|
| 39 |
8: "Technical-Feature", 9: "HR-Payroll", 10: "HR-Benefits", 11: "HR-Complaint",
|
| 40 |
12: "Legal", 13: "Legal-Contract", 14: "Legal-Compliance", 15: "Executive",
|
| 41 |
-
16: "Executive-Strategic", 17: "Finance", 18: "Marketing", 19: "Operations"
|
|
|
|
| 42 |
}
|
| 43 |
}
|
| 44 |
-
|
| 45 |
def __init__(self, task_difficulty="easy", seed=None):
|
| 46 |
super().__init__()
|
| 47 |
-
|
| 48 |
if task_difficulty not in ["easy", "medium", "hard"]:
|
| 49 |
raise ValueError(f"Pick easy, medium, or hard. Got: {task_difficulty}")
|
| 50 |
-
|
| 51 |
-
self.task_difficulty = task_difficulty
|
| 52 |
-
|
| 53 |
if seed is not None:
|
| 54 |
np.random.seed(seed)
|
| 55 |
random.seed(seed)
|
| 56 |
-
|
| 57 |
self.task_config = get_task_config(task_difficulty)
|
| 58 |
self.num_categories = self.task_config["num_categories"]
|
| 59 |
self.feature_dim = self.task_config["feature_dim"]
|
| 60 |
-
|
| 61 |
self.action_space = spaces.Discrete(self.num_categories)
|
| 62 |
-
|
| 63 |
self.observation_space = spaces.Dict({
|
| 64 |
"document_id": spaces.Text(max_length=20),
|
| 65 |
"content": spaces.Text(max_length=10000),
|
|
@@ -69,60 +65,48 @@ class DocumentClassificationEnv(gym.Env):
|
|
| 69 |
"document_index": spaces.Box(low=0, high=10000, shape=(1,), dtype=np.int32),
|
| 70 |
"total_documents": spaces.Box(low=0, high=10000, shape=(1,), dtype=np.int32),
|
| 71 |
})
|
| 72 |
-
|
| 73 |
self.data_generator = TaskDataGenerator(task_difficulty, seed)
|
| 74 |
-
|
| 75 |
self.documents = None
|
| 76 |
self.labels = None
|
| 77 |
self.current_document_index = 0
|
| 78 |
-
|
| 79 |
self.episode_rewards = []
|
| 80 |
self.correct = 0
|
| 81 |
self.total = 0
|
| 82 |
self.episode_times = []
|
| 83 |
self.start_time = None
|
| 84 |
-
|
| 85 |
def reset(self, seed=None, options=None):
|
| 86 |
if seed is not None:
|
| 87 |
np.random.seed(seed)
|
| 88 |
random.seed(seed)
|
| 89 |
-
|
| 90 |
self.documents, self.labels = self.data_generator.generate_task_data()
|
| 91 |
-
|
| 92 |
self.current_document_index = 0
|
| 93 |
self.episode_rewards = []
|
| 94 |
self.correct = 0
|
| 95 |
self.total = 0
|
| 96 |
self.episode_times = []
|
| 97 |
self.start_time = time.time()
|
| 98 |
-
|
| 99 |
return self._obs(), {}
|
| 100 |
-
|
| 101 |
def step(self, action):
|
| 102 |
t0 = time.time()
|
| 103 |
-
|
| 104 |
if self.current_document_index >= len(self.documents):
|
| 105 |
return self._obs(), 0.0, True, False, {"episode_complete": True}
|
| 106 |
-
|
| 107 |
true_label = self.labels[self.current_document_index]
|
| 108 |
is_correct = action == true_label
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
reward = 1.0 if is_correct else -0.5
|
| 112 |
-
|
| 113 |
self.episode_rewards.append(reward)
|
| 114 |
-
|
| 115 |
if is_correct:
|
| 116 |
self.correct += 1
|
| 117 |
-
|
| 118 |
self.total += 1
|
| 119 |
self.episode_times.append(time.time() - t0)
|
| 120 |
-
|
| 121 |
self.current_document_index += 1
|
| 122 |
done = self.current_document_index >= len(self.documents)
|
| 123 |
-
|
| 124 |
obs = self._obs()
|
| 125 |
-
|
| 126 |
info = {
|
| 127 |
"is_correct": is_correct,
|
| 128 |
"true_category": self.CATEGORY_MAPS[self.task_difficulty][true_label],
|
|
@@ -130,24 +114,21 @@ class DocumentClassificationEnv(gym.Env):
|
|
| 130 |
"processing_time_ms": (time.time() - t0) * 1000,
|
| 131 |
"episode_accuracy": self.correct / self.total if self.total > 0 else 0.0,
|
| 132 |
}
|
| 133 |
-
|
| 134 |
if done:
|
| 135 |
info["episode_summary"] = self._summary()
|
| 136 |
-
|
| 137 |
return obs, reward, done, False, info
|
| 138 |
-
|
| 139 |
-
# ✅ FIXED: matches test expectations
|
| 140 |
def state(self):
|
| 141 |
return {
|
| 142 |
"current_observation": self._obs(),
|
| 143 |
-
"
|
| 144 |
"total_documents": len(self.documents) if self.documents else 0,
|
| 145 |
-
"
|
| 146 |
-
"
|
| 147 |
-
"
|
| 148 |
"difficulty": self.task_difficulty,
|
| 149 |
}
|
| 150 |
-
|
| 151 |
def _obs(self):
|
| 152 |
if self.current_document_index >= len(self.documents):
|
| 153 |
return {
|
|
@@ -159,9 +140,7 @@ class DocumentClassificationEnv(gym.Env):
|
|
| 159 |
"document_index": np.array([self.current_document_index], dtype=np.int32),
|
| 160 |
"total_documents": np.array([len(self.documents)], dtype=np.int32),
|
| 161 |
}
|
| 162 |
-
|
| 163 |
doc = self.documents[self.current_document_index]
|
| 164 |
-
|
| 165 |
return {
|
| 166 |
"document_id": doc["id"],
|
| 167 |
"content": doc["content"],
|
|
@@ -171,14 +150,13 @@ class DocumentClassificationEnv(gym.Env):
|
|
| 171 |
"document_index": np.array([self.current_document_index], dtype=np.int32),
|
| 172 |
"total_documents": np.array([len(self.documents)], dtype=np.int32),
|
| 173 |
}
|
| 174 |
-
|
| 175 |
def _summary(self):
|
| 176 |
total_reward = sum(self.episode_rewards)
|
| 177 |
avg_reward = total_reward / len(self.episode_rewards) if self.episode_rewards else 0.0
|
| 178 |
acc = self.correct / self.total if self.total > 0 else 0.0
|
| 179 |
avg_time = np.mean(self.episode_times) * 1000 if self.episode_times else 0.0
|
| 180 |
elapsed = time.time() - self.start_time
|
| 181 |
-
|
| 182 |
return {
|
| 183 |
"total_reward": total_reward,
|
| 184 |
"avg_reward": avg_reward,
|
|
@@ -187,11 +165,11 @@ class DocumentClassificationEnv(gym.Env):
|
|
| 187 |
"avg_time_ms": avg_time,
|
| 188 |
"total_time_s": elapsed,
|
| 189 |
}
|
| 190 |
-
|
| 191 |
-
# ✅ FIXED render
|
| 192 |
def render(self):
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
| 11 |
"render_modes": ["human"],
|
| 12 |
"name": "DocumentClassification-v1"
|
| 13 |
}
|
| 14 |
+
|
| 15 |
+
# Different difficulty levels = different number of categories
|
| 16 |
+
# Easy = 5, Medium = 10, Hard = 20 (gets gnarly fast)
|
| 17 |
CATEGORY_MAPS = {
|
| 18 |
"easy": {
|
| 19 |
0: "General",
|
| 20 |
+
1: "Billing",
|
| 21 |
2: "Support",
|
| 22 |
3: "Technical",
|
| 23 |
4: "HR"
|
|
|
|
| 34 |
8: "Legal",
|
| 35 |
9: "Executive"
|
| 36 |
},
|
|
|
|
| 37 |
"hard": {
|
| 38 |
0: "General", 1: "Billing", 2: "Billing-Dispute", 3: "Billing-Refund",
|
| 39 |
4: "Support-Urgent", 5: "Support-Normal", 6: "Technical", 7: "Technical-Bug",
|
| 40 |
8: "Technical-Feature", 9: "HR-Payroll", 10: "HR-Benefits", 11: "HR-Complaint",
|
| 41 |
12: "Legal", 13: "Legal-Contract", 14: "Legal-Compliance", 15: "Executive",
|
| 42 |
+
16: "Executive-Strategic", 17: "Finance", 18: "Marketing", 19: "Operations",
|
| 43 |
+
20: "HR", 21: "Support"
|
| 44 |
}
|
| 45 |
}
|
| 46 |
+
|
| 47 |
def __init__(self, task_difficulty="easy", seed=None):
|
| 48 |
super().__init__()
|
| 49 |
+
self.task_difficulty = task_difficulty
|
| 50 |
if task_difficulty not in ["easy", "medium", "hard"]:
|
| 51 |
raise ValueError(f"Pick easy, medium, or hard. Got: {task_difficulty}")
|
|
|
|
|
|
|
|
|
|
| 52 |
if seed is not None:
|
| 53 |
np.random.seed(seed)
|
| 54 |
random.seed(seed)
|
|
|
|
| 55 |
self.task_config = get_task_config(task_difficulty)
|
| 56 |
self.num_categories = self.task_config["num_categories"]
|
| 57 |
self.feature_dim = self.task_config["feature_dim"]
|
|
|
|
| 58 |
self.action_space = spaces.Discrete(self.num_categories)
|
|
|
|
| 59 |
self.observation_space = spaces.Dict({
|
| 60 |
"document_id": spaces.Text(max_length=20),
|
| 61 |
"content": spaces.Text(max_length=10000),
|
|
|
|
| 65 |
"document_index": spaces.Box(low=0, high=10000, shape=(1,), dtype=np.int32),
|
| 66 |
"total_documents": spaces.Box(low=0, high=10000, shape=(1,), dtype=np.int32),
|
| 67 |
})
|
|
|
|
| 68 |
self.data_generator = TaskDataGenerator(task_difficulty, seed)
|
|
|
|
| 69 |
self.documents = None
|
| 70 |
self.labels = None
|
| 71 |
self.current_document_index = 0
|
|
|
|
| 72 |
self.episode_rewards = []
|
| 73 |
self.correct = 0
|
| 74 |
self.total = 0
|
| 75 |
self.episode_times = []
|
| 76 |
self.start_time = None
|
| 77 |
+
|
| 78 |
def reset(self, seed=None, options=None):
|
| 79 |
if seed is not None:
|
| 80 |
np.random.seed(seed)
|
| 81 |
random.seed(seed)
|
|
|
|
| 82 |
self.documents, self.labels = self.data_generator.generate_task_data()
|
|
|
|
| 83 |
self.current_document_index = 0
|
| 84 |
self.episode_rewards = []
|
| 85 |
self.correct = 0
|
| 86 |
self.total = 0
|
| 87 |
self.episode_times = []
|
| 88 |
self.start_time = time.time()
|
|
|
|
| 89 |
return self._obs(), {}
|
| 90 |
+
|
| 91 |
def step(self, action):
|
| 92 |
t0 = time.time()
|
|
|
|
| 93 |
if self.current_document_index >= len(self.documents):
|
| 94 |
return self._obs(), 0.0, True, False, {"episode_complete": True}
|
| 95 |
+
|
| 96 |
true_label = self.labels[self.current_document_index]
|
| 97 |
is_correct = action == true_label
|
| 98 |
+
reward = 1.0 if is_correct else -0.4
|
| 99 |
+
|
|
|
|
|
|
|
| 100 |
self.episode_rewards.append(reward)
|
|
|
|
| 101 |
if is_correct:
|
| 102 |
self.correct += 1
|
|
|
|
| 103 |
self.total += 1
|
| 104 |
self.episode_times.append(time.time() - t0)
|
| 105 |
+
|
| 106 |
self.current_document_index += 1
|
| 107 |
done = self.current_document_index >= len(self.documents)
|
|
|
|
| 108 |
obs = self._obs()
|
| 109 |
+
|
| 110 |
info = {
|
| 111 |
"is_correct": is_correct,
|
| 112 |
"true_category": self.CATEGORY_MAPS[self.task_difficulty][true_label],
|
|
|
|
| 114 |
"processing_time_ms": (time.time() - t0) * 1000,
|
| 115 |
"episode_accuracy": self.correct / self.total if self.total > 0 else 0.0,
|
| 116 |
}
|
|
|
|
| 117 |
if done:
|
| 118 |
info["episode_summary"] = self._summary()
|
|
|
|
| 119 |
return obs, reward, done, False, info
|
| 120 |
+
|
|
|
|
| 121 |
def state(self):
|
| 122 |
return {
|
| 123 |
"current_observation": self._obs(),
|
| 124 |
+
"document_index": self.current_document_index,
|
| 125 |
"total_documents": len(self.documents) if self.documents else 0,
|
| 126 |
+
"total_reward": sum(self.episode_rewards),
|
| 127 |
+
"accuracy": self.correct / self.total if self.total > 0 else 0.0,
|
| 128 |
+
"avg_time_ms": np.mean(self.episode_times) * 1000 if self.episode_times else 0.0,
|
| 129 |
"difficulty": self.task_difficulty,
|
| 130 |
}
|
| 131 |
+
|
| 132 |
def _obs(self):
|
| 133 |
if self.current_document_index >= len(self.documents):
|
| 134 |
return {
|
|
|
|
| 140 |
"document_index": np.array([self.current_document_index], dtype=np.int32),
|
| 141 |
"total_documents": np.array([len(self.documents)], dtype=np.int32),
|
| 142 |
}
|
|
|
|
| 143 |
doc = self.documents[self.current_document_index]
|
|
|
|
| 144 |
return {
|
| 145 |
"document_id": doc["id"],
|
| 146 |
"content": doc["content"],
|
|
|
|
| 150 |
"document_index": np.array([self.current_document_index], dtype=np.int32),
|
| 151 |
"total_documents": np.array([len(self.documents)], dtype=np.int32),
|
| 152 |
}
|
| 153 |
+
|
| 154 |
def _summary(self):
|
| 155 |
total_reward = sum(self.episode_rewards)
|
| 156 |
avg_reward = total_reward / len(self.episode_rewards) if self.episode_rewards else 0.0
|
| 157 |
acc = self.correct / self.total if self.total > 0 else 0.0
|
| 158 |
avg_time = np.mean(self.episode_times) * 1000 if self.episode_times else 0.0
|
| 159 |
elapsed = time.time() - self.start_time
|
|
|
|
| 160 |
return {
|
| 161 |
"total_reward": total_reward,
|
| 162 |
"avg_reward": avg_reward,
|
|
|
|
| 165 |
"avg_time_ms": avg_time,
|
| 166 |
"total_time_s": elapsed,
|
| 167 |
}
|
| 168 |
+
|
|
|
|
| 169 |
def render(self):
|
| 170 |
+
pass
|
| 171 |
+
|
| 172 |
+
def close(self):
|
| 173 |
+
pass
|
| 174 |
+
|
| 175 |
+
|