本文整理汇总了Python中history.History.set_agent_spent方法的典型用法代码示例。如果您正苦于以下问题:Python History.set_agent_spent方法的具体用法?Python History.set_agent_spent怎么用?Python History.set_agent_spent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类history.History
的用法示例。
在下文中一共展示了History.set_agent_spent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sim
# 需要导入模块: from history import History [as 别名]
# 或者: from history.History import set_agent_spent [as 别名]
#.........这里部分代码省略.........
per_click_payments = {}
slot_payments = {}
values = {}
bids = {}
history = History(bids, slot_occupants, slot_clicks,
per_click_payments, slot_payments, n)
def total_spent(agent_id, end):
"""
Compute total amount spent by agent_id through (not including)
round end.
"""
s = 0
for t in range(end):
slot = agent_slot(slot_occupants, agent_id, t)
if slot != -1:
s += slot_payments[t][slot]
return s
def run_round(top_slot_clicks, t):
""" top_slot_clicks is the expected number of clicks in the top slot
k is the round number
"""
if t == 0:
bids[t] = [(a.id, a.initial_bid(reserve)) for a in agents]
else:
# Bids from agents with no money get reduced to zero
have_money = lambda a: total_spent(a.id, t) < config.budget
still_have_money = filter(have_money, agents)
current_bids = []
for a in agents:
b = a.bid(t, history, reserve)
if total_spent(a.id, t) < config.budget:
current_bids.append( (a.id, b))
else:
# Out of money: make bid zero.
current_bids.append( (a.id, 0))
bids[t] = current_bids
## Ignore those below reserve price
active_bidders = len(filter(lambda (i,b): b >= reserve, bids[t]))
#####################################
## 1a. Define no. of slots (TO-DO: Check what the # of available slots should be)
#num_slots = max(1, active_bidders-1)
num_slots = max(1, n-1)
## 1b. Calculate clicks/slot
slot_clicks[t] = [iround(top_slot_clicks * pow(config.dropoff, i))
for i in range(num_slots)]
## 2. Run mechanism and allocate slots
(slot_occupants[t], per_click_payments[t]) = (
mechanism.compute(slot_clicks[t],
reserve, bids[t]))
## 3. Define payments
slot_payments[t] = map(lambda (x,y): x*y,
zip(slot_clicks[t], per_click_payments[t]))
## 4. Save utility (misnamed as values)
values[t] = dict(zip(agent_ids, zeros))
def agent_value(agent_id, clicks, payment):
if agent_id is not None:
values[t][agent_id] = by_id[agent_id].value * clicks - payment
return None
map(agent_value, slot_occupants[t], slot_clicks[t], slot_payments[t])
## Debugging. Set to True to see what's happening.
log_console = False
if log_console:
logging.info("\t=== Round %d ===" % t)
logging.info("\tnum_slots: %d" % num_slots)
logging.info("\tbids: %s" % bids[t])
logging.info("\tslot occupants: %s" % slot_occupants[t])
logging.info("\tslot_clicks: %s" % slot_clicks[t])
logging.info("\tper_click_payments: %s" % per_click_payments[t])
logging.info("\tslot_payments: %s" % slot_payments[t])
logging.info("\tUtility: %s" % values[t])
logging.info("\ttotals spent: %s" % [total_spent(a.id, t+1) for a in agents])
for t in range(0, config.num_rounds):
# Over 48 rounds, go from 80 to 20 and back to 80. Mean 50.
# Makes sense when 48 rounds, to simulate a day
top_slot_clicks = iround(30*math.cos(math.pi*t/24) + 50)
if t == config.num_rounds / 2 and config.mechanism == 'switch':
mechanism = VCG
## 0. Runs one round
run_round(top_slot_clicks, t)
for a in agents:
history.set_agent_spent(a.id, total_spent(a.id, t))
for a in agents:
history.set_agent_spent(a.id, total_spent(a.id, config.num_rounds))
return history
示例2: sim
# 需要导入模块: from history import History [as 别名]
# 或者: from history.History import set_agent_spent [as 别名]
def sim(config):
agents = init_agents(config)
n = len(agents)
by_id = dict((a.id, a) for a in agents)
agent_ids = [a.id for a in agents]
if (config.mechanism.lower() == 'cawdp'):
mechanism = caWDP
elif config.mechanism.lower() == 'vcg':
mechanism = VCG
else:
raise ValueError("mechanism must be one of 'caWDP' or 'VCG'")
allocation = {}
payments = {}
values = {}
bids = {}
history = History(bids, payments, allocation, n)
def total_spent(agent_id, end):
"""
Compute total amount spent by agent_id through (not including)
round end.
"""
s = 0
for t in range(end):
agent_payment_t = filter(lambda p : p[0] == agent_id, payments[t])
s += agent_payment_t[0][1]
return s
def run_round(t):
# Get the bids from the agents
if t == 0:
bids[t] = [a.initial_bid() for a in agents]
else:
bids[t] = [a.bid(t, history) for a in agents]
# Run the mechanism to determine allocation
allocation[t] = mechanism.compute(config.capacities, bids[t])
# Find the payment for each agent
payments[t] = []
for aid in agent_ids:
agent_allocation = filter(lambda b : b[0] == aid, allocation[t])
if len(agent_allocation) == 0:
payments[t].append((aid,0))
else:
payments[t].append((aid, sum(b[2] for b in agent_allocation)))
values[t] = dict(zip(agent_ids, zeros))
def agent_value(agent_id, allocation, payments):
agent_allocation = filter(lambda b : b[0] == agent_id, allocation)
agent_value = 0
if len(agent_allocation) > 0:
for choice in agent_allocation:
agent_value += sum([by_id[agent_id].values[i]*choice[1][i] for i in range(0, len(choice[1]))])
agent_payment = filter(lambda p : p[0] == agent_id, payments)
values[t][agent_id] = agent_value - agent_payment[0][1]
return None
map(lambda a : agent_value(a, allocation[t], payments[t]), agent_ids)
log_console = False
if log_console:
logging.info("\t=== Round %d ===" % t)
logging.info("\tbids: %s" % bids[t])
logging.info("\tpayments: %s" % payments[t])
logging.info("\tallocation: %s" % allocation[t])
logging.info("\tUtility: %s" % values[t])
logging.info("\ttotals spent: %s" % [total_spent(a.id, t+1) for a in agents])
for t in range(0, config.num_rounds):
run_round(t)
for a in agents:
history.set_agent_spent(a.id, total_spent(a.id, t))
for a in agents:
history.set_agent_spent(a.id, total_spent(a.id, config.num_rounds))
return history