File size: 5,354 Bytes
848d4b7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | #!/usr/bin/env python3
"""
Validator for problem keich_thin_triangles_128
We fix:
N = 128
delta = 1/128
slopes a_i = i/128, i=0..127
A solution provides intercepts b_i defining lines y = a_i x + b_i on x in [0,1].
Each defines a thin triangle R_delta(l_i) whose vertical cross-section at x is
[a_i x + b_i - delta*(1-x), a_i x + b_i]
We score by Area(E) where E = union_i R_delta(l_i).
Baseline: Keich's Theorem 1 construction for N=2^n slopes, instantiated at n=7,
gives intercepts via the formula
b(l_k) = sum_{i=1}^{n} ((1-i)/n) * eps_i(k) * 2^{-i}
where eps_i(k) are the binary digits of k/2^n. The exact area of that construction
is 191403/1605632 ≈ 0.11920726542570154.
See: https://www.cs.cornell.edu/~keich/papers/Kakeya.pdf (Theorem 1, property (i)).
"""
import argparse
import math
from typing import Any, List, Tuple
from . import ValidationResult, load_solution, output_result, success, failure
N = 128
DELTA = 1.0 / 128.0
BASELINE = 191403.0 / 1605632.0 # exact Keich n=7 area
def _union_length(intervals: List[Tuple[float, float]]) -> float:
"""Compute length of union of closed intervals [l,r] with l<=r."""
if not intervals:
return 0.0
intervals.sort(key=lambda t: t[0])
total = 0.0
cur_l, cur_r = intervals[0]
for l, r in intervals[1:]:
if l > cur_r:
total += (cur_r - cur_l)
cur_l, cur_r = l, r
else:
if r > cur_r:
cur_r = r
total += (cur_r - cur_l)
return total
def _union_length_at(bs: List[float], x: float) -> float:
"""Compute union length of thin-triangle cross-sections at position x."""
one_minus_x = 1.0 - x
intervals = []
for i, b in enumerate(bs):
a = i / 128.0
top = a * x + b
bot = top - DELTA * one_minus_x
intervals.append((bot, top))
return _union_length(intervals)
def _exact_area_from_intercepts(bs: List[float]) -> float:
"""
Compute exact area of union of thin triangles via piecewise-linear integration.
Each line i defines an interval at position x:
top_i(x) = a_i * x + b_i
bot_i(x) = (a_i + delta) * x + (b_i - delta)
All 256 endpoint functions are linear in x. The union length is piecewise-
linear, changing slope only at x-values where two endpoint functions cross.
Between crossings, the trapezoid rule is exact.
"""
n = len(bs)
delta = 1.0 / n
# Build linear functions: f(x) = slope * x + const
# top_i(x) = a_i * x + b_i
# bot_i(x) = (a_i + delta) * x + (b_i - delta)
slopes = []
consts = []
for i in range(n):
a_i = i / n
slopes.append(a_i)
consts.append(bs[i])
slopes.append(a_i + delta)
consts.append(bs[i] - delta)
# Find all crossings in (0, 1)
crossings = [0.0, 1.0]
nf = len(slopes)
for j in range(nf):
for k in range(j + 1, nf):
ds = slopes[j] - slopes[k]
if abs(ds) < 1e-15:
continue
x_cross = (consts[k] - consts[j]) / ds
if 0.0 < x_cross < 1.0:
crossings.append(x_cross)
crossings.sort()
# Remove near-duplicates
unique = [crossings[0]]
for x in crossings[1:]:
if x - unique[-1] > 1e-14:
unique.append(x)
crossings = unique
# Integrate using trapezoid rule (exact for piecewise-linear)
area = 0.0
prev_x = crossings[0]
prev_len = _union_length_at(bs, prev_x)
for x in crossings[1:]:
cur_len = _union_length_at(bs, x)
area += 0.5 * (prev_len + cur_len) * (x - prev_x)
prev_x = x
prev_len = cur_len
return area
def validate(solution: Any) -> ValidationResult:
try:
if not isinstance(solution, dict):
return failure("Invalid format: expected dict")
if "intercepts" not in solution:
return failure("Missing key 'intercepts'")
bs = solution["intercepts"]
if not isinstance(bs, list):
return failure("'intercepts' must be a list")
if len(bs) != N:
return failure(f"Expected {N} intercepts, got {len(bs)}")
# Convert to floats and sanity-check for NaN/inf
bs_f: List[float] = []
for j, v in enumerate(bs):
if not isinstance(v, (int, float)):
return failure(f"Intercept {j} is not a number")
f = float(v)
if not math.isfinite(f):
return failure(f"Intercept {j} is not finite")
bs_f.append(f)
area = _exact_area_from_intercepts(bs_f)
return success(
f"Valid. Union area={area:.15f}, baseline={BASELINE:.15f}.",
area=float(area),
baseline=float(BASELINE),
N=N,
delta=float(DELTA),
)
except Exception as e:
return failure(f"Validation error: {e}")
def main():
parser = argparse.ArgumentParser(description="Validate thin-triangle Kakeya (N=128) construction")
parser.add_argument("solution", help="Solution as JSON string or path to JSON file")
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose output")
args = parser.parse_args()
sol = load_solution(args.solution)
res = validate(sol)
output_result(res)
if __name__ == "__main__":
main()
|