Exemplo n.º 1
0
def test_counts():
  init_threads(-1,-1)
  for slice in xrange(5):
    with Log.scope('counting slice %d'%slice):
      sections = sections_t(slice,all_boards_sections(slice,8))
      good_counts = meaningless_counts(all_boards(slice,1))
      good_nodes = sum(product(section_shape(s)) for s in sections.sections)
      for key in 0,1,17:
        with Log.scope('partition key %d'%key):
          partition = random_partition_t(key,1,sections) if key else simple_partition_t(1,sections,False)
          store = compacting_store_t(estimate_block_heap_size(partition,0))
          blocks = meaningless_block_store(partition,0,0,store)
          Log.write('blocks = %d, correct = %d'%(blocks.total_nodes,good_nodes))
          assert blocks.total_nodes==good_nodes
          bad_counts = sum_section_counts(sections.sections,blocks.section_counts)
          Log.write('bad counts  = %s\ngood counts = %s'%(bad_counts,good_counts))
          assert all(bad_counts==good_counts)
Exemplo n.º 2
0
def run(cmd):
    Log.write(cmd)
    if not nop:
        subprocess.check_call(cmd.split())
Exemplo n.º 3
0
def run(cmd):
    Log.write(cmd)
    if not nop:
        subprocess.check_call(cmd.split())
Exemplo n.º 4
0
def optimize_generator(x0,step0,tolerance,verbose=False):
  # Initialize simplex 
  d = len(x0)
  x = empty((d+1,d))
  x[:] = asarray(x0).reshape(1,d)
  for i in xrange(d):
    x[i+1,i] += step0
  f = empty(d+1)
  for i in xrange(d+1):
    f[i] = yield x[i],False
    if verbose:
      Log.write('nelder-mead: initialized f(%s) = %g'%(x[i],f[i]))

  # Control parameters
  alpha = 1.
  gamma = 2.
  rho = .5
  sigma = .5

  # Loop until convergence
  while 1:
    # Sort vertices in increasing order of f
    p = sorted(xrange(d+1),key=lambda i:f[i])
    f = f[p]
    x = x[p]

    if verbose:
      Log.write('nelder-mead: best  x = %s, f(x) = %g'%(x[0],f[0]))
      Log.write('nelder-mead: worst x = %s, f(x) = %g'%(x[-1],f[-1]))

    # Check if we're converged 
    diameter = max(magnitude(x[i]-x[j]) for i in xrange(d+1) for j in xrange(i+1,d+1))
    if verbose:
      Log.write('nelder-mead: diameter = %g'%diameter)
    if diameter <= tolerance:
      yield x[0],True
      return

    def replace(fn,xn):
      f[-1] = fn
      x[-1] = xn

    # Perform reflection
    xm = x[:d].mean(axis=0)
    xr = xm + alpha*(xm-x[-1])
    fr = yield xr,False
    if f[0] <= fr <= f[-2]: # Accept reflection
      if verbose:
        Log.write('nelder-mead: reflected')
      replace(fr,xr)
    elif fr <= f[0]: # other expansion
      xe = xm + gamma*(xm-x[-1])
      fe = yield xe,False
      if fe < fr:
        if verbose:
          Log.write('nelder-mead: expansion succeeded')
        replace(fe,xe)
      else:
        if verbose:
          Log.write('nelder-mead: expansion failed')
        replace(fr,xr)
    else: # other contraction
      xc = x[-1] + rho*(xm-x[-1])
      fc = yield xc,False
      if fc < f[-1]:
        if verbose:
          Log.write('nelder-mead: contracted')
        replace(fc,xc)
      else: # All else failed; perform reduction
        if verbose:
          Log.write('nelder-mead: reduced')
        for i in xrange(1,d+1):
          x[i] = x[0] + sigma*(x[i]-x[0])
          f[i] = yield x[i],False