# The floor's LP, level by level, in parallel chunks: python3 sweep.py 4 8 48 out.pkl 0.02,0.1,... (needs scipy);
# merge the pickles, then python3 to_json.py merged.pkl certs.json.
import sys, numpy as np, time, pickle
from solve_lp import build, solve
wn,Ll,M=int(sys.argv[1]),int(sys.argv[2]),int(sys.argv[3]); out=sys.argv[4]
B=build(wn,Ll,M)
fs=np.array([float(v) for v in sys.argv[5].split(',')])
certs=[]
for f in fs:
    t=time.time(); lb,res=solve(f,B,verbose=True); x=res.x
    certs.append(dict(f=f,lb=lb,x=x))
    print(f'f {f:.3f} LB {lb:.6f} {time.time()-t:.0f}s',flush=True)
    pickle.dump(dict(certs=certs,wn=wn,Ll=Ll,M=M,S=B['S'],pairs=B['pairs'],lag=B['lag'],mult=B['mult'],layout=dict(iw=B['iw'],irho=B['irho'],is0=B['is0'],ib=B['ib'],nS=B['nS'],P=B['P'],nq=B['nq'])),open(out,'wb'))
