示例#1
0
 def test_condMVLocal(self):
     nsamples = 5
     m=2
     initbids = [3,3]
     samples = numpy.zeros((nsamples,m))
     samples[:,0] = numpy.arange(nsamples)
     samples[:,1] = 2
     bundles = listBundles(m)
     l = 1
     v = [45,20]
     revenue = msListRevenue(bundles, v, l)
     
     print bundles
     print revenue
     brd = {}
     for b,r in zip(bundles,revenue):
         brd[tuple(b)] = r
     newbid = condMVLocalUpdate(brd, initbids, 0, samples, True) 
                                      
     
     print newbid
     
     bids = condMVLocal(bundles,revenue, initbids, samples)
     
     print bids
示例#2
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)
示例#3
0
 def test1(self):
     pp = [5,5]
     l = 1
     v = [20,10]
     bundles = listBundles(2)
     rev = msListRevenue(bundles, v, l)
     
     bid = targetPrice(bundles, rev, pp, True)
     numpy.testing.assert_array_equal(bid, [5,0], "Error: targetPrice test1 failed.")
示例#4
0
 def setUp(self):
     self.samples2d = numpy.atleast_2d([[1,1],   [20,3], [10,7], [15,5],
                                        [10,30], [15,26],[17,33], [2,40],
                                        [30,30], [27,42],[30,38], [29,40],
                                        [42,20], [38,7], [40,15], [33,10]])
     
     self.bundles2d = listBundles(2)
     self.l2d = 1
     self.v2d = [20,10] 
     self.revenue2d = msListRevenue(self.bundles2d, self.v2d, self.l2d)
示例#5
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)
示例#6
0
 def test_jointLocalMc1(self):
     """
     Updates computed by hand given:
     v = [45,20], l = 1
     p(q_1 = 20) = 0.5 
     p(q_1 = 30) = 0.5
     p(q_2 = 15) = 0.2
     p(q_2 = 20) =  0.8
     
     Under independent prices p(q_1,q_2) = p(q_1)p(q_2)
     Hence:
     p(q_1 = 20, q_2 = 15) = 0.1
     p(q_1 = 20, q_2 = 20) = 0.4
     p(q_1 = 30, q_2 = 15) = 0.1
     p(q_1 = 30, q_2 = 20) = 0.4
     
     The joint pdf is represented with 1000 samples. 
     100 samples of [20,15]
     400 samples of [20,20]
     100 samples of [30,15]
     400 samples of [30,20]
     
     Ground Truth Anwers:
     Starting at [25,25]
     1.1) b1 <- 25
     1.2) b2 <- 10
     2.1) b1 <- 45
     2.2) b2 <- 0
     3.1) b1 <- 25
     3.2) b2 <- 0
     
     Therefore, starting at [25,25] converges to [45,0] after 3 iterations
     """
     
     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.])
     
     bids[0] = jointLocalUpdateMc(bundles,revenue,bids,0,samples,True)
     print bids
     numpy.testing.assert_equal(bids[0], 25, "Update 1.1 Failed", True)
             
     bids[1] = jointLocalUpdateMc(bundles,revenue,bids,1,samples,True)
     print bids
     numpy.testing.assert_equal(bids[1], 10, "Update 1.2 Failed", True )
             
     bids[0] = jointLocalUpdateMc(bundles,revenue,bids,0,samples,True)
     print bids
     numpy.testing.assert_equal(bids[0], 45, "Update 2.1 Failed", True)
             
     bids[1] = jointLocalUpdateMc(bundles,revenue,bids,1,samples,True)
     print bids
     numpy.testing.assert_equal(bids[1], 0, "Update 2.2 Failed", True)
             
     bids[0] = jointLocalUpdateMc(bundles,revenue,bids,0,samples,True)
     print bids
     numpy.testing.assert_equal(bids[0], 45, "Update 3.1 Failed", True)
             
     bids[1] = jointLocalUpdateMc(bundles,revenue,bids,1,samples,True)
     print bids
     numpy.testing.assert_equal(bids[1], 0, "Update 3.2 Failed", True)
     
     bids = numpy.asarray([25.,25.])
     bids, converged, itr, tol = jointLocalMc(bundles, revenue, bids, samples, verbose=True,ret='all')
     print bids
     print converged
     print itr
     print tol
示例#7
0
    def test_margLocalUpdate1(self):
        """
        Updates computed by hand given:
        v = [45,20], l = 1
        p(q_1 = 20) = 0.5 
        p(q_1 = 30) = 0.5
        p(q_2 = 15) = 0.2
        p(q_2 = 20) =  0.8
        
        Under independent prices p(q_1,q_2) = p(q_1)p(q_2)
        Hence:
        p(q_1 = 20, q_2 = 15) = 0.1
        p(q_1 = 20, q_2 = 20) = 0.4
        p(q_1 = 30, q_2 = 15) = 0.1
        p(q_1 = 30, q_2 = 20) = 0.4
        
        The joint pdf is represented with 1000 samples. 
        100 samples of [20,15]
        400 samples of [20,20]
        100 samples of [30,15]
        400 samples of [30,20]
        
        Ground Truth Anwers:
        Starting at [25,25]
        1.1) b1 <- 25
        1.2) b2 <- 10
        2.1) b1 <- 45
        2.2) b2 <- 0
        3.1) b1 <- 25
        3.2) b2 <- 0
        
        Therefore, starting at [25,25] converges to [45,0] after 3 iterations
        """
        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.])

        bids[0] = margLocalUpdate(bundles,revenue,bids,0,samples,True)
        numpy.testing.assert_equal(bids[0], 25, "Update 1.1 Failed", True)
                
        bids[1] = margLocalUpdate(bundles,revenue,bids,1,samples,True)
        numpy.testing.assert_equal(bids[1], 10, "Update 1.2 Failed", True )
                
        bids[0] = margLocalUpdate(bundles,revenue,bids,0,samples,True)
        numpy.testing.assert_equal(bids[0], 45, "Update 2.1 Failed", True)
                
        bids[1] = margLocalUpdate(bundles,revenue,bids,1,samples,True)
        numpy.testing.assert_equal(bids[1], 0, "Update 2.2 Failed", True)
                
        bids[0] = margLocalUpdate(bundles,revenue,bids,0,samples,True)
        numpy.testing.assert_equal(bids[0], 45, "Update 3.1 Failed", True)
                
        bids[1] = margLocalUpdate(bundles,revenue,bids,1,samples,True)
        numpy.testing.assert_equal(bids[1], 0, "Update 3.2 Failed", True)
        
        #should converge after 3 iterations
        bids = numpy.asarray([25.,25.])
        
        bids, converged, itr, tol = margLocal(bundles, revenue, bids, samples, ret = 'all')
        
        numpy.testing.assert_array_equal(bids, numpy.asarray([45,0]), "margLocal bids test failed", True)
        
        numpy.testing.assert_equal(converged, True, "margLocal converged failed", True)
        
        numpy.testing.assert_equal(itr,3,"margLocal number of iterations failed.", True)
        
        numpy.testing.assert_almost_equal(tol, 0., 8, "margLocal tol failed", True)