|
|
import json |
|
|
import numpy as np |
|
|
from tqdm import tqdm |
|
|
from pymilvus import connections, Collection |
|
|
|
|
|
connections.connect("default", host="127.0.0.1", port="19530") |
|
|
col = Collection("sumobot_states") |
|
|
|
|
|
def encode_state(state_str): |
|
|
|
|
|
parts = dict(item.split('=') for item in state_str.strip('.').split(', ')) |
|
|
return np.array([ |
|
|
float(parts["AngleToEnemy"]) / 180.0, |
|
|
float(parts["AngleToEnemyScore"]), |
|
|
float(parts["DistanceToEnemyScore"]), |
|
|
float(parts["NearBorderArenaScore"]), |
|
|
float(parts["FacingToArena"]), |
|
|
], dtype=np.float32) |
|
|
|
|
|
BATCH_SIZE = 5000 |
|
|
DATA_PATH = "cleaned_dataset.jsonl" |
|
|
|
|
|
batch_vecs, batch_actions = [], [] |
|
|
with open(DATA_PATH, "r") as f: |
|
|
for line in tqdm(f, desc="Reading dataset"): |
|
|
item = json.loads(line) |
|
|
vec = encode_state(item["state"]) |
|
|
batch_vecs.append(vec.tolist()) |
|
|
batch_actions.append(item["action"]) |
|
|
|
|
|
if len(batch_vecs) >= BATCH_SIZE: |
|
|
col.insert([batch_vecs, batch_actions]) |
|
|
batch_vecs, batch_actions = [], [] |
|
|
|
|
|
|
|
|
if batch_vecs: |
|
|
col.insert([batch_vecs, batch_actions]) |
|
|
|
|
|
col.flush() |
|
|
print("✅ All data inserted & flushed to Milvus.") |
|
|
|