Beispiel #1
0
def _print_RB(alloc_RB_i, RB_needed, rate, rate_reduce_ij, rate_reduce_ji,
              sumrate_i, algo_name):

    num_bs, num_subcarriers, num_time_slots, num_users, num_users_i = setting._setting(
    )
    all_bs = range(num_bs)
    all_users = range(num_users)
    all_subcarriers = range(num_subcarriers)
    all_time_slots = range(num_time_slots)
    all_users_i = []
    for i in all_bs:
        all_users_i.append(range(num_users_i[i]))

    num_itf_users, itf_idx_i, rate_reduce_ij, rate_reduce_ji, SNR_reduce_ij, SNR_reduce_ji, rate_reduce, rate_pair = setting_SIC._setting_SIC(
    )

    print('-----', algo_name, '-----')

    orthogonal_alloc_i = [[0 for u in all_users_i[i]] for i in all_bs]
    muting_RB_i = []
    unallocated_RB = 0
    RB_waste_i = [0 for i in all_bs]
    RB_used_i = [[0 for u in all_users_i[i]] for i in all_bs]
    for i in all_bs:
        muting_RB_i.append(0)
        print()
        print('BS', i)
        print('Total RB:', num_subcarriers * num_time_slots)
        #print('sumrate:', sumrate_i[i])
        print()
        for f in all_subcarriers:
            for t in all_time_slots:
                j = (i + 1) % 2
                if alloc_RB_i[i][f][t] == 'x':
                    RB_waste_i[i] += 1
                else:
                    RB_used_i[i][alloc_RB_i[i][f][t]] += 1
                # count for unallocate RB
                if alloc_RB_i[i][f][t] == 'x' and alloc_RB_i[j][f][t] == 'x':
                    unallocated_RB += 1
                # count for muting RB
                if alloc_RB_i[i][f][t] == 'x' and alloc_RB_i[j][f][t] != 'x':
                    for u in itf_idx_i[j]:
                        if alloc_RB_i[j][f][t] == u:
                            muting_RB_i[i] += 1
                            alloc_RB_i[i][f][t] = 'm'
                            orthogonal_alloc_i[j][u] += 1
                            break
                print(alloc_RB_i[i][f][t], ' ', sep='', end='')
            print()
        print()
        unallocated_RB /= num_bs  # r_{f,t} in different bs only count for 1 unallocated RB
        #print('RB wastes:', RB_waste_i[i])
        #print('muting RB:', muting_RB_i[i])
        print()
        for u in all_users_i[i]:
            if u in itf_idx_i[i]:  #itf user
                break
            print('user', u, 'uses', RB_used_i[i][u], 'RB with rate',
                  rate[i][u] / 10000, ',(', RB_used_i[i][u], '/',
                  RB_needed[i][u], ')RB')
        j = (i + 1) % 2
        for u in itf_idx_i[i]:  #itf user
            print('user', u, 'uses', RB_used_i[i][u], 'RB ,(', RB_used_i[i][u],
                  '/', RB_needed[i][u], ')RB')
            RB_SIC = 0
            pair_user_RB = [0 for v in all_users_i[j]]
            for f in all_subcarriers:
                for t in all_time_slots:
                    if alloc_RB_i[i][f][t] == 'x' or alloc_RB_i[i][f][
                            t] == 'm' or alloc_RB_i[i][f][t] != u:
                        continue
                    if alloc_RB_i[j][f][t] == 'x' or alloc_RB_i[j][f][t] == 'm':
                        continue
                    else:
                        v = alloc_RB_i[j][f][t]
                        pair_user_RB[v] += 1
            #print(pair_user_RB)
            for v in all_users_i[j]:
                if pair_user_RB[v] == 0:
                    continue
                if i == 0:
                    print(pair_user_RB[v], 'RB with rate',
                          (rate[i][u] - rate_reduce_ij[u][v]) / 10000, ',(',
                          pair_user_RB[v], '/', RB_used_i[i][u],
                          ')RB (pair with user', v, ')')
                    #sumrate_i[i] -= rate_reduce_ij[u][v] * pair_user_RB[v] / 10000
                else:
                    print(pair_user_RB[v], 'RB with rate',
                          (rate[i][u] - rate_reduce_ji[u][v]) / 10000, ',(',
                          pair_user_RB[v], '/', RB_used_i[i][u],
                          ')RB (pair with user', v, ')')
                    #sumrate_i[i] -= rate_reduce_ji[u][v] * pair_user_RB[v] / 10000
                #print(pair_user_RB[v], 'RB with rate', (rate[i][u] - rate_reduce[u][v] / 2) / 10000, ',(', pair_user_RB[v], '/', RB_used_i[i][u], ')RB (pair with user', v, ')')
                #sumrate_i[i] -= rate_reduce[u][v] / 2 * pair_user_RB[v] / 10000
            muting_RB = RB_used_i[i][u] - sum(pair_user_RB)
            if muting_RB != 0:
                print(muting_RB, 'RB with rate', (rate[i][u]) / 10000, ',(',
                      muting_RB, '/', RB_used_i[i][u], ')RB (muting)')
            print()
        print('BS', i, 'sumrate:', round(sumrate_i[i], 4))

    print('sumrate', algo_name, '=', round(sum(sumrate_i), 4))
Beispiel #2
0
def _exhausted_search(Z, RB_needed, rate, rate_pair, rate_reduce_ij,
                      rate_reduce_ji, traffic_demands, time_threshold,
                      optimal):

    num_bs, num_subcarriers, num_time_slots, num_users, num_users_i = _setting(
    )
    num_itf_users, itf_idx_i, rate_reduce_ij, rate_reduce_ji, SNR_reduce_ij, SNR_reduce_ji, rate_reduce, rate_pair = setting_SIC._setting_SIC(
    )
    all_bs = range(num_bs)
    all_users = range(num_users)
    all_subcarriers = range(num_subcarriers)
    all_time_slots = range(num_time_slots)
    all_users_i = []
    for i in all_bs:
        all_users_i.append(range(num_users_i[i]))

    # Creates the model
    model = cp_model.CpModel()
    # Creates RB variables.
    # RB[(u
    # , t, f)]: user u is allocated RB_{f,t}
    RB = {}
    for i in all_bs:
        for u in all_users_i[i]:
            for t in all_time_slots:
                for f in all_subcarriers:
                    RB[(i, u, t, f)] = model.NewBoolVar('RB_i%iu%it%if%i' %
                                                        (i, u, t, f))
    X = {}
    for u in all_users_i[0]:
        for v in all_users_i[1]:
            for t in all_time_slots:
                for f in all_subcarriers:
                    X[(u, v, t,
                       f)] = model.NewBoolVar('X_u%iv%it%if%i' % (u, v, t, f))

    # constraints 1,2
    # allocated RB should not exceed the total RB
    for i in all_bs:
        model.Add(
            sum(RB[i, u, t, f] for u in all_users_i[i] for t in all_time_slots
                for f in all_subcarriers) <= num_subcarriers * num_time_slots)

    # constraints 3,4
    # Each RB_{f,t} is allocated to no more than one user u
    for i in all_bs:
        for t in all_time_slots:
            for f in all_subcarriers:
                model.Add(sum(RB[(i, u, t, f)] for u in all_users_i[i]) <= 1)

    # constraints: allocated RB <= needed RB (traffic demand)
    for i in all_bs:
        for u in all_users_i[i]:
            model.Add(
                sum(RB[(i, u, t, f)] for t in all_time_slots
                    for f in all_subcarriers) <= RB_needed[i][u])

    # constraints: for interference users I_{i,u,t,f} + I_{j,v,t,f} <= Z_{u, v} + 1
    for u in all_users_i[0]:
        for v in all_users_i[1]:
            for t in all_time_slots:
                for f in all_subcarriers:
                    model.Add(
                        RB[(0, u, t, f)] + RB[(1, v, t, f)] <= Z[u][v] + 1)
    # old one
    # constraints: x_{u,v,f,t} <= I_{i,u,f,t} && x_{u,v,f,t} <= I_{j,v,f,t} (wrong)
    for u in all_users_i[0]:
        for v in all_users_i[1]:
            for t in all_time_slots:
                for f in all_subcarriers:
                    #model.Add(X[(u, v, t, f)] <= RB[0, u, t, f])
                    #model.Add(X[(u, v, t, f)] <= RB[1, v ,t ,f])
                    #model.AddBoolOr([RB[(0, u, t, f)].Not(), RB[(1, v, t, f)].Not(), X[(u, v, t, f)]])
                    pass

    # constraits: I_{i,u,f,t} + i_{j,v,f,t} <= X_{u,v,f,t} + 1
    for u in all_users_i[0]:
        for v in all_users_i[1]:
            for t in all_time_slots:
                for f in all_subcarriers:
                    model.Add(RB[(0, u, t, f)] +
                              RB[(1, v, t, f)] <= X[(u, v, t, f)] + 1)

    # objective function
    #model.Maximize(sum(rate[i][u] * RB[(i, u, t, f)] - sum(RB[(1, v, t, f)] * rate_reduce[u][v] for v in all_users_i[1]) for i in all_bs for u in all_users_i[i] for t in all_time_slots for f in all_subcarriers))
    model.Maximize(
        sum(
            sum(rate[0][u] * RB[(0, u, t, f)]
                for u in all_users_i[0]) + sum(rate[1][v] * RB[(1, v, t, f)]
                                               for v in all_users_i[1])
            #+ sum (X[(u ,v ,t, f)] * (rate_reduce[u][v])
            + sum(X[(u, v, t, f)] * (rate_pair[u][v] - rate[0][u] - rate[1][v])
                  for u in all_users_i[0] for v in all_users_i[1])
            for t in all_time_slots for f in all_subcarriers))
    # Creates the solver and solve.

    solver = cp_model.CpSolver()
    #solver.Solve(model)
    if not optimal:
        solver.parameters.max_time_in_seconds = time_threshold

    status = solver.Solve(model)
    stat = ''
    if status == cp_model.OPTIMAL:
        #print('optimal')
        stat = 'optimal'
    if status == cp_model.FEASIBLE:
        #print('feasible')
        stat = 'feasible'
    if status == cp_model.INFEASIBLE:
        #print('infeasible')
        stat = 'infeasible'
    if status == cp_model.MODEL_INVALID:
        #print('model invalid')
        stat = 'model invalid'
    if status == cp_model.UNKNOWN:
        #print('unknown')
        stat = 'unknown'

    if 1:
        #if status == cp_model.FEASIBLE:
        alloc_RB_i = []
        RB_waste_i = []
        RB_used_i = []
        sumrate_i = []
        muting_RB_i = []
        sumrate_bit_i = []

        for i in all_bs:
            # print('BS', i)
            alloc_RB_i.append([])
            RB_waste_i.append(0)
            RB_used_i.append([0 for u in all_users_i[i]])
            sumrate = 0
            sumrate_bit = 0
            for f in all_subcarriers:
                #print('time slot', t)
                alloc_RB_i[i].append([])
                for t in all_time_slots:
                    for u in all_users_i[i]:
                        if solver.Value(RB[(i, u, t, f)]) == 1:
                            #print('User', u + i * num_users_i[0], 'is allocated RB',t, f, 'by rate ', rate[i][u] / 10000)
                            sumrate = sumrate + rate[i][u] / 10000
                            sumrate_bit = sumrate_bit + traffic_demands[i][
                                u] / RB_needed[i][u]
                            alloc_RB_i[i][f].append(u)
                            RB_used_i[i][u] = RB_used_i[i][u] + 1
                            break
                    if len(alloc_RB_i[i][f]) <= t:
                        alloc_RB_i[i][f].append('x')
                        RB_waste_i[i] = RB_waste_i[i] + 1
                #print()
            #print('BS', i,'sumrate :', round(sumrate, 4))
            #print()
            sumrate_i.append(round(sumrate, 4))
            sumrate_bit_i.append(sumrate_bit)
        for f in all_subcarriers:
            for t in all_time_slots:
                #j = (i + 1) % 2
                if alloc_RB_i[0][f][t] != 'x' and alloc_RB_i[1][f][t] != 'x':
                    sumrate_i[0] -= rate_reduce_ij[alloc_RB_i[0][f][t]][
                        alloc_RB_i[1][f][t]] / 10000
                    sumrate_i[1] -= rate_reduce_ji[alloc_RB_i[1][f][t]][
                        alloc_RB_i[0][f][t]] / 10000
        '''
        # find X 
        x_RB_i = []
        x_RB_j = []
        for f in all_subcarriers:
            x_RB_i.append([])
            x_RB_j.append([])
            for t in all_time_slots:
                for u in all_users_i[0]:
                    for v in all_users_i[1]:
                        if solver.Value(X[(u, v, t, f)]) == 1 :
                            #print()
                            x_RB_i[f].append(u)
                            x_RB_j[f].append(v)
                            break
                    if len(x_RB_i[f]) > t:
                        break
                if len(x_RB_i[f]) == t:
                    x_RB_i.append('x')
                    x_RB_j.append('x')

        for f in all_subcarriers:
            for t in all_time_slots: 
                print(x_RB_i[f][t],' ' , sep = '',end = '')
                #print()
            print()
        print()
        for f in all_subcarriers:
            for t in all_time_slots: 
                print(x_RB_j[f][t],' ' , sep = '',end = '')
                #print()
            print()

        for f in all_subcarriers:
            for t in all_time_slots:
                for u in all_users_i[0]:
                    for v in all_users_i[1]:
                        print(solver.Value(X[(u, v, t, f)]), ' ', sep = '', end = '')
                    print()
                print()
            print()
        '''
        '''
        print('Statistics')
        print('  - sumrate = %f' % (solver.ObjectiveValue() / 10000), '(out of', num_bs * num_subcarriers * num_time_slots, 'RBs)')
        print('  - RB waste = %i' % (sum(RB_waste_i)))
        print('  - RB utilization = %f' % (100 - sum(RB_waste_i) / (num_bs * num_subcarriers * num_time_slots) * 100 ), '%')
        print('  - wall time       : %f s' % solver.WallTime())
        #print('unallocated RB:', unallocated_RB)
        '''
        objective_value = solver.ObjectiveValue()
        wall_time = solver.WallTime()
    return alloc_RB_i, RB_waste_i, RB_used_i, sumrate_i, sumrate_bit_i, objective_value, wall_time, stat
Beispiel #3
0
def _greedy_merge(match, RB_needed, rate, rate_pair, rate_reduce_ij,
                  rate_reduce_ji):

    num_bs, num_subcarriers, num_time_slots, num_users, num_users_i = setting._setting(
    )

    all_bs = range(num_bs)
    all_users = range(num_users)
    all_subcarriers = range(num_subcarriers)
    all_time_slots = range(num_time_slots)
    all_users_i = []
    for i in all_bs:
        all_users_i.append(range(num_users_i[i]))

    num_itf_users, itf_idx_i, rate_reduce_ij, rate_reduce_ji, SNR_reduce_ij, SNR_reduce_ji, rate_reduce, rate_pair = setting_SIC._setting_SIC(
    )

    ##------------- algo start -------------------------------------
    queue_single_i = []
    queue_itf_i = []
    print(rate)
    #---- sorting single users by rate
    for i in all_bs:
        queue_single_i.append(
            sorted(range(len(rate[i]) - num_itf_users),
                   key=lambda u: rate[i][u]))
    print(queue_single_i)

    #---- sorting interfering users by rate
    for i in all_bs:
        queue_itf_i.append(
            sorted(range(len(rate[i]) - num_itf_users, len(rate[i])),
                   key=lambda u: rate[i][u]))
    print(queue_itf_i)

    alloc_RB_i = []
    for i in all_bs:
        alloc_RB_i.append([])

    RB_sufficient = False
    muting_RB_idx_i = [[[] for u in all_users_i[i]] for i in all_bs]
    #muting_RB_i = [ [0 for u in all_users_i[i]] for i in all_bs ]

    #------------- initial allocate
    for t in all_time_slots:
        for i in all_bs:
            alloc_RB_i[i].append(['x' for f in all_subcarriers])
        f = num_subcarriers
        while f:
            sumrate_single = 0
            sumrate_itf = 0

            #-- calculate best sumrate of single users
            for i in all_bs:
                if queue_single_i[i]:
                    sumrate_single += rate[i][queue_single_i[i][-1]]
            #print(sumrate_single)

            #-- calculate best sumrate of interfering users
            max_sumrate_i = [0 for i in all_bs]
            for i in all_bs:
                if queue_itf_i[i]:
                    max_sumrate_i[i] = rate[i][queue_itf_i[i][-1]]
            sumrate_itf = max(max_sumrate_i)
            #print(sumrate_itf)

            #-- no user to allocate
            if not sumrate_itf and not sumrate_single:
                RB_sufficient = True
                break
            #-- choose single user to allocate
            elif sumrate_single >= sumrate_itf:
                for i in all_bs:
                    if queue_single_i[i]:
                        alloc_RB_i[i][t][f - 1] = queue_single_i[i][-1]
                        RB_needed[i][queue_single_i[i][-1]] -= 1
                        if RB_needed[i][queue_single_i[i][-1]] == 0:
                            queue_single_i[i].pop()

            #-- choose interfering user to allocate
            else:
                i = max_sumrate_i.index(max(max_sumrate_i))
                alloc_RB_i[i][t][f - 1] = queue_itf_i[i][-1]
                RB_needed[i][queue_itf_i[i][-1]] -= 1
                #muting_RB_i[i][queue_itf_i[i][-1]] += 1
                muting_RB_idx_i[i][queue_itf_i[i][-1]].append(f - 1 + t *
                                                              num_subcarriers)
                if RB_needed[i][queue_itf_i[i][-1]] == 0:
                    queue_itf_i[i].pop()
            f -= 1
    '''
    print(alloc_RB_i[0])
    print(alloc_RB_i[1])
    print(muting_RB_idx_i[0])
    print(muting_RB_idx_i[1])
    print()
    '''

    for i in all_bs:
        for u in all_users_i[i]:
            muting_RB_idx_i[i][u].sort()

    #---- sorting pairing user by reduce rate
    queue_reduce = sorted(range(len(rate[i]) - num_itf_users, len(rate[i])),
                          key=lambda u: rate_reduce[u][match[0, u]],
                          reverse=True)
    #print(rate_reduce)
    #print(queue_reduce)
    print('RB_needed')
    print(RB_needed)

    if not queue_itf_i and not queue_single_i:
        RB_sufficient = True

    #---------- do merging when RB is not sufficient
    while not RB_sufficient:
        #print(alloc_RB_i[0])
        #print(alloc_RB_i[1])
        for i in all_bs:
            print()
            for f in all_subcarriers:
                for t in all_time_slots:
                    print(alloc_RB_i[i][t][f], sep='', end='')
                print()

        print(muting_RB_idx_i[0])
        print(muting_RB_idx_i[1])

        #-- find the pairing user which will reduce least rate when merging
        merge_pair = -1
        for u in queue_reduce:
            if muting_RB_idx_i[0][u] and muting_RB_idx_i[1][match[0, u]]:
                merge_pair = u
                break

        if merge_pair != -1:
            RB_idx = [
                muting_RB_idx_i[0][merge_pair][-1],
                muting_RB_idx_i[1][match[0, merge_pair]][-1]
            ]
            t = RB_idx[1] // num_subcarriers
            f = RB_idx[1] % num_subcarriers
            #muting_RB_idx_i[0][merge_pair].pop()
            #muting_RB_idx_i[1][match[0, merge_pair]].pop()

            print('merge_pair:')
            print(merge_pair)
            print('RB_idx:')
            print(RB_idx)
            #print('t:',t,'f:',f)

        sumrate_single = 0
        sumrate_itf = 0
        sumrate_pair = 0

        #-- calculate best sumrate of single users
        for i in all_bs:
            if queue_single_i[i]:
                sumrate_single += rate[i][queue_single_i[i][-1]]
        #print(sumrate_single)

        #----- calculate best sumrate of interfering users
        max_sumrate_i = [0 for i in all_bs]
        for i in all_bs:
            while queue_itf_i[i] and RB_needed[i][queue_itf_i[i][-1]] == 0:
                queue_itf_i[i].pop()
            if queue_itf_i[i]:
                max_sumrate_i[i] = rate[i][queue_itf_i[i][-1]]
        sumrate_itf = max(max_sumrate_i)
        #print(sumrate_itf)

        #----- case addtion's rate
        # TODO: maybe can init
        addition_pair = -1
        for u in queue_reduce:
            sumrate_increase = 0
            if muting_RB_idx_i[0][u] and not muting_RB_idx_i[1][match[
                    0, u]] and RB_needed[1][match[0, u]]:
                sumrate_increase = rate[1][match[0,
                                                 u]] - rate_reduce[u][match[0,
                                                                            u]]
            elif not muting_RB_idx_i[0][u] and muting_RB_idx_i[1][match[
                    0, u]] and RB_needed[0][u]:
                sumrate_increase = rate[0][u] - rate_reduce[u][match[0, u]]
            if sumrate_increase > sumrate_pair:
                sumrate_pair = sumrate_increase
                addition_pair = u
        if addition_pair != -1:
            print('addition:')
            print(addition_pair)

        print('case merge:')
        print('merge pair:', merge_pair)
        #print('RB_idx:', RB_idx[1])
        print('sumrate_single:', sumrate_single)
        print('sumrate_itf:', sumrate_itf)

        print('-----------------------------------------')

        print('case addition')
        print('addition:', addition_pair)
        #print('RB:', t * num_subcarriers + f )
        print('sumrate_pair:', sumrate_pair)

        #----- no more user to allocate
        if merge_pair == -1 and addition_pair == -1:
            break

        #----- case addition to allocate
        elif addition_pair != -1 and (
                merge_pair == -1
                or sumrate_pair >= max(sumrate_itf, sumrate_single) -
                rate_reduce[merge_pair][match[0, merge_pair]]):
            print('Case addition')
            u = addition_pair
            if not muting_RB_idx_i[0][u]:
                t = muting_RB_idx_i[1][match[0, u]][-1] // num_subcarriers
                f = muting_RB_idx_i[1][match[0, u]][-1] % num_subcarriers
                alloc_RB_i[0][t][f] = u
                RB_needed[0][u] -= 1
                muting_RB_idx_i[1][match[0, u]].pop()
                print('i: 0')
                print('u:', u)
                print('RB:', t * num_subcarriers + f)
            elif not muting_RB_idx_i[1][match[0, u]]:
                t = muting_RB_idx_i[0][u][-1] // num_subcarriers
                f = muting_RB_idx_i[0][u][-1] % num_subcarriers
                alloc_RB_i[1][t][f] = match[0, u]
                RB_needed[1][match[0, u]] -= 1
                muting_RB_idx_i[0][u].pop()
                print('i: 1')
                print('u:', match[0, u])
                print('RB:', t * num_subcarriers + f)

        #----- case merge to allocate
        elif merge_pair != -1:
            print('Case merge')
            if not sumrate_single and not sumrate_itf:
                break
            #----- choose single user to allocate
            elif sumrate_single >= sumrate_itf:
                for i in all_bs:
                    if queue_single_i[i]:
                        alloc_RB_i[i][t][f] = queue_single_i[i][-1]
                        RB_needed[i][queue_single_i[i][-1]] -= 1
                        if RB_needed[i][queue_single_i[i][-1]] == 0:
                            queue_single_i[i].pop()
                    else:
                        alloc_RB_i[i][t][f] = 'x'

            #----- choose interfering user to allocate
            else:
                i = max_sumrate_i.index(max(max_sumrate_i))
                alloc_RB_i[i][t][f] = queue_itf_i[i][-1]
                print('i:', i)
                print('u:', queue_itf_i[i][-1])
                RB_needed[i][queue_itf_i[i][-1]] -= 1
                muting_RB_idx_i[i][queue_itf_i[i][-1]].append(f + t *
                                                              num_subcarriers)
                muting_RB_idx_i[i][queue_itf_i[i][-1]].sort()
                j = (i + 1) % 2
                alloc_RB_i[j][t][f] = 'x'
                if RB_needed[i][queue_itf_i[i][-1]] == 0:
                    queue_itf_i[i].pop()

            #-- merge the pairing user
            t = RB_idx[0] // num_subcarriers
            f = RB_idx[0] % num_subcarriers
            alloc_RB_i[1][t][f] = match[0, merge_pair]
            muting_RB_idx_i[0][merge_pair].pop()
            muting_RB_idx_i[1][match[0, merge_pair]].pop()

    sumrate_i = [0 for i in all_bs]
    sumrate_RB_i = [[0 for r in range(num_subcarriers * num_time_slots)]
                    for i in all_bs]
    sumrate_RB = [0 for r in range(num_subcarriers * num_time_slots)]
    for t in all_time_slots:
        for f in all_subcarriers:
            for i in all_bs:
                if alloc_RB_i[i][t][f] != 'x':
                    sumrate_RB_i[i][t * num_subcarriers +
                                    f] += rate[i][alloc_RB_i[i][t][f]] / 10000
                    #sumrate_i[i] += rate[i][alloc_RB_i[i][t][f]] / 10000
            if alloc_RB_i[0][t][f] != 'x' and alloc_RB_i[1][t][f] != 'x':
                sumrate_RB_i[0][t * num_subcarriers + f] -= rate_reduce_ij[
                    alloc_RB_i[0][t][f]][alloc_RB_i[1][t][f]] / 10000
                sumrate_RB_i[1][t * num_subcarriers + f] -= rate_reduce_ji[
                    alloc_RB_i[1][t][f]][alloc_RB_i[0][t][f]] / 10000

                #sumrate_i[0] -= rate_reduce_ij[alloc_RB_i[0][t][f]][alloc_RB_i[1][t][f]] / 10000
                #sumrate_i[1] -= rate_reduce_ji[alloc_RB_i[1][t][f]][alloc_RB_i[0][t][f]] / 10000

    ##-------- test if there is rate of pair user >= alloc
    for r in range(num_subcarriers * num_time_slots):
        sumrate_RB[r] = sumrate_RB_i[0][r] + sumrate_RB_i[1][r]

    #----- sort pairing user by rate_pair
    queue_pair = sorted(range(len(rate[i]) - num_itf_users, len(rate[i])),
                        key=lambda u: rate_pair[u][match[0, u]],
                        reverse=True)
    while True:
        print('sumrate_RB')
        print(sumrate_RB)
        for i in all_bs:
            print()
            for f in all_subcarriers:
                for t in all_time_slots:
                    print(alloc_RB_i[i][t][f], sep='', end='')
                print()
        u = -1
        for idx in range(len(queue_pair)):
            if RB_needed[0][queue_pair[idx]] and RB_needed[1][match[
                    0, queue_pair[idx]]]:
                u = queue_pair[idx]
                break
        if u == -1:
            break
        sumrate_pair = round(rate_pair[u][match[0, u]] / 10000, 4)
        sumrate_min = round(min(sumrate_RB), 4)
        if sumrate_pair > sumrate_min:
            min_RB_idx = sumrate_RB.index(min(sumrate_RB))
            print('min_RB_idx:', min_RB_idx)
            print('u:', u)
            t = min_RB_idx // num_subcarriers
            f = min_RB_idx % num_subcarriers
            if alloc_RB_i[0][t][f] != 'x':
                RB_needed[0][alloc_RB_i[0][t][f]] += 1
            if alloc_RB_i[1][t][f] != 'x':
                RB_needed[1][alloc_RB_i[1][t][f]] += 1
            alloc_RB_i[0][t][f] = u
            alloc_RB_i[1][t][f] = match[0, u]
            RB_needed[0][u] -= 1
            RB_needed[1][match[0, u]] -= 1
            sumrate_RB_i[0][min_RB_idx] = (
                rate[0][u] - rate_reduce_ij[u][match[0, u]]) / 10000
            sumrate_RB_i[1][min_RB_idx] = (
                rate[1][match[0, u]] - rate_reduce_ji[match[0, u]][u]) / 10000
            sumrate_RB[min_RB_idx] = sumrate_RB_i[0][
                min_RB_idx] + sumrate_RB_i[1][min_RB_idx]
        else:
            break
    #print(rate_pair)
    #print(queue_pair)

    for i in all_bs:
        sumrate_i[i] = sum(sumrate_RB_i[i])

    RB_used_i = [[0 for u in all_users_i[i]] for i in all_bs]
    #print(RB_used_i)
    for i in all_bs:
        for t in all_time_slots:
            for f in all_subcarriers:
                if alloc_RB_i[i][t][f] != 'x':
                    RB_used_i[i][alloc_RB_i[i][t][f]] += 1
    #transpose alloc_RB_i[i][t][f] into alloc_RB_i[i][f][t]
    for i in all_bs:
        alloc_RB_i[i] = list(map(list, zip(*alloc_RB_i[i])))

    return alloc_RB_i, sumrate_i, RB_used_i
Beispiel #4
0
def _report():

    # read mcs table file
    book = xlrd.open_workbook('CQI_index.xlsx')
    mcs_table = book.sheets()[0]
    #print (mcs_table.nrows)
    #print (mcs_table.ncols)
    #print (mcs_table.row_values(2))
    #print (mcs_table.cell(0,2).value)

    num_bs, num_subcarriers, num_time_slots, num_users, num_users_i = _setting(
    )
    #print(num_bs)

    all_bs = range(num_bs)
    all_users = range(num_users)
    all_subcarriers = range(num_subcarriers)
    all_time_slots = range(num_time_slots)
    all_users_i = []
    for i in all_bs:
        all_users_i.append(range(num_users_i[i]))
    #rtd_setting2.init()
    traffic_demands, SNR, SNR_db, rate = rtd_setting2._rtd_setting2()
    num_itf_users, itf_idx_i, rate_reduce_ij, rate_reduce_ji, SNR_reduce_ij, SNR_reduce_ji, rate_reduce, rate_pair = setting_SIC._setting_SIC(
    )

    # pairing matrix
    Z = [[1 for col in all_users_i[1]] for row in all_users_i[0]]
    #print (Z)
    for i in all_users_i[0]:
        #Z.append([])
        for j in all_users_i[1]:
            if i in itf_idx_i[0] or j in itf_idx_i[1]:
                Z[i][j] = 0

    Z_single = deepcopy(Z)

    for i in itf_idx_i[0]:
        for j in itf_idx_i[1]:
            if i == j:
                Z[i][j] = 1
    #print('Z:')
    #print(Z)

    #print('Z_single:')
    #print(Z_single)
    # record the matching pair user
    match = {}
    for i in all_users_i[0]:
        #Z.append([])
        for j in all_users_i[1]:
            if Z[i][j] == 1 and i in itf_idx_i[0] and j in itf_idx_i[1]:
                match[0, i] = j
                match[1, j] = i
    #print('match:')
    #print(match)
    # TODO: rb_needed may be sufficient(cause program stopping)

    RB_needed = []
    for i in all_bs:
        RB_needed.append([])
        for u in all_users_i[i]:
            reduce = 0
            if u in itf_idx_i[i]:
                v = match[i, u]
                if i == 0:
                    reduce = SNR_reduce_ij[u][v]
                else:
                    reduce = SNR_reduce_ji[u][v]
            snr = SNR[i][u] - reduce
            snr_db = 10 * math.log(snr, 10)
            for idx in range(1, mcs_table.nrows):
                if snr_db < mcs_table.cell(idx, 4).value:
                    rb = traffic_demands[i][u] / (
                        mcs_table.cell(idx - 1, 3).value * 84)
                    RB_needed[i].append(math.ceil(rb))
                    break
    #print('RB_needed: ')
    #print(RB_needed)
    for i in all_bs:
        for u in all_users_i[i]:
            #RB_needed[i][u] = random.randint(3, 7)
            #RB_needed[i][u] = 4
            pass
    #RB_needed[0][3] = 5
    #RB_needed[1][3] = 5
    #RB_needed[0][4] = 5
    #RB_needed[1][4] = 5
    #print('RB_needed: ')
    #print(RB_needed)

    ## exhausted search (opt)
    time_threshold = 5.0
    alloc_RB_i, RB_waste_i, RB_used_i, sumrate_i, sumrate_bit_i, objective_value, wall_time = _exhausted_search(
        Z, RB_needed, rate, rate_pair, rate_reduce_ij, rate_reduce_ji,
        traffic_demands, time_threshold)
    if round(sum(sumrate_i), 4) != objective_value / 10000:
        print('objective function wrong!!')
    #print('exhausted')
    #print(alloc_RB_i)

    #_print_RB(alloc_RB_i, RB_needed, rate, rate_reduce_ij, rate_reduce_ji, sumrate_i, 'opt')

    ## exhausted search (singleton)
    single_alloc_RB_i, single_RB_waste_i, single_RB_used_i, single_sumrate_i, single_sumrate_bit_i, single_objective_value, single_wall_time = _exhausted_search(
        Z_single, RB_needed, rate, rate_pair, rate_reduce_ij, rate_reduce_ji,
        traffic_demands, time_threshold)
    #print('exhausted')
    #print(alloc_RB_i)

    ## exhausted search (pairing)
    pairing_traffic_demands = deepcopy(traffic_demands)
    pairing_rate = deepcopy(rate)
    pairing_RB_needed = deepcopy(RB_needed)
    for u in itf_idx_i[0]:
        v = match[0, u]
        pairing_rate[0][u] = rate_pair[u][v]
        pairing_RB_needed[0][u] = min(RB_needed[0][u], RB_needed[1][v])
        pairing_traffic_demands[0][u] += traffic_demands[1][v]
        pairing_rate[1][v] = 0
        pairing_RB_needed[1][v] = 0
        pairing_traffic_demands[1][v] = 0
    #print('pairing_rate')
    #print(pairing_rate)
    pairing_alloc_RB_i, pairing_RB_waste_i, pairing_RB_used_i, pairing_sumrate_i, pairing_sumrate_bit_i, pairing_objective_value, pairing_wall_time = _exhausted_search(
        Z_single, pairing_RB_needed, pairing_rate, rate_pair, rate_reduce_ij,
        rate_reduce_ji, pairing_traffic_demands, time_threshold)
    #print('exhausted')
    #print(alloc_RB_i)

    #_print_RB(single_alloc_RB_i, RB_needed, rate, rate_reduce_ij, rate_reduce_ji, single_sumrate_i, 'singleton')
    value = [
        round(sum(sumrate_i, 2)) / num_time_slots,
        round(sum(single_sumrate_i, 2)) / num_time_slots,
        round(sum(pairing_sumrate_i, 2)) / num_time_slots
    ]
    bit = [
        sum(sumrate_bit_i),
        sum(single_sumrate_bit_i),
        sum(pairing_sumrate_bit_i)
    ]
    ## plot
    '''
    algo = ['opt', 'singleton']
    value = [round(sum(sumrate_i, 2)), round(sum(single_sumrate_i, 2))]
    plt.scatter(algo, value)
    plt.ylim(0, max(value) + 5)
    plt.show()
    print()
    '''
    return value, bit
Beispiel #5
0
def main(mode_idx, sim_times, exp):
    # [x] TODO: rb_needed bug // fix: rb_needed are decided by pair SNR
    # [ ] TODO: num_itf_users may be different

    # [ ] TODO: minimum pilot
    # [ ] TODO: different freq, different rate
    # [ ] TODO: real sim deployment

    #_test()

    # read mcs table file
    book = xlrd.open_workbook('CQI_index.xlsx')
    mcs_table = book.sheets()[0]
    #print (mcs_table.nrows)
    #print (mcs_table.ncols)
    #print (mcs_table.row_values(2))
    #print (mcs_table.cell(0,2).value)

    num_bs, num_subcarriers, num_time_slots, num_users, num_users_i = setting._setting(
    )
    #print(num_bs)

    all_bs = range(num_bs)
    all_users = range(num_users)
    all_subcarriers = range(num_subcarriers)
    all_time_slots = range(num_time_slots)
    all_users_i = []
    for i in all_bs:
        all_users_i.append(range(num_users_i[i]))

    opt_similar = [0 for i in range(5)]
    pair_similar = [0 for i in range(5)]
    #freq_similar = [0 for i in range(5)]
    if_similar = [0 for i in range(5)]
    #greedy_if_sumrate = [0 for i in range(5)]
    greedy_merge_similar = [0 for i in range(5)]

    opt_sumrate = [0 for i in range(5)]
    pair_sumrate = [0 for i in range(5)]
    freq_sumrate = [0 for i in range(5)]
    if_sumrate = [0 for i in range(5)]
    greedy_if_sumrate = [0 for i in range(5)]
    greedy_merge_sumrate = [0 for i in range(5)]

    mode_type = ['itf_percentage', 'td distribution']
    mode = mode_type[mode_idx]

    #sim_times = 1
    t = 0
    while t < sim_times:
        t += 1
        #print(t)
        sim_parameter = 0
        rtd_setting2.set_rate()
        if mode == mode_type[0]:
            itf_percent = 0
            lo = 200
            mean = exp
            rtd_setting2.set_traffic_demand(mean, lo)
        elif mode == mode_type[1]:
            pass
        while sim_parameter < 5:
            if mode == mode_type[0]:
                itf_percent += 1
            elif mode == mode_type[1]:
                itf_percent = 2
                lo = 200
                mean = [500, 700, 900, 1100, 1300]
                rtd_setting2.set_traffic_demand(mean[sim_parameter], lo)
            '''
            print(t)
            print('itf_percent:', itf_percent * 20)
            '''
            setting_SIC.set_itf_users(itf_percent)
            sim_parameter += 1

            #setting_SIC.set_itf_users(num_users // 5) # 20%
            #traffic_demands, SNR, SNR_db, rate, rate_reduce_ij, rate_reduce_ji, SNR_reduce_ij, SNR_reduce_ji, rate_reduce, rate_pair = _rate_traffic_demand_setting()
            #rtd_setting2.init()
            traffic_demands, SNR, SNR_db, rate = rtd_setting2._rtd_setting2()
            num_itf_users, itf_idx_i, rate_reduce_ij, rate_reduce_ji, SNR_reduce_ij, SNR_reduce_ji, rate_reduce, rate_pair = setting_SIC._setting_SIC(
            )

            # pairing matrix
            Z = [[1 for col in all_users_i[1]] for row in all_users_i[0]]
            #print (Z)
            for i in all_users_i[0]:
                #Z.append([])
                for j in all_users_i[1]:
                    if i in itf_idx_i[0] or j in itf_idx_i[1]:
                        Z[i][j] = 0

            for i in itf_idx_i[0]:
                for j in itf_idx_i[1]:
                    if i == j:
                        Z[i][j] = 1
            #print('Z:')
            #print(Z)

            # record the matching pair user
            # TODO: itf_idx_i
            match = {}
            for i in all_users_i[0]:
                #Z.append([])
                for j in all_users_i[1]:
                    if Z[i][j] == 1 and i in itf_idx_i[0] and j in itf_idx_i[1]:
                        match[0, i] = j
                        match[1, j] = i
            #print('match:')
            #print(match)

            # TODO: rb_needed may be sufficient(cause program stopping)

            RB_needed = []
            for i in all_bs:
                RB_needed.append([])
                for u in all_users_i[i]:
                    reduce = 0
                    if u in itf_idx_i[i]:
                        v = match[i, u]
                        if i == 0:
                            reduce = SNR_reduce_ij[u][v]
                        else:
                            reduce = SNR_reduce_ji[u][v]
                    snr = SNR[i][u] - reduce
                    snr_db = 10 * math.log(snr, 10)
                    for idx in range(1, mcs_table.nrows):
                        if snr_db <= mcs_table.cell(idx, 4).value:
                            rb = traffic_demands[i][u] / (
                                mcs_table.cell(idx - 1, 3).value * 84)
                            RB_needed[i].append(math.ceil(rb))
                            break
            #print('RB_needed: ')
            #print(RB_needed)
            for i in all_bs:
                for u in all_users_i[i]:
                    #RB_needed[i][u] = random.randint(3, 7)
                    #RB_needed[i][u] = 6
                    pass
            '''
            RB_needed[0][0] = 3
            RB_needed[1][0] = 4
            RB_needed[0][1] = 3
            RB_needed[1][1] = 3  
            RB_needed[0][2] = 3
            RB_needed[1][2] = 2
            RB_needed[0][3] = 5
            RB_needed[1][3] = 3
            RB_needed[0][4] = 3
            RB_needed[1][4] = 2
            '''
            #print('RB_needed: ')
            #print(RB_needed)
            #break
            time_threshold = 10.0

            #-----------------------------------------------------------------------
            ## optimal solution
            ## exhausted search
            alloc_RB_i, RB_waste_i, RB_used_i, sumrate_i, sumrate_bit_i, objective_value, wall_time, status = _exhausted_search(
                Z, RB_needed, rate, rate_pair, rate_reduce_ij, rate_reduce_ji,
                traffic_demands, time_threshold, False)
            if round(sum(sumrate_i), 4) != objective_value / 10000:
                print('objective function wrong!!')
            #print('exhausted')
            #print(alloc_RB_i)
            print('--------------------------------')
            print(status)
            print('Wall time:', round(wall_time))
            print('--------------------------------')
            _print_RB(alloc_RB_i, RB_needed, rate, rate_reduce_ij,
                      rate_reduce_ji, sumrate_i, 'exhausted search')

            #-----------------------------------------------------------------------
            ## proposed greedy algorithm
            ## greedy merge
            RB_needed_cp = deepcopy(RB_needed)
            greedy_m_alloc_RB_i, greedy_m_sumrate_i, greedy_m_RB_used_i = _greedy_merge(
                match, RB_needed_cp, rate, rate_pair, rate_reduce_ij,
                rate_reduce_ji)
            _print_RB(greedy_m_alloc_RB_i, RB_needed, rate, rate_reduce_ij,
                      rate_reduce_ji, greedy_m_sumrate_i, 'greedy merge')

            #-----------------------------------------------------------------------
            ## benchmarking algorithm
            ## greedy (always pair)
            ## greedy algorithm (pilot)
            RB_needed_cp = deepcopy(RB_needed)
            pilot = 2
            greedy_pair_alloc_RB_i, greedy_pair_sumrate_i, greedy_pair_RB_used_i = _greedy_pilot(
                match, RB_needed_cp, rate, rate_pair, rate_reduce_ij,
                rate_reduce_ji, pilot)
            _print_RB(greedy_pair_alloc_RB_i, RB_needed, rate, rate_reduce_ij,
                      rate_reduce_ji, greedy_pair_sumrate_i, 'greedy_pilot')
            #print('greedy:')
            #print(greedy_alloc_RB_i)

            #------------------------------------------------------------------------
            ## exhausted search interference-free
            # pairing matrix
            RB_needed_cp = deepcopy(RB_needed)
            Z_itf_free = [[1 for col in all_users_i[1]]
                          for row in all_users_i[0]]
            #print (Z)
            for i in all_users_i[0]:
                #Z.append([])
                for j in all_users_i[1]:
                    if i in itf_idx_i[0] or j in itf_idx_i[1]:
                        Z_itf_free[i][j] = 0
            if_alloc_RB_i, if_RB_waste_i, if_RB_used_i, if_sumrate_i, if_sumrate_bit_i, if_objective_value, if_wall_time, if_status = _exhausted_search(
                Z_itf_free, RB_needed_cp, rate, rate_pair, rate_reduce_ij,
                rate_reduce_ji, traffic_demands, time_threshold, False)
            #_print_RB(if_alloc_RB_i, RB_needed, rate, rate_reduce_ij, rate_reduce_ji, if_sumrate_i, 'interference free')

            #------------------------------------------------------------------------
            ## greedy interference-free (muting)
            RB_needed_cp = deepcopy(RB_needed)
            greedy_if_alloc_RB_i, greedy_if_sumrate_i, greedy_if_RB_used_i = _greedy_muting(
                RB_needed_cp, rate)

            ## similarity of allocated RB
            greedy_m_similar_RB_i = [0 for i in all_bs]
            greedy_pair_similar_RB_i = [0 for i in all_bs]
            if_similar_RB_i = [0 for i in all_bs]

            for i in all_bs:
                for u in all_users_i[i]:
                    greedy_m_similar_RB_i[i] += min(RB_used_i[i][u],
                                                    greedy_m_RB_used_i[i][u])
                    greedy_pair_similar_RB_i[i] += min(
                        RB_used_i[i][u], greedy_pair_RB_used_i[i][u])
                    if_similar_RB_i[i] += min(RB_used_i[i][u],
                                              if_RB_used_i[i][u])
                greedy_m_similar_RB_i[
                    i] += num_subcarriers * num_time_slots - max(
                        sum(RB_used_i[i]), sum(greedy_m_RB_used_i[i]))
                greedy_pair_similar_RB_i[
                    i] += num_subcarriers * num_time_slots - max(
                        sum(RB_used_i[i]), sum(greedy_pair_RB_used_i[i]))
                if_similar_RB_i[i] += num_subcarriers * num_time_slots - max(
                    sum(RB_used_i[i]), sum(if_RB_used_i[i]))
                greedy_m_similar_RB_i[i] /= num_subcarriers * num_time_slots
                greedy_pair_similar_RB_i[i] /= num_subcarriers * num_time_slots
                if_similar_RB_i[i] /= num_subcarriers * num_time_slots
            ##Performance comparison between different RA algorithm
            if round(sum(sumrate_i), 4) >= round(sum(greedy_m_sumrate_i), 4):
                os.system('cls')
            print('sim times:', t)
            if mode == mode_type[0]:
                print('itf_percent:', itf_percent * 20)
            elif mode == mode_type[1]:
                print('mean:', mean[sim_parameter - 1])
            #print('RB_needed')
            #print(RB_needed)
            print('------Comparison--------')
            '''
            for i in all_bs:
                print()
                print('BS', i, 'sumrate')
                print('Exhausted search:', round(sumrate_i[i], 4))
                print('Greedy_pilot:', round(greedy_sumrate_i[i], 4))
                print('Greedy_freq:', round(greedy_f_sumrate_i[i], 4))
            '''
            print('------------------------')
            print('Total Sumrate')
            print('Exhausted search:', round(sum(sumrate_i), 4), '(', status,
                  ')')
            print('Greedy merge:', round(sum(greedy_m_sumrate_i), 4))
            print('Greedy pair:', round(sum(greedy_pair_sumrate_i), 4))
            print('interference free:', round(sum(if_sumrate_i), 4))
            #print('Greedy muting', round(sum(greedy_if_sumrate_i), 4))
            #print('Greedy_freq:', round(sum(greedy_f_sumrate_i), 4))
            print('------------------------')
            if round(sum(sumrate_i), 4) < round(sum(greedy_m_sumrate_i), 4):
                #os.system('pause')
                pass

            if round(sum(sumrate_i), 4) < round(sum(greedy_pair_sumrate_i),
                                                4) and status == 'optimal':
                #os.system('pause')
                pass

            print('------------------------')
            print('Similarity')
            for i in all_bs:
                print('BS', i, 'similarity')
                #print('Exhausted search:', round(sum(sumrate_i), 4), '(', status, ')')
                print('Greedy merge:', round(greedy_m_similar_RB_i[i], 4))
                print('Greedy pair:', round(greedy_pair_similar_RB_i[i], 4))
                print('interference free:', round(if_similar_RB_i[i], 4))
            print('total similarity')
            print('Greedy merge:',
                  round(sum(greedy_m_similar_RB_i) / num_bs, 4))
            print('Greedy pair:',
                  round(sum(greedy_pair_similar_RB_i) / num_bs, 4))
            print('interference free:',
                  round(sum(if_similar_RB_i) / num_bs, 4))
            print('------------------------')

            #opt_similar[sim_parameter - 1] += round(sum(sumrate_i), 4)
            pair_similar[sim_parameter -
                         1] += sum(greedy_pair_similar_RB_i) / num_bs
            #freq_sumrate[itf_percent - 1] += round(sum(greedy_f_sumrate_i), 4)
            if_similar[sim_parameter - 1] += sum(if_similar_RB_i) / num_bs
            #greedy_if_sumrate[sim_parameter - 1] += round(sum(greedy_if_sumrate_i), 4)
            greedy_merge_similar[sim_parameter -
                                 1] += sum(greedy_m_similar_RB_i) / num_bs

            opt_sumrate[sim_parameter - 1] += round(sum(sumrate_i), 4)
            pair_sumrate[sim_parameter - 1] += round(
                sum(greedy_pair_sumrate_i), 4)
            #freq_sumrate[itf_percent - 1] += round(sum(greedy_f_sumrate_i), 4)
            if_sumrate[sim_parameter - 1] += round(sum(if_sumrate_i), 4)
            greedy_if_sumrate[sim_parameter - 1] += round(
                sum(greedy_if_sumrate_i), 4)
            greedy_merge_sumrate[sim_parameter - 1] += round(
                sum(greedy_m_sumrate_i), 4)
            ## plot
            '''
            algo = ['exhausted', 'greedy_pilot', 'greedy_freq']
            value = [round(sum(sumrate_i, 2)), round(sum(greedy_sumrate_i, 2)), round(sum(greedy_f_sumrate_i, 2))]
            plt.scatter(algo, value)
            plt.ylim(0, max(value) + 5)
            plt.show()
            print()
            '''
            #break
        os.system('cls')
    for i in range(len(opt_sumrate)):
        opt_sumrate[i] /= sim_times
        pair_sumrate[i] /= sim_times
        #freq_sumrate[i] /= sim_times
        if_sumrate[i] /= sim_times
        greedy_if_sumrate[i] /= sim_times
        greedy_merge_sumrate[i] /= sim_times
        opt_sumrate[i] *= 0.18
        pair_sumrate[i] *= 0.18
        #freq_sumrate[i] *= 0.18
        if_sumrate[i] *= 0.18
        greedy_if_sumrate[i] *= 0.18
        greedy_merge_sumrate[i] *= 0.18

    #
    data = [[] for i in range(4)]

    data[0] = opt_sumrate
    data[1] = greedy_merge_sumrate
    data[2] = pair_sumrate
    data[3] = if_sumrate

    today = date.today()
    d = today.strftime("%m%d")
    path = 'C:\\Users\\srtun\\Desktop\\thesis\\user pairing\\'

    if mode == mode_type[0]:
        exp_str = str(exp)
        filename = path + 'sim1_exp=' + exp_str + '_' + d + '.dat'
        np.savetxt(filename, data)
    elif mode == mode_type[1]:
        filename = path + 'sim2_' + d + '.dat'
        np.savetxt(filename, data)
    #np.savetxt("sim1_data.dat",data)
    '''
    algo = ['opt', 'pair', 'if', 'greedy_if', 'greedy']
    #value = [round(sum(sumrate_i, 2)), round(sum(single_sumrate_i, 2))]
    x = ['20','40','60','80','100']
    if mode == mode_type[1]:
        for i in range(len(x)):
            x[i] = str(mean[i]) 
    plt.plot(x, opt_sumrate, label = 'optimal', color = 'blue', linewidth = 3 , marker = '^')
    plt.plot(x, pair_sumrate, label = 'pair', color = 'red', linewidth = 3 ,marker = 'o')
    plt.plot(x, if_sumrate, label = 'interference free', color = 'cyan',linewidth = 3 , marker = 's')
    #plt.plot(x, greedy_if_sumrate, label = 'greedy_if', color = 'yellow', marker = 'x')
    plt.plot(x, greedy_merge_sumrate, label = 'greedy', color = 'green',linewidth = 3 , marker = 'D')
    plt.ylabel('sumrate(Mbps)', fontsize = 20)
    if mode == mode_type[0]:
        plt.xlabel('percentage of interfering UEs(%)', fontsize = 20)
    elif mode == mode_type[1]:
        plt.xlabel('mean of traffic demands(bits)', fontsize = 20)
    plt.ylim(ymin = 0)
    plt.xticks(fontsize = 20)
    plt.yticks(fontsize = 20)
    #plt.scatter(algo, sumrate)
    #plt.ylim(0, max(sumrate) + 2)
    plt.legend(fontsize = 20 )
    #plt.show() 
    if mode == mode_type[0]:
        #txt = 'proportion' 
        #plt.savefig('proportion.png', dpi = 200 , bbox_inches='tight')
        pass
    elif mode == mode_type[1]:
        #plt.savefig('traffic_demand.png', dpi = 200 , bbox_inches='tight')
        pass
    #plt.show()
    '''
    algo = ['opt', 'pair', 'if', 'greedy_if', 'greedy']
    #value = [round(sum(sumrate_i, 2)), round(sum(single_sumrate_i, 2))]
    x = ['20', '40', '60', '80', '100']
    if mode == mode_type[1]:
        for i in range(len(x)):
            x[i] = str(mean[i])
    #plt.plot(x, opt_similar, label = 'optimal', color = 'blue', linewidth = 3 , marker = '^')
    plt.plot(x,
             pair_similar,
             label='pair',
             color='red',
             linewidth=3,
             marker='o')
    plt.plot(x,
             if_similar,
             label='interference free',
             color='cyan',
             linewidth=3,
             marker='s')
    #plt.plot(x, greedy_if_sumrate, label = 'greedy_if', color = 'yellow', marker = 'x')
    plt.plot(x,
             greedy_merge_similar,
             label='greedy',
             color='green',
             linewidth=3,
             marker='D')
    plt.ylabel('sumrate(Mbps)', fontsize=20)
    if mode == mode_type[0]:
        plt.xlabel('percentage of interfering UEs(%)', fontsize=20)
    elif mode == mode_type[1]:
        plt.xlabel('mean of traffic demands(bits)', fontsize=20)
    plt.ylim(ymin=0)
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    #plt.scatter(algo, sumrate)
    #plt.ylim(0, max(sumrate) + 2)
    plt.legend(fontsize=20)
    #plt.show()
    if mode == mode_type[0]:
        #txt = 'proportion'
        #plt.savefig('proportion.png', dpi = 200 , bbox_inches='tight')
        pass
    elif mode == mode_type[1]:
        #plt.savefig('traffic_demand.png', dpi = 200 , bbox_inches='tight')
        pass
Beispiel #6
0
def _greedy_freq(match, RB_needed, rate, rate_pair, rate_reduce_ij,
                 rate_reduce_ji):
    # old version, need to be modified
    # [ ] TODO: 2/27 check wasted RB after all alloc??
    # [x] TODO: method which not be taken will waste the traffic demands
    # [x] TODO: better method may have wasted RB
    # [ ] TODO: pilot check
    # [ ] TODO: fairness (user fairness?? method fairness??)
    #by times (same method should't be taken more than x time slots consistently)
    #by weight (starving method increase the weight)
    # [ ] TODO: benchmarking algo? baseline
    #random
    #round robin

    num_bs, num_subcarriers, num_time_slots, num_users, num_users_i = setting._setting(
    )

    all_bs = range(num_bs)
    all_users = range(num_users)
    all_subcarriers = range(num_subcarriers)
    all_time_slots = range(num_time_slots)
    all_users_i = []
    for i in all_bs:
        all_users_i.append(range(num_users_i[i]))

    num_itf_users, itf_idx_i, rate_reduce_ij, rate_reduce_ji, SNR_reduce_ij, SNR_reduce_ji, rate_reduce, rate_pair = setting_SIC._setting_SIC(
    )

    #algorithm start

    ## sorting single users by rate
    queue_single_i = []
    for i in all_bs:
        queue_single_i.append(
            sorted(range(len(rate[i]) - num_itf_users),
                   key=lambda u: rate[i][u]))  #sort user by rate using index
    #print(queue_single_i)

    ## sorting pair users by rate
    queue_pair = []
    for u in all_users_i[0]:
        if u not in itf_idx_i[0]:
            queue_pair.append(0)
        else:
            queue_pair.append(rate_pair[u][match[0, u]])
    #sort all user and delete single users
    queue_pair = sorted(range(len(queue_pair)), key=lambda u: queue_pair[u])
    queue_pair = queue_pair[len(queue_pair) - num_itf_users:]
    #print(queue_pair)

    sumrate_i = [0 for i in all_bs]
    alloc_RB_i = []
    for i in all_bs:
        alloc_RB_i.append([])

    ## each time slot choose the better side (single or pair)
    for t in all_time_slots:
        #alloc_RB_i.append([])
        sumrate_single_i = [0 for i in all_bs
                            ]  # sumrate of single users in this time slot
        sumrate_pair_i = [0 for i in all_bs
                          ]  # sumrate of pair users in this time slot
        #sumrate_pair = 0
        f = num_subcarriers
        single_alloc_RB_i = [['x' for f in all_subcarriers] for i in all_bs]
        #single_sumrate_i = [0 for i in all_bs]
        pair_alloc_RB_i = [['x' for f in all_subcarriers] for i in all_bs]
        #pair_sumrate_i = [0 for i in all_bs]

        single_RB_return = [[0 for u in all_users_i[j]] for j in all_bs]
        single_temp_queue = [[] for i in all_bs]

        ## choosing best single user
        for i in all_bs:
            f = num_subcarriers
            while (f):
                if not queue_single_i[i]:
                    break
                elif RB_needed[i][queue_single_i[i][-1]] <= f:
                    sumrate_single_i[i] += rate[i][queue_single_i[i][
                        -1]] * RB_needed[i][queue_single_i[i][-1]]
                    #f -= RB_needed[queue_single_i[i][-1]]
                    for n in range(RB_needed[i][queue_single_i[i][-1]]):
                        f -= 1
                        single_alloc_RB_i[i][f] = queue_single_i[i][-1]
                        single_RB_return[i][queue_single_i[i][-1]] += 1
                    RB_needed[i][queue_single_i[i][-1]] = 0
                    single_temp_queue[i].append(queue_single_i[i][-1])
                    queue_single_i[i].pop()
                else:
                    sumrate_single_i[i] += rate[i][queue_single_i[i][-1]] * f
                    RB_needed[i][queue_single_i[i][-1]] -= f
                    while (f):
                        f -= 1
                        single_alloc_RB_i[i][f] = queue_single_i[i][-1]
                        single_RB_return[i][queue_single_i[i][-1]] += 1
                    #f = 0

        ## choosing best pairing users
        pair_RB_return = [[0 for u in all_users_i[j]] for j in all_bs]
        pair_temp_queue = []
        f = num_subcarriers
        while (f):
            if not queue_pair:
                break
            min_rb = min(RB_needed[0][queue_pair[-1]],
                         RB_needed[1][match[0, queue_pair[-1]]])
            if min_rb <= f:
                #sumrate_pair += rate_pair[queue_pair[-1]][ match[0, queue_pair[-1] ] ] * min_rb
                sumrate_pair_i[0] += (rate[0][queue_pair[-1]] - rate_reduce_ij[
                    queue_pair[-1]][match[0, queue_pair[-1]]]) * min_rb
                sumrate_pair_i[1] += (rate[1][match[0, queue_pair[-1]]] -
                                      rate_reduce_ji[match[0, queue_pair[-1]]][
                                          queue_pair[-1]]) * min_rb
                #if sumrate_pair != sum(sumrate_pair_i):
                #os.system('pause')
                #f -= min_rb
                for n in range(min_rb):
                    f -= 1
                    pair_alloc_RB_i[0][f] = queue_pair[-1]
                    pair_alloc_RB_i[1][f] = match[0, queue_pair[-1]]
                    pair_RB_return[0][queue_pair[-1]] += 1
                    pair_RB_return[1][match[0, queue_pair[-1]]] += 1
                RB_needed[0][queue_pair[-1]] -= min_rb
                RB_needed[1][match[0, queue_pair[-1]]] -= min_rb
                pair_temp_queue.append(queue_pair[-1])
                queue_pair.pop()
            else:
                #sumrate_pair += rate_pair[queue_pair[-1]][ match[0, queue_pair[-1] ] ] * f
                sumrate_pair_i[0] += (rate[0][queue_pair[-1]] - rate_reduce_ij[
                    queue_pair[-1]][match[0, queue_pair[-1]]]) * f
                sumrate_pair_i[1] += (rate[1][match[0, queue_pair[-1]]] -
                                      rate_reduce_ji[match[0, queue_pair[-1]]][
                                          queue_pair[-1]]) * f
                RB_needed[0][queue_pair[-1]] -= f
                RB_needed[1][match[0, queue_pair[-1]]] -= f
                while (f):
                    f -= 1
                    pair_alloc_RB_i[0][f] = queue_pair[-1]
                    pair_alloc_RB_i[1][f] = match[0, queue_pair[-1]]
                    pair_RB_return[0][queue_pair[-1]] += 1
                    pair_RB_return[1][match[0, queue_pair[-1]]] += 1
                #f = 0
        #print('pair')
        #print(pair_alloc_RB_i)

        ## comparison
        ## choose pair users to allcoate
        if sum(sumrate_pair_i) > sum(sumrate_single_i):
            for i in all_bs:
                alloc_RB_i[i].append(pair_alloc_RB_i[i])
                sumrate_i[i] += sumrate_pair_i[i] / 10000
                # return the state of single user
                while (single_temp_queue[i]):
                    queue_single_i[i].append(single_temp_queue[i][-1])
                    single_temp_queue[i].pop()
                for u in all_users_i[i]:
                    RB_needed[i][u] += single_RB_return[i][u]
            # allocate wasted RB to single user
            wasted_RB = num_subcarriers - max(sum(pair_RB_return[0]),
                                              sum(pair_RB_return[1]))
            for i in all_bs:
                f = wasted_RB
                while (f):
                    if not queue_single_i[i]:
                        break
                    elif RB_needed[i][queue_single_i[i][-1]] <= f:
                        sumrate_i[i] += rate[i][queue_single_i[i][
                            -1]] * RB_needed[i][queue_single_i[i][-1]] / 10000
                        for n in range(RB_needed[i][queue_single_i[i][-1]]):
                            f -= 1
                            alloc_RB_i[i][t][f] = queue_single_i[i][-1]
                            #single_RB_return[i][queue_single_i[i][-1]] += 1
                        RB_needed[i][queue_single_i[i][-1]] = 0
                        queue_single_i[i].pop()
                    else:
                        sumrate_i[i] += rate[i][queue_single_i[i]
                                                [-1]] * f / 10000
                        RB_needed[i][queue_single_i[i][-1]] -= f
                        while (f):
                            f -= 1
                            alloc_RB_i[i][t][f] = queue_single_i[i][-1]
                            #single_RB_return[i][queue_single_i[i][-1]] += 1

        ## choose single user to allocate
        else:
            for i in all_bs:
                alloc_RB_i[i].append(single_alloc_RB_i[i])
                sumrate_i[i] += sumrate_single_i[i] / 10000
            # return the state of pair users
            while (pair_temp_queue):
                queue_pair.append(pair_temp_queue[-1])
                pair_temp_queue.pop()
            for i in all_bs:
                for u in all_users_i[i]:
                    RB_needed[i][u] += pair_RB_return[i][u]

            # allocate wasted RB to pair users
            wasted_RB = num_subcarriers - max(sum(single_RB_return[0]),
                                              sum(single_RB_return[1]))
            f = wasted_RB
            while (f):
                if not queue_pair:
                    break
                min_rb = min(RB_needed[0][queue_pair[-1]],
                             RB_needed[1][match[0, queue_pair[-1]]])
                if min_rb <= f:
                    sumrate_i[0] += (rate[0][queue_pair[-1]] -
                                     rate_reduce_ij[queue_pair[-1]][match[
                                         0, queue_pair[-1]]]) * min_rb / 10000
                    sumrate_i[1] += (rate[1][match[0, queue_pair[-1]]] -
                                     rate_reduce_ji[match[0, queue_pair[-1]]][
                                         queue_pair[-1]]) * min_rb / 10000
                    for n in range(min_rb):
                        f -= 1
                        alloc_RB_i[0][t][f] = queue_pair[-1]
                        alloc_RB_i[1][t][f] = match[0, queue_pair[-1]]
                        #pair_RB_return[0][queue_pair[-1]] += 1
                        #pair_RB_return[1][match[0, queue_pair[-1]]] += 1
                    RB_needed[0][queue_pair[-1]] -= min_rb
                    RB_needed[1][match[0, queue_pair[-1]]] -= min_rb
                    #pair_temp_queue.append(queue_pair[-1])
                    queue_pair.pop()
                else:
                    sumrate_i[0] += (rate[0][queue_pair[-1]] - rate_reduce_ij[
                        queue_pair[-1]][match[0, queue_pair[-1]]]) * f / 10000
                    sumrate_i[1] += (rate[1][match[0, queue_pair[-1]]] -
                                     rate_reduce_ji[match[0, queue_pair[-1]]][
                                         queue_pair[-1]]) * f / 10000
                    RB_needed[0][queue_pair[-1]] -= f
                    RB_needed[1][match[0, queue_pair[-1]]] -= f
                    while (f):
                        f -= 1
                        alloc_RB_i[0][t][f] = queue_pair[-1]
                        alloc_RB_i[1][t][f] = match[0, queue_pair[-1]]
                        #pair_RB_return[0][queue_pair[-1]] += 1
                        #pair_RB_return[1][match[0, queue_pair[-1]]] += 1

    RB_used_i = [[0 for u in all_users_i[i]] for i in all_bs]
    #print(RB_used_i)
    for i in all_bs:
        for t in all_time_slots:
            for f in all_subcarriers:
                if alloc_RB_i[i][t][f] != 'x':
                    RB_used_i[i][alloc_RB_i[i][t][f]] += 1

    #a = 0
    #print(alloc_RB_i)

    #transpose alloc_RB_i[i][t][f] into alloc_RB_i[i][f][t]
    for i in all_bs:
        alloc_RB_i[i] = list(map(list, zip(*alloc_RB_i[i])))
        pass
    #print(alloc_RB_i)
    return alloc_RB_i, sumrate_i, RB_used_i
Beispiel #7
0
def _greedy_pilot_fix(match, RB_needed, rate, rate_pair, rate_reduce_ij,
                      rate_reduce_ji, pilot):
    # [ ] TODO: 2/27 check wasted RB after all alloc??
    # [ ] TODO: 3/2 with pilot number, pair user only choose 1 pair?
    # [ ] TODO: 3/3 itf user need to be added in single queue
    # [x] TODO: method which not be taken will waste the traffic demands
    # [x] TODO: better method may have wasted RB
    # [ ] TODO: pilot check
    # [ ] TODO: fairness
    # [ ] TODO: benchmarking algo? baseline
    #random
    #round robin

    num_bs, num_subcarriers, num_time_slots, num_users, num_users_i = setting._setting(
    )

    all_bs = range(num_bs)
    all_users = range(num_users)
    all_subcarriers = range(num_subcarriers)
    all_time_slots = range(num_time_slots)
    all_users_i = []
    for i in all_bs:
        all_users_i.append(range(num_users_i[i]))

    num_itf_users, itf_idx_i, rate_reduce_ij, rate_reduce_ji, SNR_reduce_ij, SNR_reduce_ji, rate_reduce, rate_pair = setting_SIC._setting_SIC(
    )

    #algorithm start

    ## sorting single users by rate
    queue_single_i = []
    for i in all_bs:
        queue_single_i.append(
            sorted(range(len(rate[i]) - num_itf_users),
                   key=lambda u: rate[i][u]))  #sort user by rate using index
    #print(queue_single_i)

    ## sorting pair users by rate
    queue_pair = []
    for u in all_users_i[0]:
        if u not in itf_idx_i[0]:
            queue_pair.append(0)
        else:
            queue_pair.append(rate_pair[u][match[0, u]])
    #sort all user and delete single users
    queue_pair = sorted(range(len(queue_pair)), key=lambda u: queue_pair[u])
    queue_pair = queue_pair[len(queue_pair) - num_itf_users:]
    #print(queue_pair)

    sumrate_i = [0 for i in all_bs]
    alloc_RB_i = []
    for i in all_bs:
        alloc_RB_i.append([])

    ## each time slot choose the better side (single or pair)
    for t in all_time_slots:
        for i in all_bs:
            alloc_RB_i[i].append(['x' for f in all_subcarriers])
        #alloc_RB_i.append([])
        f = num_subcarriers
        #pilot = 2

        while (f):
            sumrate_single_i = [0 for i in all_bs
                                ]  # sumrate of single users in this time slot
            sumrate_pair_i = [0 for i in all_bs
                              ]  # sumrate of pair users in this time slot
            single_alloc_RB_i = [['x' for f in range(pilot)] for i in all_bs]
            #single_sumrate_i = [0 for i in all_bs]
            pair_alloc_RB_i = [['x' for f in range(pilot)] for i in all_bs]
            #pair_sumrate_i = [0 for i in all_bs]

            single_RB_return = [[0 for u in all_users_i[j]] for j in all_bs]
            single_temp_queue = [[] for i in all_bs]

            ## choosing best single user
            for i in all_bs:
                #f = num_subcarriers
                p = pilot
                while (p):
                    if not queue_single_i[i]:
                        break
                    elif RB_needed[i][queue_single_i[i][-1]] <= p:
                        sumrate_single_i[i] += rate[i][queue_single_i[i][
                            -1]] * RB_needed[i][queue_single_i[i][-1]]
                        #f -= RB_needed[queue_single_i[i][-1]]
                        for n in range(RB_needed[i][queue_single_i[i][-1]]):
                            p -= 1
                            single_alloc_RB_i[i][p] = queue_single_i[i][-1]
                            single_RB_return[i][queue_single_i[i][-1]] += 1
                        RB_needed[i][queue_single_i[i][-1]] = 0
                        single_temp_queue[i].append(queue_single_i[i][-1])
                        queue_single_i[i].pop()
                    else:
                        sumrate_single_i[i] += rate[i][queue_single_i[i]
                                                       [-1]] * p
                        RB_needed[i][queue_single_i[i][-1]] -= p
                        while (p):
                            p -= 1
                            single_alloc_RB_i[i][p] = queue_single_i[i][-1]
                            single_RB_return[i][queue_single_i[i][-1]] += 1
                        #f = 0

            ## choosing best pairing users
            pair_RB_return = [[0 for u in all_users_i[j]] for j in all_bs]
            pair_temp_queue = []
            #f = num_subcarriers
            p = pilot
            while (p):
                if not queue_pair:
                    break
                min_rb = min(RB_needed[0][queue_pair[-1]],
                             RB_needed[1][match[0, queue_pair[-1]]])
                if min_rb <= p:
                    #sumrate_pair += rate_pair[queue_pair[-1]][ match[0, queue_pair[-1] ] ] * min_rb
                    sumrate_pair_i[0] += (rate[0][queue_pair[-1]] -
                                          rate_reduce_ij[queue_pair[-1]][match[
                                              0, queue_pair[-1]]]) * min_rb
                    sumrate_pair_i[1] += (
                        rate[1][match[0, queue_pair[-1]]] - rate_reduce_ji[
                            match[0, queue_pair[-1]]][queue_pair[-1]]) * min_rb
                    #if sumrate_pair != sum(sumrate_pair_i):
                    #os.system('pause')
                    #f -= min_rb
                    for n in range(min_rb):
                        p -= 1
                        pair_alloc_RB_i[0][p] = queue_pair[-1]
                        pair_alloc_RB_i[1][p] = match[0, queue_pair[-1]]
                        pair_RB_return[0][queue_pair[-1]] += 1
                        pair_RB_return[1][match[0, queue_pair[-1]]] += 1
                    RB_needed[0][queue_pair[-1]] -= min_rb
                    RB_needed[1][match[0, queue_pair[-1]]] -= min_rb
                    pair_temp_queue.append(queue_pair[-1])
                    queue_pair.pop()
                else:
                    #sumrate_pair += rate_pair[queue_pair[-1]][ match[0, queue_pair[-1] ] ] * f
                    sumrate_pair_i[0] += (rate[0][queue_pair[-1]] -
                                          rate_reduce_ij[queue_pair[-1]][match[
                                              0, queue_pair[-1]]]) * p
                    sumrate_pair_i[1] += (
                        rate[1][match[0, queue_pair[-1]]] - rate_reduce_ji[
                            match[0, queue_pair[-1]]][queue_pair[-1]]) * p
                    RB_needed[0][queue_pair[-1]] -= p
                    RB_needed[1][match[0, queue_pair[-1]]] -= p
                    while (p):
                        p -= 1
                        pair_alloc_RB_i[0][p] = queue_pair[-1]
                        pair_alloc_RB_i[1][p] = match[0, queue_pair[-1]]
                        pair_RB_return[0][queue_pair[-1]] += 1
                        pair_RB_return[1][match[0, queue_pair[-1]]] += 1
                    #f = 0
            #print('pair')
            #print(pair_alloc_RB_i)

            ## comparison
            ## choose pair users to allcoate
            if sum(sumrate_pair_i) > sum(sumrate_single_i):
                for i in all_bs:
                    for p in range(pilot):
                        alloc_RB_i[i][t][f - 1 -
                                         p] = pair_alloc_RB_i[i][pilot - 1 - p]
                    sumrate_i[i] += sumrate_pair_i[i] / 10000
                    # return the state of single user
                    while (single_temp_queue[i]):
                        queue_single_i[i].append(single_temp_queue[i][-1])
                        single_temp_queue[i].pop()
                    for u in all_users_i[i]:
                        RB_needed[i][u] += single_RB_return[i][u]
                # allocate wasted RB to single user
                wasted_RB = pilot - max(sum(pair_RB_return[0]),
                                        sum(pair_RB_return[1]))
                for i in all_bs:
                    p = wasted_RB
                    while (p):
                        if not queue_single_i[i]:
                            break
                        elif RB_needed[i][queue_single_i[i][-1]] <= p:
                            sumrate_i[i] += rate[i][
                                queue_single_i[i][-1]] * RB_needed[i][
                                    queue_single_i[i][-1]] / 10000
                            for n in range(
                                    RB_needed[i][queue_single_i[i][-1]]):
                                p -= 1
                                alloc_RB_i[i][t][f - pilot +
                                                 p] = queue_single_i[i][-1]
                                #single_RB_return[i][queue_single_i[i][-1]] += 1
                            RB_needed[i][queue_single_i[i][-1]] = 0
                            queue_single_i[i].pop()
                        else:
                            sumrate_i[i] += rate[i][queue_single_i[i]
                                                    [-1]] * p / 10000
                            RB_needed[i][queue_single_i[i][-1]] -= p
                            while (p):
                                p -= 1
                                alloc_RB_i[i][t][f - pilot +
                                                 p] = queue_single_i[i][-1]
                                #single_RB_return[i][queue_single_i[i][-1]] += 1

            ## choose single user to allocate
            else:
                for i in all_bs:
                    for p in range(pilot):
                        alloc_RB_i[i][t][f - 1 -
                                         p] = single_alloc_RB_i[i][pilot - 1 -
                                                                   p]
                    sumrate_i[i] += sumrate_single_i[i] / 10000
                # return the state of pair users
                while (pair_temp_queue):
                    queue_pair.append(pair_temp_queue[-1])
                    pair_temp_queue.pop()
                for i in all_bs:
                    for u in all_users_i[i]:
                        RB_needed[i][u] += pair_RB_return[i][u]

                # allocate wasted RB to pair users
                wasted_RB = pilot - max(sum(single_RB_return[0]),
                                        sum(single_RB_return[1]))
                p = wasted_RB
                while (p):
                    if not queue_pair:
                        break
                    min_rb = min(RB_needed[0][queue_pair[-1]],
                                 RB_needed[1][match[0, queue_pair[-1]]])
                    if min_rb <= p:
                        sumrate_i[0] += (
                            rate[0][queue_pair[-1]] -
                            rate_reduce_ij[queue_pair[-1]][match[
                                0, queue_pair[-1]]]) * min_rb / 10000
                        sumrate_i[1] += (
                            rate[1][match[0, queue_pair[-1]]] -
                            rate_reduce_ji[match[0, queue_pair[-1]]][
                                queue_pair[-1]]) * min_rb / 10000
                        for n in range(min_rb):
                            p -= 1
                            alloc_RB_i[0][t][f - pilot + p] = queue_pair[-1]
                            alloc_RB_i[1][t][f - pilot +
                                             p] = match[0, queue_pair[-1]]
                            #pair_RB_return[0][queue_pair[-1]] += 1
                            #pair_RB_return[1][match[0, queue_pair[-1]]] += 1
                        RB_needed[0][queue_pair[-1]] -= min_rb
                        RB_needed[1][match[0, queue_pair[-1]]] -= min_rb
                        #pair_temp_queue.append(queue_pair[-1])
                        queue_pair.pop()
                    else:
                        sumrate_i[0] += (rate[0][queue_pair[-1]] -
                                         rate_reduce_ij[queue_pair[-1]][match[
                                             0, queue_pair[-1]]]) * p / 10000
                        sumrate_i[1] += (
                            rate[1][match[0, queue_pair[-1]]] -
                            rate_reduce_ji[match[0, queue_pair[-1]]][
                                queue_pair[-1]]) * p / 10000
                        RB_needed[0][queue_pair[-1]] -= p
                        RB_needed[1][match[0, queue_pair[-1]]] -= p
                        while (p):
                            p -= 1
                            alloc_RB_i[0][t][f - pilot + p] = queue_pair[-1]
                            alloc_RB_i[1][t][f - pilot +
                                             p] = match[0, queue_pair[-1]]
                            #pair_RB_return[0][queue_pair[-1]] += 1
                            #pair_RB_return[1][match[0, queue_pair[-1]]] += 1
            f -= pilot
            #print(sumrate_i)
            pass
    RB_used_i = [[0 for u in all_users_i[i]] for i in all_bs]
    #print(RB_used_i)
    for i in all_bs:
        for t in all_time_slots:
            for f in all_subcarriers:
                if alloc_RB_i[i][t][f] != 'x':
                    RB_used_i[i][alloc_RB_i[i][t][f]] += 1
    #transpose alloc_RB_i[i][t][f] into alloc_RB_i[i][f][t]
    for i in all_bs:
        alloc_RB_i[i] = list(map(list, zip(*alloc_RB_i[i])))
        pass
    #a = 0
    return alloc_RB_i, sumrate_i, RB_used_i
Beispiel #8
0
def _greedy_muting(RB_needed, rate):

    num_bs, num_subcarriers, num_time_slots, num_users, num_users_i = setting._setting(
    )

    all_bs = range(num_bs)
    all_users = range(num_users)
    all_subcarriers = range(num_subcarriers)
    all_time_slots = range(num_time_slots)
    all_users_i = []
    for i in all_bs:
        all_users_i.append(range(num_users_i[i]))

    num_itf_users, itf_idx_i, rate_reduce_ij, rate_reduce_ji, SNR_reduce_ij, SNR_reduce_ji, rate_reduce, rate_pair = setting_SIC._setting_SIC(
    )

    ##---- algo start -------------------------------------
    queue_single_i = []
    queue_itf_i = []
    print(rate)
    #-- sorting single users by rate
    for i in all_bs:
        queue_single_i.append(
            sorted(range(len(rate[i]) - num_itf_users),
                   key=lambda u: rate[i][u]))
    print(queue_single_i)

    #-- sorting interfering users by rate
    for i in all_bs:
        queue_itf_i.append(
            sorted(range(len(rate[i]) - num_itf_users, len(rate[i])),
                   key=lambda u: rate[i][u]))
    print(queue_itf_i)

    alloc_RB_i = []
    for i in all_bs:
        alloc_RB_i.append([])

    for t in all_time_slots:
        for i in all_bs:
            alloc_RB_i[i].append(['x' for f in all_subcarriers])
        f = num_subcarriers
        while f:
            sumrate_single = 0
            sumrate_itf = 0

            #-- calculate best sumrate of single users
            for i in all_bs:
                if queue_single_i[i]:
                    sumrate_single += rate[i][queue_single_i[i][-1]]
            #print(sumrate_single)

            #-- calculate best sumrate of interfering users
            max_sumrate_i = [0 for i in all_bs]
            for i in all_bs:
                if queue_itf_i[i]:
                    max_sumrate_i[i] = rate[i][queue_itf_i[i][-1]]
            sumrate_itf = max(max_sumrate_i)
            #print(sumrate_itf)

            #-- no user to allocate
            if not sumrate_itf and not sumrate_single:
                break
            #-- choose single user to allocate
            elif sumrate_single >= sumrate_itf:
                for i in all_bs:
                    if queue_single_i[i]:
                        alloc_RB_i[i][t][f - 1] = queue_single_i[i][-1]
                        RB_needed[i][queue_single_i[i][-1]] -= 1
                        if RB_needed[i][queue_single_i[i][-1]] == 0:
                            queue_single_i[i].pop()

            #-- choose interfering user to allocate
            else:
                i = max_sumrate_i.index(max(max_sumrate_i))
                alloc_RB_i[i][t][f - 1] = queue_itf_i[i][-1]
                RB_needed[i][queue_itf_i[i][-1]] -= 1
                if RB_needed[i][queue_itf_i[i][-1]] == 0:
                    queue_itf_i[i].pop()
            f -= 1

    #print(alloc_RB_i[0])
    #print(alloc_RB_i[1])
    #print()
    RB_used_i = [[0 for u in all_users_i[i]] for i in all_bs]
    sumrate_i = [0 for i in all_bs]
    for i in all_bs:
        for t in all_time_slots:
            for f in all_subcarriers:
                if alloc_RB_i[i][t][f] != 'x':
                    RB_used_i[i][alloc_RB_i[i][t][f]] += 1
                    sumrate_i[i] += rate[i][alloc_RB_i[i][t][f]] / 10000
    #transpose alloc_RB_i[i][t][f] into alloc_RB_i[i][f][t]
    for i in all_bs:
        alloc_RB_i[i] = list(map(list, zip(*alloc_RB_i[i])))

    return alloc_RB_i, sumrate_i, RB_used_i