def test_interchange(self): print 'Single Scan Interchange algorithm' for N in map(int, [10, 1e2, 1e3, 1e4, 1e5, 1e6]): pr = PPReader('population.csv', 'dummy.csv', N=N) # Read dummy files into buffer start_time = time.time() #pr.init() elapsed_time = time.time() - start_time sys.stdout.write('init done: %f seconds taken.\t' % elapsed_time) sys.stdout.flush() # Actual test start_time = time.time() lin = Linear(dist_func, K=5) lin.update(pr) elapsed_time = time.time() - start_time sampled = lin.get_sampled() sys.stdout.write('%d: %f\n' % (N, elapsed_time)) sys.stdout.flush()
loss += 1.0 / dist_func(x, y) return loss np.random.seed(1) for psize in xrange(10, 110, 10): #for psize in xrange(100, 1100, 100): # Generate population population = np.random.rand(psize, 2) # Generate sampled data K = 2 # First: we get samples using linear algorithm lin = Linear(dist_func, K = K, r = 1) lin.update(population) lin_sample = lin.get_sampled() # Second: we enumerate all the samples of size K, and find the one with the # minimal loss. min_loss = float('Inf') bf_sample = None for c in combinations(population, K): loss = loss_of(c) if loss < min_loss: min_loss = loss bf_sample = c print 'Population size: %d' % psize print 'Linear\tsize: %d, loss: %f' % (len(lin_sample), loss_of(lin_sample)) print 'BF\tSize: %d, loss: %f' % (len(bf_sample), loss_of(bf_sample))
fout.write('x,y,label\n') # Generate population pp = population1() for x, y in pp: fout.write('%f,%f,a\n' % (x, y)) x, y = zip(*pp) plt.scatter(x, y, s=1, c='b') # Generate sampled data def dist_func(a, b): d = pow(np.linalg.norm(a - b), 2) return d lin = Linear(dist_func, K = 50, r = 0.3) lin.update(pp) sampled = lin.get_sampled() for x, y in sampled: fout.write('%f,%f,b\n' % (x, y)) fout.close() x, y = zip(*sampled) plt.scatter(x, y, s=30, c='r') plt.show()