Пример #1
0
 def test_expectedSurplus(self):
     """
     [20,15] -> (45 - 35)*.1
     [20,20] -> (45 -40)*.4
     [30,15] -> (20 - 5)*.1
     [30,20] -> (20 - 20)*.4
     Expected Surplus = 3.5
     """
     samples = numpy.zeros((1000,2))
     samples[:100,:] = numpy.asarray([20,15])
     samples[100:500,:] = numpy.asarray([20,20])
     samples[500:600,:] = numpy.asarray([30,15])
     samples[600:,:] = numpy.asarray([30,20])
     
     m=2
     l = 1
     v = [45,20]
     bundles = listBundles(m)
     revenue = msListRevenue(bundles, v, l)
     bids = numpy.asarray([25.,25.])
     
     bundleRevenueDict = {}
     for b,r in zip(bundles,revenue):
         bundleRevenueDict[tuple(b)] = r
     
     numpy.testing.assert_equal(expectedSurplus_(bundleRevenueDict, bids, samples), 
                                3.5,'test_expetedSurplus failed.',True)
Пример #2
0
def bruteForceS(bundleRevenueDict, evalSamples=None, min=0.0, max=50.0, step=1.0, ret="bid"):
    m = evalSamples.shape[1]

    xx = numpy.arange(min, max + 1.0, step)

    maxSurplus = -numpy.float("inf")
    bid = None
    for c in itertools.product(xx, repeat=m):
        es = expectedSurplus_(bundleRevenueDict, numpy.atleast_1d(c), evalSamples)
        if es > maxSurplus:
            bid = numpy.atleast_1d(c)
            maxSurplus = es
            print bid
            print maxSurplus

    if ret == "bid":
        return bid
    elif ret == "all":
        return bid, maxSurplus
Пример #3
0
def bidEvalS(bundleRevenueDict, candidateSamples, evalSamples, ret='bid'):
    
    maxSurplus = 0.0
    bid = numpy.zeros(candidateSamples.shape[1])
    
    for candidateSample in candidateSamples:
        es = expectedSurplus_(bundleRevenueDict, 
                              numpy.atleast_1d(candidateSample), 
                              evalSamples)
        
        if es > maxSurplus:
            bid = numpy.atleast_1d(candidateSample)
            maxSurplus = es
            
    if ret == 'bid':
        return bid
    
    elif ret == 'all':
        return bid, maxSurplus
Пример #4
0
def main():
    desc="Compute jointLocal bids given v's, l's initial bids and parameters"
    
    parser = argparse.ArgumentParser(description=desc)
    
    parser.add_argument('-s','--samplesFile',dest='samplesFile',
                        required=True,type=str,
                        help='Samples (text file)')
    
    parser.add_argument('-v','--vfile',dest='vfile',
                        required=True,type=str,
                        help='Valuation v vector txt file.')
    
    parser.add_argument('-l','--lfile',dest='lfile',
                        required=True,type=str,
                        help='Valuation lambda txt file.')
    
    parser.add_argument('-ib','--initBidFile',dest='initBidFile',
                        required=True,type=str,
                        help='Initial Bids txt file.')
    
    parser.add_argument('-es','--evalSamples',dest='evalFile',
                        required=True,type=str,
                        help='Evaluation samples file.')
    
    parser.add_argument('-o','--odir',dest='odir',
                        required=True,type=str,
                        help='Output directory.')
    
    parser.add_argument('-eps','--eps',dest='eps',
                        required=False,default=1,type=float,
                        help='Eps condLocal parameter')
    
    parser.add_argument('-mi','--maxitr', dest='maxitr',
                        required=False, default=100, type=int,
                        help='Maximum Update Iterations')
    
    parser.add_argument('-t','--tol',dest='tol',
                        required=False, default=1e-5,
                        type=float,help='L2 Update Stopping Tolerance')
    
    parser.add_argument('--verbose',dest='verbose',
                        required=False,default=False,
                        type=bool,help='Output debugg information')
    
    args = parser.parse_args()
    
    args.odir = os.path.realpath(args.odir)
    
    jointSamples = numpy.loadtxt(os.path.realpath(args.samplesFile))
    initBids     = numpy.loadtxt(os.path.realpath(args.initBidFile))
    vmat         = numpy.loadtxt(os.path.realpath(args.vfile))
    lmat         = numpy.loadtxt(os.path.realpath(args.lfile))
    evalSamples  = numpy.loadtxt(os.path.realpath(args.evalFile))
    
    m = initBids.shape[1]
    
    bundles = listBundles(m)
    
    bids = numpy.zeros(initBids.shape)
    es   = numpy.zeros(initBids.shape[0])
    
    for itr, initBid, v, l in zip(xrange(vmat.shape[0]),initBids,vmat,lmat):
        print 'iteration {0}'.format(itr)
        revenue = msListRevenue(bundles,v,l)
        
        bids[itr,:] = \
            condLocal(bundles,revenue,
                      initBid,jointSamples,
                      maxItr = args.maxitr,
                      tol = args.tol,
                      verbose = args.verbose,
                      ret = 'bids',
                      eps = args.eps)
        
        brd = {}
        for b,r in zip(bundles,revenue):
            brd[tuple(b)] = r
        
        es[itr] = expectedSurplus_(brd, bids[itr,:], evalSamples)
        
    numpy.savetxt(os.path.join(args.odir,'condLocalBids.txt'), bids)
    numpy.savetxt(os.path.join(args.odir,'condLocalExpectedSurplus.txt'),es)
    
    with open(os.path.join(args.odir,'condLocalStats.txt'),'w') as f:
        print >> f, numpy.mean(es)
        print >> f, numpy.var(es)
Пример #5
0
def localSearchDominance(nbids = 100, n_samples = 1000, rootDir = ".",
                         scppFile = None, vfile = None, lfile = None,
                         njs = 1000, nms = 1000):
         
    scppFile = os.path.realpath(scppFile)                
    with open(scppFile,'r') as f:
        scpp = pickle.load(f)
        
    m = scpp.m()
    
    bundles = listBundles(m)
    
    
    
    rootDir = os.path.realpath(rootDir)
    oDir = os.path.join(rootDir,timestamp_())
    if not os.path.exists(oDir):
        os.makedirs(oDir)
        
    table = [["parameter", "value"]]
    table.append(["nbids", nbids])
    table.append(["n_samples",n_samples])
    table.append(["m",m])
    table.append(["njs",njs])
    table.append(["nms",nms])
    table.append(["vfile",vfile])
    table.append(["lfile",lfile])
    table.append(["sccpFile", scppFile])
    
    
    
    pprint_table(sys.stdout,table)
    
    with open(os.path.join(oDir,'params.txt'),'a') as f:
        pprint_table(f,table)
    
    jointBidFile = os.path.join(oDir,"jointLocalBids.txt")
    condBidFile = os.path.join(oDir,"condLocalBids.txt")
    margBidFile = os.path.join(oDir,"margLocalBids.txt")
    
    useExternalValuations = False
    if vfile == None and lfile == None:
        vfile = os.path.join(oDir,'v.txt')
        lfile = os.path.join(oDir,'l.txt')
    else:
        vmat = numpy.loadtxt(vfile)
        lmat = numpy.loadtxt(lfile)
        nbids = lmat.shape[0]
        useExternalValuations = True
        
    surplusSamples = scpp.sample(n_samples = n_samples)
    numpy.savetxt(os.path.join(oDir,'ppSamples.txt'),surplusSamples)
    
    jsdir = os.path.join(oDir,'jointSamples')
    if not os.path.exists(jsdir):
        os.makedirs(jsdir)
        
    msdir = os.path.join(oDir,'margSamples')
    if not os.path.exists(msdir):
        os.makedirs(msdir)
    
    es = numpy.zeros((nbids,3))
    for i in xrange(nbids):
        print 'Bid number {0}'.format(i)
        
        if useExternalValuations:
            v = vmat[i,:]
            l = lmat[i]
            
        else:
            print 'randomizing valuation'
            v,l = randomValueVector(m=m)    
            with open(vfile,'a') as f:
                numpy.savetxt(f, v.reshape(1,v.shape[0]))
#                print >> f, v
            with open(lfile,'a') as f:
                numpy.savetxt(f,numpy.atleast_1d(l))
        
        print 'v = {0}'.format(v)
        print 'l = {0}'.format(l)
        
        revenue = listRevenue(bundles, v, l)
        bundleRevenueDict = {}
        for b, r in zip(bundles,revenue):
            bundleRevenueDict[tuple(b)] = r
        
        jointSamples = scpp.sample(n_samples = njs)
        margSamples = scpp.sampleMarg(n_samples = nms)
        
        numpy.savetxt(
            os.path.join(jsdir,'jointSamples_{0:04}.txt'.format(i)),
                jointSamples)
        
        numpy.savetxt(
            os.path.join(msdir,'margSamples_{0:04}.txt'.format(i)),
                margSamples)
        
        initBid = straightMUa(bundles, revenue, scpp)
    
        jbid = jointLocal(bundles,revenue,initBid,jointSamples)
        cbid = condLocal(bundles, revenue, initBid, jointSamples)
        mbid = margLocal(bundles, revenue, initBid, margSamples)
        
        with open(jointBidFile,'a') as f:
            numpy.savetxt(f, jbid.T)
        
        with open(condBidFile,'a') as f:
            numpy.savetxt(f, cbid.T)
        
        with open(margBidFile,'a') as f:
            numpy.savetxt(f, mbid.T)
            
        es[i,0] = expectedSurplus_(bundleRevenueDict, jbid, surplusSamples)
        es[i,1] = expectedSurplus_(bundleRevenueDict, cbid, surplusSamples)   
        es[i,2] = expectedSurplus_(bundleRevenueDict, mbid, surplusSamples)
        
        with open(os.path.join(oDir,"jointLocalExpectedSurplus.txt"),'a') as f:
            numpy.savetxt(f,numpy.atleast_1d(es[i,0]))
            
        with open(os.path.join(oDir,"condLocalExpectedSurplus.txt"),'a') as f:
            numpy.savetxt(f,numpy.atleast_1d(es[i,1])) 
        
        with open(os.path.join(oDir,"margLocalExpectedSurplus.txt"),'a') as f:
            numpy.savetxt(f,numpy.atleast_1d(es[i,2])) 
        
    jmean = numpy.mean(es[:,0])
    jvar  = numpy.var(es[:,0])
    cmean = numpy.mean(es[:,1])
    cvar  = numpy.var(es[:,1])
    mmean = numpy.mean(es[:,2])
    mvar  = numpy.var(es[:,2])
    
    print 'jointLocal Expected Surplus Mean {0}'.format(jmean)
    print 'jointLocal Expected Surplus Variance {0}'.format(jvar)
    print 'condLocal Expected Surplus Mean {0}'.format(cmean)
    print 'condLocal Expected Surplus Variance {0}'.format(cvar)
    print 'margLocal Expected Surplus Mean {0}'.format(mmean)
    print 'margLocal Expected Surplus Variance {0}'.format(mvar)
    
    with open(os.path.join(oDir,'jointLocalStats.txt'),'a') as f:
        print >> f, 'mean ', jmean
        print >> f, 'var ', jvar
        
    with open(os.path.join(oDir,'condLocalStats.txt'),'a') as f:
        print >> f, 'mean ', cmean
        print >> f, 'var ', cvar
        
    with open(os.path.join(oDir,'margLocalStats.txt'),'a') as f:
        print >> f, 'mean ', mmean
        print >> f, 'var ', mvar
        
    with open(os.path.join(oDir,'stats.txt'),'a') as f:
        print >> f, 'jointLocal expected surplus mean: {0}'.format(jmean)
        print >> f, 'jointLocal expected surplus variation: {0}'.format(jvar)
        print >> f, 'condLocal expected surplus mean: {0}'.format(cmean)
        print >> f, 'condLocal expected surplus variation: {0}'.format(cvar)
        print >> f, 'margLocal expected surplus mean: {0}'.format(mmean)
        print >> f, 'margLocal expected surplus variation: {0}'.format(mvar)
        
        
    bins = range(int(es.min())-1, int(es.max())+1)
    
    jhist, jbins = numpy.histogram(es[:,0], bins, normed = True)
    chist, cbins = numpy.histogram(es[:,1], bins, normed = True)
    mhist, mbins = numpy.histogram(es[:,2], bins, normed = True)
    
    f,ax = plt.subplots(3,1, sharex = True)
    ax[0].bar( (jbins[:-1]+jbins[1:])/2, jhist, align = 'center')
    ax[0].set_title('jointLocal expected surplus')
    ax[1].bar( (cbins[:-1]+cbins[1:])/2, chist, align = 'center' )
    ax[1].set_title('condLocal expected surplus')
    ax[2].bar( (mbins[:-1]+mbins[1:])/2, mhist, align = 'center')
    ax[2].set_title('margLocal expected surplus')
    
    plt.savefig(os.path.join(oDir,'expectedSurplus.pdf'))
Пример #6
0
def main():
    desc="Compute jointLocal bids given v's, l's initial bids and parameters"
    
    parser = argparse.ArgumentParser(description=desc)
    
    parser.add_argument('-s','--samplesFile',dest='samplesFile',
                        required=True,type=str,
                        help='Samples (text file)')
    
    parser.add_argument('-v','--vfile',dest='vfile',
                        required=True,type=str,
                        help='Valuation v vector txt file.')
    
    parser.add_argument('-l','--lfile',dest='lfile',
                        required=True,type=str,
                        help='Valuation lambda txt file.')
    
    parser.add_argument('-es','--evalSamples',dest='evalFile',
                        required=True,type=str,
                        help='Evaluation samples file.')
    
    parser.add_argument('-ib','--initBidFile',dest='initBidFile',
                        required=True,type=str,
                        help='Initial Bids txt file.')
    
    parser.add_argument('-o','--odir',dest='odir',
                        required=True,type=str,
                        help='Output directory.')
    
    parser.add_argument('-mcs','--maxCandidateSamples', dest='maxCandidateSamples',
                        required=False, default=-1, type=int,
                        help='Limit the number of samples used as candidates.')
    
    parser.add_argument('-mes','--maxEvalSamples',dest='maxEvalSamples',
                        required=False, default=-1,type=int,
                        help='Limit the number of evaluation samples used.')
    
    parser.add_argument('--verbose',dest='verbose',
                        required=False,default=False,
                        type=bool,help='Output debugg information')
    
    
    
    args = parser.parse_args()
    
    args.odir = os.path.realpath(args.odir)
    
    jointSamples = numpy.loadtxt(os.path.realpath(args.samplesFile))
    initBids     = numpy.loadtxt(os.path.realpath(args.initBidFile))
    vmat         = numpy.loadtxt(os.path.realpath(args.vfile))
    lmat         = numpy.loadtxt(os.path.realpath(args.lfile))
    evalSamples  = numpy.loadtxt(os.path.realpath(args.evalFile))
    
    numSamples = jointSamples.shape[0]
    m = jointSamples.shape[1]
    
    if args.maxCandidateSamples > 0:
        if args.maxCandidateSamples < jointSamples.shape[0]:
            test = jointSamples[:args.maxCandidateSamples,:]
               
    print jointSamples.shape    
    bids = numpy.zeros(vmat.shape)
    es   = numpy.zeros(vmat.shape[0])
    
    for itr, initBid, v, l in zip(xrange(vmat.shape[0]),initBids,vmat,lmat):
        print 'iteration {0}'.format(itr)
        bundleRevenueDict = msDictRevenue(v,l)
        
        bid = downHillSS(bundleRevenueDict,
                         initBid = initBid,
                         evalSamples = jointSamples,
                         ret = 1) 
        
        print '\t bid = {0}'.format(bid)
            
            
        bids[itr,:] = bid
        es[itr] = expectedSurplus_(bundleRevenueDict, bid, evalSamples)
        
        print '\t Expected Surplus = {0}'.format(es[itr])
        
    numpy.savetxt(os.path.join(args.odir,'downHillBids.txt'), bids)
    numpy.savetxt(os.path.join(args.odir,'downHillExpectedSurplus.txt'),es)
    
    with open(os.path.join(args.odir,'downHillStats.txt'),'w') as f:
        print >> f, numpy.mean(es)
        print >> f, numpy.var(es)
Пример #7
0
def main():
    desc="Compute jointLocal bids given v's, l's initial bids and parameters"
    
    parser = argparse.ArgumentParser(description=desc)
    
    parser.add_argument('-s','--samplesFile',dest='samplesFile',
                        required=True,type=str,
                        help='Samples (text file)')
    
    parser.add_argument('-ms','--maxBidSamples',dest='maxBidSamples',
                        required=False,type=int, default=-1,
                        help='Maximum samples used to compute bids.')
    
    parser.add_argument('-v','--vfile',dest='vfile',
                        required=True,type=str,
                        help='Valuation v vector txt file.')
    
    parser.add_argument('-l','--lfile',dest='lfile',
                        required=True,type=str,
                        help='Valuation lambda txt file.')
    
    parser.add_argument('-ib','--initBidFile',dest='initBidFile',
                        required=True,type=str,
                        help='Initial Bids txt file.')
    
    parser.add_argument('-es','--evalSamples',dest='evalFile',
                        required=True,type=str,
                        help='Evaluation samples file.')
    
    parser.add_argument('-o','--odir',dest='odir',
                        required=True,type=str,
                        help='Output directory.')
        
    parser.add_argument('-mi','--maxitr', dest='maxitr',
                        required=False, default=100, type=int,
                        help='Maximum Update Iterations')
    
    parser.add_argument('-t','--tol',dest='tol',
                        required=False, default=1e-5,
                        type=float,help='L2 Update Stopping Tolerance')
    
    parser.add_argument('--verbose',dest='verbose',
                        required=False,default=False,
                        type=bool,help='Output debugg information')
    
    parser.add_argument('-n','--name',dest='name',
                        required=False, default='margLocalMc',
                        type=str,help='Output filename.')
    
    args = parser.parse_args()
    
    args.odir = os.path.realpath(args.odir)
    
    samples = numpy.loadtxt(os.path.realpath(args.samplesFile))
    initBids     = numpy.loadtxt(os.path.realpath(args.initBidFile))
    vmat         = numpy.loadtxt(os.path.realpath(args.vfile))
    lmat         = numpy.loadtxt(os.path.realpath(args.lfile))
    evalSamples  = numpy.loadtxt(os.path.realpath(args.evalFile))
    
    m = initBids.shape[1]
    
#    bundles = listBundles(m)
    
    if samples.shape[0] > args.maxBidSamples:
        samples = samples[:args.maxBidSamples,:]
    
    bids = numpy.zeros(initBids.shape)
    es   = numpy.zeros(initBids.shape[0])
    
    bidsFile   = os.path.join(args.odir, args.name + 'Bids.txt')
    if os.path.exists(bidsFile):
        os.remove(bidsFile)
        
    esFile    = os.path.join(args.odir, args.name + 'ExpectedSurplus.txt')
    if os.path.exists(esFile):
        os.remove(esFile)
        
    statsFile = os.path.join(args.odir, args.name + 'Stats.txt')
    if os.path.exists(statsFile):
        os.remove(statsFile)
    
    for itr, initBid, v, l in zip(xrange(vmat.shape[0]),initBids,vmat,lmat):
        print 'iteration {0}'.format(itr)
        brd = msDictRevenue(v,l)
        
        bids[itr,:], converged, nitr, d = \
            margLocalMc(brd,
                        initBid,samples,
                        maxItr = args.maxitr,
                        tol = args.tol,
                        verbose = args.verbose,
                        ret = 'all')
                             
        es[itr] = expectedSurplus_(brd, bids[itr,:], evalSamples)
        
        with open(bidsFile,'a+') as f:
            numpy.savetxt(f, bids[itr,:][None])
            
        with open(esFile,'a+') as f:
            numpy.savetxt(f,es[itr][None])
#            numpy.savetxt(f,es[itr])
        
        print '\t bid              = {0}'.format(bids[itr,:])
        print '\t converged        = {0}'.format(converged)
        print '\t nItr             = {0}'.format(nitr)
        print '\t d                = {0}'.format(d)
        print '\t Expected Surplus = {0}'.format(es[itr])
        
#    numpy.savetxt(os.path.join(args.odir, args.name +'Bids.txt'), bids)
#    numpy.savetxt(os.path.join(args.odir, args.name +'ExpectedSurplus.txt'),es)
    
    with open(os.path.join(args.odir,args.name + 'Stats.txt'),'w') as f:
        print >> f, numpy.mean(es)
        print >> f, numpy.var(es)
        
    print numpy.mean(es)
    print numpy.var(es)