Skip to content
Open
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
25 changes: 20 additions & 5 deletions examples/runVerySimpleGW.wppl
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,33 @@ if (false) {
console.log("KLdiv", KLdiv(mdp.startState, aleph0));
console.log("messPot", messingPotential(mdp.startState, aleph0));

var gd = agent.getData, agentData = gd();
var infered = Infer({ model() {
return simulate(mdp.startState, aleph0).trajectory;
}});

console.log("\nDATA FOR REGRESSION TESTS: \n");
console.log(
webpplAgents.debug.inspect(
webpplAgents.debug.trajectoriesDistribution.diagrams.forward(infered),
{ colors: true, depth: Number.MAX_SAFE_INTEGER }
)
)
console.log("END OF DATA FOR REGRESSION TESTS\n");

// estimate distribution of trajectories:
var trajDist = infered.getDist()

var trajDist = Infer({ model() {
return simulate(mdp.startState, aleph0).trajectory;
}}).getDist();
/*
console.log("\nDATA FOR REGRESSION TESTS: \ntrajDist");
var regressionTestData = webpplAgents.trajDist2simpleJSON(trajDist);
console.log(JSON.stringify(regressionTestData));
console.log("END OF DATA FOR REGRESSION TESTS\n");
var trajData = trajDist2TrajData(trajDist, agent);

console.log("\nDATA FOR REGRESSION TESTS: \ntrajDist");
var regressionTestData = webpplAgents.trajDist2simpleJSON(trajDist);
console.log(JSON.stringify(regressionTestData));
console.log("END OF DATA FOR REGRESSION TESTS\n");
*/

var trajData = trajDist2TrajData(trajDist, agent);

Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@mapbox/node-pre-gyp": "^1.0.11",
"canvas": "^2.11.2",
"jsdom": "^8.0.0",
"object-hash": "^3.0.0",
"paper": "^0.9.25",
"underscore": "^1.8.3",
"webppl-call-async": "github:null-a/webppl-call-async",
Expand Down
11 changes: 11 additions & 0 deletions src/dynamic-trees/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const structure = {
structure: {
PrefixTree: require('./structure/prefix-tree.js')
},
visitors: {
forward: require('./visitors/forward-diagram-visitor.js'),
backward: require('./visitors/backward-diagram-visitor.js')
}
}

module.exports = structure
29 changes: 29 additions & 0 deletions src/dynamic-trees/structure/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const util = require('util')

class Location {
#data
constructor(data) {
this.#data = data
}

equals(other) {
if (this.#data.length != other.#data.length) return false

for (const index in this.#data) {
if (other.#data[index] != this.#data[index]) return false
}

return true
}

get id() {
return `@(${this.#data.join(',')})`
}

[util.inspect.custom](depth, options, inspect) {
if (depth < 0) return options.stylize('[Location]', 'special')
return `Location (${this.id})`
}
}

module.exports = Location
174 changes: 174 additions & 0 deletions src/dynamic-trees/structure/prefix-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
const Location = require('./location.js')

class Vertex {
#vertices = null // of LocationVertex | StateVertex | ActionVertex
#associations = null
static #hash = require('object-hash')

visit(fun, ...args) { return fun.call(this, ...args) }

get vertices() {
if (!this.#vertices) this.#vertices = new Array()
return this.#vertices
}

get associations() {
if (!this.#associations) this.#associations = new Array()
return this.#associations
}

associate(data) {
this.associations.push(data)
}

get hash() {
return Vertex.#hash
}

[require('util').inspect.custom](depth, options, inspect) {
if (!this.#vertices && !this.#associations) {
return options.stylize(`${this.constructor.name}::Empty`, 'special');
}


if (depth < 0) {
return options.stylize(`[${this.constructor.name}]`, 'special')
}
else {
const newOptions = Object.assign({}, options, {
depth: options.depth === null ? null : options.depth - 1,
});

let projection = Object.create({})

if (!!this.#vertices) {
for (const [key, value] of Object.entries(this.#vertices)) {
projection = Object.assign(projection, { [value.id || key ]: value })
}
}

if (!!this.#associations) {
projection = Object.assign(projection, { ['?']: this.#associations })
}

return `${this.constructor.name} ${inspect(projection, newOptions)}`
}

}
}

class ActionVertex extends Vertex {
constructor(data) {
super(data)
this.action = data.action
this.associate({ aleph: data.aleph4action })
}

append(data) {
for (const index in this.vertices) {
const vertex = this.vertices[index]
if (vertex instanceof LocationVertex) {
if (vertex.matches(data)) {
return vertex.append(data)
}
}
else throw { context: this, append: { data, vertex } }
}

const locationVertex = new LocationVertex(data)
this.vertices.push(locationVertex)
return locationVertex.append(data)
}

get id() {
return `/${this.action}/`
}

matches (data) {
return this.action == data.action
}
}

class LocationVertex extends Vertex {
#location
constructor(data) {
super(data)
this.#location = new Location(data.state.loc)
}

append(data) {
for (const index in this.vertices) {
const vertex = this.vertices[index]
if (vertex instanceof StateVertex) {
if (vertex.matches(data)) {
return vertex.append(data)
}
}
else throw { context: this, append: { data, vertex } }
}
const stateVertex = new StateVertex(data)
this.vertices.push(stateVertex)
return stateVertex.append(data)
}

get id() {
return this.#location.id
}

matches(data) {
return this.#location.equals(new Location(data.state.loc))
}
}

class StateVertex extends Vertex {
#state
constructor(data) {
super(data)
this.#state = data.state
this.associate({ aleph: data.aleph4state })
}

append(data) {
for (const index in this.vertices) {
const vertex = this.vertices[index]
if (vertex instanceof ActionVertex) {
if (vertex.matches(data)) {
return vertex
}
}
else throw { context: this, append: { data, vertex } }
}
const actionVertex = new ActionVertex(data)
this.vertices.push(actionVertex)
return actionVertex
}

get id() {
return `#|${this.hash(this.#state, { encoding: 'base64' })}|`
}

matches(data) {
return this.hash(this.#state) === this.hash(data.state)
}
}


class PrefixTree extends Vertex {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up until here the code is mostly self-explanatory, but from here on it really needs inline comments to be understandable for me...

append(data) {
for (const index in this.vertices) {
const vertex = this.vertices[index]
if (vertex instanceof LocationVertex) {
if (vertex.matches(data)) {
return vertex.append(data)
}
}
else throw { context: this, append: { data, vertex } }
}

const locationVertex = new LocationVertex(data)
this.vertices.push(locationVertex)
return locationVertex.append(data)
}
}

module.exports = PrefixTree
16 changes: 16 additions & 0 deletions src/dynamic-trees/visitors/backward-diagram-visitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class BackwardDiagramVisitor {
static visit (root, trajectory, parameters) {
const reminder = [...trajectory]
const data = reminder.pop()
const vertex = root.append(data)
if (reminder.length > 0) {
vertex.visit(TrajectoriesDistributionBackwardDiagramVisitor.visit,
vertex, reminder, parameters
)
}

vertex.associate({ P: parameters.prob } )
}
}

module.exports = BackwardDiagramVisitor
14 changes: 14 additions & 0 deletions src/dynamic-trees/visitors/forward-diagram-visitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class TrajectoriesDistributionForwardDiagramVisitor {
static visit (root, trajectory, parameters) {
const [data, ...reminder] = trajectory
const vertex = root.append(data)
if (reminder.length > 0) {
vertex.visit(TrajectoriesDistributionForwardDiagramVisitor.visit,
vertex, reminder, parameters
)
}

vertex.associate({ P: parameters.prob } )
}
}
module.exports = TrajectoriesDistributionForwardDiagramVisitor
36 changes: 35 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//

const dynamicTrees = require('./dynamic-trees')
const PrefixTree = dynamicTrees.structure.PrefixTree
const TrajectoriesDistributionForwardDiagramVisitor = dynamicTrees.visitors.forward
const TrajectoriesDistributionBackwardDiagramVisitor = dynamicTrees.visitors.backward

// TODO: how to move the functions to other js files and still make them available in webppl code???

var locActionData2ASCIIdefaultFormat = function (x) {
Expand Down Expand Up @@ -184,6 +191,33 @@ module.exports = {
return result;
},

debug: {
inspect: function(...args) {
const util = require('util')
return util.inspect(...args)
},
trajectoriesDistribution: {
diagrams: {
forward: function (data) {
const distribution = data.getDist()
const result = new PrefixTree()
for(var key in distribution) {
TrajectoriesDistributionForwardDiagramVisitor.visit(result, JSON.parse(key), distribution[key])
}
return result
},
backward: function(data) {
const distribution = data.getDist()
const result = new PrefixTree()
for(var key in distribution) {
TrajectoriesDistributionBackwardDiagramVisitor.visit(result, JSON.parse(key), distribution[key])
}
return result
}
}
}
},

// TO BE MOVED TO src/utils/metalog.js:

/* TODO:
Expand Down Expand Up @@ -264,4 +298,4 @@ module.exports = {
// TO BE MOVED TO src/utils/ceres.js:

// TODO...
};
};