Example #1
0
def main_exp3(bidder, curr_rep, T, num_bidders, num_slots, outcome_space,
              rank_scores, ctr, reserve, values, bids, threshold, noise):
    algo_util = []
    temp_regr = []
    clean_alloc = [[] for _ in range(0, T)]
    for t in range(0, T):
        bid_chosen = bidder.bidding()
        bids[t][0] = bid_chosen
        bid_vec = deepcopy(bids[t])
        gsp_instance = GSP(ctr[t], reserve[t], bid_vec, rank_scores[t],
                           num_slots, num_bidders)
        allocated = gsp_instance.alloc_func(bidder.id, bids[t][bidder.id])
        clean_alloc[t] = [
            gsp_instance.alloc_func(bidder.id, bid * bidder.eps)
            for bid in range(0, bidder.bid_space)
        ]

        temp_alloc = deepcopy(clean_alloc[t])

        noise_cp = deepcopy(noise)
        bidder.alloc_func[t] = noise_mask(temp_alloc, noise_cp[t], ctr[t],
                                          num_slots)
        #reward function: value - payment(coming from GSP module)
        bidder.pay_func[t] = [
            gsp_instance.pay_func(bidder.id, bid * bidder.eps)
            for bid in range(0, bidder.bid_space)
        ]
        if allocated > threshold[t]:
            bidder.reward_func[t] = [(values[t][0] - bidder.pay_func[t][b])
                                     for b in range(0, bidder.bid_space)]
        else:
            bidder.reward_func[t] = [0 for _ in range(0, bidder.bid_space)]

        bidder.utility[t] = normalize(bidder.reward_func[t], bidder.bid_space,
                                      0, 1)

        #weights update
        arm_chosen = int(math.ceil(bids[t][0] / bidder.eps))

        if bidder.pi[arm_chosen] < 0.0000000001:
            bidder.pi[arm_chosen] = 0.0000000001
        estimated_loss = bidder.utility[t][arm_chosen] / bidder.pi[arm_chosen]
        bidder.loss[arm_chosen] += estimated_loss
        arr = np.array([(-bidder.eta_exp3) * bidder.loss[b]
                        for b in range(0, bidder.bid_space)],
                       dtype=np.float128)
        bidder.weights = np.exp(arr)
        bidder.pi = [
            bidder.weights[b] / sum(bidder.weights)
            for b in range(0, bidder.bid_space)
        ]

        #algo_util.append((bidder.reward_func[t][arm_chosen]*bidder.alloc_func[t][arm_chosen]))
        algo_util.append(
            (bidder.reward_func[t][arm_chosen] * clean_alloc[t][arm_chosen]))
        temp_regr.append(
            regret(bidder.reward_func, clean_alloc, bidder.bid_space,
                   algo_util, t))

    return temp_regr
def main_winexp(bidder, curr_rep, T, num_bidders, num_slots, outcome_space,
                rank_scores, ctr, reserve, values, bids, threshold, noise):
    algo_util = []
    temp_regr = []
    clean_alloc = [[] for _ in range(0, T)]
    for t in range(0, T):
        bid_chosen = bidder.bidding()
        bids[t][0] = bid_chosen
        bid_vec = deepcopy(bids[t])
        gsp_instance = GSP(ctr[t], reserve[t], bid_vec, rank_scores[t],
                           num_slots, num_bidders)
        # this is not reported to the bidder, and thus is cleaned of noise
        allocated = gsp_instance.alloc_func(bidder.id, bids[t][bidder.id])
        clean_alloc[t] = [
            gsp_instance.alloc_func(bidder.id, bid * bidder.eps)
            for bid in range(0, bidder.bid_space)
        ]

        temp = deepcopy(clean_alloc[t])

        # bidder sees noisy data as his allocation
        noise_cp = deepcopy(noise)
        bidder.alloc_func[t] = noise_mask(temp, noise_cp[t], ctr[t], num_slots)

        #reward function: value - payment(coming from GSP module)
        bidder.pay_func[t] = [
            gsp_instance.pay_func(bidder.id, bid * bidder.eps)
            for bid in range(0, bidder.bid_space)
        ]
        #### WIN-EXP computations ####
        # computation of reward will only be used for the regret
        if allocated > threshold[t]:
            bidder.reward_func[t] = [(values[t][0] - bidder.pay_func[t][b])
                                     for b in range(0, bidder.bid_space)]
            bidder.utility[t] = bidder.compute_utility(1,
                                                       bidder.reward_func[t],
                                                       bidder.alloc_func[t])
        else:
            bidder.reward_func[t] = [0 for _ in range(0, bidder.bid_space)]
            bidder.utility[t] = (bidder.compute_utility(
                0, bidder.reward_func[t], bidder.alloc_func[t]))

        (bidder.weights,
         bidder.pi) = bidder.weights_update_winexp(bidder.eta_winexp,
                                                   bidder.utility[t])
        # for each auction (at the same t) you choose the same arm
        arm_chosen = int(math.ceil(bids[t][bidder.id] / bidder.eps))

        algo_util.append(
            (bidder.reward_func[t][arm_chosen] * clean_alloc[t][arm_chosen]))
        temp_regr.append(
            regret(bidder.reward_func, clean_alloc, bidder.bid_space,
                   algo_util, t))

    return temp_regr
Example #3
0
def main_exp3(bidder,curr_rep, T,num_bidders, num_slots, outcome_space, rank_scores, ctr, reserve, values,bids,threshold,noise,num_adaptive):
    algo_util  = []
    temp_regr  = []
    clean_alloc = [[] for _ in range(0,T)]
    for t in range(0,T):
        bid_chosen = [bidder[i].bidding() for i in range(0,num_adaptive)]
        for i in range(0,num_adaptive): 
            bids[t][i] = bid_chosen[i]
        bid_vec = deepcopy(bids[t])
        gsp_instance =GSP(ctr[t], reserve[t], bid_vec, rank_scores[t], num_slots, num_bidders) 
        arm_chosen =[0]*num_adaptive
        for i in range(0,num_adaptive):
            allocated = gsp_instance.alloc_func(bidder[i].id, bids[t][bidder[i].id])
            temp      = [gsp_instance.alloc_func(bidder[i].id, bid*bidder[i].eps)  for bid in range(0, bidder[i].bid_space)]
            if (i == 0):
                clean_alloc[t] = deepcopy(temp)        

            noise_cp = deepcopy(noise)
            bidder[i].alloc_func[t] = noise_mask(temp,noise_cp[t],ctr[t], num_slots)
            #reward function: value - payment(coming from GSP module)
            bidder[i].pay_func[t] = [gsp_instance.pay_func(bidder[i].id, bid*bidder[i].eps) for bid in range(0, bidder[i].bid_space)]  
            if (i == 0):
                if allocated > threshold[t]:    
                    bidder[i].reward_func[t] = [(values[t][i] - bidder[i].pay_func[t][b]) for b in range(0,bidder[i].bid_space)] 
                else:
                    bidder[i].reward_func[t] = [0 for _ in range(0,bidder[i].bid_space)]

                bidder[i].utility[t] = bidder[i].reward_func[t]

                #weights update
                arm_chosen[i] = int(math.ceil(bids[t][i]/bidder[i].eps))
                
                if bidder[i].pi[arm_chosen[i]] < 0.0000000001:
                    bidder[i].pi[arm_chosen[i]] = 0.0000000001
                estimated_loss = -bidder[i].utility[t][arm_chosen[i]]/bidder[i].pi[arm_chosen[i]]
                bidder[i].loss[arm_chosen[i]] += estimated_loss
                arr = np.array([(-bidder[i].eta_exp3)*bidder[i].loss[b] for b in range(0,bidder[i].bid_space)], dtype=np.float128)
                bidder[i].weights = np.exp(arr)
                bidder[i].pi = [bidder[i].weights[b]/sum(bidder[i].weights) for b in range(0,bidder[i].bid_space)]
            else: 
                if allocated > threshold[t]:    
                    bidder[i].reward_func[t] = [(values[t][0] - bidder[i].pay_func[t][b]) for b in range(0,bidder[i].bid_space)] 
                    bidder[i].utility[t] = bidder[i].compute_utility(1, bidder[i].reward_func[t], bidder[i].alloc_func[t])
                else:
                    bidder[i].reward_func[t] = [0 for _ in range(0,bidder[i].bid_space)]
                    bidder[i].utility[t] = (bidder[i].compute_utility(0, bidder[i].reward_func[t], bidder[i].alloc_func[t]))


                (bidder[i].weights, bidder[i].pi) = bidder[i].weights_update_winexp(bidder[i].eta_winexp, bidder[i].utility[t])        
        
        
        algo_util.append((bidder[0].reward_func[t][arm_chosen[0]]*clean_alloc[t][arm_chosen[0]]))
        temp_regr.append(regret(bidder[0].reward_func,clean_alloc,bidder[0].bid_space, algo_util,t))    

    return temp_regr   
Example #4
0
    def expected_utils(self, t, history, reserve):
        """
        Figure out the expected utility of bidding such that we win each
        slot, assuming that everyone else keeps their bids constant from
        the previous round.

        returns a list of utilities per slot.
        """
        # TODO: Fill this in
        last_round = history.round(t-1)
        last_round_bids = filter(lambda (ID, bid):ID != self.id, last_round.bids)


        last_round_clicks = last_round.clicks

        allocation, _ = GSP.compute(last_round_clicks, reserve, last_round_bids)
        num_slots = len(allocation)

        utilities = []

        for slot in range(num_slots):
            last_round_k = allocation[slot] #take the person from last round in slot k
            bid_i = reserve # default bid to reserve price
            for ID, bid in last_round_bids: # go through each of last rounds' bids, and set that slot's bid 
                if last_round_k == ID:
                    bid_i = bid
            utilities += [(self.value - bid_i) * last_round_clicks[slot]]

        if utilities == []:
            return [0]

        return utilities
Example #5
0
    def runSecondPriceAuction(self, reqList, seller):
        '''
        Function to call GSP mechanism
        '''
        sellerGraph = seller.getSellerGraph()

        allocation = []
        self.__logger.debug("[AdExchange][runSecondPriceAuction]")
        self.__logger.debug("Fiber allocation decisions.")

        allocationDict = {}
        ip_port_pairs = []
        for key, v in reqList.items():
            k1, k2 = key.split("#")
            # n denotes the number of customers bidding for that conduit
            n = len(v)
            slot_click = [1] * n

            try:
                shortestPath = nx.shortest_path(sellerGraph,
                                                source=k1,
                                                target=k2)
                gCoP = self.getCostOfPath(shortestPath, sellerGraph)
                lIP = self.linksInPath(shortestPath)
                k = len(lIP)
                reserve = max(self.__reserve, gCoP / k)

                bids = []
                for item in v:
                    bids.append((item.clientName, item.bidPerStrand))

                if nx.has_path(sellerGraph, k1, k2) and self.resourceAvailable(
                        sellerGraph, shortestPath, v):
                    (alloc, payments) = GSP.compute(slot_click, reserve, bids)
                    allocation.extend(zip(alloc, [i * k for i in payments]))
                    for (kTest, vTest) in allocation:
                        allocationDict[kTest] = vTest

                    # Updates sellerGraph with the allocation
                    self.__logger.debug("Before > {}".format(
                        self.availableAttributes(shortestPath, sellerGraph)))
                    ip_port_pairs = self.updateSellerGraph_and_getResources(
                        seller, shortestPath, v)
                    self.__logger.debug("After > {}".format(
                        self.availableAttributes(shortestPath, sellerGraph)))

                else:
                    self.__logger.info(
                        "Link does not exists between {} and {}. No resource available for request"
                        .format(k1, k2))

            except nx.NodeNotFound:
                self.__logger.info(
                    "Path does not exists between {} and {}. No resource available for request"
                    .format(k1, k2))
                allocationDict = {}
                break
        return (self.updateRequestList(reqList, allocationDict), ip_port_pairs)
Example #6
0
 def bid_range_for_slot(slot, slot_clicks, reserve, bids):
     """
     Compute the range of bids that would result in the bidder ending up
     in slot, given that the other bidders submit bidders.
     Returns a tuple (min_bid, max_bid).
     If slot == 0, returns None for max_bid, since it's not well defined.
     """
     # Conveniently enough, bid ranges are the same for GSP and VCG:
     return GSP.bid_range_for_slot(slot, slot_clicks, reserve, bids)
Example #7
0
 def bid_range_for_slot(slot, slot_clicks, reserve, bids):
     """
     Compute the range of bids that would result in the bidder ending up
     in slot, given that the other bidders submit bidders.
     Returns a tuple (min_bid, max_bid).
     If slot == 0, returns None for max_bid, since it's not well defined.
     """
     # Conveniently enough, bid ranges are the same for GSP and VCG:
     return GSP.bid_range_for_slot(slot, slot_clicks, reserve, bids)
Example #8
0
def test_mechanism():
    num_slots = 4
    slot_clicks = [1] * 4  # don't actually matter for gsp
    bids = zip(range(1, 6), [10, 12, 18, 14, 20])

    reserve = 0

    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5, 3, 4, 2]
    assert payments == [18, 14, 12, 10]

    reserve = 11
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5, 3, 4, 2]
    assert payments == [18, 14, 12, 11]

    reserve = 14
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5, 3, 4]
    assert payments == [18, 14, 14]

    reserve = 15
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5, 3]
    assert payments == [18, 15]

    reserve = 19
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5]
    assert payments == [19]

    reserve = 22
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == []
    assert payments == []
def main_winexp(bidder,curr_rep, T,num_bidders, num_slots, outcome_space, rank_scores, ctr, reserve, values,bids,threshold, noise,num_adaptive):
    algo_util = []
    temp_regr = []
    clean_alloc = [[] for _ in range(0,T)]
    clean_pay   = [[] for _ in range(0,T)]
    clean_reward = [[] for _ in range(0,T)]
    for t in range(0,T):
        bid_chosen = [bidder[i].bidding() for i in range(0,num_adaptive)]
        for i in range(0,num_adaptive): 
            bids[t][i] = bid_chosen[i]
        bid_vec = deepcopy(bids[t])
        gsp_instance =GSP(ctr[t], reserve[t], bid_vec, rank_scores[t], num_slots, num_bidders) 

        arm_chosen = [int(math.ceil(bids[t][i]/bidder[i].eps)) for i in range(0,num_adaptive)] 
        for i in range(0,num_adaptive):
            allocated = gsp_instance.alloc_func(bidder[i].id, bids[t][bidder[i].id])
            temp      = [gsp_instance.alloc_func(bidder[i].id, bid*bidder[i].eps)  for bid in range(0, bidder[i].bid_space)]
            if (i == 0):
                clean_alloc[t]          = deepcopy(temp)
                clean_pay[t]            = [gsp_instance.pay_func(bidder[i].id, bid*bidder[i].eps) for bid in range(0,bidder[i].bid_space)]
                
            temp_pay                = gsp_instance.pay_func(bidder[i].id, bid_vec[i])
            bidder[i].payment[t]    = temp_pay
            
            # bidder sees noisy data as his allocation
            noise_cp = deepcopy(noise)
            bidder[i].currbid[t]   = arm_chosen[i]*bidder[i].eps   
            if allocated > threshold[t]:
                bidder[i].allocated[t]                  = 1
                bidder[i].alloc_func[t]                 = compute_allocation_function(bidder[i].currbid[:t+1], bidder[i].allocated[:t+1], bidder[i].bid_space, bidder[i].eps)
                bidder[i].alloc_func[t][arm_chosen[i]]  = allocated
                bidder[i].pay_func[t]                   = compute_payment_function(bidder[i].currbid[:t+1], bidder[i].payment[:t+1], bidder[i].bid_space, bidder[i].eps)       
                bidder[i].pay_func[t][arm_chosen[i]]    = bidder[i].payment[t]
                temp_reward                             = [(values[t][0] - bidder[i].pay_func[t][b]) for b in range(0,bidder[i].bid_space)] 
                if (i == 0):
                    clean_reward[t]                     = [(values[t][0] - clean_pay[t][b]) for b in range(0,bidder[i].bid_space)]
                bidder[i].reward_func[t]   = normalize(temp_reward,bidder[i].bid_space,-1,1)
                bidder[i].utility[t]                    = (bidder[i].compute_utility(1, bidder[i].reward_func[t], bidder[i].alloc_func[t]))
            else:
                bidder[i].allocated[t] =0
                bidder[i].alloc_func[t]                 = compute_allocation_function(bidder[i].currbid[:t+1], bidder[i].allocated[:t+1], bidder[i].bid_space, bidder[i].eps)
                bidder[i].alloc_func[t][arm_chosen[i]]  = allocated
                bidder[i].payment[t]                    = 0 
                bidder[i].pay_func[t]                   = [0]*bidder[i].bid_space
                temp_reward                             = [0 for _ in range(0,bidder[i].bid_space)]
                bidder[i].reward_func[t]                = normalize(temp_reward,bidder[i].bid_space,-1,1)
                if (i == 0):
                    clean_reward[t]                     = [0 for _ in range(0,bidder[i].bid_space)]
                bidder[i].utility[t]                    = (bidder[i].compute_utility(0, bidder[i].reward_func[t], bidder[i].alloc_func[t]))
        
            (bidder[i].weights, bidder[i].pi) = bidder[i].weights_update_winexp(bidder[i].eta_winexp, bidder[i].utility[t])        
                

        algo_util.append((clean_reward[t][arm_chosen[0]]*clean_alloc[t][arm_chosen[0]]))
        temp_regr.append(regret(clean_reward,clean_alloc,bidder[0].bid_space, algo_util,t))    
        

    return temp_regr   
Example #10
0
def extract_patterns(transactions, min_support_threshold):
    user_gsp_patterns = {}
    for uid, transList in transactions.items():
        gsp_patterns = GSP(transList).search(min_support_threshold)

        print("========= Status =========", uid)
        # print("Transactions: {}".format(transactions))
        print("GSP: {}".format(gsp_patterns))

        if len(gsp_patterns) > 1:
            len_wise_patterns = list(
                map(lambda d: list(d.keys()), gsp_patterns[1:]))
            all_len_patterns = [
                patt for len_patts in len_wise_patterns for patt in len_patts
            ]
            print("Found patterns of length >=2 for user {} and patts: {}".
                  format(uid, all_len_patterns))
            user_gsp_patterns[uid] = all_len_patterns

    print("Filtered GSP patterns:\n", user_gsp_patterns)
    return user_gsp_patterns
Example #11
0
    def runSecondPriceAuction(self, reqList, sellerGraph):
        '''
        Function to call GSP mechanism
        '''
        allocation = []
        self.__logger.debug("[AdExchange][runSecondPriceAuction]")
        self.__logger.debug("Fiber allocation decisions.")

        allocationDict = {}
        for key, v in reqList.items():
            k1,k2=key.split("#")
            # n denotes the number of customers bidding for that conduit
            n = len(v)
            slot_click = [1] * n

            shortestPath = nx.shortest_path(sellerGraph, source=k1, target=k2)
            gCoP = self.getCostOfPath(shortestPath, sellerGraph)
            lIP = self.linksInPath(shortestPath)
            k = len(lIP)
            reserve = max(self.__reserve, gCoP/k)

            bids = []
            for item in v:
                bids.append((item.clientName, item.bidPerStrand))

            if nx.has_path(sellerGraph, k1, k2) and self.resourceAvailable(sellerGraph, shortestPath, v):
                (alloc, payments) = GSP.compute(slot_click, reserve, bids)
                allocation.extend(zip(alloc, [i * k for i in payments]))
                for (kTest, vTest) in allocation:
                    allocationDict[kTest] = vTest

                # Updates sellerGraph with the allocation
                self.__logger.debug("Before > {}".format(self.availableAttributes(shortestPath, sellerGraph)))
                self.updateSellerGraph(sellerGraph, shortestPath, v)
                self.__logger.debug("After > {}".format(self.availableAttributes(shortestPath, sellerGraph)))
            else:
                self.__logger.info("Link does not exists or No resources available for the request")
        return self.updateRequestList(reqList, allocationDict)
def test_mechanism():
    num_slots = 4
    slot_clicks = [1] * 4   # don't actually matter for gsp
    bids = zip(range(1,6), [10, 12, 18, 14, 20])

    reserve = 0

    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5,3,4,2]
    assert payments == [18, 14, 12, 10]

    reserve = 11
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5,3,4,2]
    assert payments == [18, 14, 12, 11]

    reserve = 14
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5,3,4]
    assert payments == [18, 14, 14]

    reserve = 15
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5,3]
    assert payments == [18, 15]

    reserve = 19
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == [5]
    assert payments == [19]


    reserve = 22
    (alloc, payments) = GSP.compute(slot_clicks, reserve, bids)
    assert alloc == []
    assert payments == []
Example #13
0
 def compute(s):
     (min, max) = GSP.bid_range_for_slot(s, clicks, reserve, other_bids)
     if max == None:
         max = 2 * min
     return (s, min, max)
Example #14
0
import argparse
import logging
import random
import re

from gsp import GSP

logging.basicConfig(level=logging.DEBUG)


def read_file(filepath) -> [str]:
    rows = []
    with open(filepath) as fp:
        for cnt, line in enumerate(fp):
            rows.append(line.replace("\r", "").replace("\n", "").split())

    return rows


transactions = read_file('test.txt')

result = GSP(transactions).search(0.1)

print("========= Status =========")
print("Transactions: {}".format(transactions))
print("GSP: {}".format(result))
 def compute(s):
     (min, max) = GSP.bid_range_for_slot(s, clicks, reserve, other_bids)
     if max == None:
         max = 2 * min
     return (s, min, max)
Example #16
0
def main_gexp3(bidder, curr_rep, T, num_bidders, num_slots, outcome_space,
               rank_scores, ctr, reserve, values, bids, num_auctions):
    algo_util = []
    temp_regr = []
    gamma = 1.05 * math.sqrt(
        bidder.bid_space * math.log(bidder.bid_space, 2) / T)
    for t in range(0, T):
        #bid_chosen = round(bidder.gbidding(bidder.weights,gamma),2)
        bid_chosen = round(bidder.bidding(), 2)
        # the bid chosen by the learner is the same for all auctions in the batch
        for auction in range(0, num_auctions):
            #bid_chosen = round(np.random.uniform(0,1),1)
            #bid_chosen = round(np.random.uniform(0,1),2)
            #print ("Bid chosen for timestep t=%d is:%f"%(t,bid_chosen))
            #bid_chosen = round(np.random.uniform(0,1),2)
            bids[auction][t][0] = bid_chosen
            #print ("Repetition=%d, timestep t=%d, auction=%d"%(curr_rep, t, auction))
            #print ("Rank scores")
            #print rank_scores[auction][t]
            #print ("CTR")
            #print ctr[auction][t]
            #print ("reserve")
            #print reserve[auction][t]
            #print ("bids")
            #print bids[auction][t]
            allocated = GSP(ctr[auction][t], reserve[auction][t],
                            bids[auction][t], rank_scores[auction][t],
                            num_slots, num_bidders).alloc_func(
                                bidder.id, bids[auction][t][bidder.id])
            bidder.alloc_func[auction][t] = [
                GSP(ctr[auction][t], reserve[auction][t], bids[auction][t],
                    rank_scores[auction][t], num_slots,
                    num_bidders).alloc_func(bidder.id, bid * bidder.eps)
                for bid in range(0, bidder.bid_space)
            ]
            #print ("alloc func")
            #print bidder.alloc_func
            #reward function: value - payment(coming from GSP module)
            bid_vec = deepcopy(bids[auction][t])
            bidder.pay_func[t] = [
                GSP(ctr[auction][t], reserve[auction][t], bid_vec,
                    rank_scores[auction][t], num_slots,
                    num_bidders).pay_func(bidder.id, bid * bidder.eps)
                for bid in range(0, bidder.bid_space)
            ]
            #print ("pay func")
            #print bidder.pay_func

            bidder.reward_func[auction][t] = compute_reward(
                bidder.alloc_func[auction][t], bidder.pay_func[t],
                ctr[auction][t], values[auction][t])
            #print ("Bidder's Reward function")
            #print bidder.reward_func
            #### EXP3 computations ####
            #bidder.utility[auction][t] = [bidder.reward_func[auction][t][b]*bidder.alloc_func[auction][t][b] - 1 for b in range(0, bidder.bid_space)]
            #bidder.utility[t] = [bidder.reward_func[t][b]*bidder.alloc_func[t][b] - 1 for b in range(0, bidder.bid_space)]
            #print ("Bidder %d utility"%bidder.id)
            #print bidder.utility

        u_s = [0 for _ in range(0, bidder.bid_space)]
        for b in range(0, bidder.bid_space):
            for auction in range(0, num_auctions):
                u_s[b] += bidder.reward_func[auction][t][
                    b] * bidder.alloc_func[auction][t][b]
            bidder.avg_utility[t].append(u_s[b] / num_auctions - 1)

        #weights update
        arm_chosen = int(math.ceil(bids[0][t][0] / bidder.eps))
        #print ("Arm Chosen")
        #print arm_chosen
        #print ("Average reward at timestep t=%d is:"%t)
        #print (bidder.avg_reward[t])
        #print ("Average utility at timestep t=%d is:"%t)
        #print (bidder.avg_utility[t])

        #print ("Probability vector before computing estimated loss")
        #print bidder.pi

        if bidder.pi[arm_chosen] < 0.0000000001:
            bidder.pi[arm_chosen] = 0.0000000001
        #estimated_loss = bidder.avg_utility[t][arm_chosen]/bidder.pi[arm_chosen]
        estimated_loss = (bidder.avg_utility[t][arm_chosen] -
                          bidder.beta) / bidder.pi[arm_chosen]
        #print ("Estimated Loss")
        #print estimated_loss
        bidder.loss[arm_chosen] += estimated_loss
        #print bidder.loss
        bidder.weights = [
            math.exp(-bidder.eta_gexp3 * bidder.loss[b])
            for b in range(0, bidder.bid_space)
        ]
        bidder.pi = [(1 - gamma) * bidder.weights[b] / sum(bidder.weights) +
                     gamma / bidder.bid_space
                     for b in range(0, bidder.bid_space)]

        #print ("Probability vector after computing estimated loss")
        #print bidder.pi
        # compute the algorithm's utility at every step
        #algo_util.append(bidder.avg_utility[t][int(math.ceil(bids[0][t][bidder.id]/bidder.eps))])

        algo_util.append(bidder.avg_utility[t][int(
            math.ceil(bids[0][t][0] / bidder.eps))])
        #print ("Algorithm's average utility")
        #print algo_util
        temp_regr.append(
            regret(0, bidder.reward_func, bidder.alloc_func, bidder.bid_space,
                   algo_util, t, num_auctions))
        #print ("Regret inside" )
        #print temp_regr

    return temp_regr
def main_winexp(bidder, curr_rep, T, num_bidders, num_slots, outcome_space,
                rank_scores, ctr, reserve, values, bids, threshold, noise,
                num_adaptive):
    algo_util = []
    temp_regr = []
    clean_alloc = [[] for _ in range(0, T)]
    for t in range(0, T):
        bid_chosen = [bidder[i].bidding() for i in range(0, num_adaptive)]
        for i in range(0, num_adaptive):
            bids[t][i] = bid_chosen[i]
        bid_vec = deepcopy(bids[t])
        gsp_instance = GSP(ctr[t], reserve[t], bid_vec, rank_scores[t],
                           num_slots, num_bidders)
        # this is not reported to the bidder, and thus is cleaned of noise
        for i in range(0, num_adaptive):
            allocated = gsp_instance.alloc_func(bidder[i].id,
                                                bids[t][bidder[i].id])
            temp = [
                gsp_instance.alloc_func(bidder[i].id, bid * bidder[i].eps)
                for bid in range(0, bidder[i].bid_space)
            ]

            if (i == 0):
                clean_alloc[t] = deepcopy(temp)
            # bidder sees noisy data as his allocation
            noise_cp = deepcopy(noise)
            bidder[i].alloc_func[t] = noise_mask(temp, noise_cp[t], ctr[t],
                                                 num_slots)

            #reward function: value - payment(coming from GSP module)
            bidder[i].pay_func[t] = [
                gsp_instance.pay_func(bidder[i].id, bid * bidder[i].eps)
                for bid in range(0, bidder[i].bid_space)
            ]
            #### WIN-EXP computations ####
            # computation of reward will only be used for the regret
            if (i == 0):
                if allocated > threshold[t]:
                    bidder[i].reward_func[t] = [
                        (values[t][0] - bidder[i].pay_func[t][b])
                        for b in range(0, bidder[i].bid_space)
                    ]
                    bidder[i].utility[t] = bidder[i].compute_utility(
                        1, bidder[i].reward_func[t], bidder[i].alloc_func[t])
                else:
                    bidder[i].reward_func[t] = [
                        0 for _ in range(0, bidder[i].bid_space)
                    ]
                    bidder[i].utility[t] = (bidder[i].compute_utility(
                        0, bidder[i].reward_func[t], bidder[i].alloc_func[t]))

                (bidder[i].weights,
                 bidder[i].pi) = bidder[i].weights_update_winexp(
                     bidder[i].eta_winexp, bidder[i].utility[t])

            else:
                if allocated > threshold[t]:
                    bidder[i].reward_func[t] = [
                        (values[t][i] - bidder[i].pay_func[t][b])
                        for b in range(0, bidder[i].bid_space)
                    ]
                else:
                    bidder[i].reward_func[t] = [
                        0 for _ in range(0, bidder[i].bid_space)
                    ]

                bidder[i].utility[t] = bidder[i].reward_func[t]

                #weights update
                arm_chosen = int(math.ceil(bids[t][i] / bidder[i].eps))

                if bidder[i].pi[arm_chosen] < np.exp(-700):
                    bidder[i].pi[arm_chosen] = np.exp(-700)
                estimated_loss = -bidder[i].utility[t][arm_chosen] / bidder[
                    i].pi[arm_chosen]
                bidder[i].loss[arm_chosen] += estimated_loss
                arr = np.array([(-bidder[i].eta_exp3) * bidder[i].loss[b]
                                for b in range(0, bidder[i].bid_space)],
                               dtype=np.float128)
                bidder[i].weights = np.exp(arr)
                bidder[i].pi = [
                    bidder[i].weights[b] / sum(bidder[i].weights)
                    for b in range(0, bidder[i].bid_space)
                ]

        # for each auction (at the same t) you choose the same arm
        arm = int(math.ceil(bids[t][0] / bidder[0].eps))

        algo_util.append((bidder[0].reward_func[t][arm] * clean_alloc[t][arm]))
        temp_regr.append(
            regret(bidder[0].reward_func, clean_alloc, bidder[0].bid_space,
                   algo_util, t))

    return temp_regr
 def bid_range(slot, reserve):
     return GSP.bid_range_for_slot(slot, slot_clicks, reserve, bids)
Example #19
0
from gsp import GSP

threshold = 5  # minimum support
level = 3  # number of levels for GSP
g = GSP(threshold, './Data_GSP/')
g.get_support_items(level)
Example #20
0
 def bid_range(slot, reserve):
     return GSP.bid_range_for_slot(slot, slot_clicks, reserve, bids)