-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelectric_avenue.rb
46 lines (35 loc) · 1.22 KB
/
electric_avenue.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require_relative 'lib/monkey_patch.rb'
Dir['./lib/**/*.rb'].each {|f| require_relative f }
# Handy helper methods to get started
#
# `number` is the number of nodes
# `grouping` is the number of nodes per generator
# `range` is how big the scene is: x and y are 0-`range`
def mst_grid(number: nil, grouping: nil, draw: nil, range: [10, 10])
nodes = number.times.map do |i|
n = Node.new(range[0] * PRNG.rand, range[1] * PRNG.rand, :id => i)
n.load = draw
n
end
pairs = nodes.combination 2
edges = pairs.map.with_index do |(p_1, p_2), i|
Edge.new p_1,
p_2,
p_1.euclidean_distance(p_2),
:id => i
end
mst = []
# Builds edges between nodes according to the MST
parallel_filter_kruskal edges, UnionF.new(nodes), mst
# Give edges new IDs so that they are 0..|edges|
edges = nodes.map(&:edges).flatten.uniq
edges.each.with_index {|e, i| e.id = i }
grid = Grid.new nodes, []
# FIXME does this really need to be a separate graph?
graph = ConnectedGraph.new grid.nodes
# Needed for the global adjacency matrix for doing faster manhattan distance
# calculations
KMeansClusterer::Distance.graph = graph
grid.build_generators_for_unreached grouping
grid
end