Class: RandomConnectStrategy

Inherits:
ConnectStrategy show all
Defined in:
lib/gimuby/genetic/archipelago/connect_strategy/random_connect_strategy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRandomConnectStrategy

Returns a new instance of RandomConnectStrategy.



6
7
8
# File 'lib/gimuby/genetic/archipelago/connect_strategy/random_connect_strategy.rb', line 6

def initialize
  @average_degree = 4.0
end

Instance Attribute Details

#average_degreeObject

Returns the value of attribute average_degree.



10
11
12
# File 'lib/gimuby/genetic/archipelago/connect_strategy/random_connect_strategy.rb', line 10

def average_degree
  @average_degree
end

Instance Method Details

#connect(archipelago) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gimuby/genetic/archipelago/connect_strategy/random_connect_strategy.rb', line 12

def connect(archipelago)
  nodes = get_nodes(archipelago)
  connections_to_make = @average_degree * nodes.length
  check_connections_to_make(connections_to_make, nodes)
  made = 0
  begin
    from_node = nodes[rand(nodes.length)]
    to_node = nodes[rand(nodes.length)]
    has_edge = archipelago.has_edge(from_node, to_node)
    if from_node != to_node || has_edge
      archipelago.add_edge(from_node, to_node)
      made += 1
    end
  end while made < connections_to_make
  made
end