Created
February 1, 2020 18:50
-
-
Save vznvzn/d069b166ff32e52f0dad7f3e44dc48c7 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) | |
return n.odd? ? (n * 3 + 1) / 2 : n / 2 | |
end | |
def seq(n) | |
n1 = n | |
l = [n1] | |
while (n != 1) | |
n = f2(n) | |
l << n | |
end | |
return l | |
end | |
def flip(s, n) | |
s = s.dup | |
s[n, 1] = (s[n, 1].to_i ^ 1).to_s | |
return s | |
end | |
def delete(s, n) | |
s = s.dup | |
s[n, 1] = '' | |
return s | |
end | |
def insert(s, n) | |
s0 = s.dup | |
s0[n, 0] = '0' | |
s1 = s.dup | |
s1[n, 0] = '1' | |
return s0, s1 | |
end | |
def edit1(s) | |
l = [] | |
s.length.times \ | |
{ | |
|n| | |
l << flip(s, n) | |
l << delete(s, n) | |
} | |
(s.length + 1).times \ | |
{ | |
|n| | |
l += insert(s, n) | |
} | |
return l.select { |x| x[0, 1] != '0' } | |
end | |
def sum(l) | |
return l.inject { |t, x| t + x } | |
end | |
def d(s) | |
c = s.split('').select { |x| x == '1' }.size | |
d = c.to_f / s.length | |
return d | |
end | |
def lenx(ns, p) | |
l = ns.split(p) | |
l = [] if (l.nil?) | |
l.shift if (l[0] == '') | |
return l.map { |x| x.length } | |
end | |
def len01x(ns) | |
return lenx(ns, /0+/), lenx(ns, /1+/) | |
end | |
def count(ns) | |
l1 = seq(ns.to_i(2)) | |
cg = ns == '1' ? 0 : l1.index { |x| x < l1[0] } | |
cm = (0..cg).max_by { |x| l1[x] } | |
nw = ns.length | |
mx01 = mx01s = 0 | |
l1[nw..cg].each \ | |
{ | |
|x| | |
m01 = len01x(ns = x.to_s(2)).flatten.max | |
m01s = m01.to_f / ns.length | |
mx01 = [mx01, m01].max | |
mx01s = [mx01s, m01s].max | |
} | |
pmx01 = len01x(l1[nw..cg].map { |x| x % 2 }.join).flatten.max | |
pmx01 = 0 if (pmx01.nil?) | |
a = {'ns' => ns, | |
'mx01s' => mx01s, | |
'mx01' => mx01 * 10, | |
'pmx01' => pmx01 * 10, | |
'c' => l1.size, | |
'cg' => cg, | |
'cm' => cm, | |
'cgnw' => cg - nw, | |
'cgcm' => cg - cm, | |
'cmnw' => cm - nw, | |
'nw' => nw, | |
} | |
return a | |
end | |
def out(a, a2 = {}, t = nil, ct = "(column('t')):", w = ' with line ') | |
fn = $fn.nil? ? 'out.txt' : $fn | |
k = a.keys | |
if ($f1.nil?) then | |
$f1 = File.open(fn, 'w') | |
$f1.puts(k.join("\t")) | |
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 top left box opaque; #{st}; plot \\") | |
(a.keys - ['t']).each \ | |
{ | |
|x| | |
next if (a2.member?(x) && a2[x].nil?) | |
m = a2.fetch(x, '') | |
m += w if (!m.include?('with')) | |
f2.puts("'#{fn}' using #{ct}(column('#{x}')) title '#{x}' #{m}, \\") | |
} | |
f2.puts | |
f2.close | |
end | |
$f1.puts(a.values_at(*k).join("\t")) | |
$f1.flush | |
end | |
def keys(ks2) | |
ksmn = ks2.select { |x| x.end_with?('-') } | |
ksmx = ks2 - ksmn | |
ksmn.each { |x| x.replace(x.chop) } | |
x = {'ks' => ksmn + ksmx, 'ksmx' => ksmx, 'ksmn' => ksmn} | |
$stderr.puts(x.inspect) | |
return x | |
end | |
def order(l, ks2) | |
l2 = (0...l.size).map { {} } | |
$k = keys(ks2) if ($k.nil?) | |
$k['ks'].each \ | |
{ | |
|k| | |
l1 = (0...l.size).map { |x| {k => l[x][k], 'i' => x} } | |
s = $k['ksmx'].member?(k) ? -1 : 1 | |
l1.sort_by! { |x| s * x[k] } | |
(0...l1.size).each { |x| l2[l1[x]['i']][k] = x } | |
} | |
l2.each { |x| x['z'] = x.values.inject(0) { |t, x| t + x } } | |
return (0...l2.size).sort_by { |x| l2[x]['z'] } | |
end | |
def opt(ks) | |
l1 = [count('1')] | |
seen = {'' => nil, '0' => nil, '1' => nil} | |
l = [] | |
c = 5e3.to_i | |
m = 5e3.to_i | |
d = c / 2000 | |
d = 1 if ($d == 0) | |
k = 'ba' | |
c.times \ | |
{ | |
|t| | |
$stderr.puts(t) if (t % 100 == 0) | |
l2 = l1.map { |x| x[k] } | |
l2 = order(l1, ks) | |
i = l2[rand([l2.size, 50].min)] | |
l3 = l2[m..-1] | |
l3 = [] if (l3.nil?) | |
x = nil | |
(l3 | [i]).sort.reverse.each \ | |
{ | |
|x1| | |
x2 = l1.delete_at(x1) | |
x = x2 if (x1 == i) | |
} | |
l << x | |
out(x.merge({'t' => t}), | |
{'ns' => nil, | |
'mx01s' => 'axes x1y2'}) if (t % d == 0) | |
edit1(x['ns']).each \ | |
{ | |
|ns| | |
next if (seen.member?(ns)) | |
l1 << count(ns) | |
seen[ns] = nil | |
} | |
} | |
end | |
#ks = ['nw', 'cm', 'cg', 'c', 'cmnw', 'cgnw', 'cgcm', 'mx01', 'mx01s', 'pmx01'] | |
ks = ['nw', 'cgnw'] | |
opt(ks) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment