Ejemplo n.º 1
0
def random_puzzle(N=17):
    """Make a random puzzle with N or more assignments. Restart on contradictions.
    Note the resulting puzzle is not guaranteed to be solvable, but empirically about 99.8% of them are solvable.
    Some have multiple solutions."""
    values = dict((s, digits) for s in squares)
    for s in shuffled(squares):
        if not assign(values, s, random.choice(values[s])):
            break
        ds = [values[s] for s in squares if len(values[s]) == 1]
        if len(ds) >= N and len(set(ds)) >= 8:
            return ''.join(values[s] if len(values[s]) == 1 else '.'
                           for s in squares)
    # Give up and make a new puzzle
    return random_puzzle(N)
Ejemplo n.º 2
0
Archivo: camo.py Proyecto: jwgu/camo
def greedy_project(scan, mesh, stable_angle=np.radians(70)):
    filled = np.zeros(mesh.ntexels, 'bool')
    texel_colors = np.zeros((mesh.ntexels, 3))

    visible_by_view = np.zeros((scan.length, mesh.ntexels), 'bool')
    stable_by_view = np.zeros((scan.length, mesh.ntexels), 'bool')

    for frame in xrange(scan.length):
        visible = mesh.texel_visible(scan, frame)
        visible_by_view[frame] = visible

        for j in xrange(mesh.nfaces):
            d = np.dot(
                ut.normalized(-mesh.face_center[j] + scan.center(frame)),
                mesh.face_planes[j][:3])
            assert -1 <= d <= 1
            angle = np.arccos(abs(d))
            print frame, j, np.rad2deg(angle), (angle <= stable_angle)
            on_face = (mesh.tex2juv[:, 0] == j)
            stable_by_view[frame,
                           on_face] = visible_by_view[frame, on_face] * (
                               angle <= stable_angle)
        print 'stable texels', np.sum(stable_by_view[frame])

    projectable = stable_by_view.copy()
    unstable = np.all(-stable_by_view, axis=0)
    print 'Unstable faces:', np.unique(mesh.tex2juv[unstable, 0])
    projectable[:, unstable] = visible_by_view[:, unstable]

    unused = range(scan.length)
    while np.sum(projectable) > 0:
        # it's ok to project onto a face if it's (1) unfilled (2) it's either stable, or it's unstable in every view
        frame = max(ut.shuffled(unused),
                    key=lambda f: np.sum(projectable[f, :]))

        projs = scan.project(frame, mesh.texel_pts)
        ok = projectable[frame]
        print 'chose', frame, np.sum(
            projectable[frame, :]), np.sum(ok), 'projecting to', np.unique(
                mesh.tex2juv[projectable[frame, :], 0])
        texel_colors[ok] = ig.lookup_bilinear(scan.im(frame), projs[ok, 0],
                                              projs[ok, 1])
        projectable[:, ok] = 0

        unused.remove(frame)
        print 'projected', frame, np.sum(filled)

    return [texel_colors]
Ejemplo n.º 3
0
def random_project(scan, mesh, order = None):
  # totally random projection order
  if order is None:
    order = ut.shuffled(range(scan.length))
  filled = np.zeros(mesh.ntexels, 'bool')
  fill = np.nan # 0
  texel_colors = fill + np.zeros((mesh.ntexels, 3))
  print 'projection order =', order
  for frame in order:
    ok = mesh.texel_visible(scan, frame)
    ok[filled] = 0
    projs = scan.project(frame, mesh.texel_pts)
    if np.isnan(fill):
      assert(np.all(np.isnan(texel_colors[ok])))
    else:
      assert(np.all(texel_colors[ok] == fill))
    texel_colors[ok] = ig.lookup_bilinear(scan.im(frame), projs[ok, 0], projs[ok, 1])
    filled[ok] = 1
  return [texel_colors]
Ejemplo n.º 4
0
Archivo: camo.py Proyecto: jwgu/camo
def random_project(scan, mesh, order=None):
    # totally random projection order
    if order is None:
        order = ut.shuffled(range(scan.length))
    filled = np.zeros(mesh.ntexels, 'bool')
    fill = np.nan  # 0
    texel_colors = fill + np.zeros((mesh.ntexels, 3))
    print 'projection order =', order
    for frame in order:
        ok = mesh.texel_visible(scan, frame)
        ok[filled] = 0
        projs = scan.project(frame, mesh.texel_pts)
        if np.isnan(fill):
            assert (np.all(np.isnan(texel_colors[ok])))
        else:
            assert (np.all(texel_colors[ok] == fill))
        texel_colors[ok] = ig.lookup_bilinear(scan.im(frame), projs[ok, 0],
                                              projs[ok, 1])
        filled[ok] = 1
    return [texel_colors]
Ejemplo n.º 5
0
def greedy_project(scan, mesh, stable_angle = np.radians(70)):
  filled = np.zeros(mesh.ntexels, 'bool')
  texel_colors = np.zeros((mesh.ntexels, 3))

  visible_by_view = np.zeros((scan.length, mesh.ntexels), 'bool')
  stable_by_view = np.zeros((scan.length, mesh.ntexels), 'bool')
  
  for frame in xrange(scan.length):
    visible = mesh.texel_visible(scan, frame)
    visible_by_view[frame] = visible
    
    for j in xrange(mesh.nfaces):
      d = np.dot(ut.normalized(-mesh.face_center[j] + scan.center(frame)), mesh.face_planes[j][:3])
      assert -1 <= d <= 1
      angle = np.arccos(abs(d))
      print frame, j, np.rad2deg(angle), (angle <= stable_angle)
      on_face = (mesh.tex2juv[:, 0] == j)
      stable_by_view[frame, on_face] = visible_by_view[frame, on_face]*(angle <= stable_angle)
    print 'stable texels', np.sum(stable_by_view[frame])

  projectable = stable_by_view.copy()
  unstable = np.all(-stable_by_view, axis = 0)
  print 'Unstable faces:', np.unique(mesh.tex2juv[unstable, 0])
  projectable[:, unstable] = visible_by_view[:, unstable]

  unused = range(scan.length)
  while np.sum(projectable) > 0:
    # it's ok to project onto a face if it's (1) unfilled (2) it's either stable, or it's unstable in every view
    frame = max(ut.shuffled(unused), key = lambda f : np.sum(projectable[f, :]))

    projs = scan.project(frame, mesh.texel_pts)
    ok = projectable[frame]
    print 'chose', frame, np.sum(projectable[frame, :]), np.sum(ok), 'projecting to', np.unique(mesh.tex2juv[projectable[frame, :], 0])
    texel_colors[ok] = ig.lookup_bilinear(scan.im(frame), projs[ok, 0], projs[ok, 1])
    projectable[:, ok] = 0

    unused.remove(frame)
    print 'projected', frame, np.sum(filled)
      
  return [texel_colors]
Ejemplo n.º 6
0
def main(args):

    usage_msg = "Usage:  %prog [options] PeerClass1[,cnt] PeerClass2[,cnt2] ..."
    parser = OptionParser(usage=usage_msg)

    def usage(msg):
        print "Error: %s\n" % msg
        parser.print_help()
        sys.exit()

    parser.add_option("--loglevel",
                      dest="loglevel",
                      default="info",
                      help="Set the logging level: 'debug' or 'info'")

    parser.add_option("--mech",
                      dest="mechanism",
                      default="gsp",
                      help="Set the mechanim: 'gsp' or 'vcg' or 'switch'")

    parser.add_option("--num-rounds",
                      dest="num_rounds",
                      default=48,
                      type="int",
                      help="Set number of rounds")

    parser.add_option("--min-val",
                      dest="min_val",
                      default=25,
                      type="int",
                      help="Min per-click value, in cents")

    parser.add_option("--max-val",
                      dest="max_val",
                      default=175,
                      type="int",
                      help="Max per-click value, in cents")

    parser.add_option("--budget",
                      dest="budget",
                      default=500000,
                      type="int",
                      help="Total budget, in cents")

    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))

    ###############
    # ERASE LATER #
    ###############

    return (0.01 * m, 0.01 * std, list(map(lambda x: 0.01 * x / N,
                                           total_spent)),
            list(map(lambda x: 0.01 * totals[x] / N, totals.keys())))
Ejemplo n.º 7
0
def main(args):

    usage_msg = "Usage:  %prog [options] PeerClass1[,cnt] PeerClass2[,cnt2] ..."
    parser = OptionParser(usage=usage_msg)

    def usage(msg):
        print "Error: %s\n" % msg
        parser.print_help()
        sys.exit()
    
    parser.add_option("--loglevel",
                      dest="loglevel", default="info",
                      help="Set the logging level: 'debug' or 'info'")

    parser.add_option("--mech",
                      dest="mechanism", default="gsp",
                      help="Set the mechanim: 'gsp' or 'vcg' or 'switch'")

    parser.add_option("--num-rounds",
                      dest="num_rounds", default=48, type="int",
                      help="Set number of rounds")

    parser.add_option("--min-val",
                      dest="min_val", default=25, type="int",
                      help="Min per-click value, in cents")

    parser.add_option("--max-val",
                      dest="max_val", default=175, type="int",
                      help="Max per-click value, in cents")

    parser.add_option("--budget",
                      dest="budget", default=500000, type="int",
                      help="Total budget, in cents")
    
    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))
Ejemplo n.º 8
0
def main(args):

    usage_msg = "Usage: %prog [options] PeerClass1[,cnt] PeerClass2[,cnt2] ..."
    parser = OptionParser(usage=usage_msg)

    def usage(msg):
        print "Error: %s\n" % msg
        parser.print_help()
        sys.exit()

    parser.add_option("--loglevel",
                      dest="loglevel",
                      default="info",
                      help="Set the logging level: 'debug' or 'info'")

    parser.add_option("--mech",
                      dest="mechanism",
                      default="gsp",
                      help="Set the mechanim: 'gsp' or 'vcg' or 'switch'")

    parser.add_option("--num-rounds",
                      dest="num_rounds",
                      default=48,
                      type="int",
                      help="Set number of rounds")

    parser.add_option("--min-val",
                      dest="min_val",
                      default=25,
                      type="int",
                      help="Min per-click value, in cents")

    parser.add_option("--max-val",
                      dest="max_val",
                      default=175,
                      type="int",
                      help="Max per-click value, in cents")

    parser.add_option("--budget",
                      dest="budget",
                      default=500000,
                      type="int",
                      help="Total budget, in cents")

    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)]
    """ 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
Ejemplo n.º 9
0
def main(args):

    usage_msg = "Usage: %prog [options] PeerClass1[,cnt] PeerClass2[,cnt2] ..."
    parser = OptionParser(usage=usage_msg)

    def usage(msg):
        print "Error: %s\n" % msg
        parser.print_help()
        sys.exit()
    
    parser.add_option("--loglevel",
                      dest="loglevel", default="info",
                      help="Set the logging level: 'debug' or 'info'")

    parser.add_option("--mech",
                      dest="mechanism", default="gsp",
                      help="Set the mechanim: 'gsp' or 'vcg' or 'switch'")

    parser.add_option("--num-rounds",
                      dest="num_rounds", default=48, type="int",
                      help="Set number of rounds")

    parser.add_option("--min-val",
                      dest="min_val", default=25, type="int",
                      help="Min per-click value, in cents")

    parser.add_option("--max-val",
                      dest="max_val", default=175, type="int",
                      help="Max per-click value, in cents")

    parser.add_option("--budget",
                      dest="budget", default=500000, type="int",
                      help="Total budget, in cents")
    
    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)]
    
    """ 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
Ejemplo n.º 10
0
def run_sim(options, args):
    """
    Runs the auction simulation given a parsed set of options and its leftover
    elements. 

    Returns a dictionary of {key: value} pairings with information on the results
    of the simulation. 
    """
    # 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
    statistics = []
    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 j,vals in enumerate(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)

            # save stats
            statistics.append(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))

#print "config", config.budget
    

    #for t in range(47, 48):
    #for a in agents:
        #print a,"'s added values is", av_value[a.id]

    # we return mean revenue for these iterations
    return {'revenue'   : m,
            'revenues'  : total_revenues,
            'stats'     : statistics}