-
Notifications
You must be signed in to change notification settings - Fork 37
feat: enhance pathfinding options with maxCost, maxNodes, and allowedCallback #47
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
123FLO321
wants to merge
9
commits into
albertorestifo:master
Choose a base branch
from
123FLO321:br_46
base: master
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8375321
feat: add native types
123FLO321 a9014f5
feat: enhance pathfinding options with maxCost, maxNodes, and allowed…
123FLO321 6ed472e
refactor: fix lint errors
123FLO321 a3dbc16
refactor: rename allowed callback to can visit
123FLO321 308a669
build: separate test lint from test
123FLO321 1690ef8
refactor: fix lint errors
123FLO321 94447b3
test: add tests for expected depths
123FLO321 548f56c
test: add tests for expected weights
123FLO321 a1791bd
refactor: fix types for can visit
123FLO321 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
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| node_modules | ||
| *.log | ||
| coverage | ||
| .nyc_output | ||
| .nyc_output | ||
| .idea |
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,160 @@ | ||
| declare class Graph { | ||
| /** | ||
| * Creates a new Graph, optionally initializing it a nodes graph representation. | ||
| * | ||
| * A graph representation is an object that has as keys the name of the point and as values | ||
| * the points reachable from that node, with the cost to get there: | ||
| * | ||
| * { | ||
| * node (Number|String): { | ||
| * neighbor (Number|String): cost (Number), | ||
| * ..., | ||
| * }, | ||
| * } | ||
| * | ||
| * In alternative to an object, you can pass a `Map` of `Map`. This will | ||
| * allow you to specify numbers as keys. | ||
| * | ||
| * @param [graph] - Initial graph definition | ||
| * @example | ||
| * | ||
| * const route = new Graph(); | ||
| * | ||
| * // Pre-populated graph | ||
| * const route = new Graph({ | ||
| * A: { B: 1 }, | ||
| * B: { A: 1, C: 2, D: 4 }, | ||
| * }); | ||
| * | ||
| * // Passing a Map | ||
| * const g = new Map() | ||
| * | ||
| * const a = new Map() | ||
| * a.set('B', 1) | ||
| * | ||
| * const b = new Map() | ||
| * b.set('A', 1) | ||
| * b.set('C', 2) | ||
| * b.set('D', 4) | ||
| * | ||
| * g.set('A', a) | ||
| * g.set('B', b) | ||
| * | ||
| * const route = new Graph(g) | ||
| */ | ||
| constructor(nodes?: { [key: string]: { [key: string]: number } } | Map<string, Map<string, number>>); | ||
|
|
||
| /** | ||
| * Adds a node to the graph | ||
| * | ||
| * @param name - Name of the node | ||
| * @param neighbors - Neighboring nodes and cost to reach them | ||
| * @example | ||
| * | ||
| * const route = new Graph(); | ||
| * | ||
| * route.addNode('A', { B: 1 }); | ||
| * | ||
| * // It's possible to chain the calls | ||
| * route | ||
| * .addNode('B', { A: 1 }) | ||
| * .addNode('C', { A: 3 }); | ||
| * | ||
| * // The neighbors can be expressed in a Map | ||
| * const d = new Map() | ||
| * d.set('A', 2) | ||
| * d.set('B', 8) | ||
| * | ||
| * route.addNode('D', d) | ||
| */ | ||
| addNode(name: string, neighbors: any): Graph; | ||
|
|
||
| /** | ||
| * Removes a node and all of its references from the graph | ||
| * | ||
| * @param key - Key of the node to remove from the graph | ||
| * @example | ||
| * | ||
| * const route = new Graph({ | ||
| * A: { B: 1, C: 5 }, | ||
| * B: { A: 3 }, | ||
| * C: { B: 2, A: 2 }, | ||
| * }); | ||
| * | ||
| * route.removeNode('C'); | ||
| * // The graph now is: | ||
| * // { A: { B: 1 }, B: { A: 3 } } | ||
| */ | ||
| removeNode(name: string): Graph; | ||
|
|
||
| /** | ||
| * Compute the shortest path between the specified nodes | ||
| * | ||
| * @param start - Starting node | ||
| * @param goal - Node we want to reach | ||
| * @param [options] - Options | ||
| * | ||
| * @param [options.trim] - Exclude the origin and destination nodes from the result | ||
| * @param [options.reverse] - Return the path in reversed order | ||
| * @param [options.cost] - Also return the cost of the path when set to true | ||
| * @param [options.maxCost] - Only consider paths with total cost ≤ this value | ||
| * @param [options.maxNodes] - Maximum number of nodes allowed in the resulting path (including start and goal) | ||
| * @param [options.canVisit] - Predicate called for each potential expansion; returns true to allow the move | ||
| * | ||
| * @return Computed path between the nodes. | ||
| * | ||
| * When `option.cost` is set to true, the returned value will be an object with shape: | ||
| * - `path` *(Array)*: Computed path between the nodes | ||
| * - `cost` *(Number)*: Cost of the path | ||
| * | ||
| * @example | ||
| * | ||
| * const route = new Graph() | ||
| * | ||
| * route.addNode('A', { B: 1 }) | ||
| * route.addNode('B', { A: 1, C: 2, D: 4 }) | ||
| * route.addNode('C', { B: 2, D: 1 }) | ||
| * route.addNode('D', { C: 1, B: 4 }) | ||
| * | ||
| * route.path('A', 'D') // => ['A', 'B', 'C', 'D'] | ||
| * | ||
| * // trimmed | ||
| * route.path('A', 'D', { trim: true }) // => [B', 'C'] | ||
| * | ||
| * // reversed | ||
| * route.path('A', 'D', { reverse: true }) // => ['D', 'C', 'B', 'A'] | ||
| * | ||
| * // include the cost | ||
| * route.path('A', 'D', { cost: true }) | ||
| * // => { | ||
| * // path: [ 'A', 'B', 'C', 'D' ], | ||
| * // cost: 4 | ||
| * // } | ||
| */ | ||
| path(start: any, goal: any, options?: PathOption): string[] | PathResult; | ||
| } | ||
|
|
||
| interface PathOption { | ||
| trim?: boolean | undefined; | ||
| reverse?: boolean | undefined; | ||
| cost?: boolean | undefined; | ||
| avoid?: any[] | undefined; | ||
| maxCost?: number | undefined; | ||
| maxNodes?: number | undefined; | ||
| canVisit?: ((arg: CanVisitArg) => boolean) | undefined; | ||
| } | ||
|
|
||
| interface CanVisitArg { | ||
| from: string; | ||
| to: string; | ||
| cost: number; | ||
| accumulatedCost: number; | ||
| depth: number; | ||
| } | ||
|
|
||
| interface PathResult { | ||
| path: string[] | null; | ||
| cost: number; | ||
| } | ||
|
|
||
| export = Graph; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,13 @@ | |
| "description": "A NodeJS implementation of Dijkstra's algorithm", | ||
| "author": "Alberto Restifo <[email protected]>", | ||
| "main": "libs/Graph.js", | ||
| "types": "libs/index.d.ts", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/albertorestifo/node-dijkstra" | ||
| }, | ||
| "scripts": { | ||
| "test": "eslint . && nyc --reporter=html mocha -t 5000", | ||
| "test": "nyc --reporter=html mocha -t 5000", | ||
| "lint": "eslint .", | ||
| "lint:fix": "eslint . --fix", | ||
| "compile": "gulp build" | ||
|
|
||
Oops, something went wrong.
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.
Should depth start at 0 or 1 for the root node?