-
Notifications
You must be signed in to change notification settings - Fork 51
weightFunction to calculate weight of path ... #107
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
Conversation
Created weightfunction to calculate weights of the path instead of hardcoded value ...
@@ -12,22 +19,25 @@ export function getPath<Node, LinkProps>( | |||
tracks: TraversingTracks<NoInfer<Node>>, | |||
source: NoInfer<Node>, | |||
destination: NoInfer<Node>, | |||
weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction |
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.
I would call it "nextWeightFn", as "weightFunction" is more ambiguous
): { | ||
nodes: [Node, Node, ...Node[]]; | ||
weight: number; | ||
} { | ||
const { p } = tracks; | ||
const nodeList: Node[] & { weight?: EdgeWeight } = []; | ||
|
||
let totalWeight = 0; | ||
let totalWeight = undefined as unknown as EdgeWeight; |
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.
Why cast the type to an invalid one instead of setting the correct type ?
@@ -12,22 +19,25 @@ export function getPath<Node, LinkProps>( | |||
tracks: TraversingTracks<NoInfer<Node>>, | |||
source: NoInfer<Node>, | |||
destination: NoInfer<Node>, | |||
weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction |
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.
Prefer an object as a single parameter instead of multi parameters. It makes evolutions easier.
Also, make the properties more explicit + add jsdoc
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.
Great start! Happy to merge when:
- New test cases are added that test the new behavior
- Documentation is added to the README that explains the new option
* Added custom function document for shortestPath ... * Exporting WeightParams ... * Added WeightParams type ... * Changed weightFunction to use WeightParams ... * Changed the name of parameter ... * Added condition to check if the pathWeight is undefined ...
@JesusTheHun updated based on your comments. Please review. @curran I updated the Readme.md. I could add tests on addWeightFunction, but its a simple function. Any guidance is welcome. |
Everything starts that way ^^
|
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.
Your work is appreciated !
@@ -12,22 +19,25 @@ export function getPath<Node, LinkProps>( | |||
tracks: TraversingTracks<NoInfer<Node>>, | |||
source: NoInfer<Node>, | |||
destination: NoInfer<Node>, | |||
weightFunction: (edgeWeight: number, currentPathWeight: number, hop: number) => number = addWeightFunction |
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.
It's a good practice to make a reference to the graph accessible to the function
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.
Done
src/types.ts
Outdated
@@ -18,3 +18,9 @@ export type SerializedInput<Node = unknown, LinkProps = unknown> = { | |||
}; | |||
|
|||
export type NoInfer<T> = [T][T extends any ? 0 : never]; | |||
|
|||
export type WeightParams = { |
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.
Maybe be more specific with NextWeightFnParams ?
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.
This is done
Updated weight function name to NextWeightFnParams ... Added new unit tests to test the weight function related functionality
@JesusTheHun, @curran updated the code and Readme based on the changes suggested. Btw, I realized getPath function traverses the graph in reverse order. It doesnt effect the default weight function OR my usecase. However, if there is a weight function that depends on how the graph is traversed, this can have an impact (e.g. newWeight = currentPathWeight + edgeWeight ** hop ) I don't have capacity to look into this right away, but can tackle this in a few weeks. |
@ravann how do tests feel like now ? 😄 |
@curran is it ok to merge the PR? |
src/types.ts
Outdated
edgeWeight: EdgeWeight; | ||
currentPathWeight: EdgeWeight | undefined; | ||
hop: number; | ||
sourceGraph: Graph<Node, LinkProps>; |
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.
This should be called "graph" to be consistent with other functions
@@ -18,3 +20,10 @@ export type SerializedInput<Node = unknown, LinkProps = unknown> = { | |||
}; | |||
|
|||
export type NoInfer<T> = [T][T extends any ? 0 : never]; | |||
|
|||
export type NextWeightFnParams<Node = unknown, LinkProps = unknown> = { |
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.
I think we should also add the "path" as parameter, that would allow to get the total number of hops for the cases when the calculation is based on the distance from the source.
Edit: actually, I think we should also add the previous node and the current node, so the properties of the link can be retrieved from the graph.
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.
This is done.
@JesusTheHun do you think the change can be merged now? |
currentPathWeight: EdgeWeight | undefined; | ||
hop: number; | ||
graph: Graph<Node, LinkProps>; | ||
path: TraversingTracks<NoInfer<Node>>; |
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.
"NoInfer" only matters in a function signature.
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.
This is fantastic! I really appreciate the work on this @ravann and thank you so much @JesusTheHun for reviewing.
Created weightfunction to calculate weights of the path instead of hardcoded value ...