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

Improve topological sorting #33

Merged
merged 1 commit into from
Nov 2, 2023
Merged
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
40 changes: 21 additions & 19 deletions src/AI-Algorithms-Graph/AIAstar.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,36 @@ A* is a graph traversal and path search algorithm, which is used in many fields
Pseudocode and implementation for the approach is taken from: https://github.com/tatut/aoc2021-smalltalk/blob/main/src/AoC2021/AStar.class.st
"
Class {
#name : #AIAstar,
#superclass : #AIGraphAlgorithm,
#name : 'AIAstar',
#superclass : 'AIGraphAlgorithm',
#instVars : [
'start',
'end'
],
#category : #'AI-Algorithms-Graph-Shortest path'
#category : 'AI-Algorithms-Graph-Shortest path',
#package : 'AI-Algorithms-Graph',
#tag : 'Shortest path'
}

{ #category : #configuration }
{ #category : 'configuration' }
AIAstar >> edgeClass [

^ AIWeightedEdge
]

{ #category : #accessing }
{ #category : 'accessing' }
AIAstar >> end [

^ end
]

{ #category : #accessing }
{ #category : 'accessing' }
AIAstar >> end: endModel [

end := self findNode: endModel
]

{ #category : #running }
{ #category : 'running' }
AIAstar >> heuristicFrom: startModel to: endModel [
"We are using a version of dijkstra algorithm here with all weights the for every node
(value 1).
Expand Down Expand Up @@ -78,27 +80,27 @@ AIAstar >> heuristicFrom: startModel to: endModel [
^ pathD
]

{ #category : #actions }
{ #category : 'actions' }
AIAstar >> newPriorityQueue [
"We use the Heap object defined in the SequenceableCollections package."

^ Heap new
]

{ #category : #configuration }
{ #category : 'configuration' }
AIAstar >> nodeClass [

^ AIPathDistanceNode
]

{ #category : #running }
{ #category : 'running' }
AIAstar >> pathDistance [
"Needs to be editted"

^ self end pathDistance
]

{ #category : #backtracking }
{ #category : 'backtracking' }
AIAstar >> reconstructPath [

| path previous |
Expand All @@ -113,13 +115,13 @@ AIAstar >> reconstructPath [
^ path
]

{ #category : #actions }
{ #category : 'actions' }
AIAstar >> removeMostPromisingPair: aPriorityQueue [

^ aPriorityQueue removeFirst
]

{ #category : #initialization }
{ #category : 'initialization' }
AIAstar >> reset [

self nodes do: [ :node |
Expand All @@ -129,7 +131,7 @@ AIAstar >> reset [
previousNode: nil ]
]

{ #category : #running }
{ #category : 'running' }
AIAstar >> run [

| pq cameFrom gScore fScore gs fs |
Expand Down Expand Up @@ -179,35 +181,35 @@ AIAstar >> run [
]
]

{ #category : #running }
{ #category : 'running' }
AIAstar >> runFrom: startModel [

self start: startModel.
self run
]

{ #category : #running }
{ #category : 'running' }
AIAstar >> runFrom: startModel to: endModel [

self start: startModel.
self end: endModel.
^ self run
]

{ #category : #accessing }
{ #category : 'accessing' }
AIAstar >> start [

^ start
]

{ #category : #accessing }
{ #category : 'accessing' }
AIAstar >> start: startModel [

start := self findNode: startModel.
start pathDistance: 0
]

{ #category : #private }
{ #category : 'private' }
AIAstar >> updateDistance: newDistance of: aNode previousNode: previousNode [

aNode previousNode: previousNode.
Expand Down
28 changes: 15 additions & 13 deletions src/AI-Algorithms-Graph/AIBFS.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,37 @@ shortestPath := bfs runFrom: 1 to: 4
This will return `#( 1 4 )`.
"
Class {
#name : #AIBFS,
#superclass : #AIGraphAlgorithm,
#name : 'AIBFS',
#superclass : 'AIGraphAlgorithm',
#instVars : [
'start',
'end',
'queue'
],
#category : #'AI-Algorithms-Graph-Shortest path'
#category : 'AI-Algorithms-Graph-Shortest path',
#package : 'AI-Algorithms-Graph',
#tag : 'Shortest path'
}

{ #category : #accessing }
{ #category : 'accessing' }
AIBFS >> end [

^ end
]

{ #category : #accessing }
{ #category : 'accessing' }
AIBFS >> end: endModel [

end := self findNode: endModel
]

{ #category : #configuration }
{ #category : 'configuration' }
AIBFS >> nodeClass [

^ AIBFSNode
]

{ #category : #actions }
{ #category : 'actions' }
AIBFS >> reconstructPath [

| path previous |
Expand All @@ -57,7 +59,7 @@ AIBFS >> reconstructPath [
^ path
]

{ #category : #actions }
{ #category : 'actions' }
AIBFS >> resetValues [

nodes do: [ :aNode |
Expand All @@ -67,7 +69,7 @@ AIBFS >> resetValues [
distance: nil ]
]

{ #category : #running }
{ #category : 'running' }
AIBFS >> run [

| node neighbours |
Expand All @@ -89,28 +91,28 @@ AIBFS >> run [
next previousNode: node ] ] ]
]

{ #category : #running }
{ #category : 'running' }
AIBFS >> runFrom: startModel [

self start: startModel.
self run
]

{ #category : #running }
{ #category : 'running' }
AIBFS >> runFrom: startModel to: endModel [

self runFrom: startModel.
self end: endModel.
^ self reconstructPath
]

{ #category : #accessing }
{ #category : 'accessing' }
AIBFS >> start [

^ start
]

{ #category : #accessing }
{ #category : 'accessing' }
AIBFS >> start: startModel [

start := self findNode: startModel
Expand Down
30 changes: 16 additions & 14 deletions src/AI-Algorithms-Graph/AIBellmanFord.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@
The Bellman Ford algorithm calculates the shortest path in any kind of graph. The graph edges can have negative weights and this algo hanldes negative cycles. If a negative cycle is detected, the path distance of that node is set to negative infinity
"
Class {
#name : #AIBellmanFord,
#superclass : #AIGraphAlgorithm,
#name : 'AIBellmanFord',
#superclass : 'AIGraphAlgorithm',
#instVars : [
'start',
'end'
],
#category : #'AI-Algorithms-Graph-Shortest path'
#category : 'AI-Algorithms-Graph-Shortest path',
#package : 'AI-Algorithms-Graph',
#tag : 'Shortest path'
}

{ #category : #configuration }
{ #category : 'configuration' }
AIBellmanFord >> edgeClass [

^ AIWeightedEdge
]

{ #category : #accessing }
{ #category : 'accessing' }
AIBellmanFord >> end: aModel [

end := (self findNode: aModel)
]

{ #category : #configuration }
{ #category : 'configuration' }
AIBellmanFord >> nodeClass [

^ AIPathDistanceNode
]

{ #category : #actions }
{ #category : 'actions' }
AIBellmanFord >> reconstructPath [

| path previous |
Expand All @@ -47,7 +49,7 @@ AIBellmanFord >> reconstructPath [
^ path
]

{ #category : #running }
{ #category : 'running' }
AIBellmanFord >> relaxEdges [

| anEdgeHasBeenRelaxed |
Expand All @@ -65,7 +67,7 @@ AIBellmanFord >> relaxEdges [
anEdgeHasBeenRelaxed ifFalse: [ ^ self ] ]
]

{ #category : #running }
{ #category : 'running' }
AIBellmanFord >> relaxEdgesToNegativeInfinity [

"This method is called after a first relaxation has ocurred already. The algorithm is the same as the previous one but with the only difference that now if an edge can be relaxed we set the path distance as negative infinity because means that the edge is part of a negative cycle."
Expand All @@ -84,7 +86,7 @@ AIBellmanFord >> relaxEdgesToNegativeInfinity [
anEdgeHasBeenRelaxed ifFalse: [ ^ self ] ]
]

{ #category : #actions }
{ #category : 'actions' }
AIBellmanFord >> reset [

self nodes do: [ :node |
Expand All @@ -93,7 +95,7 @@ AIBellmanFord >> reset [
previousNode: nil ]
]

{ #category : #running }
{ #category : 'running' }
AIBellmanFord >> run [

start pathDistance: 0.
Expand All @@ -105,22 +107,22 @@ AIBellmanFord >> run [
self relaxEdgesToNegativeInfinity
]

{ #category : #running }
{ #category : 'running' }
AIBellmanFord >> runFrom: startModel [

self start: startModel.
self run
]

{ #category : #running }
{ #category : 'running' }
AIBellmanFord >> runFrom: startModel to: endModel [

self runFrom: startModel.
self end: endModel.
^ self reconstructPath
]

{ #category : #accessing }
{ #category : 'accessing' }
AIBellmanFord >> start: aModel [

start := (self findNode: aModel).
Expand Down
Loading