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

Testing:tree structures #1

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 17 additions & 6 deletions examples/runVerySimpleGW.wppl
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,26 @@ var gd = agent.getData, agentData = gd();

// estimate distribution of trajectories:

var trajDist = Infer({ model() {
var infered = 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("\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(
webpplAgents.debug.inspect(
webpplAgents.debug.trajectoriesDistribution.diagrams.backward(infered),
{ colors: true, depth: Number.MAX_SAFE_INTEGER }
)
)
console.log("END OF DATA FOR REGRESSION TESTS\n");

var trajDist = infered.getDist()
var trajData = trajDist2TrajData(trajDist, agent);

//console.log("trajData", trajData);
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
255 changes: 254 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,229 @@
//

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(',')})`
}

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

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 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 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 PrefixTree extends Vertex {
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)
}
}

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 } )
}
}

class TrajectoriesDistributionBackwardDiagramVisitor {
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 } )
}
}
// 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 @@ -175,6 +401,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 @@ -255,4 +508,4 @@ module.exports = {
// TO BE MOVED TO src/utils/ceres.js:

// TODO...
};
};