本文整理汇总了Python中stats.Stats.total_utility方法的典型用法代码示例。如果您正苦于以下问题:Python Stats.total_utility方法的具体用法?Python Stats.total_utility怎么用?Python Stats.total_utility使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stats.Stats
的用法示例。
在下文中一共展示了Stats.total_utility方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from stats import Stats [as 别名]
# 或者: from stats.Stats import total_utility [as 别名]
#.........这里部分代码省略.........
parser.add_option("--reserve",
dest="reserve", default=0, type="int",
help="Reserve price, in cents")
parser.add_option("--perms",
dest="max_perms", default=120, type="int",
help="Max number of value permutations to run. Set to 1 for debugging.")
parser.add_option("--iters",
dest="iters", default=1, type="int",
help="Number of different value draws to sample. Set to 1 for debugging.")
parser.add_option("--seed",
dest="seed", default=None, type="int",
help="seed for random numbers")
(options, args) = parser.parse_args()
# leftover args are class names:
# e.g. "Truthful BBAgent CleverBidder Fred"
if len(args) == 0:
# default
agents_to_run = ['Truthful', 'Truthful', 'Truthful']
else:
agents_to_run = parse_agents(args)
configure_logging(options.loglevel)
if options.seed != None:
random.seed(options.seed)
# Add some more config options
options.agent_class_names = agents_to_run
options.agent_classes = load_modules(options.agent_class_names)
options.dropoff = 0.75
logging.info("Starting simulation...")
n = len(agents_to_run)
totals = dict((id, 0) for id in range(n))
total_revenues = []
approx = math.factorial(n) > options.max_perms
if approx:
num_perms = options.max_perms
logging.warning(
"Running approximation: taking %d samples of value permutations"
% options.max_perms)
else:
num_perms = math.factorial(n)
av_value=range(0,n)
total_spent = [0 for i in range(n)]
## iters = no. of samples to take
for i in range(options.iters):
values = get_utils(n, options)
logging.info("==== Iteration %d / %d. Values %s ====" % (i, options.iters, values))
## Create permutations (permutes the random values, and assigns them to agents)
if approx:
perms = [shuffled(values) for i in range(options.max_perms)]
else:
perms = itertools.permutations(values)
total_rev = 0
## Iterate over permutations
for vals in perms:
options.agent_values = list(vals)
values = dict(zip(range(n), list(vals)))
## Runs simulation ###
history = sim(options)
### simulation ends.
stats = Stats(history, values)
# Print stats in console?
# logging.info(stats)
for id in range(n):
totals[id] += stats.total_utility(id)
total_spent[id] += history.agents_spent[id]
total_rev += stats.total_revenue()
total_revenues.append(total_rev / float(num_perms))
## total_spent = total amount of money spent by agents, for all iterations, all permutations, all rounds
# Averages are over all the value permutations considered
N = float(num_perms) * options.iters
logging.info("%s\t\t%s\t\t%s" % ("#" * 15, "RESULTS", "#" * 15))
logging.info("")
for a in range(n):
logging.info("Stats for Agent %d, %s" % (a, agents_to_run[a]) )
logging.info("Average spend $%.2f (daily)" % (0.01 *total_spent[a]/N) )
logging.info("Average utility $%.2f (daily)" % (0.01 * totals[a]/N))
logging.info("-" * 40)
logging.info("\n")
m = mean(total_revenues)
std = stddev(total_revenues)
logging.warning("Average daily revenue (stddev): $%.2f ($%.2f)" % (0.01 * m, 0.01*std))
示例2: main
# 需要导入模块: from stats import Stats [as 别名]
# 或者: from stats.Stats import total_utility [as 别名]
#.........这里部分代码省略.........
else:
num_perms = math.factorial(n)
av_value=range(0,n)
total_spent = [0 for i in range(n)]
""" Additional statistics to follow """
agents_spent_stats = [] ### For spending statistics information
agents_util_stats = [] ### For utility statistics
for i in range(n):
agents_spent_stats.append([])
agents_util_stats.append([])
""" ------------------------------- """
## iters = no. of samples to take
for i in range(options.iters):
values = get_utils(n, options)
logging.info("==== Iteration %d / %d. Values %s ====" % (i, options.iters, values))
## Create permutations (permutes the random values, and assigns them to agents)
if approx:
perms = [shuffled(values) for i in range(options.max_perms)]
else:
perms = itertools.permutations(values)
total_rev = 0
## Iterate over permutations
for vals in perms:
options.agent_values = list(vals)
values = dict(zip(range(n), list(vals)))
## Runs simulation ###
history = sim(options)
### simulation ends.
stats = Stats(history, values)
# Print stats in console?
# logging.info(stats)
for id in range(n):
totals[id] += stats.total_utility(id)
total_spent[id] += history.agents_spent[id]
""" Additional stats ---------------- """
agents_spent_stats[id].append(history.agents_spent[id])
agents_util_stats[id].append(stats.total_utility(id))
""" --------------------------------- """
total_rev += stats.total_revenue()
total_revenues.append(total_rev / float(num_perms))
## total_spent = total amount of money spent by agents, for all iterations, all permutations, all rounds
# Averages are over all the value permutations considered
N = float(num_perms) * options.iters
print("Total permutations: %s") % num_perms
logging.info("%s\t\t%s\t\t%s" % ("#" * 15, "RESULTS", "#" * 15))
logging.info("")
for a in range(n):
logging.info("Stats for Agent %d, %s" % (a, agents_to_run[a]) )
logging.info("Average spend $%.2f (daily)" % (0.01 * total_spent[a]/N))
logging.info("Average utility $%.2f (daily)" % (0.01 * totals[a]/N))
logging.info("-" * 40)
logging.info("\n")
m = mean(total_revenues)
std = stddev(total_revenues)
logging.warning("Average daily revenue (stddev): $%.2f ($%.2f)" % (0.01 * m, 0.01*std))
logging.debug("agents_spent_stats:")
logging.debug(agents_spent_stats)
logging.debug("agents_util_stats:")
logging.debug(agents_util_stats)
""" Implementing output into csv """
filename = 'Stats/bb_only_reserve' + str(options.reserve) + '_seed' + str(options.seed) + '_iters' + str(options.iters) + '.csv'
with open(filename, 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=';',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['Number of iterations'] + [options.iters]+['']+['']+['']+[''])
spamwriter.writerow(['Reserve'] + [options.reserve]+['']+['']+['']+[''])
spamwriter.writerow(['Seed'] + [options.seed]+['']+['']+['']+[''])
spamwriter.writerow(['Agent #'] +
['Agent type'] +
['Average spent'] +
['SD spent'] +
['Average Utility'] +
['SD Utility'])
for a in range(n):
spamwriter.writerow([a] +
[agents_to_run[a]] +
[0.01*sum(agents_spent_stats[a])/N] +
[0.01*stddev(agents_spent_stats[a])] +
[0.01*sum(agents_util_stats[a])/N] +
[0.01*stddev(agents_util_stats[a])/N])
spamwriter.writerow([n] +
['Seller'] +
[0.01 * m] + [0.01 * std] + [0] + [0])
#for t in range(47, 48):
#for a in agents:
#print a,"'s added values is", av_value[a.id]
print options