Exemplo n.º 1
0
def worker_row(wkid, task_queue, res_dict):
    while True:
        st_time = time.time()

        i = task_queue.get()
        if i == -1: break  # Break when stop token received
        print "Worker #%d is working on transition of row #%d." % (wkid, i),

        res = pv.KWTable()

        row_vec = m_mat[i]
        u = u_vec[i]

        for j in range(len(row_vec)):
            if row_vec[j] == 0.0: continue  # No need to do 0-coeff aggregation
            res.aggr_inplace(x_vec[j], 1.0, row_vec[j])

        if not u == 0.0: res.aggr_inplace(y, 1.0, u)

        ori_size = len(res)
        res.rsvr_sample(RSVR_SIZE, in_place=True)
        print "   Size = %d -> %d" % (ori_size, len(res)),
        el_time = time.time() - st_time
        print "   Elapsed time = %f" % (el_time)
        res_dict[i] = res
    return
Exemplo n.º 2
0
def forecast(r):
    # r is the # of steps to look ahead.
    st_time = time.time()
    ret = pv.KWTable()
    l = x_vec[0]
    b = x_vec[1]
    s = x_vec[r + 1]  # r-th seasonal component
    ret.aggr_inplace(l, 1.0, 1.0)
    ret.aggr_inplace(b, 1.0, float(r))
    ret.aggr_inplace(s, 1.0, 1.0)
    ret.rsvr_sample(RSVR_SIZE, in_place=True)
    el_time = time.time() - st_time
    print "Making %d-step forecast. Time stamp = %d. Elapsed time = %f" % (
        R, TS_CURR + INTERVAL * (R - 1), el_time)
    return ret
Exemplo n.º 3
0
def read_rec(ts):
    return pv.KWTable(filetype=FILETYPE,
                      fn=os.path.join(DATA_DIR,
                                      str(ts) + ".rec"))
Exemplo n.º 4
0
INTERVAL = 3600  # Seconds in a time slot
TS_START = 1181088000  # Starting timestamp (in seconds)
TS_END = TS_START + INTERVAL * 60  # Ending timestamp
FILETYPE = "flowbin"
PERIOD = 24  # # of time slots in a period
R = 1  # Forecast # of steps
ALPHA = 0.2
BETA = 0.2
GAMMA = 0.2
N_WORKERS = 4

# ---------- Global variables and objects ----------
TS_CURR = 0  # Current timestamp
x_vec = mp.Manager().dict(
)  # State vector. Make it globally accessible (shared dictionary)
y = pv.KWTable()  # Current time slot's observation
m_mat = []  # Transition matrix
u_vec = []  # U-Vector for transition


def make_trans_matrix():
    ret = []
    for i in range(PERIOD + 1):
        ret.append([0.0] * (PERIOD + 1))

    ret[0][0] = 1.0 - ALPHA
    ret[0][1] = 1.0 - ALPHA
    ret[0][2] = -1.0 * ALPHA
    ret[1][0] = -1.0 * BETA
    ret[1][1] = 1.0 - BETA
    ret[1][2] = -1.0 * BETA
Exemplo n.º 5
0
    return pv.KWTable(filetype=FILETYPE,
                      fn=os.path.join(DATA_DIR,
                                      str(ts) + ".rec"))


def worker_samp(i):
    # Grab a KWTable from queue and sample it down to size
    print "Worker is working on x_vec[%d]: KWTable(%x). Current size = %d" % (
        i, id(x_vec[i]), len(x_vec[i]))
    ret = x_vec[i].rsvr_sample(RSVR_SIZE, in_place=False)
    # To do multiprocessing, I have no choice but turn off in_place option. But I still get large speedup.
    return ret


if __name__ == "__main__":
    l0 = pv.KWTable()
    b0 = pv.KWTable()
    s0_list = [pv.KWTable()
               for i in range(PERIOD)]  # create (w-1) empty tables
    # note that s0_list[0] is not used
    # indices 1..(PERIOD-1) are used

    print "Using Python interpreter:", sys.executable

    # ---------- First training period ----------
    print "First training period"
    for i in range(PERIOD):
        st_time = time.time()
        y = read_rec(TS_CURR)
        b0.aggr_inplace(y, 1.0, -1.0)
        print "Read time slot", TS_CURR, "   b0 -= y",