| 1 | """A seat of a kaggle_environments game for a bot in its own process: env.run gets a function that passes each |
| 2 | observation to the bot's process and its answer back. A late or broken answer is the game's no-op.""" |
| 3 | import json, os, sys |
| 4 | |
| 5 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 6 | sys.path.insert(0, os.path.join(os.path.dirname(HERE), 'kit')) |
| 7 | from host import Child |
| 8 | |
| 9 | |
| 10 | def seat(folder, limit=1.0, idle=None): |
| 11 | main = os.path.join(folder, 'main.py') |
| 12 | if not os.path.isfile(main): |
| 13 | sys.exit(f'No bot in {folder}: add main.py with agent(obs, config), as on Kaggle') |
| 14 | child = Child([sys.executable, '-s', os.path.join(HERE, 'kaggle_child.py'), main], cwd=folder, limit=limit, start=30) |
| 15 | |
| 16 | def agent(obs, config): |
| 17 | answer = child.ask({'obs': json.loads(json.dumps(obs)), 'config': json.loads(json.dumps(config))}) |
| 18 | return idle if answer is None else answer |
| 19 | |
| 20 | agent.child = child |
| 21 | return agent |