コード例 #1
0
ファイル: tool.py プロジェクト: BIG-S2/GEM
def cal_grid_loc(g):
    cal = Calculator()
    cal.SetLayer(13)
    _, c = cal.HexCellVertexesAndCenter(g)
    return c.lng, c.lat
コード例 #2
0
ファイル: global_model.py プロジェクト: BIG-S2/GEM
def global_transport(o_t, d_t, gridsKey, grids_to_id, k, w):
    indice_count = np.zeros((_N, ), dtype=int)
    for j in range(_N):
        hex_cal = Calculator()
        hex_cal.SetLayer(12)
        key = gridsKey[j]
        indices = [j]
        for i in range(k):
            bhex_ids = hex_cal.HexCellBoudary(key, i + 1)
            inter = set(gridsKey).intersection(bhex_ids)
            indices_add = [grids_to_id[x] for x in inter]
            if len(indices_add) > 0:
                indices = indices + indices_add
        indice_count[j] = len(indices)

    gamma = cvx.Variable((np.sum(indice_count), 1))
    S = cvx.Variable((_N, 1))
    C = np.ones((_N, 1))

    A_1 = np.zeros((_N, np.sum(indice_count)))
    O_1 = np.zeros((_N, np.sum(indice_count)))
    cost = np.zeros((1, np.sum(indice_count)))
    for j in range(_N):
        hex_cal = Calculator()
        hex_cal.SetLayer(12)
        key = gridsKey[j]
        indices = [j]
        for i in range(k):
            bhex_ids = hex_cal.HexCellBoudary(key, i + 1)
            inter = set(gridsKey).intersection(bhex_ids)
            indices_add = [grids_to_id[x] for x in inter]
            if len(indices_add) > 0:
                indices = indices + indices_add
        A_1[j, np.sum(indice_count[:j]):np.sum(indice_count[:(j + 1)])] = 1
        for p in range(int(indice_count[j])):
            cost[:,
                 np.sum(indice_count[:j]) + p] = w * cost_norm[j, indices[p]]
            O_1[j, np.sum(indice_count[:j]) + p] = w * cost_norm[j, indices[p]]

    A_2 = np.zeros((_N, np.sum(indice_count)))
    O_2 = np.zeros((_N, np.sum(indice_count)))
    for j in range(_N):
        hex_cal = Calculator()
        hex_cal.SetLayer(12)
        key = gridsKey[j]
        indices = [j]
        for i in range(k):
            bhex_ids = hex_cal.HexCellBoudary(key, i + 1)
            inter = set(gridsKey).intersection(bhex_ids)
            indices_add = [grids_to_id[x] for x in inter]
            if len(indices_add) > 0:
                indices = indices + indices_add

        for p in indices:
            key = gridsKey[p]
            indices_1 = [p]
            for i in range(k):
                bhex_ids = hex_cal.HexCellBoudary(key, i + 1)
                inter = set(gridsKey).intersection(bhex_ids)
                indices_add = [grids_to_id[x] for x in inter]
                if len(indices_add) > 0:
                    indices_1 = indices_1 + indices_add
            index = indices_1.index(j)
            A_2[j, np.sum(indice_count[:p]) + index] = 1
            O_2[j, np.sum(indice_count[:p]) + index] = w * cost_norm[j, p]

    obj = cvx.Minimize(C.T * S + cost * gamma)

    constr = [
        A_2 * gamma + S >= o_t.reshape(-1, 1),
        A_2 * gamma - S <= o_t.reshape(-1, 1), 0 <= gamma, 0 <= S,
        A_1 * gamma == d_t.reshape(-1, 1)
    ]
    prob = cvx.Problem(obj, constr)
    d = prob.solve(solver=cvx.GLPK)

    tmp = o_t.reshape(_N, 1)
    ratio = np.zeros((_N, ))
    tild_d = np.dot(A_2, gamma.value) - np.dot(O_2, gamma.value)
    tild_d[tild_d < 0] = 0
    for i in range(_N):
        if (tmp[i, 0] == 0) & (tild_d[i, 0] == 0):
            ratio[i] = 1
        elif tild_d[i, 0] == 0:
            ratio[i] = tmp[i, 0] / (tild_d[i, 0] + 1)
        else:
            ratio[i] = tmp[i, 0] / tild_d[i, 0]

    return d, ratio
コード例 #3
0
ファイル: tool.py プロジェクト: BIG-S2/GEM
def cal_grid(lng, lat):
    cal = Calculator()
    cal.SetLayer(13)
    return cal.HexCellKey(GeoCoord(lat, lng))