File size: 8,352 Bytes
784a7e2 |
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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"with open(\"raw/ltl.txt\", \"r\") as f:\n",
" raw_ltls = f.read().splitlines()\n",
" unique_ltls = set(raw_ltls)\n",
"with open(\"raw/eng.txt\", 'r') as f:\n",
" raw_engs = f.read().splitlines()\n",
"\n",
"DPs = []\n",
"for ltl, eng in zip(raw_ltls, raw_engs):\n",
" DPs.append({'ltl': ltl, 'eng': eng})"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"G & U S ! C F C\n",
"G & U S ! A F A\n",
"G & U S ! Y F Y\n",
"G & U S ! R F R\n",
"G & U S ! B F B\n"
]
}
],
"source": [
"for ltl in sorted(unique_ltls, key=lambda x: len(x)):\n",
" print(ltl)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Clean-up Domain, with augmentation\n",
"Number of Data Points 744\n",
"Number of unique LTL expressions: 5\n",
"Number of unique LTL structures: 1\n"
]
}
],
"source": [
"print(\"Clean-up Domain, with augmentation\")\n",
"print(\"Number of Data Points\", len(DPs))\n",
"print(\"Number of unique LTL expressions:\", len(unique_ltls))\n",
"print(\"Number of unique LTL structures:\", 1)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"G & U S ! A F A\n",
"move the arm to the block and pick it up next placing it in the bin and coming back to pick up the next block\n",
"\n",
"G & U S ! R F R\n",
"watch for blocks to be set down and then move them into the basket but not any orange ones\n",
"\n",
"G & U S ! B F B\n",
"watch for blocks to be set down and then move them into the basket but not any blue ones\n",
"\n",
"G & U S ! C F C\n",
"watch for blocks to be set down and then move them into the basket but not any green ones\n",
"\n",
"G & U S ! Y F Y\n",
"watch for blocks to be set down and then move them into the basket but not any yellow ones\n",
"\n"
]
}
],
"source": [
"seen = set()\n",
"from random import shuffle, seed\n",
"seed(0)\n",
"# shuffle(DPs)\n",
"for dp in DPs:\n",
" if dp['ltl'] not in seen:\n",
" seen.add(dp['ltl'])\n",
" print(dp['ltl'])\n",
" print(dp['eng'])\n",
" print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clean up LTL expressions "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"APs = [\n",
" {'ap': \"A\", 'eng': \"any cubes\"},\n",
" {'ap': \"R\", 'eng': \"any non red cubes\"},\n",
" {'ap': \"B\", 'eng': \"any non blue cubes\"},\n",
" {'ap': \"Y\", 'eng': \"any non yellow cubes\"},\n",
" {'ap': \"C\", 'eng': \"any non green cubes\"},\n",
"]\n",
"APs = [\n",
" {'ap': \"P01\", 'eng': \"any cubes\"},\n",
" {'ap': \"P02\", 'eng': \"any non red cubes\"},\n",
" {'ap': \"P03\", 'eng': \"any non blue cubes\"},\n",
" {'ap': \"P04\", 'eng': \"any non yellow cubes\"},\n",
" {'ap': \"P05\", 'eng': \"any non green cubes\"},\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def build_type_A(ap1):\n",
" return {\n",
" 'raw_ltl': f\"G & U S ! {ap1['ap']} F {ap1['ap']}\",\n",
" 'canonical_ltl': f\"globally ( and ( until ( scan , not ( {ap1['eng']} ) ) , finally ( {ap1['eng']} ) ) )\",\n",
" 'eng': f\"Look for and pick up {ap1['eng']} and put them in crate.\"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"translation_seeds = []\n",
"for r1 in APs:\n",
" translation_seeds.append(build_type_A(r1))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(len(translation_seeds))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set()\n"
]
}
],
"source": [
"seed_ltls = set([t['raw_ltl'] for t in translation_seeds])\n",
"print(unique_ltls - seed_ltls)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save the canonical decoding list"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"possible_decodings = {}\n",
"for seed in translation_seeds:\n",
" canonical = seed['canonical_ltl']\n",
" possible_decodings[canonical] = {\n",
" 'formula': canonical,\n",
" 'raw': seed['raw_ltl'],\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"with open(\"canonical.json\", 'w') as f:\n",
" f.write(json.dumps(possible_decodings, indent=2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save translation seed for zero shot"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"with open(\"train_seed.jsonl\", \"w\") as f:\n",
" for dp in translation_seeds:\n",
" better_ltl = dp['canonical_ltl']\n",
" entry = {'canonical': better_ltl, 'formula': better_ltl, 'natural': dp['eng']}\n",
" json.dump(entry, f)\n",
" f.write('\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save golden data for evaluation "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"raw_canonical_mapping = {\n",
" seed['raw_ltl']: seed['canonical_ltl'] for seed in translation_seeds\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"744"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(DPs)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"with open(\"golden.jsonl\", \"w\") as f:\n",
" for dp in DPs:\n",
" entry = {\n",
" 'canonical': raw_canonical_mapping[dp['ltl']],\n",
" 'formula': raw_canonical_mapping[dp['ltl']],\n",
" 'natural': dp['eng'],\n",
" 'raw_ltl': dp['ltl'],\n",
" }\n",
" json.dump(entry, f)\n",
" f.write('\\n')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.12 ('GPML')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "92a84f77637d1d47b588cbbaac9b07f8c628b67f58e672e955ed4902878afbbe"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|