shinka-backup / ccevolve /tx_workspace /ac1_ours /encode_template.py
JustinTX's picture
Add files using upload-large-folder tool
2facf1f verified
Raw
History Blame Contribute Delete
1.99 kB
import numpy as np
import zlib, base64
from scipy.signal import fftconvolve
def actual_obj_np(a):
a = np.maximum(a, 0)
n = len(a)
conv = np.convolve(a, a)
s = np.sum(a)
if s < 0.01: return float('inf')
return 2.0 * n * np.max(conv) / s**2
# Load the best n=2000 template
a = np.load('/workspace/best_n2000_v2.npy')
print(f"n=2000 obj (fftconvolve-based): {actual_obj_np(a):.6f}")
# Verify with evaluator's method
a_list = [float(max(0.0, min(1000.0, x))) for x in a.tolist()]
n = len(a_list)
conv = np.convolve(a_list, a_list)
max_b = float(np.max(conv))
sum_a = float(np.sum(a_list))
obj_eval = float(2.0 * n * max_b / (sum_a**2))
print(f"n=2000 obj (evaluator method): {obj_eval:.6f}")
# Encode as float32
a32 = a.astype(np.float32)
compressed = zlib.compress(a32.tobytes(), 9)
encoded = base64.b64encode(compressed).decode()
print(f"Base64 length: {len(encoded)}")
# Verify decode
decoded = base64.b64decode(encoded)
decompressed = zlib.decompress(decoded)
a_decoded = np.frombuffer(decompressed, dtype=np.float32).astype(np.float64)
print(f"Decode obj: {actual_obj_np(a_decoded):.6f}")
# Also try the n=1000 template
a1000 = np.load('/workspace/best_n1000_v2.npy')
a1000_32 = a1000.astype(np.float32)
compressed1000 = zlib.compress(a1000_32.tobytes(), 9)
encoded1000 = base64.b64encode(compressed1000).decode()
print(f"\nn=1000 obj: {actual_obj_np(a1000):.6f}, base64_len: {len(encoded1000)}")
# Try n=3000 and n=5000
for fn in ['best_n3000.npy', 'best_n5000.npy']:
try:
a_x = np.load(f'/workspace/{fn}')
a_x_32 = a_x.astype(np.float32)
cx = zlib.compress(a_x_32.tobytes(), 9)
ex = base64.b64encode(cx).decode()
print(f"{fn}: obj={actual_obj_np(a_x):.6f}, base64_len: {len(ex)}")
except FileNotFoundError:
print(f"{fn}: not found")
# Write the encoded template to a file
with open('/workspace/template_encoded.txt', 'w') as f:
f.write(encoded)
print(f"\nTemplate saved to template_encoded.txt")