Class: Trixer::Combinator
- Inherits:
-
Object
- Object
- Trixer::Combinator
- Defined in:
- lib/trixer/combinator.rb
Instance Attribute Summary collapse
-
#matrix ⇒ Object
readonly
Returns the value of attribute matrix.
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
Instance Method Summary collapse
- #calculate ⇒ Object
- #calculate_groups(group_size) ⇒ Object
- #combinations ⇒ Object
- #groups_of_two ⇒ Object
-
#initialize(matrix:, objects: nil) ⇒ Combinator
constructor
A new instance of Combinator.
Constructor Details
#initialize(matrix:, objects: nil) ⇒ Combinator
Returns a new instance of Combinator.
18 19 20 21 22 23 |
# File 'lib/trixer/combinator.rb', line 18 def initialize(matrix:, objects: nil) @matrix = matrix.dup @objects = objects @size = @matrix.size @groups = {} end |
Instance Attribute Details
#matrix ⇒ Object (readonly)
Returns the value of attribute matrix.
4 5 6 |
# File 'lib/trixer/combinator.rb', line 4 def matrix @matrix end |
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
6 7 8 |
# File 'lib/trixer/combinator.rb', line 6 def objects @objects end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
5 6 7 |
# File 'lib/trixer/combinator.rb', line 5 def size @size end |
Class Method Details
.combinations(adjacency_list:, objects: nil) ⇒ Object
10 11 12 13 14 |
# File 'lib/trixer/combinator.rb', line 10 def combinations(adjacency_list:, objects: nil) objects ||= adjacency_list.keys matrix = Matrix.from_adjacency_list(adjacency_list: adjacency_list, objects: objects) Combinator.new(matrix: matrix, objects: objects).combinations end |
Instance Method Details
#calculate ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/trixer/combinator.rb', line 36 def calculate @groups[2] = groups_of_two if @size > 2 (3..matrix.size).each do |group_size| break unless calculate_groups(group_size) end end @groups end |
#calculate_groups(group_size) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/trixer/combinator.rb', line 46 def calculate_groups(group_size) previous_groups = @groups[group_size-1] return false if previous_groups.nil? @groups[group_size] = Set[] previous_groups.each do |group| group.each_with_index do |node, i| matrix[node].each_with_index do |is_linked, neighbour| if is_linked == 1 && !group.include?(neighbour) new_group = group.dup new_group << neighbour @groups[group_size] << new_group end end end end true end |
#combinations ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/trixer/combinator.rb', line 25 def combinations calculate if @groups.empty? res = [] @groups.each do |group_size, sub_groups| sub_groups.each do |group| res << group.map { |i| @objects.nil? ? i : @objects[i] } end end res end |
#groups_of_two ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/trixer/combinator.rb', line 64 def groups_of_two groups = Set[] for x in (0..@size-1) for y in (x+1..@size-1) groups << Set[x, y] if @matrix[x][y] == 1 end end groups end |