Public
Edited
Dec 25, 2023
Insert cell
Insert cell
Insert cell
parameters = ({
gamma: .9,
movingProbabilities: {front: 0.8, left: 0.1, right: 0.1, back: 0.0},
worldSize: new Vector(4, 3),
wallFunc: (x, y) => (x === 1 && y === 1),
rewardFunc: (x, y) => {
if (x === 3 && y === 1) return +1;
if (x === 3 && y === 2) return -1;
return 0;
},
listOfActions: [A.left, A.up, A.right, A.down],
agent: {
startPosition: Vector.ZERO,
}
})
Insert cell
Insert cell
graphics = ({
color: themeOptions.themeColors[themeOptions.currentTheme],
prop: {
rectSize: 25,
canvasSize: new Vector(500, 300),
}
})
Insert cell
THEMES = ({ light: "lightTheme", dark: "darkTheme" })
Insert cell
canvas = {
const canvas = document.createElement('canvas');
const size = graphics.prop.canvasSize;
canvas.width = size.x;
canvas.height = size.y;
const c = canvas.getContext('2d');
const worldSize = new Vector(4, 3);
const rectSize = Math.min(size.x / worldSize.x, size.y / worldSize.y);
c.clearRect(0, 0, size.x, size.y);
c.fillStyle = graphics.color.background;
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 3; j++) {
c.fillRect(rectSize * i, rectSize * j, rectSize - 1, rectSize - 1);
}
}
return canvas;
}
Insert cell
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;
// size.y = number of rows, size.x = number of columns
this.agent = {};
this.agent.pos = parameters.agent.startPosition.copy();
// Q matrix is (set of states) * (set of actions), |Q| = |states| * |actions| = (width * height) * 4
this.agent.Q = new Array(this.size.y).fill() // create rows
.map(_ => new Array(this.size.x).fill() // create colums
.map(_ => new Array(this.listOfActions.length).fill(0))); // create actions
}
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) { // in case of gridworld: action ~ direction vector
const lastAgentPosition = this.agent.pos;
// modify action using moving probabilities
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};
}
}
Insert cell
Insert cell
Insert cell
Insert cell
{
let world = new Gridworld({x: 4, y: 3});
world.agent.pos = new Vector(3, 2);
return world.agent.Q;
}
Insert cell
Insert cell
Insert cell
Insert cell
test.all()
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more