def bayesMargDistSCPP(self): """ Return a MargDistSCPP with distribution according to dirichlet formulation where alpha = [1 ... 1] """ tempDist = [] for c in self.counts: tempDist.append( (numpy.atleast_1d((1 + c)/(len(c) + numpy.sum(c))), numpy.atleast_1d(self.binEdges)) ) return margDistSCPP(tempDist)
def yw2SCPP(**kwargs): oDir = kwargs.get('oDir') if not oDir: raise ValueError("Must provide oDir") oDir = os.path.realpath(oDir) agentType = kwargs.get('agentType') nAgents = kwargs.get('nAgents',8) m = kwargs.get('m', 5) L = kwargs.get('L',100) d = kwargs.get('d', 0.01) g = kwargs.get('g', 1000000) minPrice = kwargs.get('minPrice',0) maxPrice = kwargs.get('maxPrice',50) serial = kwargs.get('serial', False) dampen = kwargs.get('dampen', True) nProc = kwargs.get('nProc', multiprocessing.cpu_count() - 1) verbose = kwargs.get('verbose',True) pltItr = kwargs.get('pltItr', True) if verbose: print'Computing Symmetric Self Confirming Point Price Prediction.' print'Agent Type = {0}'.format(agentType) print 'Termination Threshold (d) = {0}'.format(d) print'Number of Iterations = {0}'.format(L) print'Number of Games = {0}'.format(g) print'Number of Items per Auction = {0}'.format(m) print 'Using Dampining = {0}'.format(dampen) if serial: print 'Using serial implementation' else: print'Number of Parallel Cores = {0}'.format(nProc) if not serial: nGamesList = [g/nProc]*nProc nGamesList[-1] += (g%nProc) #initial uniform distribution tempDist = [] p = float(1)/round(maxPrice - minPrice) a = [p]*(maxPrice - minPrice) # binEdges = [bin for bin in xrange( int(minPrice - maxPrice)+1 ) ] binEdges = numpy.arange(minPrice,maxPrice+1,1) for i in xrange(m): tempDist.append((numpy.atleast_1d(a),numpy.atleast_1d(binEdges))) currentDist = margDistSCPP(tempDist) #clean up # keep the binEdges for later histograms del p,a,tempDist ksList = [] klList = [] for t in xrange(0,L): if pltItr: cs = cs = ['y--p', 'm-*', 'r-o','y-^','y-*'] graphname = os.path.join(oDir,'ywSCPP_itr_{0}.png'.format(t)) if not ksList: title = "yw2SCPP, {0}, \n itr = {1}".format(agentType,t) else: title = "ywSCPP, {0} \n kld = {1}, ks = {2} \nitr = {3}".format(agentType,klList[-1],ksList[-1],t) currentDist.graphPdfToFile(fname = graphname, colorStyles = cs, title = title) if verbose: print "" print 'Iteration = {0}'.format(t) if dampen: kappa = float(L - t) / L else: kappa = 1 if serial: result = numpy.zeros(g,m) for i in xrange(g): result[i,:] = simulateAuction(agentType = agentType, nAgents = nAgents, margDist = currentDist) else: tasks = multiprocessing.JoinableQueue() results = multiprocessing.Queue() consumers = [Consumer(tasks,results) for i in xrange(nProc)] #start the consumers [w.start() for w in consumers] [tasks.put( yw2Task(agentType = agentType, nAgents = nAgents, margDist = currentDist, nGames = nGames) ) for nGames in nGamesList] [tasks.put(None) for i in xrange(nProc)] if verbose: start = time.time() print "Waiting for {0} game simulation Simulation...".format(g) tasks.join() if verbose: print "" print "Finished {0} simulations in {1} seconds.".format(g,time.time()-start) if verbose: print "" print "Collecting Results" start = time.time() rList = [] while not results.empty(): rList.append(results.get()) result = numpy.vstack(rList) if verbose: print "" print "Done collecting results in {0} seconds".format(time.time() - start) [w.terminate() for w in consumers] del results,tasks,consumers histData = [] histCount = [] for m in xrange(result.shape[1]): histData.append(numpy.histogram(result[:,m],binEdges,density=True)) # histCount.append(numpy.histogram(result[:,m],binEdges,density=False)) newDist = margDistSCPP(histData) ksList.append(ksStat(currentDist, newDist)) klList.append(klDiv(currentDist, newDist)) if ksList[-1] < d or t == (L-1): postfix = '{0}_{1}_{2}_{3}_{4}_{5}'.format(agentType, g, m, d,minPrice,maxPrice) pklName = 'distPricePrediction_' + postfix + '.pkl' txtName = 'distPricePrediction_' + postfix + '.txt' pricePredictionPklFilename = os.path.join(oDir, pklName) currentDist.savePickle(pricePredictionPklFilename) pricePredictionTxtFilename = os.path.join(oDir, txtName) #this section could be improved.... testdata = [] for m in xrange(currentDist.m): if m == 0: textdata = numpy.vstack([currentDist.data[m][0],currentDist.data[m][1][:-1]]) else: textdata = numpy.vstack([textdata, numpy.vstack([currentDist.data[m][0],currentDist.data[m][1][:-1]])]) numpy.savetxt(pricePredictionTxtFilename,textdata) print '' print'Terminated after {0} Iterations'.format(t) print'Final Expected Price Vector = {0}'.format(currentDist.expectedPrices()) ksListName = os.path.join(oDir,'ksList.json') with open(ksListName,'w') as f: json.dump(ksList,f) klListName = os.path.join(oDir,'klList.json') with open(klListName,'w') as f: json.dump(klList,f) break else: currentDist = updateDist(currentDist, newDist, kappa) del result, newDist
def yw2Hob(**kwargs): oDir = kwargs.get('oDir') if oDir == None: raise ValueError("Must specify oDir") if not os.path.exists(oDir): os.makedirs(oDir) agentType = kwargs.get('agentType') if agentType == None: raise ValueError("Must specify agentType.") nAgents = kwargs.get('nAgents',8) selfIdx = kwargs.get('selfIdx',numpy.random.randint(nAgents)) nGames = kwargs.get('nGames',100) m = kwargs.get('m',5) minPrice = int(kwargs.get('minPrice',0)) maxPrice = int(kwargs.get('maxPrice',50)) minValuation = kwargs.get('minValuation',0) maxValuation = kwargs.get('maxValuation',50) maxItr = kwargs.get('maxItr', 100) tol = kwargs.get('tol', 0.01) dampen = kwargs.get('dampen', True) parallel = kwargs.get('parallel',False) nProc = kwargs.get('nProc',multiprocessing.cpu_count()-1) savePkl = kwargs.get('savePkl',True) saveTime = kwargs.get('saveTime',True) pltMarg = kwargs.get('pltMarg',True) pltKld = kwargs.get('pltKld',True) pltKs = kwargs.get('pltKs',True) verbose = kwargs.get('verbose',True) if verbose: print 'oDir = {0}'.format(oDir) print 'agentType = {0}'.format(agentType) print 'nAgents = {0}'.format(nAgents) print 'nGames = {0}'.format(nGames) print 'm = {0}'.format(m) print 'minPrice = {0}'.format(minPrice) print 'maxPrice = {0}'.format(maxPrice) print 'minValuation = {0}'.format(minValuation) print 'maxValuation = {0}'.format(maxValuation) print 'selfIdx = {0}'.format(selfIdx) print 'maxItr = {0}'.format(maxItr) print 'tol = {0}'.format(tol) print 'parallel = {0}'.format(parallel) print 'nProc = {0}'.format(nProc) print 'savePkl = {0}'.format(savePkl) print 'saveTime = {0}'.format(saveTime) print 'pltMarg = {0}'.format(pltMarg) print 'pltKld = {0}'.format(pltKld) print 'pltKs = {0}'.format(pltKs) if savePkl: pklDir = os.path.join(oDir, 'gmmPkl') if not os.path.exists(pklDir): os.makedirs(pklDir) else: [os.remove(f) for f in glob.glob(os.path.join(pklDir,'*.pkl'))] if pltMarg: margDir = os.path.join(oDir,'pltMarg') if not os.path.exists(margDir): os.makedirs(margDir) else: [os.remove(f) for f in glob.glob(os.path.join(margDir,'*.png'))] [os.remove(f) for f in glob.glob(os.path.join(margDir,'*.pdf'))] if saveTime: timeFile = os.path.join(oDir,'time.txt') if os.path.exists(timeFile): os.remove(timeFile) klFile = os.path.join(oDir,'kld.txt') if os.path.exists(klFile): os.remove(klFile) ksFile = os.path.join(oDir,'ks.txt') if os.path.exists(ksFile): os.remove(ksFile) #initial uniform distribution tempDist = [] p = float(1)/round(maxPrice - minPrice) a = [p]*(maxPrice - minPrice) binEdges = numpy.arange(minPrice,maxPrice+1,1) for i in xrange(m): tempDist.append((numpy.atleast_1d(a),numpy.atleast_1d(binEdges))) currentDist = margDistSCPP(tempDist) del tempDist, p, a if pltMarg: of = os.path.join(margDir,'yw2SccpHob_{0}_m{1}_n{2}_init.pdf'.format(agentType,m,nAgents)) title = 'Marginal SCPP {0}'.format(agentType) xlabel = r'$q$' ylabel = r'$p(q)$' currentDist.graphPdfToFile(fname = of, title = title, xlabel = xlabel, ylabel = ylabel) klList = [] ksList = [] for t in xrange(maxItr): if verbose: print '' print 'Iteration = {0}'.format(t) if dampen: kappa = numpy.float(maxItr - t) / maxItr else: kappa = 1 if verbose: print 'kappa = {0}'.format(kappa) if verbose or saveTime: start = time.time() hob = simulateAuction( agentType = agentType, pricePrediction = currentDist, m = m, nAgents = nAgents, nGames = nGames, parallel = parallel, nProc = nProc, minValuation = minValuation, maxValuation = maxValuation, retType = 'hob', selfIdx = selfIdx, verbose = verbose) if verbose or saveTime: end = time.time() if verbose: print 'Simulated {0} auctions with {1} {2} agents in {3} seconds.'.format(nGames, nAgents, agentType, end-start) if savePkl: pklFile = os.path.join(pklDir, 'yw2ScppHob_{0}_m{1}_n{2}_{3:05d}.pkl'.format(agentType,m,nAgents,t)) with open(pklFile,'wb') as f: pickle.dump(currentDist,f) histData = [] for goodIdx in xrange(hob.shape[1]): histData.append(numpy.histogram(hob[:,goodIdx],binEdges,density=True)) newDist = margDistSCPP(histData) klList.append( klDiv(currentDist, newDist) ) if verbose: print 'kld = {0}'.format(klList[-1]) with open(klFile,'a') as f: f.write("{0}\n".format(klList[-1])) ksList.append( ksStat(currentDist,newDist) ) if verbose: print 'kls = {0}'.format(ksList[-1]) with open(ksFile,'a') as f: f.write("{0}\n".format(ksList[-1])) if saveTime: with open(timeFile,'a') as f: f.write("{0}\n".format(end-start)) if pltMarg: of = os.path.join(margDir,'yw2SccpHob_{0}_m{1}_n{2}_{3:05d}.pdf'.format(agentType,m,nAgents,t)) if verbose: print 'Plotting Marginal pdf to {0}'.format(of) title = 'Marginal SCPP {0}'.format(agentType) xlabel = r'$q$' ylabel = r'$p(q)$' newDist.graphPdfToFile(fname = of, title = title, xlabel = xlabel, ylabel = ylabel) if klList[-1] < tol or t == (maxItr - 1): if verbose: print 'TERMINATED:' if klList[-1] < tol: print '\tkld = {0} < tol = {1}'.format(klList[-1],tol) else: print '\tt = {0}, maxItr = {1}'.format(t,maxItr) if pltKld: if verbose: print 'Plotting K-L Divergence vs. Iteration.' klPdf = os.path.join(oDir,'kld.pdf') plt.figure() plt.plot(range(len(klList)), klList) plt.xlim((0,maxItr)) plt.ylabel('K-L Divergence') plt.xlabel('Iteration') plt.title('Marginal SCPP {0}'.format(agentType)) plt.savefig(klPdf) plt.close() if pltKs: if verbose: print 'Plotting K-S Stat vs. Iteration' ksPdf = os.path.join(oDir,'ks.pdf') plt.figure() plt.plot(range(len(ksList)), ksList) plt.xlim((0,maxItr)) plt.ylabel('K-S Statistic') plt.xlabel('Iteration') plt.title('Marginal SCPP {0}'.format(agentType)) plt.savefig(ksPdf) plt.close() if verbose: print 'DONE!!!!!' break else: if verbose: print 'Updating distribution.' currentDist = updateDist(currentDist,newDist,kappa) del hob, newDist
if __name__ == "__main__": oDir = "C:/auctionResearch/experiments/yw2/debug" agentType = "targetMU8" minPrice = 0 maxPrice = 50 m = 5 L = 100 d = 0.01 g = 5 tempDist = [] p = float(1)/round(maxPrice - minPrice) a = [p]*(maxPrice - minPrice) # binEdges = [bin for bin in xrange( int(minPrice - maxPrice)+1 ) ] binEdges = numpy.arange(minPrice,maxPrice+1,1) for i in xrange(m): tempDist.append((numpy.atleast_1d(a),numpy.atleast_1d(binEdges))) margDist = margDistSCPP(tempDist) task = yw2Task(agentType = "straightMU8", nAgents = 8, margDist = margDist, nGames = 1) winningBids = task() # yw2SCPP(oDir = oDir, agentType = agentType, minPrice = minPrice, maxPrice = maxPrice, m = m, L = L, d = d, g = g) pass
agentTypeList = ["straightMU8"]*8 selfIdx = 0 nGames = 20 parallel = False m=5 tempDist = [] p = float(1)/round(maxPrice - minPrice) a = [p]*(maxPrice - minPrice) # binEdges = [bin for bin in xrange( int(minPrice - maxPrice)+1 ) ] binEdges = numpy.arange(minPrice,maxPrice+1,1) for i in xrange(m): tempDist.append((numpy.atleast_1d(a),numpy.atleast_1d(binEdges))) pricePrediction = margDistSCPP(tempDist) print hobSim(pricePrediction = pricePrediction, agentTypeList = agentTypeList, selfIdx = selfIdx, nGames = nGames, parallel = parallel, m=m)