Skip to content

Instantly share code, notes, and snippets.

@vznvzn

vznvzn/outline.rb

Created Oct 10, 2020
Embed
What would you like to do?
def midpt(ns)
w2 = ns.length / 4
l = ns.split('')
i = j = 0
while (i < w2 && j < l.size)
i += l[j].to_i
j += 1
end
return j.to_f / ns.length
end
def len(ns, p)
l = ns.split(p)
l = [] if (l.nil?)
l.shift if (l[0] == '')
return l.map { |x| x.length }
end
def len1(ns)
return len(ns, /0+/)
end
def len0(ns)
return len(ns, /1+/)
end
def len01(ns)
return len1(ns), len0(ns)
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def e(ns)
return len01(ns).flatten.size.to_f / ns.length
end
def features(ns, k)
d = d(ns)
e = e(ns)
nw = ns.length
nw2 = nw / 2
nshi = ns[0...nw2]
nslo = ns[nw2..-1]
dlo = d(nslo)
dhi = d(nshi)
elo = e(nslo)
ehi = e(nshi)
mp = midpt(ns)
return {
"d#{k}" => d,
"e#{k}" => e,
"dlo#{k}" => dlo,
"dhi#{k}" => dhi,
"elo#{k}" => elo,
"ehi#{k}" => ehi,
"mp#{k}" => mp
}
end
def seq(n, c)
l = [n]
while (n != 1 && l.size < c)
n = f2(n)
l << n
end
return l
end
def count(ns)
l = seq(n = ns.to_i(2), 1)
ns2 = l[-1].to_s(2)
nw = ns.length
return {'ns' => ns,
'nw' => nw,
}.merge(features(ns, '')).merge(features(ns2, '2'))
end
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def fmt(a)
['ns', 'l'].each { |x| a.delete(x) }
return a
end
def axes2(x2)
return x2.inject({}) { |h, x| h.merge({x => 'axes x1y2' }) }
end
def x1y2()
return $a2 if (!$a2.nil?)
$a2 = axes2(['nw']) if ($a2.nil?)
return $a2
end
def out(fn, a, d, a2 = {}, t = nil)
if ($f1.nil?) then
$f1 = File.open(fn, 'w')
$f1.puts(a.keys.join("\t"))
$c = 0
end
$f1.puts(a.values.join("\t"))
$f1.flush
$c += 1
return if ($c % 1000 != 0)
d = ($c / 2000).to_i
d = 1 if (d == 0)
f2 = File.open('gnuplot.cmd', 'w')
st = t.nil? ? 'unset title; ' : "set title '#{t}'; "
f2.puts("set ytics nomirror; set y2tics; set colors classic; set key bottom left box opaque; #{st}; plot \\")
(a.keys - ['t']).each \
{
|x|
next if (a2.member?(x) && a2[x].nil?)
f2.puts("'#{fn}' using (column('t')):(column('#{x}')) every #{d} with line title '#{x}' #{a2[x]}, \\")
}
f2.puts
f2.close
end
def rank(x)
return -x['z']
end
def adj(x, wt, km)
z = 0
km.keys.each \
{
|k|
if (x[k].nil?) then
z = nil
break
end
z += (x[k] - wt["#{k}_a"]) / wt["#{k}_sd"] * km[k]
}
x['z'] = z.nil? ? 0.0 : z
raise [x['z'], wt].to_s if (!x['z'].finite?)
return x
end
def stat1(k, l)
a, sd = stat(l)
return {"#{k}_a" => a,
"#{k}_s" => sd}
end
def add(bs, km, h, ns, l1, wt, t, d)
return 0 if ((a1 = count(ns)).nil?)
# a1.merge!(stat1('h', h.keys))
j = ns.length
h[j] = [] if (!h.member?(j))
x = adj(a1, wt, km)
# a, b = [h[j][0], h[j][-1]]
return 0 if (h[j].map { |x| x['ns'] }.member?(x['ns']))
x['t'] = t
h[j] << x
h[j].sort_by! { |x| rank(x) }
h[j][bs..-1] = [] if (h[j].size > bs)
x1 = x.dup
out('out.txt', x1, d, x1y2().merge({'ns' => nil, 'r' => nil, 'z' => nil}))
l1 << x1 if (!l1.nil?)
return 1
end
def dense(w, d)
w2 = w - 1
a = (0...w2).to_a
s = '0' * w2
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' }
return ('1' + s)
end
def initd(w)
return(dense(w, rand))
end
def initb(w)
r = rand
return '1' + (1...w).map { rand < r ? '0' : '1' }.join
end
def initw(w)
s = dense(w, 0.5)
m = rand(w)
i = rand(w - m) + 1
s[i, m] = rand(2).to_s * m
return s
end
def initr(w)
s = '1'
b = 0
m = rand(w - 1) + 1
while (s.length < w)
z = w - s.length
s += b.to_s * [rand(m - 1) + 1, z].min
b ^= 1
end
return s
end
def init(w)
m = rand(w)
ns1 = dense(w, 0.5)
ns2 = initr(w)
ns1, ns2 = [ns2, ns1] if (rand(2) == 0)
i = rand(w - m) + 1
ns1[i, m] = ns2[i, m]
return ns1
end
def range(w)
a = (1..w).to_a
c = (r = (rand(2) == 0)) ? 2 : (rand(a.size) + 1)
l = (1..c).map { a.delete_at(rand(a.size)) }.sort
return r ? (l[0]..l[1]).to_a : l
end
def mutb(ns)
ns2 = ns.dup
range(ns.length - 1).each \
{
|i|
ns2[i, 1] = (ns[i, 1].to_i ^ 1).to_s
}
return ns2
end
def remix(ns)
ns2 = ns.dup
l1 = range(ns.length - 1)
l2 = l1.shuffle
l1.size.times \
{
|i|
ns2[l2[i], 1] = ns[l1[i], 1]
}
return ns2
end
def inner(ns1, ns2)
a, b = [ns1, ns2].sort_by { |x| x.length }
d = b.length - a.length
i = rand(d + 1)
c = b.dup
c[i, a.length] = a
ns1.replace(b)
ns2.replace(c)
end
def mix(ns1, ns2)
ns1 = ns1.dup
ns2 = ns2.dup
inner(ns1, ns2)
return (0...ns1.length).map { |x| [ns1, ns2][rand(2)][x, 1] }.join
end
def cut(ns1, ns2)
ns1 = ns1.dup
ns2 = ns2.dup
inner(ns1, ns2)
i = rand(ns1.length)
return ns1[0...i] + ns2[i..-1]
end
def ext(ns)
ns = ns.dup
(rand(3) + 1).times { ns[rand(ns.length) + 1, 0] = rand(2).to_s }
return ns
end
def short(ns)
return ns if (ns.length == 1)
ns = ns.dup
(rand([ns.length - 1, 3].min) + 1).times { ns[rand(ns.length - 1) + 1, 1] = '' }
return ns
end
def top(h1)
return h1[rand([h1.size / 4, h1.size].max)]
end
def stat(l)
return 0, 1.0 if (l.empty?)
t = l.inject { |a, x| a + x }
t2 = l.inject(0) { |a, x| a + x ** 2 }
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
raise if (sd.nan?)
return a, sd
end
def near(x, l, k1, l1 = [])
l.each_with_index \
{
|x1, j|
z = 0
['d', 'e', 'dlo', 'dhi', 'elo', 'ehi', 'mp'].each \
{
|k|
k = "#{k}#{k1}"
z += (x1[k] - x[k]) ** 2
}
l1 << [z, j]
}
l1.sort_by! { |x| x[0] }
i = 0
i += 1 while (l1[i][0] == l1[i + 1][0])
return (0..i).map { |x| l[l1[i][1]] }
end
def states(h, l1)
l = h.values.flatten
l.each { |x| x['nn'] = [] }
c = 0
l1.each \
{
|x|
near(x, l, '').each \
{
|x1|
near(x, l, '2').each \
{
|x2|
x1['nn'] |= [x2['ns']]
c += 1
}
}
}
l.each { |x| x['nnc'] = x['nn'].size }
end
def dist(km, h, wt, l1)
states(h, l1)
l = h.values.flatten
km.keys.each \
{
|k|
a, sd = stat(l.map { |x| x[k] })
sd = 1.0 if (sd == 0)
wt.merge!({"#{k}_a" => a, "#{k}_sd" => sd})
}
end
def nonnil(x, y)
return x.nil? ? y : x
end
def opt(km, w1, l1, n)
h = {}
ops = ['init', 'initd', 'initb', 'initw', 'initr',
'mutb1', 'remix1',
'mix2', 'cut2']
# ops += ['ext1', 'short1']
bs = 500
bc = 50
t = c1 = 0
wt = {}
ar = {}
loop \
{
if (c1 % 100 == 0) then
dist(km, h, wt, l1)
h.values.flatten.each { |x| adj(x, wt, km) }
h.each { |k, l| l.sort_by! { |x| x['z'] } }
end
break if (c1 == n)
opc = ops[r = rand(ops.size)]
op, p = opc.start_with?('init') ? [opc, 0] : [opc[0..-2], opc[-1..-1].to_i]
ks = h.keys.sort_by { |x| rank(h[x][0]) }
ks = ks[0...[ks.size, ks.size / 4].max]
case (p)
when 0
w = ks.empty? ? w1 : ks[rand(ks.size)]
ns = send(op, w)
when 1, 2
redo if (h.empty?)
h.delete(ks.pop) if (ks.size > bc)
arg = (1..p).map { ks[rand(ks.size)] }
l = arg.map { |x| top(h[x])['ns'] }
ns = send(op, *l)
ns[0, 1] = '1'
end
raise op if (ns[0, 1] != '1')
d = (t.to_f / (c1 + 1) * n / 2000).to_i
c1 += add(bs, km, h, ns, l1, wt, t, d)
ar[r] = ar.fetch(r, 0) + 1
if (t % 100 == 0) then
msg = {'t' => t,
'c1' => c1,
'f' => (c1.to_f / t * 100).round(2),
'ar' => ar.sort_by { |x| x[0] }.map { |x| x[1] },
'hmn' => ks.min,
'hmx' => ks.max,
'hc' => h.size,
'wt' => wt}
h0 = nonnil(h.fetch(ks[0], [])[0], {}).dup
msg2 = {'h0' => h0}
['h0'].each { |x| fmt(msg2[x]) }
$stderr.puts(msg)
$stderr.puts("\t" + msg2.inspect) if (t % 1000 == 0)
end
t += 1
}
return h
end
def outd(f, l)
f.puts('$dat << eof')
k = l[0].keys
f.puts(k.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.puts('eof')
return k
end
def outa(f, l, a = {}, t = '')
k = outd(f, l)
f.puts("set colors classic; set key top left opaque; set title '#{t}'; ")
f.puts("set ytics nomirror; set y2tics;")
f.puts("plot \\")
ct = ''
k, ct = [k - ['t'], "(column('t')):"] if (k.member?('t'))
k.each \
{
|x|
next if (a.member?(x) && a[x].nil?)
opt = a.fetch(x, '')
opt += ' lw 2 ' if (!opt.include?('lw'))
opt += ' with line ' if (!opt.include?('with') && !opt.include?('pt'))
f.puts("'$dat' using #{ct}(column('#{x}')) #{opt} title '#{x}',\\")
}
f.puts
# f.puts("reset; pause -1;")
end
def outafn(l, fn = nil, t = '', a = {})
fn = 'gnuplot.cmd' if (fn.nil?)
outa(f = File.open(fn, 'w'), l, a, t)
f.close
$stderr.puts([fn, l.size, t].inspect)
end
def fmt1(l, f)
w = l.max_by { |x| x.length }.length
f.puts("set colors classic; set ytics nomirror; set y2tics; set key top right opaque;\\")
f.puts("plot \\")
f.puts("[0:#{l.size - 1}][0:#{w}] '-' matrix using 2:1:3 with image title '', \\")
f.puts("'-' using (column('d')) with line axes x1y2 dt 2 lw 3, \\")
f.puts("'-' using 1 with line title 'e' axes x1y2 dt 2 lw 3")
l1 = []
l.each_with_index \
{
|z, i|
l1 << [d(z), e(z)]
c = w - z.length
z[z.length...w] = '0' * c
f.puts(z.split('').join(' '))
}
f.puts("eof")
f.puts("eof")
f.puts("d")
l1.each { |x| f.puts(x[0]) }
f.puts("eof")
l1.each { |x| f.puts(x[1]) }
f.puts("eof")
end
def grid(l, fn, t = '')
f = File.open(fn, 'w')
f.puts("set palette negative grayscale; unset colorbox; set title '#{t}';")
fmt1(l.map { |x| x.reverse }, f)
f.close
end
c = 4e3.to_i
w = 50
a = {'nnc' => -1}
h = opt(a, w, l1 = [], c.to_i)
l = h.values.flatten
l.each \
{
|x|
x.delete('nn')
}
l.sort_by! { |x| x['t'] }
outafn(l, nil, '', {'ns' => nil, 'nw' => nil})
grid(l.map { |x| x['ns'] }, 'gnuplot1.cmd')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.