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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTgxNmZlMjM1OWRiNjI3NTU4MDViNDJhYWE0MzQ3YWZmODRiNzcwOA==
4
+ MDJkOTcxNGZiMDYxZmJjYTI3MmYyZWZjZmEwMTNmZmFjZGZlNGE3OA==
5
5
  data.tar.gz: !binary |-
6
- NWZhMWUxMmYxYTYzMDE5NDBkM2YzNjU3MThjYjljNGZlN2ZjNGFmZA==
6
+ MzA4Mjc5MGQyM2I3ZDUxOWE5NTBmNjliNzBkNGRiZDA4NDhiNjZiNw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NTNmNGU5OGFiODBmODA4MDUxYTFlNGYxZmVlN2Y0NDRjNDc3NWNlMGNmY2Vh
10
- MmQ0NTFhODExNzJhNjNlNzk1ZDVhNWUzMzVhZDVkNTJiNWNhYjk4ODI4ZGJm
11
- OWRiMjJmMGI4NThkNzdhODVmZTNlYmFkYzc4YWY4ZjIxNjU3MDk=
9
+ ZTkyMzQ5NTdlYjQwZTNjYzhlMTQwMjQ4MjBkZGNhNDVlODJkZWZkZDFhMjYy
10
+ YjVmY2Y3YWJhNjQ0MDFlNWQ4OWY1ODBmNmQ1MDM1N2EwMDI1OTg5NTI2ZTBl
11
+ YTIyODI3ZTNlZDBmODZhMzczYjJlNzQ0MzMzNTk4NTIxMjE3NWY=
12
12
  data.tar.gz: !binary |-
13
- ZmYwMzViMjAzMjllNWRmNGY1ZmIyZGVjOTU1ODAyNzk4YmI5Yjc2NDExZTBm
14
- NzYzNWQzOTk1ZDdlZTg1YmQyMWQ1OTIwNTYwMTFkOTE4NTJmMjQ0NGU0NmQ3
15
- MmZiYzc1Y2RiZDVjNTQ0M2Q0N2EzMjVmMDY3ZTczMDJjNzkzZWM=
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
- require 'gastar'
33
-
34
- # Implement the abstract node class.
35
- # Note: All attributes below are custom for this implementation and none are
36
- # needed nor used by the actual AStar seach algorithm. They're my domain atts.
37
- class Node < AStarNode
38
- attr_reader :name, :x, :y
39
- def initialize(name, x, y)
40
- super()
41
- @name, @x, @y = name, x, y
42
- end
43
- def move_cost(other) 1 end
44
- def to_s() name end
45
- end
46
-
47
- # Also implement an algorithm for estimating cost of reaching the destination
48
- class Space < AStar
49
- def heuristic(node, start, goal)
50
- Math.sqrt( (goal.x - node.x)**2 + (goal.y - node.y)**2 )
51
- end
52
- end
53
-
54
- # Create a graph as an ordinary hashmap, with the key being a node and the
55
- # value a list of other nodes that can be reached from the key-node.
56
-
57
- sun = Node.new "Sundsvall", 9, 10
58
- upp = Node.new "Uppsala", 9, 6
59
- sth = Node.new "Stockholm", 10, 5
60
- jon = Node.new "Jonkoping", 4, 3
61
- got = Node.new "Goteborg", 1, 3
62
- mal = Node.new "Malmo", 2, 1
63
-
64
- cities = {
65
- sun => [upp],
66
- sth => [sun,jon,upp],
67
- jon => [sth,got,mal],
68
- upp => [sth,sun],
69
- mal => [jon],
70
- got => [jon]
71
- }
72
-
73
- puts Space.new(cities).search(sun, mal)
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
 
@@ -1,3 +1,3 @@
1
- module Gastar
2
- VERSION = "1.0.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
- openset_min_max = @maximize_cost ? openset.method(:max_by) : openset.method(:min_by)
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 > new_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.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-07 00:00:00.000000000 Z
11
+ date: 2014-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler