示例#1
0
def ExactS(traj_c, traj_q):
    subsim = 999999
    subtraj = [0, len(traj_c) - 1]
    subset = {}
    N = len(traj_c)
    for i in range(N):
        DIS = Distance(len(traj_c[i:]), len(traj_q))
        for j in range(i, N):
            temp = DIS.DTW(traj_c[i:j + 1], traj_q)
            #temp = similaritymeasures.dtw(traj_c[i:j+1], traj_q)[0]
            #print('sub-range:', [i, j], temp)
            subset[(i, j)] = temp
            if temp < subsim:
                subsim = temp
                subtraj = [i, j]
    return subsim, subtraj, subset
示例#2
0
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)))
    subsim = 999999
    subtraj = [0, len(traj_c) - 1]
    N = len(traj_c)
    for i in range(N):
        DIS = Distance(len(traj_c[i:]), len(traj_q))
        for j in range(i, N):
            if (j - i + 1) < L_lo or (j - i + 1) > L_up:
                continue
            temp = DIS.DTW(traj_c[i:j + 1], traj_q)
            #temp = similaritymeasures.dtw(traj_c[i:j+1], traj_q)[0]
            #print('sub-range:', [i, j], temp)
            if temp < subsim:
                subsim = temp
                subtraj = [i, j]
    return subsim, subtraj
示例#3
0
def heuristic(traj_c, traj_q, opt, delay_K=5):
    delay = 0
    subsim = 999999
    subtraj = [0, len(traj_c) - 1]
    split_point = 0
    DIS = Distance(len(traj_c), len(traj_q))
    DIS_R = Distance(len(traj_c), len(traj_q))
    pos_d_coll = []
    pos_d_f = False
    temp = 'non'
    if opt != 'POS-D':
        for i in range(len(traj_c)):
            #submit prefix
            presim = DIS.DTW(traj_c[split_point:i + 1], traj_q)
            sufsim = heuristic_suffix_opt(traj_c, traj_q, i + 1, opt, DIS_R)
            #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
                DIS = Distance(len(traj_c[i:]), len(traj_q))
    else:
        i = -1
        while True:
            i = i + 1
            if temp != 'non':
                i = temp
                temp = 'non'
            if i == len(traj_c) - 1:
                break
            #submit prefix
            presim = DIS.DTW(traj_c[split_point:i + 1], traj_q)
            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
                DIS = Distance(len(traj_c[sort[0][1]:]), len(traj_q))
                delay = 0
                pos_d_f = False
                pos_d_coll = []
        if subsim == 999999:  #for extreme cases
            if pos_d_coll == []:
                presim = DIS.DTW(traj_c[split_point:i + 1], traj_q)
                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
示例#4
0
class Subtraj():
    def __init__(self, cand_train, query_train):
        self.action_space = ['0', '1']
        self.n_actions = len(self.action_space)
        self.n_features = 3
        self.cand_train_name = cand_train
        self.query_train_name = query_train
        self.presim = 0
        self.sufsim = 0
        self.RW = 0.0
        self._load()

    def _load(self):
        self.cand_train_data = pickle.load(open(self.cand_train_name, 'rb'),
                                           encoding='bytes')
        self.query_train_data = pickle.load(open(self.query_train_name, 'rb'),
                                            encoding='bytes')

    def reset(self, episode):
        # prefix_state --> [split_point, index]
        # suffix_state --> [index + 1, len - 1]
        # return observation
        self.split_point = 0
        self.DIS = Distance(len(self.cand_train_data[episode]),
                            len(self.query_train_data[episode]))
        self.DIS_R = Distance(len(self.cand_train_data[episode]),
                              len(self.query_train_data[episode]))
        self.length = len(self.cand_train_data[episode])

        self.presim = self.DIS.DTW(
            self.cand_train_data[episode][self.split_point:1],
            self.query_train_data[episode])
        self.sufsim = self.DIS_R.DTW(self.cand_train_data[episode][1:][::-1],
                                     self.query_train_data[episode][::-1])
        whole = self.DIS_R.DTW(
            self.cand_train_data[episode][::-1],
            self.query_train_data[episode][::-1]
        )  #self.DIS.DTW(self.cand_train_data[episode], self.query_train_data[episode])
        observation = np.array([whole, self.presim,
                                self.sufsim]).reshape(1, -1)

        self.subsim = min(whole, self.presim, self.sufsim)
        #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]

        return observation, self.length

    def step(self, episode, action, index):
        if action == 0:  #non-split
            #state transfer
            self.presim = self.DIS.DTW(
                self.cand_train_data[episode][self.split_point:(index + 1)],
                self.query_train_data[episode])
            self.sufsim = self.DIS_R.DTW(
                self.cand_train_data[episode][(index + 1):][::-1],
                self.query_train_data[episode][::-1])
            if (index + 1) == self.length:
                self.sufsim = self.presim
            observation = np.array([self.subsim, self.presim,
                                    self.sufsim]).reshape(1, -1)

            last_subsim = self.subsim

            if self.presim < self.subsim:
                self.subsim = self.presim
                self.subtraj = [self.split_point, index]

            if self.sufsim < self.subsim:
                self.subsim = self.sufsim
                self.subtraj = [index + 1, self.length - 1]

            self.RW = last_subsim - self.subsim
            #print('action0', self.RW)
            return observation, self.RW

        if action == 1:  #split
            self.split_point = index
            self.DIS = Distance(
                len(self.cand_train_data[episode][self.split_point:]),
                len(self.query_train_data[episode]))
            #state transfer
            self.presim = self.DIS.DTW(
                self.cand_train_data[episode][self.split_point:(index + 1)],
                self.query_train_data[episode])
            self.sufsim = self.DIS_R.DTW(
                self.cand_train_data[episode][(index + 1):][::-1],
                self.query_train_data[episode][::-1])
            if (index + 1) == self.length:
                self.sufsim = self.presim
            observation = np.array([self.subsim, self.presim,
                                    self.sufsim]).reshape(1, -1)

            last_subsim = self.subsim

            if self.presim < self.subsim:
                self.subsim = self.presim
                self.subtraj = [self.split_point, index]

            if self.sufsim < self.subsim:
                self.subsim = self.sufsim
                self.subtraj = [index + 1, self.length - 1]

            self.RW = last_subsim - self.subsim
            #print('action1', self.RW)
            return observation, self.RW

    def output(self, index, episode):
        #print('check', self.subsim, self.subtraj)
        return [self.subsim, self.subtraj]
示例#5
0
class Subtraj():
    def __init__(self, cand_train, query_train):
        self.action_space = ['0', '1', '2', '3', '4']
        self.n_actions = len(self.action_space)
        self.n_features = 3
        self.cand_train_name = cand_train
        self.query_train_name = query_train
        self.presim = 0
        self.sufsim = 0
        self.RW = 0.0
        self._load()

    def _load(self):
        self.cand_train_data = pickle.load(open(self.cand_train_name, 'rb'),
                                           encoding='bytes')
        self.query_train_data = pickle.load(open(self.query_train_name, 'rb'),
                                            encoding='bytes')

    def reset(self, episode, label='E'):
        # prefix_state --> [split_point, index]
        # suffix_state --> [index + 1, len - 1]
        # return observation
        self.split_point = 0
        self.DIS = Distance(len(self.cand_train_data[episode]),
                            len(self.query_train_data[episode]))
        self.DIS_R = Distance(len(self.cand_train_data[episode]),
                              len(self.query_train_data[episode]))
        self.length = len(self.cand_train_data[episode])
        self.skip = []
        self.presim = self.DIS.DTW(
            self.cand_train_data[episode][self.split_point:1],
            self.query_train_data[episode])
        whole = self.DIS_R.DTW(self.cand_train_data[episode][::-1],
                               self.query_train_data[episode][::-1])
        self.sufsim = self.DIS_R.D[
            self.length - 2,
            -1]  #self.DIS_R.DTW(self.cand_train_data[episode][1:][::-1],self.query_train_data[episode][::-1])
        #print('reset',self.sufsim,self.DIS_R.D[self.length-2,-1])
        #self.DIS.DTW(self.cand_train_data[episode], self.query_train_data[episode])
        observation = np.array([whole, self.presim,
                                self.sufsim]).reshape(1, -1)

        self.subsim = min(whole, self.presim, self.sufsim)

        #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':
            self.REWARD_DIS = Distance(len(self.cand_train_data[episode]),
                                       len(self.query_train_data[episode]))
            self.presim_real = self.REWARD_DIS.DTW(
                self.cand_train_data[episode][self.split_point:1],
                self.query_train_data[episode])
            self.subsim_real = self.subsim
        return observation, self.length, -1

    def step(self, episode, action, index, label='E'):
        if action == 0:  #non-split
            #state transfer
            if index == self.length - 1:
                done = True
            else:
                done = False
            self.presim = self.DIS.DTW(
                self.cand_train_data[episode][self.split_point:(index + 1)],
                self.query_train_data[episode], self.skip)
            self.sufsim = self.DIS_R.D[
                self.length - 2 - index,
                -1]  #self.DIS_R.DTW(self.cand_train_data[episode][(index+1):][::-1],self.query_train_data[episode][::-1])
            #print('A0', self.sufsim, self.DIS_R.D[self.length-2-index,-1])
            if (index + 1) == self.length:
                self.sufsim = self.presim
            observation = np.array([self.subsim, self.presim,
                                    self.sufsim]).reshape(1, -1)

            if self.presim < self.subsim:
                self.subsim = self.presim
                self.subtraj = [self.split_point, index]

            if self.sufsim < self.subsim:
                self.subsim = self.sufsim
                self.subtraj = [index + 1, self.length - 1]

            if label == 'T':
                last_subsim = self.subsim_real
                self.presim_real = self.REWARD_DIS.DTW(
                    self.cand_train_data[episode][self.split_point:(index +
                                                                    1)],
                    self.query_train_data[episode])
                self.subsim_real = min(self.presim_real, self.sufsim,
                                       last_subsim)
                self.RW = last_subsim - self.subsim_real
                #print('action0', self.RW)
                #print(self.presim, self.presim_real)

            return observation, self.RW, done, -1

        if action == 1:  #split
            if index == self.length - 1:
                done = True
            else:
                done = False
            self.skip = []
            self.split_point = index
            self.DIS = Distance(
                len(self.cand_train_data[episode][self.split_point:]),
                len(self.query_train_data[episode]))
            #state transfer
            self.presim = self.DIS.DTW(
                self.cand_train_data[episode][self.split_point:(index + 1)],
                self.query_train_data[episode], self.skip)
            self.sufsim = self.DIS_R.D[
                self.length - 2 - index,
                -1]  #self.DIS_R.DTW(self.cand_train_data[episode][(index+1):][::-1],self.query_train_data[episode][::-1])
            #print('A1', self.sufsim, self.DIS_R.D[self.length-2-index,-1])
            if (index + 1) == self.length:
                self.sufsim = self.presim
            observation = np.array([self.subsim, self.presim,
                                    self.sufsim]).reshape(1, -1)

            if self.presim < self.subsim:
                self.subsim = self.presim
                self.subtraj = [self.split_point, index]

            if self.sufsim < self.subsim:
                self.subsim = self.sufsim
                self.subtraj = [index + 1, self.length - 1]

            if label == 'T':
                last_subsim = self.subsim_real
                self.REWARD_DIS = Distance(
                    len(self.cand_train_data[episode][self.split_point:]),
                    len(self.query_train_data[episode]))
                self.presim_real = self.REWARD_DIS.DTW(
                    self.cand_train_data[episode][self.split_point:(index +
                                                                    1)],
                    self.query_train_data[episode])
                self.subsim_real = min(self.presim_real, self.sufsim,
                                       last_subsim)
                self.RW = last_subsim - self.subsim_real
                #print('action1', self.RW)
                #print(self.presim, self.presim_real)

            return observation, self.RW, done, -1

        if action > 1:  #skip
            INX = min(index + action - 1, self.length - 1)
            if INX == self.length - 1:
                done = True
            else:
                done = False
            for i in range(index, INX):
                self.skip.append(list(self.cand_train_data[episode][i]))
            self.presim = self.DIS.DTW(
                self.cand_train_data[episode][self.split_point:(INX + 1)],
                self.query_train_data[episode], self.skip)
            self.sufsim = self.DIS_R.D[
                self.length - 2 - INX,
                -1]  #self.DIS_R.DTW(self.cand_train_data[episode][(INX+1):][::-1],self.query_train_data[episode][::-1])
            #print('A2', self.sufsim, self.DIS_R.D[self.length-2-INX,-1])
            if (INX + 1) == self.length:
                self.sufsim = self.presim
            observation = np.array([self.subsim, self.presim,
                                    self.sufsim]).reshape(1, -1)
            if self.presim < self.subsim:
                self.subsim = self.presim
                self.subtraj = [self.split_point, INX]
            if self.sufsim < self.subsim:
                self.subsim = self.sufsim
                self.subtraj = [INX + 1, self.length - 1]

            if label == 'T':
                last_subsim = self.subsim_real
                self.presim_real = self.REWARD_DIS.DTW(
                    self.cand_train_data[episode][self.split_point:(INX + 1)],
                    self.query_train_data[episode])
                self.subsim_real = min(self.presim_real, self.sufsim,
                                       last_subsim)
                self.RW = last_subsim - self.subsim_real
                #print(self.presim, self.presim_real)
            return observation, self.RW, done, INX

    def output(self, index, episode, label='E'):
        #print('check', self.subsim, self.subtraj)
        if label == 'T':
            #print('check', self.subsim, self.subtraj, self.subsim_real)
            return [self.subsim_real, self.subtraj]
        if label == 'E':
            self.DIS = Distance(
                len(self.cand_train_data[episode]
                    [self.subtraj[0]:self.subtraj[1] + 1]),
                len(self.query_train_data[episode]))
            self.subsim_real = self.DIS.DTW(
                self.cand_train_data[episode][self.subtraj[0]:self.subtraj[1] +
                                              1],
                self.query_train_data[episode])
            return [self.subsim_real, self.subtraj]