File size: 1,398 Bytes
81dfd31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from written_maze import *
import struct, re
from pathlib import Path
load_checkpoint(Path(__file__).with_name('checkpoint.json'))
rng=np.random.default_rng(583812)
fixtures=[maze(15,rng) for _ in range(32)]+[maze(31,rng) for _ in range(16)]
for _ in range(8):
 g=rng.random((100,100))>.30;g[0,:]=g[:,-1]=1;fixtures.append((g,(0,0),(99,99)))
fixtures.extend([(np.eye(13,dtype=bool),(0,0),(12,12)),(np.ones((4,6),dtype=bool),(1,2),(1,2)),(np.ones((31,31),dtype=bool),(0,0),(30,30)),(np.zeros((3,3),dtype=bool),(0,0),(2,2)),maze(101,rng)])
t0=t1=0.
with open('cases.bin','wb') as f:
 f.write(struct.pack('<i',len(fixtures)))
 for g,a,b in fixtures:
  t=time.perf_counter();d=bfs(g,a,b);t0+=time.perf_counter()-t
  t=time.perf_counter();mask,k=solve(g,a,b);t1+=time.perf_counter()-t
  if d is None:
   assert mask is None
   mask=np.zeros_like(g)
  else: assert bfs(mask,a,b)==d and int(mask.sum())==d+1 and (mask<=g).all() and k==2*d
  h,w=g.shape
  f.write(struct.pack('<6i',h,w,a[0]*w+a[1],b[0]*w+b[1],k,int(d is not None)))
  f.write(g.astype(np.uint8).tobytes()); f.write(mask.astype(np.uint8).tobytes())
with open('gates.h','w',newline='\n') as f:
 for out,(threshold,edges) in G.items():
  f.write(re.sub(r'\bnew\b','new_seen','const int '+out+' = (0 '+''.join((' + ' if w>0 else ' - ')+k for k,w in edges)+') >= '+str(threshold)+';\n'))
print(f'CPU_BFS_SEC={t0:.6f} CPU_NETWORK_SEC={t1:.6f}')