Esempio n. 1
0
def simulateAuction( **kwargs ):
    agentType     = kwargs.get('agentType')
    nAgents       = kwargs.get('nAgents')
    margDist      = kwargs.get('margDist')
    nGames        = kwargs.get('nGames')
    m             = margDist.m

    winningBids = numpy.zeros((nGames,m))
    
    for g in xrange(nGames):
        agentList = [agentFactory(agentType = agentType, m = m) for i in xrange(nAgents)]
    
        bids = numpy.atleast_2d([agent.bid(margDistPrediction = margDist) for agent in agentList])
        
        winningBids[g,:] = numpy.max(bids,0)
#    return numpy.max(bids,0)
    return winningBids
Esempio n. 2
0
def bayesSCPP(**kwargs):
    """
    save pkl will save the hist class; to recover bayesian probabilities must call hist.bayesMargDistSCPP()
    """
    oDir         = kwargs.get('oDir')
    agentType    = kwargs.get('agentType')
    nAgents      = kwargs.get('nAgents',8)
    m            = kwargs.get('m',5)
    minPrice     = kwargs.get('minPrice',0)
    maxPrice     = kwargs.get('maxPrice',50)
    maxSim       = kwargs.get('maxSim', 1000)
    nGames       = kwargs.get('nGames', 100)
    parallel     = kwargs.get('parallel', False)
    nProc        = kwargs.get('nProc', multiprocessing.cpu_count() - 1)
    tol          = kwargs.get('tol',0.001)
    plot         = kwargs.get('plot', True)
    log          = kwargs.get('log', True)
    saveBayesPkl = kwargs.get('saveBayesPkl',True)
#    saveBayesNpz = kwargs.get('saveBayesNpz',True)
    saveBayesTxt = kwargs.get('saveBayesTxt',True)
    saveHistPkl  = kwargs.get('saveHistPkl',False)
#    saveHistNpz  = kwargs.get('saveHistNpz',False)
    savenpz      = kwargs.get('savenpz',False)
    verbose      = kwargs.get('verbose', True)
    
    if not oDir:
        str = "-----ERROR-----\n" +\
              "In bayesSCPP(...)\n" +\
              "Must Provide output directory\n"
        raise ValueError(str)
    
    if plot:
        pltDir = os.path.join(oDir,'plots')
        if not os.path.exists(pltDir):
            os.makedirs(pltDir)
        else:
            [os.remove(filename) for filename in glob.glob(os.path.join(pltDir,'*.png'))]
#            shutil.rmtree(pltDir,ignore_errors=True)
#            os.makedirs(pltDir)
    
    if not os.path.exists(oDir):
        os.makedirs(oDir)
    
    if saveBayesPkl:
        bayesPklDir = os.path.join(oDir,'bayesSCPP_bayesPkl')
        if not os.path.exists(bayesPklDir):
            os.makedirs(bayesPklDir)
        else:
            [os.remove(f) for f in glob.glob(os.path.join(bayesPklDir,'*.pkl'))]
            
            
    if saveBayesTxt:
        bayesTxtDir = os.path.join(oDir,'bayesSCPP_bayesTxt')
        if not os.path.exists(bayesTxtDir):
            os.makedirs(bayesTxtDir)
        else:
            [os.remove(f) for f in glob.glob(os.path.join(bayesTxtDir,'*txt'))]
            
            
    if saveHistPkl:
        histPklDir = os.path.join(oDir,'bayesSCPP_histPkl')
        if not os.path.exists(histPklDir):
            os.makedirs(histPklDir)
        else:
            [os.remove(f) for f in glob.glob(os.path.join(histPklDir,'*.pkl'))]

                
    if log:
        logFile = os.path.join(oDir,'bayesSCPP_{0}.txt'.format(agentType))
        if os.path.exists(logFile):
            os.remove(logFile)
            
        with open(logFile,'a') as f:
            f.write("oDir      = {0}\n".format(oDir))
            f.write("agentType = {0}\n".format(agentType))
            f.write("nAgents   = {0}\n".format(nAgents))
            f.write("tol       = {0}\n".format(tol))
            f.write("m         = {0}\n".format(m))
            f.write("minPrice  = {0}\n".format(minPrice))
            f.write("maxPrice  = {0}\n".format(maxPrice))
            f.write("maxSim    = {0}\n".format(maxSim))
            f.write("nGames    = {0}\n".format(nGames))
            f.write("parallel  = {0}\n".format(parallel))
            f.write("plot      = {0}\n".format(plot))
        
    if verbose:
        print "oDir      = {0}".format(oDir)
        print "agentType = {0}".format(agentType)
        print "nAgents   = {0}".format(nAgents)
        print "tol       = {0}".format(tol)
        print "m         = {0}".format(m)
        print "minPrice  = {0}".format(minPrice)
        print "maxPrice  = {0}".format(maxPrice)
        print "maxSim    = {0}".format(maxSim)
        print "nGames    = {0}".format(nGames)
        print "parallel  = {0}".format(parallel)
        print "plot      = {0}".format(plot)
    
    
    currHist = hist()
    if plot:
        oFile = os.path.join(pltDir,'bayesSCPP_{0}_{1}.png'.format(agentType,0))
        title='bayesSCPP, {0}, Initial Distribution'.format(agentType)
        currHist.bayesMargDistSCPP().graphPdfToFile(fname = oFile,
                                                    title=title)
    
    klList = []
    ksList = []
    
    agentFactoryParams = {'agentType' : agentType,
                          'm'         : m,
                          'minPrice'  : minPrice,
                          'maxPrice'  : maxPrice}

    for sim in xrange(maxSim):
        oldHist = copy.deepcopy(currHist)
        
        for i in xrange(nGames):
            agentList = [agentFactory(**agentFactoryParams) for i in xrange(nAgents)]
            
            if parallel:
                pool = multiprocessing.Pool(nProc)
                
                bayesMarg = currHist.bayesMargDistSCPP()
                
                results = [pool.apply_async(bidHelper, kwds = {'agent': agent, 'bayesMargDist': bayesMarg} ) for agent in agentList]
                
                pool.close()
                
                pool.join()
                
                bids = numpy.zeros((nAgents,m))
                
                for idx,r in enumerate(results):
                    bids[idx,:] = r.get()
                    r._value = []
                    
            else:
                bids = numpy.atleast_2d([agent.bid(pricePrediction = currHist.bayesMargDistSCPP()) for agent in agentList])
                
            winningBids = numpy.max(bids,0)
            for idx, wb in enumerate(winningBids):
                currHist.upcount(idx,wb,mag=1)
#            [currHist.upcount(idx, wb, mag=1) for idx, wb in enumerate(winningBids)]

        currBayesMargDist = currHist.bayesMargDistSCPP()
        oldBayesMargDist  = oldHist.bayesMargDistSCPP()
        
        klList.append(klDiv(currBayesMargDist, oldBayesMargDist))
        
        ksList.append(ksStat(currBayesMargDist, oldBayesMargDist))
        
        if verbose:
            print ''
            print 'itr = {0}'.format(sim+1)
            print '\tNumber of Games = {0}'.format((sim+1)*nGames)
            print '\tkld             = {0}'.format(klList[-1])
            print '\tks              = {0}'.format(ksList[-1])
            
        
        if plot:
            oPlot = os.path.join(pltDir,'bayesSCPP_{0}_{1}.png'.format(agentType,(sim+1)*nGames))
            title='BayesSCPP {0}, klD = {1:.6}, ks = {2:.6} itr = {3}'.format(agentType,klList[-1],ksList[-1],(sim+1)*nGames)
            currBayesMargDist.graphPdfToFile(fname = oPlot, title=title)
            
            
        if saveBayesPkl:
            of = os.path.join(bayesPklDir,'bayesSCPP_bayesMargDist_{0}_{1}.pkl'\
                              .format(agentType,int((sim+1)*nGames)))
            with open(of, 'w') as f:
                pickle.dump(currBayesMargDist,f)
                
        if saveBayesTxt:
            of = os.path.join(bayesTxtDir,'bayesSCPP_bayesTxt_{0}_{1}.txt'\
                              .format(agentType,int((sim+1)*nGames)))
            
            currBayesMargDist.savetxt(of)
            
         
        if saveHistPkl:
            of = os.path.join(bayesPklDir,'bayesSCPP_hist_{0}_{1}.pkl'\
                              .format(agentType,(sim+1)*nGames))
            with open(of, 'w') as f:
                pickle.dump(currHist,f)
                
        
            
        if klList[-1] < tol:
            break
      
    if log:  
        with open(logFile,'a') as f:
            f.write('Done after {0} games ({1} iterations)\n'.format((sim+1)*nGames,sim))
            f.write('kl = {0}\n'.format(klList))
            f.write('ks = {0}\n'.format(ksList))
        
    klFile = os.path.join(oDir,'kl.json')
    with open(klFile,'w') as f:
        json.dump(klList,f)
        
    ksFile = os.path.join(oDir,'ks.json')
    with open(ksFile,'w') as f:
        json.dump(ksList,f)
        
    if verbose:
        print 'Done'
Esempio n. 3
0
def comp2Agents(**kwargs):
    oDir         = kwargs.get('oDir')
    pp1          = kwargs.get('pp1')
    n1           = kwargs.get('n1',4)
    pp2          = kwargs.get('pp2')
    n2           = kwargs.get('n2',4)
    agentType1   = kwargs.get('agentType1')
    agentType2   = kwargs.get('agentType2')
    m            = kwargs.get('m',5)
    minValuation = kwargs.get('minValuation',0)
    maxValuation = kwargs.get('maxValuation',50)
    nGames       = kwargs.get('nGames',1000)
    parallel     = kwargs.get('parallel',True)
    nProc        = kwargs.get('nProc', multiprocessing.cpu_count() - 1)
    verbose      = kwargs.get('verbose', True)
    
    if verbose:
        print ''
        print 'In comp2Agents(...)'
        print 'oDir         = {0}'.format(oDir)
        
        print 'agentType1   = {0}'.format(agentType1)
        print 'n1           = {0}'.format(n1)
        
        print 'agentType2   = {0}'.format(agentType2)
        print 'n2           = {0}'.format(n2)
        
        print 'm            = {0}'.format(m)
        print 'nGames       = {0}'.format(nGames)
        print 'minValuation = {0}'.format(minValuation)
        print 'maxValuation = {0}'.format(maxValuation)
        
        print ''  
    
    if parallel:
        pool = multiprocessing.Pool(nProc)
        
        nGameList = [nGames//nProc]*nProc
        nGameList[-1] += (nGames % nProc)
        
        if verbose:
            print 'Running parallel simulation.'
            print 'Number of cores = {0}'.format(nProc)
            print 'Number of simulations per core = {0}'.format(nGameList)
            print 'Total Number of simulations = {0}'.format((sum(nGameList)))
            
        results = []
        
        for p in xrange(nProc):
            subArgs = copy.deepcopy(kwargs)
            subArgs.update(kwargs)
            subArgs['parallel'] = False
            subArgs['nGames'] = nGameList[p]
            subArgs['verbose'] = False
            subArgs['oDir'] = None
                 
            results.append(pool.apply_async(comp2Agents, kwds = subArgs))

        pool.close()
        pool.join()
        
        for idx, r in enumerate(results):
            if idx == 0:
                agentSurplus = r.get()
            else:                
                agentSurplus = numpy.concatenate((agentSurplus,r.get()))
            r._value = []
        
    else:
        
        auction = simultaneousAuction( m       = m,
                                       nPrice  = 2,
                                       reserve = 0)
        
        for i in xrange(n1):
            agent = agentFactory(agentType = agentType1, 
                                 m         = m, 
                                 vmin      = minValuation, 
                                 vmax      = maxValuation  )
            
            agent.pricePrediction = pp1
            
            
            auction.attachAgents(agent)
                                 
        for i in xrange(n2):
            agent = agentFactory(agentType = agentType2, 
                                 m         = m, 
                                 vmin      = minValuation, 
                                 vmax      = maxValuation  )
            
            agent.pricePrediction = pp2
            
            auction.attachAgents(agent)
        
        agentSurplus = numpy.zeros((nGames,n1+n2))
        
        for itr in xrange(nGames):
            if verbose:
                print 'Simulating {0} out of {1} auctions'.format(itr,nGames)
            [agent.randomValuation() for agent in auction.agentList]
            
            if verbose:
                for idx, agent in enumerate(auction.agentList):
                    print 'agent[{0}] = {1} ; l = {2}, v = {3}'.format(idx, agent.type(), agent.l, agent.v)
            
            auction.runAuction()
            
            auction.notifyAgents()
            
            surplus = auction.agentSurplus()
            
            agentSurplus[itr,:] = surplus
            
            if verbose:
                print 'Agent Surplus = {0}'.format(surplus)
        
    if oDir:
        oDir = os.path.realpath(oDir)
        if not os.path.exists(oDir):
            os.makedirs(oDir)
            
        sFile = os.path.join(oDir,'{0}_{1}_agentSurplus.txt'.format(agentType1,agentType2))
        
        numpy.savetxt(sFile, agentSurplus) 
        
    return agentSurplus
Esempio n. 4
0
def simAuctionHelper(**kwargs):
    agentType = kwargs.get('agentType')
        
    if isinstance(agentType,list):
        nAgents = len(agentType)
        
    elif isinstance(agentType,str):
        nAgents   = kwargs.get('nAgents',8)
        agentType = [agentType]*nAgents
    
    nGames          = kwargs.get('nGames')
        
    pricePrediction = kwargs.get('pricePrediction')
        
    m            = kwargs.get('m',5)
    minValuation = kwargs.get('minValuation',0)
    maxValuation = kwargs.get('maxValuation',50)
    
    verbose      = kwargs.get('verbose', False)

    retType      = kwargs.get('retType','bids')
    
    l            = kwargs.get('l')
    
    if retType == 'hob':
        selfIdx  = kwargs.get('selfIdx')
        if selfIdx == None:
            raise ValueError("ERROR - simulateAuction(...):\n" + \
                             "\t Must specify selfIdx when retType == 'hob'")
            
    if verbose:
        print 'In simulateAuction(...)'
        print 'agentType    = {0}'.format(agentType)
        print 'nGames       = {0}'.format(nGames)
        print 'm            = {0}'.format(m)
        print 'minValuation = {0}'.format(minValuation)
        print 'maxValuation = {0}'.format(maxValuation)
        print 'retType      = {0}'.format(retType)
        
    if retType == 'bids':
        ret = numpy.zeros((nGames,nAgents,m))
    elif retType == 'firstPrice' or retType == 'hob':
        ret = numpy.zeros((nGames,m))
    else:
        raise ValueError("simulateAuction - Unknown return type")
    
    agents = [agentFactory(agentType = atype, m = m, vmin = minValuation, vmax = maxValuation) for atype in agentType]
    
    for itr in xrange(nGames):
        if verbose:
            print 'running serial game {0}'.format(itr)
                
        if retType == 'firstPrice' or retType == 'secondPrice' or retType == 'hob':
            gameBids = numpy.zeros((nAgents,m))   
        
        if isinstance(pricePrediction,list):  
             
            for agentIdx, agent, pp in zip(numpy.arange(nAgents),agents,pricePrediction):
                
                agent.randomValuation(l = l)     
                    
                if retType == 'bids':
                    ret[itr, agentIdx, :] = agent.bid(pricePrediction = pp)
                            
                elif retType == 'firstPrice' or retType == 'secondPrice' or retType == 'hob':
                    gameBids[agentIdx,:] = agent.bid(pricePrediction = pp)
                    
        else:
            for agentIdx, agent in enumerate(agents):
                
                agent.randomValuation(l=l)
                
                if retType == 'bids':
                    ret[itr, agentIdx, :] = agent.bid(pricePrediction = pricePrediction)
                    
                elif retType == 'firstPrice' or retType == 'secondPrice' or retType == 'hob':
                        gameBids[agentIdx,:] = agent.bid(pricePrediction = pricePrediction)
            
        if retType == 'firstPrice':
            ret[itr,:] = numpy.max(gameBids,0)
            
        elif retType == 'hob':
            
            ret[itr,:] = numpy.max( numpy.delete(gameBids,selfIdx,0), 0 )
            
        elif retType == 'secondPrice':
            for goodIdx in xrange(gameBids.shape[1]):
                goodBids = gameBids[:,m]
                goodArgMax = numpy.argmax(goodBids)
                ret[itr,goodIdx] = numpy.max(numpy.delete(goodBids,goodArgMax))
            
    return ret
Esempio n. 5
0
def hobSim(**kwargs):
    """
    Function to simulate an auction and record other highest agent bids.
    
    Parameters
    ----------
    agentTypeList : list of strings, required
        Types of agents in the auction.
        
    selfIdx : int, zero indexed, 0 <= selfIdx < len(agentTypeList)
        Index for agent in list representing self.
        
    nGames : int, optional 
        Number of simulations to run. Defaults to 10,000
        
    parallel : bool, optional
        Run simulations in parallel or serial (mainly for debugg). Defaults True.
        simulateAuctionJointGMM
    pricePrediction : Either margDistSCPP or jointGMM instance 
        Price Predictions for agents to use.
                
    m := int, optional
        Number of goods up for auction. Defaults to 5.
        
    vmin : float, optional 
        Minimum allowable valuation. Defaults to 0.
        
    vmax : float, optional
        Maximum allowable valuation. Defaults to 50.
           
    outputs
    -------
    highestOtherBids : ndarray (nGames,m)
        Array of highest other agent bids.
    """
    
    agentTypeList   = kwargs.get('agentTypeList')
    selfIdx         = kwargs.get('selfIdx')
    nGames          = kwargs.get('nGames')
    parallel        = kwargs.get('parallel',True)
    nProc           = kwargs.get('nProc', multiprocessing.cpu_count())
    pricePrediction = kwargs.get('pricePrediction')
    
    m    = kwargs.get('m',5)
    vmin = kwargs.get('vmin',0)
    vmax = kwargs.get('vmax',50)
    
    highestOtherBids = numpy.zeros((nGames,m))
    
    if parallel:
        pool = multiprocessing.Pool(nProc)
        
#        winning
    
    else:
        n = len(agentTypeList)
    
        agents = [agentFactory(agentType = agentType, m = m, vmin = vmin, vmax = vmax) for agentType in agentTypeList]
    
        otherAgents = numpy.delete(numpy.arange(n),selfIdx,0)
    
        bids = numpy.zeros((n,m))
        
        for itr in xrange(nGames):
            
            if isinstance(pricePrediction,list):
                for idx, agent, pp in zip(numpy.arange(n),agents,pricePrediction):
                    #draw a new valuation
                    agent.randomValuation()
                    bids[idx] = agent.bid(pricePrediction = pp)
            else:
                for idx, agent in enumerate(agents):
                    #draw a new valuation
                    agent.randomValuation()
                    bids[idx] = agent.bid(pricePrediction = pricePrediction)
                
            
            highestOtherBids[itr,:] = numpy.max(bids[otherAgents],0)
            
    return highestOtherBids