Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kore): ts step interpreter #195

Merged
merged 2 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions kaggle_environments/envs/kore_fleets/starter_bots/ts/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { Direction } from "./kore/Direction";
import { ShipyardAction } from "./kore/ShipyardAction";
import { KoreIO } from "./kore/KoreIO";

const io = new KoreIO();
// agent.run takes care of running your code per tick
io.run((board: Board): Board => {
export const tick = async (board: Board): Promise<Board> => {
const me = board.currentPlayer;
const turn = board.step;
const spawnCost = board.configuration.spawnCost;
Expand All @@ -29,6 +27,8 @@ io.run((board: Board): Board => {

// nextActions will be pulled off of your shipyards
return board;
});

}

// agent.run takes care of running your code per tick
const io = new KoreIO();
io.run(tick);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Board} from "./kore/Board";
import { Direction } from "./kore/Direction";
import { ShipyardAction } from "./kore/ShipyardAction";
import { KoreIO } from "./kore/KoreIO";


export const tick = async (board: Board): Promise<Board> => {
return board;
}

const io = new KoreIO();
io.run(tick);
62 changes: 62 additions & 0 deletions kaggle_environments/envs/kore_fleets/starter_bots/ts/MinerBot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {Board} from "./kore/Board";
import { Direction } from "./kore/Direction";
import { ShipyardAction } from "./kore/ShipyardAction";
import { KoreIO } from "./kore/KoreIO";

export const tick = async (board: Board): Promise<Board> => {
const me = board.currentPlayer;
const spawnCost = board.configuration.spawnCost;
const convertCost = board.configuration.convertCost;
let remainingKore = me.kore;

// the following code is mostly auto-generated using GitHub co-pilot
// using miner Python code and instruction "convert python into javascript" as comment prompts
for (let shipyard of me.shipyards) {
if(remainingKore > 1000 && shipyard.maxSpawn > 5) {
if(shipyard.shipCount >= convertCost + 10) {
const gap1 = getRandomInt(3, 9);
const gap2 = getRandomInt(3, 9);
const startDir = Math.floor(Math.random() * 4);
let flightPlan = Direction.listDirections()[startDir].toChar() + gap1;
const nextDir = (startDir + 1) % 4;
flightPlan += Direction.listDirections()[nextDir].toChar() + gap2;
const nextDir2 = (nextDir + 1) % 4;
flightPlan += Direction.listDirections()[nextDir2].toChar();
shipyard.setNextAction(ShipyardAction.launchFleetWithFlightPlan(Math.min(convertCost + 10, Math.floor(shipyard.shipCount / 2)), flightPlan));
} else if(remainingKore >= spawnCost) {
remainingKore -= spawnCost;
shipyard.setNextAction(ShipyardAction.spawnShips(Math.min(shipyard.maxSpawn, Math.floor(remainingKore / spawnCost))));
}
} else if(shipyard.shipCount >= 21) {
const gap1 = getRandomInt(3, 9);
const gap2 = getRandomInt(3, 9);
const startDir = Math.floor(Math.random() * 4);
let flightPlan = Direction.listDirections()[startDir].toChar() + gap1;
const nextDir = (startDir + 1) % 4;
flightPlan += Direction.listDirections()[nextDir].toChar() + gap2;
const nextDir2 = (nextDir + 1) % 4;
flightPlan += Direction.listDirections()[nextDir2].toChar() + gap1;
const nextDir3 = (nextDir2 + 1) % 4;
flightPlan += Direction.listDirections()[nextDir3].toChar();
shipyard.setNextAction(ShipyardAction.launchFleetWithFlightPlan(21, flightPlan));
} else if(remainingKore > board.configuration.spawnCost * shipyard.maxSpawn) {
remainingKore -= board.configuration.spawnCost;
if(remainingKore >= spawnCost) {
shipyard.setNextAction(ShipyardAction.spawnShips(Math.min(shipyard.maxSpawn, Math.floor(remainingKore / spawnCost))));
}
} else if(shipyard.shipCount >= 2) {
const dirStr = Direction.randomDirection().toChar();
shipyard.setNextAction(ShipyardAction.launchFleetWithFlightPlan(2, dirStr));
}
}

// nextActions will be pulled off of your shipyards
return board;
}

const io = new KoreIO();
io.run(tick);

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
21 changes: 19 additions & 2 deletions kaggle_environments/envs/kore_fleets/starter_bots/ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@

A basic TS interpreter has been created in `interpreter.ts`. You can use or modify this file to train machine learning models in JS/TS.

Currently it supports 2 agents and customizable number of episodes. After each episode, you can access the complete history of the game. For each turn, you can access the full observation (state) as a Board object, actions performed and the reward obtained after performing the action.
Currently it supports 2 agents and customizable number of episodes.

Sample command to run the interpreter can be found in npm scripts as `npm run interpreter`.
It has two modes: `run` and `step`.

`run` mode: After each episode, you can access the complete history of the game. For each turn, you can access the full observation (state) as a Board object, actions performed and the reward obtained after performing the action. This mode is useful for evaluating an agent.

`step` mode: The interpreter initializes new games and allows stepping through the game interactively. You have complete control over the board and the agent during each step. This mode is useful for training machine learning models.

Sample command to run the interpreter can be found in npm scripts as `npm run interpreter:run` and `npm run interpreter:step`.

## Miner bot and Do nothing bot

A sample miner bot `MinerBot.ts` is provided, with Python entrypoint as `miner.py`. It has the same logic as the Python `miner` bot in `kore_fleets.py`.

To run it aginst Python miner bot with TS interpreter for 20 episodes:

1. `npm run compile`
2. `node --require ts-node/register interpreter.ts 20 ./miner.py miner`

A sample do nothing bot `DoNothingBot.ts` is also provided.
Loading