#!/usr/bin/env ruby # frozen_string_literal: true # Robustness sweep: for each decade 10^e, query the first and last count of # the radius of 10^e and RANDOM_PER_DECADE random counts in [10^e, 10^(e+1)). # Each k is solved twice: normally (with polygon + Pick check) and with the # certified windows inflated (--inflate 2, perimeter only). The perimeters # must agree; otherwise the certificate would be unsound. # # spread.rb FROM_EXP TO_EXP [RANDOM_PER_DECADE] [SEED] BIN = File.expand_path('../target/release/kgrid', __dir__) def radius_of(k) = (Math.sqrt(k / Math::PI) + 0.5).floor def radius_range(m) lo = (Math::PI * (m - 0.5)**2).floor - 2 lo += 1 while radius_of(lo) < m hi = (Math::PI * (m + 0.5)**2).ceil + 2 hi -= 1 while radius_of(hi) > m [lo, hi] end def field(out, name) out.lines.find { |l| l.start_with?(name) }&.split(':', 2)&.last&.strip end def query(k, *flags) out = IO.popen([BIN, 'query', k.to_s, *flags], err: %i[child out], &:read) [out, $?.success?] end def solve(k) out, ok = query(k, '--poly-file', File::NULL) infl, ok2 = query(k, '--no-poly', '--inflate', '2') p1 = field(out, 'perimeter').to_f p2 = field(infl, 'perimeter').to_f same = (p1 - p2).abs <= 1e-13 * (1 + p1.abs) time = field(out, 'time') [k, radius_of(k), format('%.6f', p1), field(out, 'certified')[/Delta\* = ([\d.]+)/, 1], field(out, 'check')&.split&.first, time[/[\d.]+ s/], time[/RSS [\d.]+ MB/], ok && ok2 && same ? 'same' : 'DIFF'] end from_exp, to_exp, nrand, seed = ARGV abort('usage: spread.rb FROM_EXP TO_EXP [RANDOM_PER_DECADE] [SEED]') unless from_exp && to_exp rng = Random.new(Integer(seed || 1)) puts 'k,m,perimeter,delta_star,check,time,peak_rss,inflated' (Integer(from_exp)..Integer(to_exp)).each do |e| ks = radius_range(radius_of(10**e)) Integer(nrand || 3).times { ks << rng.rand(10**e...10**(e + 1)) } ks.each do |k| puts solve(k).join(',') $stdout.flush end end