-
Notifications
You must be signed in to change notification settings - Fork 1
/
grouper.rb
302 lines (246 loc) · 6.09 KB
/
grouper.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# take a look at Steiner
# Steiner system S(t, k, v)
group_size = 2
people_count = 9
people_count = ARGV[0].to_i if ARGV[0]
people = (1..people_count).to_a
class Grouper
def initialize(group_size, sub_group_size)
@n = group_size
@k = sub_group_size
@or_rules = [
method(:only_off_by_1)
]
end
def combine_indexes
list = (0...@n).to_a.map { |i| [i] }
(1...@k).each do
list = another_pairing(list)
end
list
end
def another_pairing(list, n=@n)
result = []
list.each do |i|
((i.max+1)...n).each do |j|
result.push(i.dup.push(j))
end
end
result
end
def permutation(k, s)
(2...s.size).each do |j|
s.swap(k % j, j-1)
k /= j
end
s
end
def permute(list)
k = @k
(2...list.size).each do |i|
swap(list, k % i, i-1)
k /= i
end
list
end
def swap(list, i, j)
list[i], list[j] = list[j], list[i]
end
def _permute(range, result)
range.each do |i|
catch :continue do
unless only_off_by_1(result[0...i], result[i])
(i+1...result.size).each do |j|
if only_off_by_1(result[0...i], result[j])
swap result, i, j
throw :continue
end
end
return false
# look backward for a possible solution?
end
end
end
result
end
def apply_rules
result_set = []
extra_set = combine_indexes
while result_set.size < @n
endless_loop = true
i = 0
while (i < extra_set.size)
if only_off_by_1(result_set, extra_set[i])
result_set.push(extra_set.delete_at(i))
endless_loop = false
end
i += 1
end
if endless_loop
# take out k-1 and find k to put back in
(@k-1).times { extra_set.push result_set.shift }
extra_set_indexes = (0...extra_set.size).to_a.map {|i| [i] }
(1...@k).each do # TODO remove this duplication
extra_set_indexes =
another_pairing(extra_set_indexes, extra_set.size)
end
extra_set_indexes.each do |i|
items = i.map {|j| extra_set[j] }
if only_off_by_1(result_set, items)
result_set << items
endless_loop = false
end
end
puts "#@n C #@k"
p result_set
p extra_set
end
break if endless_loop
end
return result_set
end
def xxxapply_rules
result = combine_indexes
solved = false
10.times do
if _permute(1...result.size, result)
solved = true
#puts "solved #@n P #@k by radomizing"
break
end
result.sort! { rand(3) - 1 }
end
puts "failed to solve #@n P #@k" unless solved
end
def xxxapply_rules
result = combine_indexes
#result.sort! { rand(3) - 1 }
@start_value = 1
puts
catch :restart do
(@start_value...result.size).each do |i|
print "#{@start_value} "
puts i
catch :continue do
unless only_off_by_1(result[0...i], result[i])
(i+1...result.size).each do |j|
if only_off_by_1(result[0...i], result[j])
swap result, i, j
throw :continue
end
end
# look backward for a possible solution
back_index = nil
(i-1).downto(0) do |j|
#i(0...i).each do |j|
print "bt: #{j} "
p result[j]
=begin
=end
if only_off_by_1(result[0...j], result[i])
swap result, i,j
@start_value = j-1
p "restarting #{@start_value}"
throw :restart
end
end
raise "shouldn't be here"
end
end
end
result
end
end
def only_off_by_1(*args)
hash = off_by_1_hash
(args).flatten.each do |i|
hash[i] += 1
end
min = hash.values.min
max = hash.values.max
return max - min <= 1
end
def off_by_1_hash
@hash = Hash[*(0...@n).collect {|i| [i, 0]}.flatten] unless @hash
@hash.dup
end
def ph(h)
print "{"
print h.keys.sort.collect {|i| "#{i}=>#{h[i]}" }.join(", ")
puts "}"
end
# evenly distribute the groupings
def combine(groupings, people, results=[], bad_path=[])
return results if groupings.empty?
print "R:"
p results
puts
print "b:"
p bad_path.sort
#print "g:"
#p groupings
puts
line = nil
groupings.each do |item|
if only_off_by_1(results, item, people)
line = groupings.delete item
break
end
end
if line
if bad_path.include? [results[-1], line]
item = results.pop
groupings.push line
groupings.push item
bad_path.push [results[-1], item]
else
results.push line
end
else
item = results.pop
bad_path.push([results[-1], item])
groupings.push item
end
combine groupings, people, results, bad_path
end
=begin
def only_off_by_1(group, item, all)
hash = all.inject({}) { |h, i| h[i] = 0; h }
(group + item).flatten.each do |i|
hash[i] += 1
end
min = hash.values.min
max = hash.values.max
return max - min <= 1
end
=end
def binom(n, k)
return 1 if ( k ==0 ) || (n== k)
# puts "(#{n}, #{k})"
n1 = binom( n - 1, k)
n2 = binom( n - 1, k - 1)
return n1 + n2
end
end
def all_indexes(index_count, group_size)
result = (0...index_count).to_a.map { |i| [i] }
(1...group_size).each do
result = another_pairing(result, index_count)
end
result
end
def another_pairing(list, index_count)
result = []
list.each do |i|
((i.max+1)...index_count).each do |j|
result.push [i, j].flatten
end
end
result
end
def all_groups(people, group_size)
all_indexes(people.size, group_size).map {|i| i.map {|j| people[j] } }
end
def print_groups(g)
g.each {|i| p i }
end