Skip to content

Instantly share code, notes, and snippets.

@vznvzn

vznvzn/outline8.rb

Created Oct 29, 2020
Embed
What would you like to do?
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def seq(n, c)
l = [n]
while (n != 1 && l.size < c)
n = f2(n)
l << n
end
raise if (l.size != c)
return l
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 sum(l)
return l.inject { |t, x| t + x }
end
def avg(l)
return (l.nil? || l.empty?) ? 0 : sum(l).to_f / l.size
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 e(ns)
return 0 if (ns.empty?)
return len01(ns).flatten.size.to_f / ns.length
end
def d(s)
return 0 if (s.empty?)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def runavg(l, k, c)
l1 = l.map { |x| x[k] }
t = sum(l1[0...c])
l2 = (['-'] * (c - 1)) + [t.to_f / c]
while (l1.size > c)
t -= l1.shift
t += l1[c - 1]
l2 << (t.to_f / c)
end
return l2
end
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 features(ns)
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" => d,
"e" => e,
"ea" => (0.5 - e).abs,
"da" => (0.5 -d).abs,
"dlo" => dlo,
"dhi" => dhi,
"elo" => elo,
"ehi" => ehi,
"mp" => mp,
"nw" => ns.length
}
end
def data(l)
l2 = l.map { |x| features(x.to_s(2)) }
a = {}
l2[0].keys.each \
{
|k|
l1 = runavg(l2, k, l2.size - 1)
a[k] = l1[-2].round(3)
a["#{k}2"] = l1[-1].round(3)
}
a['nw21'] = (a['nw2'].to_f / a['nw']).round(3)
return a
end
def near(x, l, k2 = '', l1 = [])
l.each_with_index \
{
|x1, j|
next if (x == x1)
# next if (x1.member?('x'))
z = 0
['d', 'e', 'da', 'ea', 'dlo', 'dhi', 'elo', 'ehi', 'mp'].each \
{
|k1|
k = "#{k1}#{k2}"
z += (x[k] - x1[k1]) ** 2
}
l1 << [z, j]
}
l1.sort_by! { |x| x[0] }
i = 0
i += 1 while (i + 1 < l1.size && l1[i][0] == l1[i + 1][0])
return (0..i).map { |x| l[l1[x][1]].merge({'z' => l1[x][0]}) }
end
def predict(l2, l1)
e1 = 0
l2.each { |x| x['nn'] = []; x['nn2'] = []; }
l2.each \
{
|x|
l = near(x, l1, '2')
j = x['j']
et = 0
l.each \
{
|x1|
e = (x1['nw21'] - x['nw21']).abs
x['nn'] << {'j' => x1['j'], 'z' => x1['z'], 'er' => e}
x1['nn2'] << {'j' => j, 'z' => x1['z'], 'er' => e}
et += e
}
x['er1'] = et / l.size
e1 += x['er1']
}
ea = e1 / l2.size
return ea
end
def dataset(l1, c1)
w = 200
c2 = 10
a = 0.0
b = 1.0
c1.times \
{
|j|
d = a + (b - a) * rand()
ns = dense(w, d)
n = ns.to_i(2)
l = seq(n, c2)
a1 = data(l).merge({'j' => j})
l1 << a1
}
end
c1 = 50
dataset(l1 = [], c1)
predict(l1, l1)
puts("digraph {")
l1.each \
{
|x|
raise if (x['nn'].size != 1)
j1 = x['j']
puts("#{l1[j1]['d']} [label=\"[#{j1}] #{l1[j1]['d']}\\n(#{(l1[j1]['nw21'] - 1.0).round(3)})\"]")
j2 = x['nn'][0]['j']
puts("\t" + [x['d'].inspect, l1[j2]['d'].inspect].join(" -> "))
}
puts("}")
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.