gastar 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/README.md +46 -44
- data/lib/gastar/version.rb +3 -3
- data/lib/gastar.rb +9 -2
- data/spec/gastar_spec.rb +21 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MDJkOTcxNGZiMDYxZmJjYTI3MmYyZWZjZmEwMTNmZmFjZGZlNGE3OA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MzA4Mjc5MGQyM2I3ZDUxOWE5NTBmNjliNzBkNGRiZDA4NDhiNjZiNw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZTkyMzQ5NTdlYjQwZTNjYzhlMTQwMjQ4MjBkZGNhNDVlODJkZWZkZDFhMjYy
|
10
|
+
YjVmY2Y3YWJhNjQ0MDFlNWQ4OWY1ODBmNmQ1MDM1N2EwMDI1OTg5NTI2ZTBl
|
11
|
+
YTIyODI3ZTNlZDBmODZhMzczYjJlNzQ0MzMzNTk4NTIxMjE3NWY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Y2NkNDMzMjc4OTJkYTkzNzU1YjAyNjJjYmNjNDdlNGJjMzcwMjUzOTc4ZTE2
|
14
|
+
ZDc5YWUxYjBmM2MxNGFmNzM2YTRlYzRhYzhkNDdkYWYxNWE0MGZjNjI1ZDI0
|
15
|
+
ZWJiZjY0ZmNjNjEzMzdmZWE3YWQyODJiOGQ1NDk3MmZlZGQ3NWQ=
|
data/README.md
CHANGED
@@ -27,50 +27,52 @@ Or install it yourself as:
|
|
27
27
|
|
28
28
|
## Usage
|
29
29
|
|
30
|
-
Example use
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
30
|
+
Example use for finding the shortest route between cities.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'gastar'
|
34
|
+
|
35
|
+
# Implement the abstract node class.
|
36
|
+
# Note: All attributes below are custom for this implementation and none are
|
37
|
+
# needed nor used by the actual AStar seach algorithm. They're my domain atts.
|
38
|
+
class Node < AStarNode
|
39
|
+
attr_reader :name, :x, :y
|
40
|
+
def initialize(name, x, y)
|
41
|
+
super()
|
42
|
+
@name, @x, @y = name, x, y
|
43
|
+
end
|
44
|
+
def move_cost(other) 1 end
|
45
|
+
def to_s() name end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Also implement an algorithm for estimating cost of reaching the destination
|
49
|
+
class Space < AStar
|
50
|
+
def heuristic(node, start, goal)
|
51
|
+
Math.sqrt( (goal.x - node.x)**2 + (goal.y - node.y)**2 )
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Create a graph as an ordinary hashmap, with the key being a node and the
|
56
|
+
# value a list of other nodes that can be reached from the key-node.
|
57
|
+
|
58
|
+
sun = Node.new "Sundsvall", 9, 10
|
59
|
+
upp = Node.new "Uppsala", 9, 6
|
60
|
+
sth = Node.new "Stockholm", 10, 5
|
61
|
+
jon = Node.new "Jonkoping", 4, 3
|
62
|
+
got = Node.new "Goteborg", 1, 3
|
63
|
+
mal = Node.new "Malmo", 2, 1
|
64
|
+
|
65
|
+
cities = {
|
66
|
+
sun => [upp],
|
67
|
+
sth => [sun,jon,upp],
|
68
|
+
jon => [sth,got,mal],
|
69
|
+
upp => [sth,sun],
|
70
|
+
mal => [jon],
|
71
|
+
got => [jon]
|
72
|
+
}
|
73
|
+
|
74
|
+
puts Space.new(cities).search(sun, mal)
|
75
|
+
```
|
74
76
|
|
75
77
|
## Contributing
|
76
78
|
|
data/lib/gastar/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Gastar
|
2
|
-
VERSION = "1.0.
|
3
|
-
end
|
1
|
+
module Gastar
|
2
|
+
VERSION = "1.0.1"
|
3
|
+
end
|
data/lib/gastar.rb
CHANGED
@@ -68,7 +68,14 @@ class AStar
|
|
68
68
|
openset = Set.new
|
69
69
|
closedset = Set.new
|
70
70
|
current = start
|
71
|
-
|
71
|
+
|
72
|
+
if @maximize_cost # serves to invert the comparison
|
73
|
+
openset_min_max = openset.method(:max_by)
|
74
|
+
flip = -1
|
75
|
+
else
|
76
|
+
openset_min_max = openset.method(:min_by)
|
77
|
+
flip = 1
|
78
|
+
end
|
72
79
|
|
73
80
|
openset.add(current)
|
74
81
|
while not openset.empty?
|
@@ -89,7 +96,7 @@ class AStar
|
|
89
96
|
|
90
97
|
if openset.include? node
|
91
98
|
new_g = current.g + current.move_cost(node)
|
92
|
-
if node.g >
|
99
|
+
if (node.g - new_g) * flip > 0
|
93
100
|
node.g = new_g
|
94
101
|
node.parent = current
|
95
102
|
end
|
data/spec/gastar_spec.rb
CHANGED
@@ -42,5 +42,26 @@ describe "AStar implementation" do
|
|
42
42
|
actual.should eql(expected)
|
43
43
|
|
44
44
|
end
|
45
|
+
|
46
|
+
it "finds the longest path between cities" do
|
47
|
+
|
48
|
+
a = Node.new "C1", 1, 1
|
49
|
+
b = Node.new "C2", 2, 2
|
50
|
+
c = Node.new "C3", 1, 3
|
51
|
+
d = Node.new "C4", 1, 4
|
52
|
+
|
53
|
+
|
54
|
+
cities = {
|
55
|
+
a => [b,c],
|
56
|
+
b => [c],
|
57
|
+
c => [d],
|
58
|
+
d => [c],
|
59
|
+
}
|
60
|
+
|
61
|
+
Space.new(cities, false).search(a, d).should eql([a,c,d])
|
62
|
+
Space.new(cities, true).search(a, d).should eql([a,b,c,d])
|
63
|
+
|
64
|
+
end
|
65
|
+
|
45
66
|
end
|
46
67
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gastar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Tingeborn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|