Example #1
0
def budget_killer(H, chargers, sensors, p_list):
    """utilize the remaining budget, return (Q, chargers, h_list)"""
    p_min = args["p_min"]
    Budget = args["B"]
    h_max = args["h_max"]

    # current cost
    cost = 0
    temp_c = []
    temp_h = []
    for (c, h) in H.iteritems():
        temp_c.append(c)
        temp_h.append(h)
        cost += power(h)

    max_power = total_power(sensors, p_list, temp_c, temp_h)
    while Budget - cost >= p_min:
        max_charger = None

        # current chosen sets
        temp_c = []
        temp_h = []
        for (c, h) in H.iteritems():
            temp_c.append(c)
            temp_h.append(h)

        for c in chargers:
            if H.get(c, 0) < h_max:
                temp_Q = total_power(sensors, p_list, temp_c + [c], temp_h + [1])
                if temp_Q > max_power:
                    max_power = temp_Q
                    max_charger = c

        if max_charger != None:
            cost += p_min
            H[max_charger] = H.get(max_charger, 0) + 1
        else:
            if DEBUG:
                print "Warning: remaining budget can not be full filled."
            break

    if DEBUG:
        print "============================================="
        print "#       utilize the remaining budget        #"
        print "============================================="
        print "Budget : %f" % cost

    result_c = []
    result_h = []
    for (c, h) in H.iteritems():
        result_c.append(c)
        result_h.append(h)

    result = (max_power, result_c, result_h)
    return result
Example #2
0
def greedy(chargers, sensors, p_list, func):
    """greedy algorithm, base on value function 'func'."""
    # initialize
    Budget = args["B"]
    b = 0
    Q = 0
    H = {}

    # construct matrix Z
    Z = [[(c, h) for h in xrange(1, args["h_max"] + 1)] for c in chargers]
    # Z'
    temp_c = []
    temp_h = []

    while b < Budget:
        max_power = 0
        max_delta = 0
        max_cost = 0
        max_index = None
        b_left = Budget - b

        # find the best charger & h in matrix Z
        for (i, c_h_list) in enumerate(Z):
            for (j, (c, h)) in enumerate(c_h_list):
                cost = power(h)
                if cost <= b_left:
                    temp_Q = total_power(sensors, p_list, temp_c + [c], temp_h + [h])
                    delta = func(temp_Q - Q, cost)
                    if delta > max_delta:
                        max_power = temp_Q
                        max_delta = delta
                        max_cost = cost
                        max_index = (i, j)

        # greedy!!!
        if max_index != None:
            # add max charger in Z' & remove from Z
            Q = max_power
            b += max_cost
            (i, j) = max_index
            (c, h) = Z[i][j]
            temp_c.append(c)
            temp_h.append(h)
            del Z[i][j]

            # if H[c] < h then H[c] <== h
            H[c] = max(H.get(c, 0), h)
        else:
            break

    result = (Q, H, temp_c, temp_h)
    if DEBUG:
        print "============================================="
        print "#           result of greedy part           #"
        print "============================================="
        pprint(result)

    return H
Example #3
0
def update(sensors, p_list):
    """update anser"""
    global g_Q
    global g_result
    global g_C
    global g_H

    temp_Q = total_power(sensors, p_list, g_C, g_H)
    if temp_Q > g_Q:
        g_Q = temp_Q
        g_result = (copy(g_C), copy(g_H))

        if DEBUG:
            print g_result, g_Q
Example #4
0
def greedy(chargers, h_list, sensors, p_list, func):
    """greedy part"""
    # initialize
    Budget = args['B']
    b = 0
    Q = 0
    result_c = []
    result_h = []

    while b < Budget:
        max_power = 0
        max_delta = 0
        max_cost = 0
        max_index = None
        b_left = Budget - b

        # find the charger which can increase the total power most
        for (index, (c, h)) in enumerate(zip(chargers, h_list)):
            cost = power(h)
            if cost <= b_left:
                temp_Q = total_power(sensors, p_list, result_c + [c], result_h + [h])
                delta = func(temp_Q - Q, cost)
                if delta > max_delta:
                    max_power = temp_Q
                    max_delta = delta
                    max_cost = cost
                    max_index = index

        # greedy the charger we found
        if max_index != None:
            Q = max_power
            b += max_cost
            result_c.append(chargers[max_index])
            result_h.append(h_list[max_index])
            del chargers[max_index]
            del h_list[max_index]
        else:
            break

    result = (Q, result_c, result_h)
    if DEBUG:
        print "============================================="
        print "#           result of greedy part           #"
        print "============================================="
        pprint(result)

    return result
Example #5
0
def random_solution(chargers, sensors, p_list):
    """get a random solution, return (Q, C, H)"""
    h_list = random_h();
    if DEBUG:
        print "============================================="
        print "#               random h list               #"
        print "============================================="
        pprint(h_list)

    C = []
    H = []

    for i in xrange(len(h_list)):
        if h_list[i] > 0:
            C.append(chargers[i])
            H.append(h_list[i])

    Q = total_power(sensors, p_list, C, H)

    return (Q, C, H)
Example #6
0
def charger_h(c, sensors, p_list):
    """calculate h for charger c(x, y) independently"""
    (ratio, h) = max([(total_power(sensors, p_list, [c], [h])/power(h), h) for h in xrange(1, args['h_max'] + 1)])
    return h
Example #7
0
File: iaa.py Project: kongfy/PPP
def solution(chargers, sensors, p_list, B, sensors_p, p_list_p, F, p_min):
    Q, C, H = TCA(chargers, sensors, p_list)
    Q_p, C_p, H_p = TCA(chargers, sensors_p, p_list_p)


    #F = int(B * F / p_min)
    while cost(C, H, C_p, H_p) > F:
        # op on h
        n_1 = len(C)
        n_2 = len(C_p)

        temp_i = None
        temp_max = 0

        # find index
        for i in xrange(n_2):
            for j in xrange(n_1):
                if C_p[i] == C[j]:
                    if H_p[i] > H[j]:
                        C_tt = copy.copy(C_p)
                        H_tt = copy.copy(H_p)
                        H_tt[i] -= 1
                        t = total_power(sensors_p, p_list_p, C_tt, H_tt)
                        if temp_i == None or t > temp_max:
                            temp_i = i
                            temp_max = t
                    break
            else:
                C_tt = copy.copy(C_p)
                H_tt = copy.copy(H_p)
                H_tt[i] -= 1
                t = total_power(sensors_p, p_list_p, C_tt, H_tt)
                if temp_i == None or t > temp_max:
                    temp_i = i
                    temp_max = t

        H_p[temp_i] -= 1
        if H_p[temp_i] == 0:
            C_p[temp_i: temp_i + 1] = []
            H_p[temp_i: temp_i + 1] = []
            n_2 = len(C_p)

        # random select j
        count = 0
        for j in xrange(n_1):
            for i in xrange(n_2):
                if C_p[i] == C[j]:
                    if H_p[i] < H[j]:
                        count += 1
                    break
            else:
                count += 1

        assert count > 0
        count = random.randint(0, count - 1)
        temp_j = None

        for j in xrange(n_1):
            for i in xrange(n_2):
                if C_p[i] == C[j]:
                    if H_p[i] < H[j]:
                        if count == 0:
                            temp_j = j
                        count -= 1
                    break
            else:
                if count == 0:
                    temp_j = j
                count -= 1

        charger = C[temp_j]

        # increase H'[j]
        for i in xrange(n_2):
            if C_p[i] == charger:
                H_p[i] += 1
                break
        else:
            C_p.append(charger)
            H_p.append(1)

    Q_p = total_power(sensors_p, p_list_p, C_p, H_p)
    return (Q_p, C_p, H_p)