Beispiel #1
0
def test_accept_change():
    """ Check that move is accepted if second energy is lower """
    from numpy import sqrt, count_nonzero, exp

    mc = MonteCarlo(temperature=100.0)
    # Should always be true. But do more than one draw, in case random incorrectly crept into
    # implementation
    for i in range(10):
        assert_true(mc.accept_change(0.5, 0.4))
        assert_true(mc.accept_change(0.5, 0.5))

    # This should be accepted only part of the time, depending on exponential distribution
    prior, successor = 0.4, 0.5
    accepted = [mc.accept_change(prior, successor) for i in range(10000)]
    assert_almost_equal(count_nonzero(accepted) / float(len(accepted)),
                        exp(-(successor - prior) / mc.temperature),
                        delta=3e0 / sqrt(len(accepted)))
def test_accept_change():
  from numpy import sqrt, count_nonzero, exp

  mc = MonteCarlo(temperature=100.0)
  # Should always be true. But do more than one draw, in case random incorrectly crept into
  # implementation
  for i in range(10):
    assert_true(mc.accept_change(0.5, 0.4))
    assert_true(mc.accept_change(0.5, 0.5))

  # This should be accepted only part of the time, depending on exponential distribution
  prior, successor = 0.4, 0.5
  accepted = [mc.accept_change(prior, successor) for i in range(10000)]
  assert_almost_equal( 
      count_nonzero(accepted) / float(len(accepted)),
      exp(-(successor - prior) / mc.temperature),
      delta = 3e0 / sqrt(len(accepted))
  )
Beispiel #3
0
def test_main_algorithm():
    """ Check set path through main algorithm """
    from mock import Mock, call

    mc = MonteCarlo(temperature=100.0, itermax=4)

    # Patch mc so that it takes a pre-determined path through
    acceptance = [True, True, False, True]
    mc.accept_change = Mock(side_effect=acceptance)
    densities = (
        [0, 0, 1, 0],
        [0, 1, 1, 0],
        [2, 2, 2, 2],
        [2, 3, 3, 2],
        [5, 3, 3, 5],
    )
    mc.change_density = Mock(side_effect=densities[1:])
    mc.observe = Mock(return_value=True)

    # Fake energy method
    energies = [0.1, -0.1, -0.2, -0.15, -0.25]
    energy = Mock(side_effect=energies)

    # Call simulation
    mc(energy, densities[0])

    # Now, analyze path. First check length.
    assert_equal(len(mc.accept_change.mock_calls), 4)
    assert_equal(len(mc.change_density.mock_calls), 4)
    assert_equal(len(mc.observe.mock_calls), 4)
    assert_equal(len(energy.mock_calls),
                 5)  # one extra call to get first energy

    # Easiest to look at observe, since it should have all the info about the step
    observe_path = [
        call(0, acceptance[0], densities[1], energies[1]),
        call(1, acceptance[1], densities[2], energies[2]),
        call(2, acceptance[2], densities[2], energies[2]),
        call(3, acceptance[3], densities[4], energies[4]),
    ]
    assert_equal(observe_path, mc.observe.call_args_list)
def test_main_algorithm():
  """ Check set path through main algorithm """
  from mock import Mock, call

  mc = MonteCarlo(temperature=100.0, itermax=4)

  # Patch mc so that it takes a pre-determined path through 
  acceptance = [True, True, False, True]
  mc.accept_change = Mock(side_effect=acceptance)
  densities = ( 
      [0, 0, 1, 0],
      [0, 1, 1, 0],
      [2, 2, 2, 2],
      [2, 3, 3, 2],
      [5, 3, 3, 5],
  )
  mc.change_density = Mock(side_effect=densities[1:])
  mc.observe = Mock(return_value=True)

  # Fake energy method
  energies = [0.1, -0.1, -0.2, -0.15, -0.25]
  energy = Mock(side_effect=energies)

  # Call simulation 
  mc(energy, densities[0])

  # Now, analyze path. First check length.
  assert_equal(len(mc.accept_change.mock_calls), 4)
  assert_equal(len(mc.change_density.mock_calls), 4)
  assert_equal(len(mc.observe.mock_calls), 4)
  assert_equal(len(energy.mock_calls), 5) # one extra call to get first energy

  # Easiest to look at observe, since it should have all the info about the step
  observe_path = [
      call(0, acceptance[0], densities[1], energies[1]),
      call(1, acceptance[1], densities[2], energies[2]),
      call(2, acceptance[2], densities[2], energies[2]),
      call(3, acceptance[3], densities[4], energies[4])
  ]
  assert_equal(observe_path, mc.observe.call_args_list)