Created
February 18, 2017 23:17
-
-
Save vznvzn/82df687adaad713f0a09ed8b1a7a020c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f2(n) | |
n = (n * 3 + 1) / 2 while (n.odd?) | |
n /= 2 while (n.even?) | |
return n | |
end | |
def adv(x) | |
n1 = n = x['n'] | |
l = [n] | |
while (n >= n1 && n != 1) | |
n = f2(n) | |
l << n | |
end | |
x['l'] = l | |
x['ls'] = l.size | |
x['ns'] = x['n'].to_s(2).length | |
x['h'] = x['ls'].to_f / x['ns'] | |
x['h2'] = (x['h'] * 50).to_i | |
return x | |
end | |
def next2(z) | |
l = [z] | |
p = z['p'] + 1 | |
l << adv({'n'=>z['n'] + 2**p, 'p'=>p}) | |
l << z.merge({'p'=>p}) | |
return l | |
end | |
def stat(l) | |
l = [0] if (l.empty?) | |
t = t2 = 0 | |
l.each \ | |
{ | |
|x| | |
t += x | |
t2 += x ** 2 | |
} | |
c = l.size | |
a = t.to_f / c | |
z = t2.to_f / c - a ** 2 | |
sd = Math.sqrt(z < 0 ? 0 : z) | |
return a, sd, l.max.to_f, l.min.to_f | |
end | |
def range(l, s) | |
['ls', 'ns', 'h2'].each \ | |
{ | |
|k| | |
a, sd = stat(l.map { |x| x[1][k] }) | |
s[k] = {'a' => a, 'sd' => sd} | |
} | |
end | |
def next1(x, r) | |
x = next2(x) | |
return x if (r.empty?) | |
['ls', 'ns', 'h2'].each \ | |
{ | |
|k| | |
x[1][k + 'z'] = (x[1][k] - r[k]['a']) / r[k]['sd'] | |
} | |
x[1]['z'] = x[1]['lsz'] + x[1]['nsz'] + x[1]['h2z'] | |
return x | |
end | |
def opt(c) | |
l1 = [] | |
l2 = [] | |
l1 << next2({'n'=>1, 'p'=>0}) | |
puts('# ' + Time.now.to_s) | |
t = Time.now.to_i | |
r = {} | |
c.times \ | |
{ | |
|i| | |
$stderr.puts([i, sprintf('%.1fm', (Time.now.to_i - t) / 60.0), Time.now.to_s].join("\t")) if (i % 100 == 0) | |
range(l1, r) if ((i + 1) % 100 == 0) | |
if (ARGV[0].nil? ? i.even? : ARGV[0].to_i == 0) then | |
l1 = l1.sort_by { |x| x[1]['z'].nil? ? 0 : x[1]['z'] }.reverse | |
j = [200, l1.size].min | |
else | |
j = l1.size | |
end | |
z = l1.delete_at(rand(j)) | |
l1.pop if (l1.size > 1000) | |
l2 << z | |
l1 << next1(z[1], r) | |
l1 << next1(z[2], r) | |
} | |
return l2.map { |x| x[1].reject { |k, v| k == 'l' } } | |
end | |
def out(fn, l) | |
f = File.open("#{fn}.txt", 'w') | |
f.puts(l[0].keys.join("\t")) | |
l.each { |x| f.puts(x.values.join("\t")) } | |
f.close | |
end | |
l = opt(5000) | |
out('out', l) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment