def adjust(ap_subsim, ap_subtraj, traj_c, traj_q, opt): if opt == 'PSS': if ap_subtraj[1] == len(traj_c) - 1: _, each_step_f_q = submit(m0, traj_q) Q = each_step_f_q[0,-1,:] suffix_h, _ = submit(m0, traj_c[ap_subtraj[0]:]) return np.linalg.norm(Q - suffix_h[-1,0,:]), ap_subtraj return ap_subsim, ap_subtraj
def step(self, episode, action, index, label='E'): if action == 0: #non-split #state transfer self.h0, _ = submit(m0, self.cand_train_data[episode][index:index + 1], self.h0) self.presim = np.linalg.norm(self.query_state_data[-1] - self.h0[-1]) # self.sufsim = np.linalg.norm(self.query_state_back_data[0, -1] - self.cand_state_back_data[0, self.length - index - 2]) #print(self.sufsim) observation = np.array([self.subsim, self.presim]).reshape(1,-1)# # if self.sufsim < self.subsim: # self.subsim = self.sufsim # self.subtraj = [index + 1, self.length - 1] if self.presim < self.subsim: self.subsim = self.presim self.subtraj = [self.split_point, index] if label == 'T': last_subsim = self.subsim_real #if self.subtraj[1] == self.length - 1 and self.subtraj[0] < self.length: #reward may record error self.subsim_real = min(last_subsim, self.presim)#, np.linalg.norm(self.query_state_data[-1] - self.cand_state_forw_data[-1, self.subtraj[0]]) # else: # self.subsim_real = self.subsim #print('0 Rw change') self.RW = last_subsim - self.subsim_real return observation, self.RW #return np.concatenate((self.query_state_data[-1].numpy().reshape(1, -1), self.h0[-1].numpy().reshape(1, -1), self.cand_state_back_data[0, self.length - index - 2].numpy().reshape(1, -1)), 1), self.RW if action == 1: #split self.split_point = index self.h0 = None self.h0, _ = submit(m0, self.cand_train_data[episode][index:index + 1], self.h0) #state transfer self.presim = np.linalg.norm(self.query_state_data[-1] - self.h0[-1]) # self.sufsim = np.linalg.norm(self.query_state_back_data[0, -1] - self.cand_state_back_data[0, self.length - index - 2]) #print(self.sufsim) observation = np.array([self.subsim, self.presim]).reshape(1,-1) # if self.sufsim < self.subsim: # self.subsim = self.sufsim # self.subtraj = [index + 1, self.length - 1] if self.presim < self.subsim: self.subsim = self.presim self.subtraj = [self.split_point, index] if label == 'T': last_subsim = self.subsim_real #if self.subtraj[1] == self.length - 1 and self.subtraj[0] < self.length: #reward may record error self.subsim_real = min(last_subsim, self.presim)#, np.linalg.norm(self.query_state_data[-1] - self.cand_state_forw_data[-1, self.subtraj[0]]) #print('1 Rw change') # else: # self.subsim_real = self.subsim self.RW = last_subsim - self.subsim_real return observation, self.RW
def ExactS(traj_c, traj_q): _, each_step_f_q = submit(m0, traj_q) Q = each_step_f_q[0,-1,:] subsim = 999999 subtraj = [0, len(traj_c)-1] subset = {} for i in range(len(traj_c)): _, each_step_f_c = submit(m0, traj_c[i:]) for j in range(each_step_f_c.size(1)): #print('sub-range:', [i, i+j]) temp = np.linalg.norm(Q - each_step_f_c[0,j,:]) subset[(i, i+j)] = temp if temp < subsim: subsim = temp subtraj = [i, i+j] return subsim, subtraj, subset
def SizeS(traj_c, traj_q, par=5): L = len(traj_q) L_lo = min(len(traj_c), int((L - par))) L_up = min(len(traj_c), int((L + par))) #print('Lower upper', [L_lo, L_up]) _, each_step_f_q = submit(m0, traj_q) Q = each_step_f_q[0,-1,:] subsim = 999999 subtraj = [0, len(traj_c)-1] for i in range(len(traj_c)): _, each_step_f_c = submit(m0, traj_c[i:i+L_up]) for j in range(each_step_f_c.size(1)): #print('sub-range:', [i, i+j]) if (j + 1) < L_lo: continue temp = np.linalg.norm(Q - each_step_f_c[0,j,:]) if temp < subsim: subsim = temp subtraj = [i, i+j] return subsim, subtraj
def reset(self, episode, label='E'): # prefix_state --> [split_point, index] # suffix_state --> [index + 1, len - 1] # return observation self.query_state_data, _ = submit(m0, self.query_train_data[episode]) #_, self.query_state_back_data = submit(m0, self.query_train_data[episode][::-1]) #_, self.cand_state_back_data = submit(m0, self.cand_train_data[episode][::-1]) self.split_point = 0 self.h0 = None self.h0, _ = submit(m0, self.cand_train_data[episode][0:1], self.h0) self.length = len(self.cand_train_data[episode]) #whole = np.linalg.norm(self.query_state_back_data[0, -1] - self.cand_state_back_data[0, -1]) self.presim = np.linalg.norm(self.query_state_data[-1] - self.h0[-1]) #self.sufsim = np.linalg.norm(self.query_state_back_data[0, -1] - self.cand_state_back_data[0, self.length - 2]) observation = np.array([self.presim, self.presim]).reshape(1,-1) self.subsim = min(self.presim, self.presim) #print('episode', episode, whole, self.presim, self.sufsim) # if self.subsim == whole: # self.subtraj = [0, self.length - 1] if self.subsim == self.presim: self.subtraj = [0, 0] # if self.subsim == self.sufsim: # self.subtraj = [1, self.length - 1] if label == 'T': #t = generate_suffix(self.cand_train_data[episode]) #self.cand_state_forw_data, _ = submit(m0, t) #whole_real = np.linalg.norm(self.query_state_data[-1] - self.cand_state_forw_data[-1, 0]) #suffix_real = np.linalg.norm(self.query_state_data[-1] - self.cand_state_forw_data[-1, 1]) self.subsim_real = min(self.presim, self.presim)#, suffix_real return observation, self.length
def reset(self, episode, label='E'): # prefix_state --> [split_point, index] # suffix_state --> [index + 1, len - 1] # return observation self.query_state_data, _ = submit(m0, self.query_train_data[episode]) self.split_point = 0 self.h0 = None self.h0, _ = submit(m0, self.cand_train_data[episode][0:1], self.h0) self.length = len(self.cand_train_data[episode]) self.presim = np.linalg.norm(self.query_state_data[-1] - self.h0[-1]) observation = np.array([self.presim, self.presim]).reshape(1, -1) self.subsim = min(self.presim, self.presim) if self.subsim == self.presim: self.subtraj = [0, 0] if label == 'T': self.subsim_real = min(self.presim, self.presim) self.h0_real = self.h0 return observation, self.length, -1
def output(self, index, episode, label='E'): # if self.subtraj[1] == self.length - 1 and index == self.length - 1: # suffix_h, _ = submit(m0, self.cand_train_data[episode][self.subtraj[0]:]) # self.subsim = np.linalg.norm(self.query_state_data[-1] - suffix_h[-1,0,:]) if label == 'T': print('check', self.subsim, self.subtraj, self.subsim_real) if label == 'E': hidden, _ = submit( m0, self.cand_train_data[episode][self.subtraj[0]:self.subtraj[1] + 1], None) self.subsim_real = np.linalg.norm(self.query_state_data[-1] - hidden[-1]) return [self.subsim_real, self.subtraj]
def heuristic(traj_c, traj_q, opt, delay_K=5): delay = 0 _, each_step_f_q = submit(m0, traj_q) Q = each_step_f_q[0,-1,:] invQ = -1 each_step_b_c = -1 if opt == 'PSS': _, each_step_b_q = submit(m1, traj_q[::-1]) invQ = each_step_b_q[0,-1,:] _, each_step_b_c = submit(m1, traj_c[::-1]) subsim = 999999 subtraj = [0, len(traj_c)-1] split_point = 0 h0 = None pos_d_coll = [] pos_d_f = False temp = 'non' if opt != 'POS-D': for i in range(len(traj_c)): #submit prefix h0, _ = submit(m0, traj_c[i:i+1], h0) presim = np.linalg.norm(Q - h0[-1,0,:]) sufsim = heuristic_suffix_opt(invQ, Q, traj_c, i+1, opt, each_step_b_c) #print('-> maintain:', subtraj, subsim) #print('prefix:', [split_point, i], presim) #print('suffix:', [i+1, len(traj_c)-1], sufsim)S if presim < subsim or sufsim < subsim: temp = i + 1 subsim = min(presim, sufsim) if presim < sufsim: subtraj = [split_point, (temp-1)] else: subtraj = [temp, len(traj_c)-1] split_point = temp h0 = None else: i = -1 while True: i = i + 1 if temp != 'non': i = temp temp = 'non' if i == len(traj_c) - 1: break #submit prefix h0, _ = submit(m0, traj_c[i:i+1], h0) presim = np.linalg.norm(Q - h0[-1,0,:]) if pos_d_f == False and presim < subsim: #open delay pos_d_f = True if pos_d_f == True and delay < delay_K: delay = delay + 1 pos_d_coll.append((presim, i)) continue if pos_d_f == True and delay == delay_K: sort = sorted(pos_d_coll, key=lambda d: d[0]) temp = sort[0][1] + 1 subsim = sort[0][0] subtraj = [split_point, (temp-1)] split_point = temp h0 = None delay = 0 pos_d_f = False pos_d_coll = [] if subsim == 999999: #for extreme cases if pos_d_coll == []: h0, _ = submit(m0, traj_c[i:i+1], h0) presim = np.linalg.norm(Q - h0[-1,0,:]) pos_d_coll.append((presim, i)) sort = sorted(pos_d_coll, key=lambda d: d[0]) temp = sort[0][1] + 1 subsim = sort[0][0] subtraj = [split_point, (temp-1)] return subsim, subtraj
def step(self, episode, action, index, label='E'): if action == 0: #non-split if index == self.length - 1: done = True else: done = False #state transfer self.h0, _ = submit(m0, self.cand_train_data[episode][index:index + 1], self.h0) self.presim = np.linalg.norm(self.query_state_data[-1] - self.h0[-1]) observation = np.array([self.subsim, self.presim]).reshape(1, -1) if self.presim < self.subsim: self.subsim = self.presim self.subtraj = [self.split_point, index] if label == 'T': self.h0_real, _ = submit( m0, self.cand_train_data[episode][index:index + 1], self.h0_real) self.presim_real = np.linalg.norm(self.query_state_data[-1] - self.h0_real[-1]) last_subsim = self.subsim_real self.subsim_real = min(last_subsim, self.presim_real) self.RW = last_subsim - self.subsim_real return observation, self.RW, done, -1 if action == 1: #split if index == self.length - 1: done = True else: done = False self.split_point = index self.h0 = None self.h0, _ = submit(m0, self.cand_train_data[episode][index:index + 1], self.h0) #state transfer self.presim = np.linalg.norm(self.query_state_data[-1] - self.h0[-1]) observation = np.array([self.subsim, self.presim]).reshape(1, -1) if self.presim < self.subsim: self.subsim = self.presim self.subtraj = [self.split_point, index] if label == 'T': self.h0_real = None self.h0_real, _ = submit( m0, self.cand_train_data[episode][index:index + 1], self.h0_real) self.presim_real = np.linalg.norm(self.query_state_data[-1] - self.h0_real[-1]) last_subsim = self.subsim_real self.subsim_real = min(last_subsim, self.presim_real) self.RW = last_subsim - self.subsim_real return observation, self.RW, done, -1 if action > 1: #skipping #state transfer INX = min(index + action - 1, self.length - 1) if INX == self.length - 1: done = True else: done = False self.h0, _ = submit(m0, self.cand_train_data[episode][INX:INX + 1], self.h0) self.presim = np.linalg.norm(self.query_state_data[-1] - self.h0[-1]) observation = np.array([self.subsim, self.presim]).reshape(1, -1) if self.presim < self.subsim: self.subsim = self.presim self.subtraj = [self.split_point, INX] if label == 'T': self.h0_real, _ = submit( m0, self.cand_train_data[episode][index:INX + 1], self.h0_real) self.presim_real = np.linalg.norm(self.query_state_data[-1] - self.h0_real[-1]) last_subsim = self.subsim_real self.subsim_real = min(last_subsim, self.presim_real) self.RW = last_subsim - self.subsim_real return observation, self.RW, done, INX