示例#1
0
文件: solutionOpt.py 项目: kongfy/PPP
def solve(chargers, n, cost, sensors, p_list):
    """
    n is the number of chargers have already been considered,
    enumerate every possible choise using dfs
    """
    global g_C
    global g_H

    if n == len(chargers):
        update(sensors, p_list)
        return

    # don't chose this charger
    solve(chargers, n + 1, cost, sensors, p_list)

    # chose it by every possible h
    c = chargers[n]
    for h in xrange(1, args['h_max'] + 1):
        if power(h) + cost > g_B:
            break
        g_C.append(c)
        g_H.append(h)
        solve(chargers, n + 1, cost + power(h), sensors, p_list)
        del g_C[len(g_C) - 1]
        del g_H[len(g_H) - 1]
示例#2
0
文件: solutionB.py 项目: kongfy/PPP
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
示例#3
0
文件: solutionB.py 项目: kongfy/PPP
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
示例#4
0
文件: solutionRan.py 项目: kongfy/PPP
def random_h():
    """generate a random h list"""
    B = args['B']
    cost = 0
    h_list = [0 for i in xrange(args['N'])]

    for i in xrange(len(h_list)):
        if cost >= B:
            break

        if cost + power(args['h_max']) <= B:
            h_list[i] = random.randint(0, args['h_max'])
        else:
            for h in xrange(args['h_max'] + 1):
                if cost + power(h) <= B:
                    h_list[i] = h

        cost += power(h_list[i])

    random.shuffle(h_list)
    return h_list
示例#5
0
文件: solutionA.py 项目: kongfy/PPP
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
示例#6
0
文件: solutionA.py 项目: kongfy/PPP
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