|
| 1 | +" |
| 2 | +Algorithm for shortest path in graphs with ""max"" vertices as well as ""min"" vertices, used in the implementation of ABCD. |
| 3 | +The graph can have negative cycles, as long as they contain at least one max node. |
| 4 | +If a path doesn't exist it returns Float infinity. |
| 5 | +
|
| 6 | +Possible results are {1, 0, -1}, which map to {True, Reduced, False} in the paper's algorithm. |
| 7 | +True and Reduced means the check is redundant (with Reduced meaning a cycle was reduced) |
| 8 | +C is a table used for memoization, that maps [v-a<=c] into {True, False, Reduced} |
| 9 | +active detects cycles: if active[v] != null, then active[v] is the distance of v from b, where b is the check’s index variable. |
| 10 | +active maintains the distance for each vertex v that is on the path on the current DFS stack |
| 11 | +isMaxNodePredicate is used to know if a given node is a max node (i.e. is in V_phi) |
| 12 | +" |
| 13 | +Class { |
| 14 | + #name : 'AIShortestPathWithMaxAndMinNodes', |
| 15 | + #superclass : 'AIGraphAlgorithm', |
| 16 | + #instVars : [ |
| 17 | + 'start', |
| 18 | + 'isMaxNodePredicate', |
| 19 | + 'active', |
| 20 | + 'C' |
| 21 | + ], |
| 22 | + #category : 'AI-Algorithms-Graph-Shortest path', |
| 23 | + #package : 'AI-Algorithms-Graph', |
| 24 | + #tag : 'Shortest path' |
| 25 | +} |
| 26 | + |
| 27 | +{ #category : 'running' } |
| 28 | +AIShortestPathWithMaxAndMinNodes >> distanceTo: aModel isLessThan: c [ |
| 29 | + |
| 30 | + | memo isMaxNode v | |
| 31 | + "start == a" |
| 32 | + v := self findNode: aModel. |
| 33 | + |
| 34 | + memo := C at: { v. start } ifAbsentPut: [ Dictionary new ]. |
| 35 | + |
| 36 | + memo keysAndValuesDo: [ :key :val | |
| 37 | + (key <= c and: val = 1) ifTrue: [ ^ 1 ] ]. |
| 38 | + |
| 39 | + memo keysAndValuesDo: [ :key :val | |
| 40 | + (key >= c and: val = -1) ifTrue: [ ^ -1 ] ]. |
| 41 | + |
| 42 | + memo keysAndValuesDo: [ :key :val | |
| 43 | + (key <= c and: val = 0) ifTrue: [ ^ 0 ] ]. |
| 44 | + |
| 45 | + (v = start and: c >= 0) ifTrue: [ ^ 1 ]. |
| 46 | + |
| 47 | + v incomingEdges ifEmpty: [ ^ -1 ]. |
| 48 | + |
| 49 | + active at: v ifPresent: [ :d | |
| 50 | + ^ c > d |
| 51 | + ifTrue: [ -1 ] |
| 52 | + ifFalse: [ 0 ] ]. |
| 53 | + |
| 54 | + active at: v put: c. |
| 55 | + isMaxNode := isMaxNodePredicate value: v model. |
| 56 | + v incomingEdges do: [ :e | |
| 57 | + | u res prev | |
| 58 | + u := e from. |
| 59 | + res := self distanceTo: u model isLessThan: c - e weight. |
| 60 | + prev := memo at: c ifAbsent: [isMaxNode |
| 61 | + ifTrue: [ 1 ] |
| 62 | + ifFalse: [ -1 ]]. |
| 63 | + memo at: c put: (isMaxNode |
| 64 | + ifTrue: [ prev min: res ] |
| 65 | + ifFalse: [ prev max: res ]) |
| 66 | + ]. |
| 67 | + active removeKey: v. |
| 68 | + |
| 69 | + ^ memo at: c |
| 70 | +] |
| 71 | + |
| 72 | +{ #category : 'configuration' } |
| 73 | +AIShortestPathWithMaxAndMinNodes >> edgeClass [ |
| 74 | + |
| 75 | + ^ AIWeightedEdge |
| 76 | +] |
| 77 | + |
| 78 | +{ #category : 'initialization' } |
| 79 | +AIShortestPathWithMaxAndMinNodes >> initialize [ |
| 80 | + |
| 81 | + super initialize. |
| 82 | + active := Dictionary new. "Deberia venir de arriba?" |
| 83 | +] |
| 84 | + |
| 85 | +{ #category : 'initialization' } |
| 86 | +AIShortestPathWithMaxAndMinNodes >> isMaxNodePredicate: aBlock [ |
| 87 | + |
| 88 | + isMaxNodePredicate := aBlock |
| 89 | +] |
| 90 | + |
| 91 | +{ #category : 'initialization' } |
| 92 | +AIShortestPathWithMaxAndMinNodes >> memoDictionary: aDictionary [ |
| 93 | + |
| 94 | + C := aDictionary |
| 95 | +] |
| 96 | + |
| 97 | +{ #category : 'configuration' } |
| 98 | +AIShortestPathWithMaxAndMinNodes >> nodeClass [ |
| 99 | + |
| 100 | + ^ AIWeightedHitsNode |
| 101 | +] |
| 102 | + |
| 103 | +{ #category : 'running' } |
| 104 | +AIShortestPathWithMaxAndMinNodes >> run [ |
| 105 | + |
| 106 | + |
| 107 | +] |
| 108 | + |
| 109 | +{ #category : 'initialization' } |
| 110 | +AIShortestPathWithMaxAndMinNodes >> start: aModel [ |
| 111 | + |
| 112 | + start := (self findNode: aModel). |
| 113 | + |
| 114 | +] |
0 commit comments