class Gridworld {
constructor(size = parameters.worldSize) {
this.size = size;
this.wallFunc = parameters.wallFunc;
this.rewardFunc = parameters.rewardFunc;
this.listOfActions = parameters.listOfActions;
this.movingProbabilities = parameters.movingProbabilities;
this.agent = {};
this.agent.pos = parameters.agent.startPosition.copy();
this.agent.Q = new Array(this.size.y).fill()
.map(_ => new Array(this.size.x).fill()
.map(_ => new Array(this.listOfActions.length).fill(0)));
}
isWall(pos) {
return !pos.inBounds(Vector.ZERO, this.size) || this.wallFunc(pos.x, pos.y);
}
getReward(pos) {
return this.rewardFunc(pos.x, pos.y);
}
modifyDirection(dir) {
const r = Math.random();
const relativeDirNames = ["front","right","back","left"];
const probs = relativeDirNames.map(dirName => this.movingProbabilities[dirName]);
const i = choice(probs, r);
return rotateBy90(i, dir);
}
makeAction(action) {
const lastAgentPosition = this.agent.pos;
const modifiedAction = this.modifyDirection(action);
const nextCell = this.agent.pos.add(modifiedAction);
if (!this.isWall(nextCell)) {
this.agent.pos = nextCell;
}
const reward = this.getReward(lastAgentPosition);
return {state: this.agent.pos, reward};
}
}