Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created March 2, 2017 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/5c655abe7aa7cabed2bdac24c8826f83 to your computer and use it in GitHub Desktop.
Save vznvzn/5c655abe7aa7cabed2bdac24c8826f83 to your computer and use it in GitHub Desktop.
require 'statsample'
def fit(l, y, lx)
a = {}
(lx + [y]).each { |x| a[x] = l.map { |b| b[x] }.to_vector() }
ds = a.to_dataset()
r = Statsample::Regression.multiple(ds, y)
# $stderr.puts(r.summary)
return r.coeffs.merge({'c' => r.constant})
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
def read(fn)
l = File.open('data.txt').readlines
l1 = l.shift.split
return l.map \
{
|x|
Hash[[l1, x.split.map { |y| y.to_f }].transpose]
}
end
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 stat2(l, t)
return Hash[[['a', 'sd', 'mx' , 'mn'], stat(l).map { |x| x / t }].transpose]
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def data(x)
n = x['n']
ns = n.to_s(2)
nl = ns.length
m = nl / 2
nsh = ns[0..m]
nsl = ns[m..-1]
asdm1 = stat2(ns.split(/0+/).map { |x| x.length }, nl)
l1 = ns.split(/1+/)
l1.shift
asdm0 = stat2(l1.map { |x| x.length }, nl)
return {'nl' => nl, 'h2' => x['h2'], 'd' => d(ns), 'dh' => d(nsh), 'dl' => d(nsl)}.merge(asdm1)
end
def predict1(x, z)
x.merge!(data(x))
return x if (z.nil?)
t = z['c']
(z.keys - ['c']).each { |k| t += z[k] * x[k] }
x['h2_p'] = t
x['ls_p'] = t / $g * x['nl']
x['h2_e'] = x['h2_p'] - x['h2']
x['ls_e'] = x['ls_p'] - x['ls']
puts([x['h2_e'], x['ls_e'], x['nl'], x['ls'], x['ls_p']].join("\t")) if (rand(40) == 0)
$stdout.flush
return x
end
def next2(z, v)
l = [z]
p = z['p'] + 1
l << predict1(adv({'n'=>z['n'] + 2**p, 'p'=>p}), v)
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 opt(c)
l1 = []
l2 = []
l3 = []
v = nil
l1 << next2({'n'=>1, 'p'=>0}, v)
puts('# ' + Time.now.to_s)
t = Time.now.to_i
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)
by = {0 => 'ls_e', 1 => 'ls'}[i % 2]
if (i % 2 == 0) then
l1 = l1.sort_by { |x| x[1]['ls'] }.reverse
z = l1.delete_at(rand([l1.size, 200].min))
l1.pop if (l1.size > 2000)
else
z = l2.delete_at(rand(l2.size))
l2.pop if (l2.size > 10000)
end
l3 << z
l1 << next2(z[1], v)
l2 << next2(z[2], v)
l3.shift if (l3.size > 400)
v = fit(l3.map { |x| x[1] }, 'h2', ['a', 'sd', 'mn', 'mx', 'd', 'dh', 'dl']) if ((i + 1) % 400 == 0)
}
end
$g = 50
l = opt(100000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment