def minimize(self): """ Función para minimizar un AFD """ from minimize import Minimize return Minimize(self)()
def work(self): self.env = gym_tetris.make('TetrisA-v0') self.env = JoypadSpace(self.env, SIMPLE_MOVEMENT) self.env.reset() action = np.argmax(self.env.action_space.sample()) ob, _, _, _ = self.env.step(int(action)) inx = 10 iny = 20 done = False # net = neat.nn.FeedForwardNetwork.create(self.genome, self.config) net = neat.nn.recurrent.RecurrentNetwork.create( self.genome, self.config) fitness = 0 xpos = 0 xpos_max = 16 counter = 0 max_score = 0 moving = 0 frames = 0 while not done: scaledimg = cv2.cvtColor(ob, cv2.COLOR_BGR2RGB) ob = Minimize(ob) ob = cv2.resize(ob, (10, 20)) cv2.imshow('humanwin', scaledimg) cv2.waitKey(1) imgarray = np.ndarray.flatten(ob) actions = net.activate(imgarray) action = np.argmax(actions) ob, rew, done, info = self.env.step(int(action)) frames += 1 if frames == 1200: fitness += 1 frames = 0 print( f"genome:{self.genome.key} Fitnes: {fitness} lines: {info['number_of_lines']}" ) return int(fitness)
def analysis(xf, xf_, y, rmat, rinv, htype, gtol=1e-6, method="LBFGS", maxiter=None, disp=False, save_hist=False, save_dh=False, infl=False, loc=False, tlm=False, infl_parm=1.0, model="z08", icycle=100): global zetak zetak = [] op = htype["operator"] pt = htype["perturbation"] ga = htype["gamma"] nmem = xf.shape[1] dxf = (xf - xf_[:, None]) / np.sqrt(nmem - 1) #condh = np.zeros(2) eps = 1e-4 # Bundle parameter if infl: """ if op == "linear" or op == "test": if pt == "mlef": alpha = 1.2 else: alpha = 1.2 elif op == "quadratic" or op == "quadratic-nodiff": if pt == "mlef": alpha = 1.3 else: alpha = 1.35 elif op == "cubic" or op == "cubic-nodiff": if pt == "mlef": alpha = 1.6 else: alpha = 1.65 """ alpha = infl_parm logger.info("==inflation==, alpha={}".format(alpha)) # print("==inflation==, alpha={}".format(alpha)) dxf *= alpha # logger.debug("norm(pf)={}".format(la.norm(pf))) if loc: l_sig = 4.0 logger.info("==localization==, l_sig={}".format(l_sig)) # print("==localization==, l_sig={}".format(l_sig)) dxf = pfloc(dxf, l_sig, save_dh, model, op, pt, icycle) emat = xf_[:, None] + eps * dxf hemat = obs.h_operator(emat, op, ga) dy = (hemat - np.mean(hemat, axis=1)[:, None]) / eps #dh = obs.dhdx(xf_, op, ga) @ dxf * np.sqrt(nmem-1) if save_dh: #np.save("{}_dh_{}_{}_cycle{}.npy".format(model, op, pt, icycle), dh) np.save("{}_dy_{}_{}_cycle{}.npy".format(model, op, pt, icycle), dy) ob = y - np.mean(obs.h_operator(xf, op, ga), axis=1) np.save("{}_d_{}_{}_cycle{}.npy".format(model, op, pt, icycle), ob) logger.info("save_dh={}".format(save_dh)) # print("save_dh={}".format(save_dh)) zmat = rmat @ dy # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, tinv, heinv, condh = precondition(zmat) #if icycle == 0: # print("tmat={}".format(tmat)) # print("tinv={}".format(tinv)) # logger.debug("pf.shape={}".format(pf.shape)) # logger.debug("tmat.shape={}".format(tmat.shape)) # logger.debug("heinv.shape={}".format(heinv.shape)) #gmat = dxf @ tmat #gmat = np.sqrt(nmem-1) * pf @ tmat # logger.debug("gmat.shape={}".format(gmat.shape)) w0 = np.zeros(xf.shape[1]) args_j = (xf_, dxf, y, precondition, eps, rmat, htype) iprint = np.zeros(2, dtype=np.int32) minimize = Minimize(w0.size, 7, calc_j, calc_grad_j, args_j, iprint, method) logger.info("save_hist={}".format(save_hist)) # print("save_hist={} cycle{}".format(save_hist, icycle)) cg = spo.check_grad(calc_j, calc_grad_j, w0, *args_j) logger.info("check_grad={}".format(cg)) # print("check_grad={}".format(cg)) if save_hist: w = minimize(w0, callback=callback) logger.debug(zetak) # print(len(zetak)) jh = np.zeros(len(zetak)) gh = np.zeros(len(zetak)) for i in range(len(zetak)): jh[i] = calc_j(np.array(zetak[i]), *args_j) g = calc_grad_j(np.array(zetak[i]), *args_j) gh[i] = np.sqrt(g.transpose() @ g) np.savetxt("{}_jh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), jh) np.savetxt("{}_gh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), gh) if model == "z08": xmax = max(np.abs(np.min(w)), np.max(w)) logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) xmax = np.ceil(xmax * 0.001) * 1000 logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) #cost_j(xmax, xf.shape[1], model, w, icycle, *args_j) costJ.cost_j2d(xmax, xf.shape[1], model, w, icycle, htype, calc_j, calc_grad_j, *args_j) elif model == "l96": cost_j(200, xf.shape[1], model, w, icycle, *args_j) else: w = minimize(w0) xa_ = xf_ + dxf @ w emat = xa_[:, None] + eps * dxf hemat = obs.h_operator(emat, op, ga) dy = (hemat - np.mean(hemat, axis=1)[:, None]) / eps zmat = rmat @ dy # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, tinv, heinv, dum = precondition(zmat) dxa = dxf @ tmat xa = xa_[:, None] + dxa * np.sqrt(nmem - 1) pa = dxa @ dxa.T if save_dh: np.save("{}_pa_{}_{}_cycle{}.npy".format(model, op, pt, icycle), pa) ua = np.zeros((xa_.size, nmem + 1)) ua[:, 0] = xa_ ua[:, 1:] = xa np.save("{}_ua_{}_{}_cycle{}.npy".format(model, op, pt, icycle), ua) #print("xa={}".format(xa)) d = y - np.mean(obs.h_operator(xa, op, ga), axis=1) chi2 = chi2_test(zmat, heinv, rmat, d) ds = dof(zmat) logger.info("DOF = {}".format(ds)) return xa, xa_, pa, chi2, ds, condh
def analysis(xf, xc, y, rmat, rinv, htype, gtol=1e-6, method="LBFGS", cgtype=None, maxiter=None, disp=False, save_hist=False, save_dh=False, infl=False, loc=False, infl_parm=1.0, model="z08", icycle=100): global zetak zetak = [] op = htype["operator"] pt = htype["perturbation"] ga = htype["gamma"] nmem = xf.shape[1] pf = xf - xc[:, None] # pf = (xf - xc[:, None]) / np.sqrt(nmem) #condh = np.zeros(2) if infl: """ if op == "linear" or op == "test": if pt == "mlef": alpha = 1.2 else: alpha = 1.2 elif op == "quadratic" or op == "quadratic-nodiff": if pt == "mlef": alpha = 1.3 else: alpha = 1.35 elif op == "cubic" or op == "cubic-nodiff": if pt == "mlef": alpha = 1.6 else: alpha = 1.65 """ alpha = infl_parm logger.info("==inflation==, alpha={}".format(alpha)) #print("==inflation==, alpha={}".format(alpha)) pf *= alpha # logger.debug("norm(pf)={}".format(la.norm(pf))) if loc: l_sig = 4.0 logger.info("==localization==, l_sig={}".format(l_sig)) #print("==localization==, l_sig={}".format(l_sig)) pf = pfloc(pf, l_sig, save_dh, model, op, pt, icycle) if pt == "grad": logger.debug("dhdx={}".format(obs.dhdx(xc, op, ga))) dh = obs.dhdx(xc, op, ga) @ pf else: dh = obs.h_operator(xf, op, ga) - obs.h_operator(xc, op, ga)[:, None] if save_dh: #np.save("{}_dh_{}_{}_cycle{}.npy".format(model, op, pt, icycle), dh) np.save("{}_dy_{}_{}_cycle{}.npy".format(model, op, pt, icycle), dh) ob = y - obs.h_operator(xc, op, ga) np.save("{}_d_{}_{}_cycle{}.npy".format(model, op, pt, icycle), ob) logger.info("save_dh={}".format(save_dh)) # print("save_dh={}".format(save_dh)) zmat = rmat @ dh # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, heinv, condh = precondition(zmat) #if icycle == 0: # print("tmat={}".format(tmat)) # print("heinv={}".format(heinv)) # logger.debug("pf.shape={}".format(pf.shape)) # logger.debug("tmat.shape={}".format(tmat.shape)) # logger.debug("heinv.shape={}".format(heinv.shape)) gmat = pf @ tmat #gmat = np.sqrt(nmem-1) * pf @ tmat # logger.debug("gmat.shape={}".format(gmat.shape)) x0 = np.zeros(xf.shape[1]) args_j = (xc, pf, y, tmat, gmat, heinv, rinv, htype) iprint = np.zeros(2, dtype=np.int32) iprint[0] = 1 minimize = Minimize(x0.size, calc_j, jac=calc_grad_j, args=args_j, iprint=iprint, method=method, cgtype=cgtype) logger.info("save_hist={}".format(save_hist)) # print("save_hist={} cycle{}".format(save_hist, icycle)) cg = spo.check_grad(calc_j, calc_grad_j, x0, *args_j) logger.info("check_grad={}".format(cg)) if save_hist: x = minimize(x0, callback=callback) logger.debug(zetak) # print(len(zetak)) jh = np.zeros(len(zetak)) gh = np.zeros(len(zetak)) for i in range(len(zetak)): jh[i] = calc_j(np.array(zetak[i]), *args_j) g = calc_grad_j(np.array(zetak[i]), *args_j) gh[i] = np.sqrt(g.transpose() @ g) np.savetxt("{}_jh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), jh) np.savetxt("{}_gh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), gh) if model == "z08": if len(zetak) > 0: xmax = max(np.abs(np.min(zetak)), np.max(zetak)) else: xmax = max(np.abs(np.min(x)), np.max(x)) logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) #if xmax < 1000: #cost_j(1000, xf.shape[1], model, x, icycle, *args_j) #cost_j2d(1000, xf.shape[1], model, x, icycle, *args_j) # costJ.cost_j2d(1000, xf.shape[1], model, x, icycle, # htype, calc_j, calc_grad_j, *args_j) #else: #xmax = int(xmax*0.01+1)*100 xmax = np.ceil(xmax * 0.01) * 100 logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) #cost_j(xmax, xf.shape[1], model, x, icycle, *args_j) #cost_j2d(xmax, xf.shape[1], model, x, icycle, *args_j) costJ.cost_j2d(xmax, xf.shape[1], model, x, icycle, htype, calc_j, calc_grad_j, *args_j) elif model == "l96": cost_j(200, xf.shape[1], model, x, icycle, *args_j) else: x = minimize(x0) xa = xc + gmat @ x if save_dh: np.save("{}_dx_{}_{}_cycle{}.npy".format(model, op, pt, icycle), gmat @ x) if pt == "grad": dh = obs.dhdx(xa, op, ga) @ pf else: dh = obs.h_operator(xa[:, None] + pf, op, ga) - obs.h_operator( xa, op, ga)[:, None] zmat = rmat @ dh # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, heinv, dum = precondition(zmat) pa = pf @ tmat # pa *= np.sqrt(nmem) if save_dh: np.save("{}_pa_{}_{}_cycle{}.npy".format(model, op, pt, icycle), pa) ua = np.zeros((xa.size, nmem + 1)) ua[:, 0] = xa ua[:, 1:] = xa[:, None] + pa np.save("{}_ua_{}_{}_cycle{}.npy".format(model, op, pt, icycle), ua) #print("xa={}".format(xa)) d = y - obs.h_operator(xa, op, ga) chi2 = chi2_test(zmat, heinv, rmat, d) ds = dof(zmat) logger.info("DOF = {}".format(ds)) return xa, pa, chi2, ds, condh
def analysis(xf, xc, y, ytype, rmat, rinv, htype, gtol=1e-6, method="CG", cgtype=None, maxiter=None, restart=True, maxrest=20, disp=False, save_hist=False, save_dh=False, infl=False, loc = False, infl_parm=1.0, tlm=False, l_jhi=False, model="z08", icycle=0): global zetak, alphak zetak = [] alphak = [] #op = htype["operator"] pt = htype["perturbation"] pf = xf - xc[:, None] nmem = pf.shape[1] # logger.debug("norm(pf)={}".format(la.norm(pf))) if infl: logger.info("==inflation==, alpha={}".format(infl_parm)) pf *= infl_parm #if pt == "grad": dh = np.zeros((y.size,pf.shape[1])) if tlm: # logger.debug("dhdx.shape={}".format(obs.dhdx(xc, op).shape)) for i in range(len(ytype)): op = ytype[i] logger.debug(op) if not l_jhi: logger.debug("linearized about control") dh[i,:] = obs.dhdx(xc, op) @ pf else: logger.debug("linearized about each ensemble member") for j in range(nmem): jhi = obs.dhdx(xf[:,j], op) dh[i,j] = jhi @ pf[:, j] #dh = obs.dhdx(xc, op) @ pf else: for i in range(len(ytype)): op = ytype[i] logger.debug(op) dh[i,:] = obs.h_operator(xf, op) - obs.h_operator(xc, op)[:, None] #dh = obs.h_operator(xf, op) - obs.h_operator(xc, op)[:, None] zmat = rmat @ dh # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, heinv = precondition(zmat) # logger.debug("pf.shape={}".format(pf.shape)) # logger.debug("tmat.shape={}".format(tmat.shape)) # logger.debug("heinv.shape={}".format(heinv.shape)) gmat = pf @ tmat # logger.debug("gmat.shape={}".format(gmat.shape)) x0 = np.zeros(xf.shape[1]) x = x0 args_j = (xc, pf, y, ytype, tmat, gmat, heinv, rinv, tlm, l_jhi) iprint = np.zeros(2, dtype=np.int32) irest = 0 # restart counter flg = -1 # optimization result flag #if icycle != 0: # logger.info("calculate gradient") minimize = Minimize(x0.size, calc_j, jac=calc_grad_j, hess=calc_hess, args=args_j, iprint=iprint, method=method, cgtype=cgtype, maxiter=maxiter, restart=restart) #else: # logger.info("finite difference approximation") # minimize = Minimize(x0.size, calc_j, jac=None, hess=None, # args=args_j, iprint=iprint, method=Method, cgtype=cgtype, # maxiter=maxiter) logger.info("save_hist={}".format(save_hist)) # print("save_hist={}".format(save_hist)) cg = spo.check_grad(calc_j, calc_grad_j, x0, *args_j) logger.info("check_grad={}".format(cg)) if restart: if save_hist: jh = [] gh = [] while irest < maxrest: zetak = [] xold = x if save_hist: x, flg = minimize(x0, callback=callback) else: x, flg = minimize(x0) irest += 1 if save_hist: for i in range(len(zetak)): jh.append(calc_j(np.array(zetak[i]), *args_j)) g = calc_grad_j(np.array(zetak[i]), *args_j) gh.append(np.sqrt(g.transpose() @ g)) if flg == 0: logger.info(f"Converged at {irest}th restart") break xup = x - xold #logger.debug(f"update : {xup}") if np.sqrt(np.dot(xup,xup)) < 1e-10: logger.info(f"Stagnation at {irest}th : solution not updated") break xa = xc + gmat @ x xc = xa if tlm: #if pt == "grad": for i in range(len(ytype)): op = ytype[i] logger.debug(op) if not l_jhi: logger.debug("linearized about control") dh[i,:] = obs.dhdx(xc, op) @ pf else: logger.debug("linearized about each ensemble member") for j in range(nmem): jhi = obs.dhdx(xf[:,j], op) dh[i,j] = jhi @ pf[:, j] #dh = obs.dhdx(xa, op) @ pf else: for i in range(len(ytype)): op = ytype[i] logger.debug(op) dh[i,:] = obs.h_operator(xc[:, None] + pf, op) - obs.h_operator(xc, op)[:, None] zmat = rmat @ dh tmat, heinv = precondition(zmat) #pa = pf @ tmat #pf = pa gmat = pf @ tmat args_j = (xc, pf, y, ytype, tmat, gmat, heinv, rinv, tlm, l_jhi) x0 = np.zeros(xf.shape[1]) #reload(minimize) minimize = Minimize(x0.size, calc_j, jac=calc_grad_j, hess=calc_hess, args=args_j, iprint=iprint, method=method, cgtype=cgtype, maxiter=maxiter, restart=restart) else: #x, flg = minimize_cg(calc_j, x0, args=args_j, jac=calc_grad_j, # calc_step=calc_step, callback=callback) #x, flg = minimize_bfgs(calc_j, x0, args=args_j, jac=calc_grad_j, # calc_step=calc_step, callback=callback) if save_hist: x, flg = minimize(x0, callback=callback) jh = np.zeros(len(zetak)) gh = np.zeros(len(zetak)) for i in range(len(zetak)): jh[i] = calc_j(np.array(zetak[i]), *args_j) g = calc_grad_j(np.array(zetak[i]), *args_j) gh[i] = np.sqrt(g.transpose() @ g) logger.debug(f"zetak={len(zetak)} alpha={len(alphak)}") np.savetxt("{}_jh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), jh) np.savetxt("{}_gh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), gh) np.savetxt("{}_alpha_{}_{}_cycle{}.txt".format(model, op, pt, icycle), alphak) else: x, flg = minimize(x0) xa = xc + gmat @ x if tlm: #if pt == "grad": for i in range(len(ytype)): op = ytype[i] logger.debug(op) if not l_jhi: logger.debug("linearized about control") dh[i,:] = obs.dhdx(xa, op) @ pf else: logger.debug("linearized about each ensemble member") for j in range(nmem): jhi = obs.dhdx(xa+pf[:,j], op) dh[i,j] = jhi @ pf[:, j] #dh = obs.dhdx(xa, op) @ pf else: for i in range(len(ytype)): op = ytype[i] logger.debug(op) dh[i,:] = obs.h_operator(xa[:, None] + pf, op) - obs.h_operator(xa, op)[:, None] zmat = rmat @ dh # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, heinv = precondition(zmat)#, plot=True) d = np.zeros_like(y) for i in range(len(ytype)): op = ytype[i] d[i] = y[i] - obs.h_operator(xa, op) chi2 = chi2_test(zmat, heinv, rmat, d) pa = pf @ tmat return xa, pa, chi2
def analysis(xf, binv, y, ytype, rinv, htype, gtol=1e-6, method="LBFGS", cgtype=None, maxiter=None, restart=False, maxrest=20, disp=False, save_hist=False, save_dh=False, model="model", icycle=0): global zetak, alphak zetak = [] alphak = [] op = htype["operator"] pt = htype["perturbation"] ga = htype["gamma"] JH = np.zeros((y.size, xf.size)) ob = np.zeros_like(y) for i in range(len(ytype)): op = ytype[i] JH[i, :] = obs.dhdx(xf, op, ga) ob[i] = y[i] - obs.h_operator(xf, op, ga) nobs = ob.size x0 = np.zeros_like(xf) args_j = (binv, JH, rinv, ob) iprint = np.zeros(2, dtype=np.int32) iprint[0] = 1 minimize = Minimize(x0.size, calc_j, jac=calc_grad_j, hess=calc_hess, args=args_j, iprint=iprint, method=method, cgtype=cgtype, maxiter=maxiter, restart=restart) cg = spo.check_grad(calc_j, calc_grad_j, x0, *args_j) logger.info("check_grad={}".format(cg)) if save_hist: x, flg = minimize(x0, callback=callback) jh = np.zeros(len(zetak)) gh = np.zeros(len(zetak)) for i in range(len(zetak)): jh[i] = calc_j(np.array(zetak[i]), *args_j) g = calc_grad_j(np.array(zetak[i]), *args_j) gh[i] = np.sqrt(g.transpose() @ g) np.savetxt("{}_jh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), jh) np.savetxt("{}_gh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), gh) else: x, flg = minimize(x0) xa = xf + x fun = calc_j(x, *args_j) chi2 = fun / nobs return xa, chi2
def analysis(xf, xc, y, rmat, rinv, htype, gtol=1e-6, method="CG", cgtype=None, maxiter=None, restart=True, maxrest=20, disp=False, save_hist=False, save_dh=False, infl=False, loc = False, infl_parm=1.0, model="z08", icycle=0): global zetak, alphak zetak = [] alphak = [] op = htype["operator"] pt = htype["perturbation"] pf = xf - xc[:, None] # logger.debug("norm(pf)={}".format(la.norm(pf))) if infl: logger.info("==inflation==, alpha={}".format(infl_parm)) pf *= infl_parm #if pt == "grad": if pt == "grad05": # logger.debug("dhdx.shape={}".format(obs.dhdx(xc, op).shape)) dh = obs.dhdx(xc, op) @ pf else: dh = obs.h_operator(xf, op) - obs.h_operator(xc, op)[:, None] if save_dh: np.save("{}_dxf_{}_{}_cycle{}.npy".format(model, op, pt, icycle), pf) np.save("{}_dh_{}_{}_cycle{}.npy".format(model, op, pt, icycle), dh) ob = y - obs.h_operator(xc, op) np.save("{}_d_{}_{}_cycle{}.npy".format(model, op, pt, icycle), ob) logger.info("save_dh={}".format(save_dh)) # print("save_dh={}".format(save_dh)) zmat = rmat @ dh # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, heinv = precondition(zmat) # logger.debug("pf.shape={}".format(pf.shape)) # logger.debug("tmat.shape={}".format(tmat.shape)) # logger.debug("heinv.shape={}".format(heinv.shape)) gmat = pf @ tmat # logger.debug("gmat.shape={}".format(gmat.shape)) if save_dh: np.save("{}_tmat_{}_{}_cycle{}.npy".format(model, op, pt, icycle), tmat) np.save("{}_pf_{}_{}_cycle{}.npy".format(model, op, pt, icycle), pf) np.save("{}_gmat_{}_{}_cycle{}.npy".format(model, op, pt, icycle), gmat) x0 = np.zeros(xf.shape[1]) x = x0 htype_c = htype.copy() Method = method #if icycle == 0: # htype_c["perturbation"] = "grad05" # Method="dogleg" logger.debug(f"original {htype}") logger.debug(f"copy {htype_c}") args_j = (xc, pf, y, tmat, gmat, heinv, rinv, htype_c) iprint = np.zeros(2, dtype=np.int32) irest = 0 # restart counter flg = -1 # optimization result flag #if icycle != 0: # logger.info("calculate gradient") minimize = Minimize(x0.size, calc_j, jac=calc_grad_j, hess=calc_hess, args=args_j, iprint=iprint, method=Method, cgtype=cgtype, maxiter=maxiter, restart=restart) #else: # logger.info("finite difference approximation") # minimize = Minimize(x0.size, calc_j, jac=None, hess=None, # args=args_j, iprint=iprint, method=Method, cgtype=cgtype, # maxiter=maxiter) logger.info("save_hist={}".format(save_hist)) # print("save_hist={}".format(save_hist)) cg = spo.check_grad(calc_j, calc_grad_j, x0, *args_j) logger.info("check_grad={}".format(cg)) if restart: if save_hist: jh = [] gh = [] while irest < maxrest: zetak = [] xold = x if save_hist: x, flg = minimize(x0, callback=callback) else: x, flg = minimize(x0) irest += 1 if save_hist: for i in range(len(zetak)): jh.append(calc_j(np.array(zetak[i]), *args_j)) g = calc_grad_j(np.array(zetak[i]), *args_j) gh.append(np.sqrt(g.transpose() @ g)) if flg == 0: logger.info(f"Converged at {irest}th restart") break xup = x - xold #logger.debug(f"update : {xup}") if np.sqrt(np.dot(xup,xup)) < 1e-10: logger.info(f"Stagnation at {irest}th : solution not updated") break xa = xc + gmat @ x xc = xa if pt == "grad05": dh = obs.dhdx(xc, op) @ pf else: dh = obs.h_operator(xc[:, None] + pf, op) - obs.h_operator(xc, op)[:, None] zmat = rmat @ dh tmat, heinv = precondition(zmat) #pa = pf @ tmat #pf = pa gmat = pf @ tmat args_j = (xc, pf, y, tmat, gmat, heinv, rinv, htype_c) x0 = np.zeros(xf.shape[1]) #reload(minimize) minimize = Minimize(x0.size, calc_j, jac=calc_grad_j, hess=calc_hess, args=args_j, iprint=iprint, method=Method, cgtype=cgtype, maxiter=maxiter, restart=restart) if save_hist: logger.debug(f"zetak={len(zetak)} alpha={len(alphak)}") np.savetxt("{}_jh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), jh) np.savetxt("{}_gh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), gh) np.savetxt("{}_alpha_{}_{}_cycle{}.txt".format(model, op, pt, icycle), alphak) if model=="z08": if len(zetak) > 0: xmax = max(np.abs(np.min(zetak)),np.max(zetak)) else: xmax = max(np.abs(np.min(x)),np.max(x)) logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) #if xmax < 1000: #cost_j(1000, xf.shape[1], model, x, icycle, *args_j) #cost_j2d(1000, xf.shape[1], model, x, icycle, *args_j) # costJ.cost_j2d(1000, xf.shape[1], model, x, icycle, # htype, calc_j, calc_grad_j, *args_j) #else: #xmax = int(xmax*0.01+1)*100 xmax = np.ceil(xmax*0.01)*100 logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) #cost_j(xmax, xf.shape[1], model, x, icycle, *args_j) #cost_j2d(xmax, xf.shape[1], model, x, icycle, *args_j) costJ.cost_j2d(xmax, xf.shape[1], model, np.array(zetak), icycle, htype, calc_j, calc_grad_j, *args_j) elif model=="l96": cost_j(200, xf.shape[1], model, x, icycle, *args_j) else: #x, flg = minimize_cg(calc_j, x0, args=args_j, jac=calc_grad_j, # calc_step=calc_step, callback=callback) #x, flg = minimize_bfgs(calc_j, x0, args=args_j, jac=calc_grad_j, # calc_step=calc_step, callback=callback) if save_hist: x, flg = minimize(x0, callback=callback) jh = np.zeros(len(zetak)) gh = np.zeros(len(zetak)) for i in range(len(zetak)): jh[i] = calc_j(np.array(zetak[i]), *args_j) g = calc_grad_j(np.array(zetak[i]), *args_j) gh[i] = np.sqrt(g.transpose() @ g) logger.debug(f"zetak={len(zetak)} alpha={len(alphak)}") np.savetxt("{}_jh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), jh) np.savetxt("{}_gh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), gh) np.savetxt("{}_alpha_{}_{}_cycle{}.txt".format(model, op, pt, icycle), alphak) if model=="z08": if len(zetak) > 0: xmax = max(np.abs(np.min(zetak)),np.max(zetak)) else: xmax = max(np.abs(np.min(x)),np.max(x)) logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) #if xmax < 1000: #cost_j(1000, xf.shape[1], model, x, icycle, *args_j) #cost_j2d(1000, xf.shape[1], model, x, icycle, *args_j) # costJ.cost_j2d(1000, xf.shape[1], model, x, icycle, # htype, calc_j, calc_grad_j, *args_j) #else: #xmax = int(xmax*0.01+1)*100 xmax = np.ceil(xmax*0.01)*100 logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) #cost_j(xmax, xf.shape[1], model, x, icycle, *args_j) #cost_j2d(xmax, xf.shape[1], model, x, icycle, *args_j) costJ.cost_j2d(xmax, xf.shape[1], model, np.array(zetak), icycle, htype, calc_j, calc_grad_j, *args_j) elif model=="l96": cost_j(200, xf.shape[1], model, x, icycle, *args_j) else: x, flg = minimize(x0) xa = xc + gmat @ x if pt == "grad05": #if pt == "grad": dh = obs.dhdx(xa, op) @ pf else: dh = obs.h_operator(xa[:, None] + pf, op) - obs.h_operator(xa, op)[:, None] zmat = rmat @ dh # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, heinv = precondition(zmat) d = y - obs.h_operator(xa, op) chi2 = chi2_test(zmat, heinv, rmat, d) pa = pf @ tmat if save_dh: logger.info("save ua") ua = np.zeros((xa.size,pf.shape[1]+1)) ua[:,0] = xa ua[:,1:] = xa[:, None] + pa np.save("{}_ua_{}_{}_cycle{}.npy".format(model, op, pt, icycle), ua) return xa, pa, chi2
def analysis(xf, xc, y, rmat, rinv, htype, gtol=1e-6, method="CG", cgtype=None, maxiter=None, restart=True, maxrest=20, disp=False, save_hist=False, save_dh=False, infl=False, loc=False, infl_parm=1.0, model="z08", icycle=0): global wk, alphak wk = [] alphak = [] op = htype["operator"] pt = htype["perturbation"] nmem = xf.shape[1] pf = xf - xc[:, None] #pf = (xf - xc[:, None]) / np.sqrt(nmem) # logger.debug("norm(pf)={}".format(la.norm(pf))) if infl: logger.info("==inflation==, alpha={}".format(infl_parm)) pf *= infl_parm if pt == "gradw": # logger.debug("dhdx.shape={}".format(obs.dhdx(xc, op).shape)) dh = obs.dhdx(xc, op) @ pf else: dh = obs.h_operator(xf, op) - obs.h_operator(xc, op)[:, None] if save_dh: np.save("{}_dh_{}_{}.npy".format(model, op, pt), dh) #logger.info("save_dh={}".format(save_dh)) zmat = rmat @ dh # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, heinv, prec = precondition(zmat) # print("save_dh={}".format(save_dh)) x0 = np.zeros(xf.shape[1]) args_j = (xc, pf, y, rmat, htype) iprint = np.zeros(2, dtype=np.int32) minimize = Minimize(x0.size, calc_j, jac=calc_grad_j, hess=calc_hess, prec=prec, args=args_j, iprint=iprint, method=method, cgtype=cgtype, maxiter=maxiter, restart=restart) logger.info("save_hist={}".format(save_hist)) cg = spo.check_grad(calc_j, calc_grad_j, x0, *args_j) logger.info("check_grad={}".format(cg)) # print("save_hist={}".format(save_hist)) if save_hist: #g = calc_grad_j(x0, *args_j) #print("g={}".format(g)) x, flg = minimize(x0, callback=callback) #x = minimize_newton(calc_j, x0, args=args_j, jac=calc_grad_j, hess=calc_hess, # callback=callback, maxiter=maxiter, disp=disp) logger.debug(f"wk={len(wk)} alpha={len(alphak)}") jh = np.zeros(len(wk)) gh = np.zeros(len(wk)) for i in range(len(wk)): jh[i] = calc_j(np.array(wk[i]), *args_j) g = calc_grad_j(np.array(wk[i]), *args_j) gh[i] = np.sqrt(g.transpose() @ g) np.savetxt("{}_jh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), jh) np.savetxt("{}_gh_{}_{}_cycle{}.txt".format(model, op, pt, icycle), gh) np.savetxt("{}_alpha_{}_{}_cycle{}.txt".format(model, op, pt, icycle), alphak) if model == "z08": xmax = max(np.abs(np.min(x)), np.max(x)) logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) xmax = np.ceil(xmax * 0.01) * 100 logger.debug("resx max={}".format(xmax)) # print("resx max={}".format(xmax)) #cost_j(xmax, xf.shape[1], model, x, icycle, *args_j) #cost_j2d(xmax, xf.shape[1], model, x, icycle, *args_j) costJ.cost_j2d(xmax, xf.shape[1], model, x, icycle, htype, calc_j, calc_grad_j, *args_j) elif model == "l96": cost_j(200, xf.shape[1], model, x, icycle, *args_j) else: x, flg = minimize(x0) #x = minimize_newton(calc_j, x0, args=args_j, jac=calc_grad_j, hess=calc_hess, # maxiter=maxiter, disp=disp) xa = xc + pf @ x if pt == "gradw": dh = obs.dhdx(xa, op) @ pf else: dh = obs.h_operator(xa[:, None] + pf, op) - obs.h_operator(xa, op)[:, None] zmat = rmat @ dh # logger.debug("cond(zmat)={}".format(la.cond(zmat))) tmat, heinv, prec = precondition(zmat) d = y - obs.h_operator(xa, op) chi2 = chi2_test(zmat, heinv, rmat, d) pa = pf @ tmat #* np.sqrt(nmem) return xa, pa, chi2
# get size of state and action from environment state_input_size = 200 # adjusted computer input size number_of_actions = env.action_space.n agent = Agent(state_input_size, number_of_actions) st = Statistics([], [], [0] * 10, [], 0, [], [], []) Statistics.save_data([], filename='meanIterTimes') init_csv() for e in range(1, EPISODES): st.t() # Statistics Time done = False score = 0 state = env.reset() state = Minimize(state) state = np.ndarray.flatten(state) sum_splits = 0 counter = 1 mean_splits = [] DATA_SAVE = 0 board_height = 0 cleared_lines = 0 tet_stats = {'T': 0, 'J': 0, 'Z': 0, 'O': 0, 'S': 1, 'L': 0, 'I': 0} r = random.randrange(1, 10000) frames = 0 action = agent.get_action(state) last_sum_states = [0, 0, 0] last_new_block = 0 while not done: