Skip to content

Commit 969b1af

Browse files
authored
Merge pull request #64 from Driolar/master
AIStar's heuristic corrected and few cosmetic improved.
2 parents cb5ce53 + 66e77c0 commit 969b1af

File tree

5 files changed

+29
-65
lines changed

5 files changed

+29
-65
lines changed

src/AI-Algorithms-Graph-Tests/AIFloydWarshallTest.class.st

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ AIFloydWarshallTest >> testComplexWeightedGraph2 [
8888
self assert: (floydWarshall pathFrom: 1 to: 5) equals: #( 1 3 4 5 ).
8989
self assert: (floydWarshall pathFrom: 3 to: 5) equals: #( 3 4 5 ).
9090

91-
self assert: floydWarshall hasNegativeCycle equals: false
91+
self deny: floydWarshall hasNegativeCycle
9292
]
9393

9494
{ #category : 'tests' }
@@ -141,7 +141,7 @@ AIFloydWarshallTest >> testNegativeUnconnectedWeightedGraph [
141141
self assert: (floydWarshall pathFrom: 0 to: 7) equals: #( 0 1 5 6 7 ).
142142
self assert: (floydWarshall pathFrom: 0 to: 8) equals: #( ).
143143

144-
self assert: floydWarshall hasNegativeCycle equals: true.
144+
self assert: floydWarshall hasNegativeCycle.
145145

146146
self
147147
assert: (floydWarshall distanceFrom: 0 to: 9)
@@ -175,18 +175,12 @@ AIFloydWarshallTest >> testNegativeWeightedGraph [
175175
floydWarshall run.
176176

177177
self assert: (floydWarshall distanceFrom: 0 to: 8) equals: -20.
178-
self
179-
assert: (floydWarshall distanceFrom: 0 to: 9)
180-
equals: Float negativeInfinity.
178+
self assert: (floydWarshall distanceFrom: 0 to: 9) equals: Float negativeInfinity.
181179

182-
self
183-
assert: (floydWarshall pathFrom: 0 to: 8)
184-
equals: #( 0 1 5 6 7 8 ).
185-
self
186-
assert: (floydWarshall pathFrom: 0 to: 9)
187-
equals: #( 'Path affected by negative cycle' ).
180+
self assert: (floydWarshall pathFrom: 0 to: 8) equals: #( 0 1 5 6 7 8 ).
181+
self assert: (floydWarshall pathFrom: 0 to: 9) equals: #( 'Path affected by negative cycle' ).
188182

189-
self assert: floydWarshall hasNegativeCycle equals: true
183+
self assert: floydWarshall hasNegativeCycle
190184
]
191185

192186
{ #category : 'tests' }
@@ -213,7 +207,7 @@ AIFloydWarshallTest >> testSimpleWeightedGraph [
213207
self assert: (floydWarshall pathFrom: 2 to: 5) equals: #( 2 4 5 ).
214208
self assert: (floydWarshall pathFrom: 3 to: 5) equals: #( 3 4 5 ).
215209

216-
self assert: floydWarshall hasNegativeCycle equals: false
210+
self deny: floydWarshall hasNegativeCycle
217211
]
218212

219213
{ #category : 'tests' }
@@ -236,12 +230,8 @@ AIFloydWarshallTest >> testWeightedDAG [
236230
self assert: (floydWarshall distanceFrom: $B to: $F) equals: 18.
237231
self assert: (floydWarshall distanceFrom: $G to: $F) equals: 17.
238232

239-
self
240-
assert: (floydWarshall pathFrom: $A to: $F)
241-
equals: #( $A $B $E $F ).
242-
self
243-
assert: (floydWarshall pathFrom: $G to: $F)
244-
equals: #( $G $D $E $F ).
233+
self assert: (floydWarshall pathFrom: $A to: $F) equals: #( $A $B $E $F ).
234+
self assert: (floydWarshall pathFrom: $G to: $F) equals: #( $G $D $E $F ).
245235

246-
self assert: floydWarshall hasNegativeCycle equals: false
236+
self deny: floydWarshall hasNegativeCycle
247237
]

src/AI-Algorithms-Graph/AIAstar.class.st

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,56 +35,29 @@ AIAstar >> end: endModel [
3535

3636
{ #category : 'running' }
3737
AIAstar >> heuristicFrom: startModel to: endModel [
38-
"We are using a version of dijkstra algorithm here with all weights the for every node
39-
(value 1).
40-
So it has the same time complexity as BFS and takes into account the number of nodes in
41-
between intermediate and goal as a heuristic function. Edge weight can be increased to add
42-
more weight to heuristic part of Astar."
43-
44-
| dijkstra addEdges pathD use |
45-
addEdges := OrderedCollection new.
46-
47-
edges do: [ :edge |
48-
| efrom eto eval array |
49-
efrom := edge from model.
50-
eto := edge to model.
51-
eval := edge weight.
52-
array := OrderedCollection new.
53-
array add: efrom.
54-
array add: eto.
55-
array add: eval.
56-
addEdges add: array asArray
57-
"Transcript show: array;cr." ].
58-
addEdges := addEdges asArray.
59-
38+
"We are using a version of Dijkstra's algorithm here with same weights for every node (value 1).
39+
So it has the same time complexity as BFS and takes into account the number of nodes between intermediate and goal as an heuristic function.
40+
The edge weights can be modified to make this heuristic part of AIStar more problem specific."
6041

42+
| dijkstra addEdges |
43+
addEdges := edges collect: [ :edge | edge asTuple ].
6144

6245
end ifNil: [ ^ 0 ].
6346
endModel ifNil: [ ^ 0 ].
6447

6548
dijkstra := AIDijkstra new.
6649
dijkstra nodes: (nodes first model to: nodes last model).
67-
dijkstra edges: addEdges
50+
dijkstra
51+
edges: addEdges
6852
from: [ :each | each first ]
6953
to: [ :each | each second ]
70-
weight: [ :each | each third ].
54+
weight: [ :each | 1 ].
7155

72-
use := OrderedCollection new.
73-
use add: startModel model.
74-
use add: endModel model.
75-
dijkstra start: use first.
76-
dijkstra end: use second.
56+
dijkstra start: startModel model.
57+
dijkstra end: endModel model.
7758
dijkstra run.
78-
pathD := (dijkstra findNode: endModel model) pathDistance.
79-
80-
^ pathD
81-
]
82-
83-
{ #category : 'actions' }
84-
AIAstar >> newPriorityQueue [
85-
"We use the Heap object defined in the SequenceableCollections package."
8659

87-
^ Heap new
60+
^ (dijkstra findNode: endModel model) pathDistance
8861
]
8962

9063
{ #category : 'configuration' }

src/AI-Algorithms-Graph/AIBFS.class.st

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ Class {
1818
#superclass : 'AIGraphAlgorithm',
1919
#instVars : [
2020
'start',
21-
'end',
22-
'queue'
21+
'end'
2322
],
2423
#category : 'AI-Algorithms-Graph-Shortest path',
2524
#package : 'AI-Algorithms-Graph',
@@ -72,7 +71,7 @@ AIBFS >> resetValues [
7271
{ #category : 'running' }
7372
AIBFS >> run [
7473

75-
| node neighbours |
74+
| node neighbours queue |
7675
self resetValues.
7776

7877
queue := LinkedList with: start.

src/AI-Algorithms-Graph/AIDFS.class.st

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ Class {
1818
#superclass : 'AIGraphAlgorithm',
1919
#instVars : [
2020
'start',
21-
'end',
22-
'stack'
21+
'end'
2322
],
2423
#category : 'AI-Algorithms-Graph-Shortest path',
2524
#package : 'AI-Algorithms-Graph',
@@ -77,7 +76,7 @@ AIDFS >> resetValues [
7776
{ #category : 'running' }
7877
AIDFS >> run [
7978

80-
| node unvisitedNeighbor |
79+
| node unvisitedNeighbor stack |
8180
self resetValues.
8281

8382
stack := LinkedList new.

src/AI-Algorithms-Graph/AIFloydWarshall.class.st

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"
2-
The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of vertices in a weighted graph. This implementation works with both positive and negative edge weights (as long as there are no negative weight cycles) and can detect negative weight cycles in the graph.
2+
The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of vertices in a weighted graph.
3+
This implementation works with both positive and negative edge weights (as long as there are no negative weight cycles) and can detect negative weight cycles in the graph.
34
45
The algorithm uses a distance matrix to track the shortest known distances between all pairs of nodes and a next matrix to reconstruct the shortest paths.
56
67
Usage example:
78
9+
```
810
fw := AIFloydWarshall new
911
nodes: #(1 2 3);
1012
edges: { #(1 2 4). #(2 3 -2). #(3 1 1) }
@@ -13,6 +15,7 @@ fw := AIFloydWarshall new
1315
1416
fw distanceFrom: 1 to: 3. ""Returns 2""
1517
fw pathFrom: 1 to: 3. ""Returns #(1 2 3)""
18+
```
1619
"
1720
Class {
1821
#name : 'AIFloydWarshall',

0 commit comments

Comments
 (0)