forked from agentmodels/webppl-agents
-
Notifications
You must be signed in to change notification settings - Fork 1
Structures for dyn Trees output #3
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
Open
v1ta111
wants to merge
3
commits into
pik-gane:getDynTree
Choose a base branch
from
v1ta111:testing/getDynTree/tree-structures
base: getDynTree
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...