| @@ -0,0 +1,63 @@ |
| 1 | + |
| 2 | +"""Own bounded n13 search: soft minimum continuation then signed-area SLSQP. |
| 3 | +Requires numpy/scipy. Start file is credited in README. Wall budget checked in callbacks. |
| 4 | +""" |
| 5 | +import argparse,itertools,json,time,platform |
| 6 | +from pathlib import Path |
| 7 | +import numpy as np |
| 8 | +import scipy |
| 9 | +from scipy.optimize import minimize |
| 10 | +from scipy.special import logsumexp |
| 11 | +p=argparse.ArgumentParser();p.add_argument('--seconds',type=float,default=240);p.add_argument('--seed',type=int,default=20261121);a=p.parse_args() |
| 12 | +root=Path(__file__).resolve().parent |
| 13 | +rng=np.random.default_rng(a.seed);start=time.monotonic();end=start+a.seconds |
| 14 | +tri=np.array(list(itertools.combinations(range(13),3)));ii,jj,kk=tri.T |
| 15 | +history=[];best=np.loadtxt(root/'start.dat');bestval=0.;attempt=0 |
| 16 | +class Budget(Exception):pass |
| 17 | +def areas_grad(x): |
| 18 | + q=np.asarray(x).reshape(13,2);u=q[jj]-q[ii];v=q[kk]-q[ii] |
| 19 | + ar=(u[:,0]*v[:,1]-u[:,1]*v[:,0])/2 |
| 20 | + g=np.zeros((len(tri),13,2));z=np.arange(len(tri)) |
| 21 | + g[z,ii,0]=(q[jj,1]-q[kk,1])/2;g[z,ii,1]=(q[kk,0]-q[jj,0])/2 |
| 22 | + g[z,jj,0]=v[:,1]/2;g[z,jj,1]=-v[:,0]/2 |
| 23 | + g[z,kk,0]=-u[:,1]/2;g[z,kk,1]=u[:,0]/2 |
| 24 | + return ar,g.reshape(len(tri),26) |
| 25 | +def keep(x,kind): |
| 26 | + global best,bestval |
| 27 | + q=np.clip(np.asarray(x[:26]).reshape(13,2),0,1);val=float(np.min(np.abs(areas_grad(q)[0]))) |
| 28 | + if val>bestval: |
| 29 | + best=q.copy();bestval=val;np.savetxt(root/'n13.txt',best,fmt='%.17g') |
| 30 | + print(f'progress: {min(99,int(100*(time.monotonic()-start)/a.seconds))}% attempt={attempt} area={val:.12f} {kind}',flush=True) |
| 31 | + return val |
| 32 | +def callback(x): |
| 33 | + keep(x,'iterate') |
| 34 | + if time.monotonic()>end:raise Budget() |
| 35 | +def polish(q): |
| 36 | + ar,g=areas_grad(q);sg=np.where(ar>=0,1.,-1.) |
| 37 | + x=np.r_[q.ravel(),np.min(abs(ar))] |
| 38 | + def con(x):return sg*areas_grad(x[:26])[0]-x[-1] |
| 39 | + def jac(x):return np.c_[sg[:,None]*areas_grad(x[:26])[1],-np.ones(len(tri))] |
| 40 | + res=minimize(lambda x:-x[-1],x,jac=lambda x:np.r_[np.zeros(26),-1.],bounds=[(0,1)]*26+[(0,.05)],constraints={'type':'ineq','fun':con,'jac':jac},method='SLSQP',callback=callback,options={'ftol':1e-12,'maxiter':160}) |
| 41 | + return res.x[:26].reshape(13,2),res.success |
| 42 | +keep(best.ravel(),'source');source=bestval |
| 43 | +try: |
| 44 | + while time.monotonic()<end: |
| 45 | + attempt+=1 |
| 46 | + if attempt==1:q=best.copy();kind='baseline-polish' |
| 47 | + elif attempt%7==0:q=rng.random((13,2));kind='random' |
| 48 | + else: |
| 49 | + q=best.copy();count=int(rng.integers(1,5));ids=rng.choice(13,count,replace=False) |
| 50 | + scale=[.008,.025,.06,.12,.25][attempt%5];q[ids]+=rng.normal(0,scale,(count,2));q=np.clip(q,0,1);kind='perturb' |
| 51 | + if attempt>1: |
| 52 | + for tau in [.003,.0005,.00008]: |
| 53 | + def fun(x): |
| 54 | + ar,g=areas_grad(x);b=-abs(ar)/tau;l=logsumexp(b);w=np.exp(b-l) |
| 55 | + return tau*l,-(w*np.sign(ar))@g |
| 56 | + r=minimize(fun,q.ravel(),jac=True,bounds=[(0,1)]*26,method='L-BFGS-B',callback=callback,options={'maxiter':65,'ftol':1e-12}) |
| 57 | + q=r.x.reshape(13,2) |
| 58 | + q,success=polish(q);v=keep(q.ravel(),'polish') |
| 59 | + history.append({'attempt':attempt,'kind':kind,'value':v,'success':bool(success),'elapsed':time.monotonic()-start}) |
| 60 | +except Budget:pass |
| 61 | +elapsed=time.monotonic()-start |
| 62 | +(root/'search-report.json').write_text(json.dumps({'seed':a.seed,'budget_seconds':a.seconds,'elapsed_seconds':elapsed,'attempts_started':attempt,'source_value':source,'best_float_value':bestval,'reference_value':.0270199166,'ratio':bestval/.0270199166,'numpy':np.__version__,'scipy':scipy.__version__,'python':platform.python_version(),'history':history},indent=2)+'\n') |
| 63 | +print(f'progress: 100% complete area={bestval:.12f} attempts={attempt} elapsed={elapsed:.3f}',flush=True) |