| 169 | |
| 170 | |
| 171 | def findValidActionNew(predictions, env, look, recent_actions, sbert_model, logger, k=5): |
| 172 | global rooms |
| 173 | valid_open_door = ["open door to " + i for i in rooms] |
| 174 | invalid_focus = ["focus on "+x for x in ["agent", "air"]+rooms] |
| 175 | validActions = set(env.getValidActionObjectCombinations()) |
| 176 | validActions.update(valid_open_door) |
| 177 | validActions.difference_update(invalid_focus) |
| 178 | |
| 179 | inventory = env.inventory().lower() |
| 180 | |
| 181 | validActions.difference_update(recent_actions[-3:]) |
| 182 | |
| 183 | for va in list(validActions): |
| 184 | if "door" in va and "open" not in va: |
| 185 | validActions.remove(va) |
| 186 | continue |
| 187 | if va.startswith("focus on"): |
| 188 | pattern = re.compile(r"\b(?:focus|on|in|to)\b", re.IGNORECASE) |
| 189 | used_objs = pattern.sub("", va).split(" ") |
| 190 | valid = True |
| 191 | for obj in used_objs: |
| 192 | if obj not in look + " " + inventory: |
| 193 | valid = False |
| 194 | if not valid: |
| 195 | validActions.remove(va) |
| 196 | |
| 197 | |
| 198 | # 1) if acton in top k is valid, choose it |
| 199 | found_valid_in_top = False |
| 200 | action = None |
| 201 | for pred in predictions[:k]: |
| 202 | pred = pred.replace("green house", "greenhouse") |
| 203 | if pred.strip() in validActions: |
| 204 | found_valid_in_top = True |
| 205 | action = pred.strip() |
| 206 | break |
| 207 | if found_valid_in_top: |
| 208 | return action |
| 209 | else: |
| 210 | logger.info(f"No valid action found in top k={k} predictions.") |
| 211 | validActions = list(validActions) |
| 212 | validActions.sort(key=lambda x: len(x)) |
| 213 | logger.info("Valid Predictions: "+ str(validActions)) |
| 214 | |
| 215 | |
| 216 | # 2) else, find most similar action |
| 217 | |
| 218 | if sbert_model: |
| 219 | pred_vectors = sbert_model.encode(predictions[:5], batch_size=5, show_progress_bar=False) |
| 220 | valid_action_vectors = sbert_model.encode(validActions, batch_size=min(len(validActions), 128), show_progress_bar=False) |
| 221 | |
| 222 | |
| 223 | # Calculate cosine similarity between each vector in pred_vectors and all vectors in valid_action_vectors |
| 224 | similarity_matrix = cosine_similarity(pred_vectors, valid_action_vectors) |
| 225 | |
| 226 | # Take the sum of cosine similarities for each vector in valid_action_vectors |
| 227 | sum_similarities = similarity_matrix.sum(axis=0) |
| 228 | |