-
Notifications
You must be signed in to change notification settings - Fork 0
/
weightgame.rb
337 lines (301 loc) · 9.19 KB
/
weightgame.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
def half_bits(n)
raise "sorry must be even" if n % 2 != 0
# Information not known to the player:
# The eight items each weigh a power of two,
# and the answer is four items.
(0...(1 << n)).select { |x| x.to_s(2).count(?1) == n / 2 }
end
def powerset(n)
(0...(1 << n)).to_a
end
def play_game(limit, answer, guesser)
guesses = []
limit.times { |x|
guess = guesser.guess
if guess == answer
guesses << [guess, :ok].freeze
return {
guesses: guesses,
num_guesses: x + 1,
}
end
if guess < answer
guesser[guess] = :too_light
guesses << [guess, :too_light].freeze
else
guesser[guess] = :too_heavy
guesses << [guess, :too_heavy].freeze
end
}
{
guesses: guesses,
num_guesses: nil,
}
end
def rate_guesser(n, answers, new_guesser, verbose: false)
t = Time.now
results = {
failed: 0,
guessed: 0,
total: 0,
minimum: Float::INFINITY,
maximum: 0,
}
answers[n].each { |ans|
result = play_game(1 << n, ans, new_guesser[n, verbose: verbose])
p result[:guesses].map { |guess, result|
[guess.to_s(2).rjust(n, ?0), result]
} if false #|| true
if (guesses = result[:num_guesses])
results[:guessed] += 1
results[:total] += guesses
results[:minimum] = [results[:minimum], guesses].min
results[:maximum] = [results[:maximum], guesses].max
else
results[:failed] += 1
end
}
results.merge(average: results[:total].fdiv(results[:guessed]), time: Time.now - t)
end
class Seq
attr_reader :guess
def initialize(*_)
@guess = 0
end
def []=(_, _)
@guess += 1
end
end
class IncludeExclude
def initialize(n, initial_state, verbose: false)
raise "bad state #{initial_state}" unless %i(include exclude).include?(initial_state)
@n = n
@state = initial_state
@must_include = 0
@must_exclude = 0
@unknown = n.times.map { |x| 1 << x }
@guesses = @unknown.dup
@verbose = verbose
end
def guess
return @must_include if @guesses.empty?
if @state == :include
# See if we must include an item - guess all items except this one and any must-not-haves,
# and see if it's too light.
((1 << @n) - 1) ^ @must_exclude ^ @guesses[0]
elsif @state == :exclude
# See if we must exclude an item - guess must-have items plus this one,
# and see if it's too heavy.
@must_include | @guesses[0]
else
raise "Unknown state #{@state}"
end
end
def []=(guess, feedback)
b = ->x { x.to_s(2).rjust(@n, ?0) }
puts "got #{feedback} for #{b[guess]} which was derived from #{b[@guesses[0]]}" if @verbose
if @state == :include && feedback == :too_light
@must_include |= @guesses[0]
@unknown.delete(@guesses[0])
elsif @state == :exclude && feedback == :too_heavy
@must_exclude |= @guesses[0]
@unknown.delete(@guesses[0])
end
@guesses.shift
return unless @guesses.empty?
@state = @state == :include ? :exclude : :include
@guesses = @unknown.dup
puts "include #{b[@must_include]}, exclude #{b[@must_exclude]}, unknown #{@unknown.map(&b).join(', ')}" if @verbose
end
end
class IncludeThenExclude < IncludeExclude
def initialize(n, **kwargs)
super(n, :include, **kwargs)
end
end
class ExcludeThenInclude < IncludeExclude
def initialize(n, **kwargs)
super(n, :exclude, **kwargs)
end
end
class Possibilities
def initialize(n, verbose: false)
@n = n
@possible = (0...(1 << n)).to_a
@guesses = (0...(1 << n)).to_h { |x| [x, nil] }
@verbose = verbose
end
def []=(guess, feedback)
if feedback == :too_light
@possible.reject! { |candidate| candidate & guess == candidate }
elsif feedback == :too_heavy
@possible.reject! { |candidate| candidate & guess == guess }
else
raise "unknown feedback #{feedback}"
end
puts "got #{feedback} for #{guess}, now #{@possible.size} left" if @verbose
end
end
class MinEntropy < Possibilities
def guess
return @possible[0] if @possible.size == 1
@guesses.keys.min_by { |guess|
eq = 0
subset_of_guess = 0
superset_of_guess = 0
neither = 0
@possible.each { |candidate|
if candidate == guess
eq += 1
elsif candidate & guess == candidate
subset_of_guess += 1
elsif candidate & guess == guess
superset_of_guess += 1
else
neither += 1
end
}
# Day 25 guesser doesn't know object weights, so using comparisons isn't fair.
#n_too_light = @possible.count { |candidate| candidate > guess }
#n_too_heavy = @possible.count { |candidate| candidate < guess }
# careful here,
# *result* will be "too heavy" if guess is superset of real answer (real answer is subset of guess),
# "too light" if guess is subset of real answer (real answer is superset of guess).
#
# No real way to tell for the others so just call them equally likely.
# I'd add Rational(neither, 2) to each, but I'll just multiply through by 2 to avoid Rationals (faster).
n_too_light = 2 * superset_of_guess + neither
n_too_heavy = 2 * subset_of_guess + neither
# If this is too light, any subset is also too light.
# If this is too heavy, any superset is also too heavy.
# eq contributes 1 if guess is possible, else 0, but remember we're multiplying by 2 to avoid halves.
2 * eq + n_too_light * (@possible.size - subset_of_guess) + n_too_heavy * (@possible.size - superset_of_guess)
}.tap { |g| @guesses.delete(g) }
end
end
class MinEntropyNoTrivial < MinEntropy
def initialize(n, **kwargs)
super
@possible.delete(0)
@possible.delete((1 << n) - 1)
end
end
class MinEntropyHalf < MinEntropy
def initialize(n, **kwargs)
super
if n.even?
@possible.select! { |x| x.digits(2).count(1) == n / 2 }
else
want = [n / 2, (n + 1) / 2]
@possible.select! { |x| want.include?(x.digits(2).count(1)) }
end
end
end
# Restricting to only guess possible ones makes things worse;
# sometimes you have to guess one that you know is not possible.
class MinEntropyHalfGuessPossible < MinEntropyHalf
def initialize(n, **kwargs)
super
@guesses = @possible.to_h { |x| [x, nil] }
end
end
class MinEntropyCheater < Possibilities
def guess
return @possible[0] if @possible.size == 1
(0...(1 << @n)).min_by { |guess|
eq = 0
subset_of_guess = 0
superset_of_guess = 0
greater_than_guess = 0
less_than_guess = 0
neither = 0
@possible.each { |candidate|
if candidate == guess
eq += 1
else
if candidate & guess == candidate
subset_of_guess += 1
elsif candidate & guess == guess
superset_of_guess += 1
else
neither += 1
end
if candidate > guess
greater_than_guess += 1
else
less_than_guess += 1
end
end
}
eq + greater_than_guess * (@possible.size - subset_of_guess) + less_than_guess * (@possible.size - superset_of_guess)
}
end
end
# Goes to show that if you try to use a comparison but you don't know the real weights,
# you will assign incorrect probabilities and cost a lot of guesses.
class MinEntropyRevCheater < Possibilities
def guess
return @possible[0] if @possible.size == 1
@guesses.keys.min_by { |guess|
eq = 0
subset_of_guess = 0
superset_of_guess = 0
greater_than_guess = 0
less_than_guess = 0
neither = 0
@possible.each { |candidate|
if candidate == guess
eq += 1
else
if candidate & guess == candidate
subset_of_guess += 1
elsif candidate & guess == guess
superset_of_guess += 1
else
neither += 1
end
if candidate > guess
greater_than_guess += 1
else
less_than_guess += 1
end
end
}
eq + less_than_guess * (@possible.size - subset_of_guess) + greater_than_guess * (@possible.size - superset_of_guess)
}.tap { |g| @guesses.delete(g) }
end
end
class MinMaxSize < Possibilities
def guess
return @possible[0] if @possible.size == 1
(0...(1 << @n)).min_by { |guess|
remain_if_too_light = remain_if_too_heavy = @possible.size
@possible.each { |candidate|
# If this is oo light, any subset is also too light.
remain_if_too_light -= 1 if candidate & guess == candidate
# If this is too heavy, any superset is also too heavy.
remain_if_too_heavy -= 1 if candidate & guess == guess
}
[remain_if_too_light, remain_if_too_heavy].max
}
end
end
#play_game(256, 0b01010101, MinEntropyHalf.new(8, verbose: true))
#exit 0
[
Seq,
IncludeThenExclude,
ExcludeThenInclude,
MinEntropy,
MinEntropyNoTrivial,
MinEntropyHalf,
MinEntropyHalfGuessPossible,
MinEntropyCheater,
MinMaxSize,
MinEntropyRevCheater,
].each { |c|
#puts "#{c}: #{rate_guesser(8, method(:powerset), c.method(:new))}"
puts "#{c}: #{rate_guesser(8, method(:half_bits), c.method(:new))}"
#puts "#{c}: #{rate_guesser(3, method(:powerset), c.method(:new))}"
#puts "#{c}: #{rate_guesser(4, method(:powerset), c.method(:new))}"
}