def test_accept_reject_probabilistic(self, new_e, temp_step, accepted, accept_rate): # Test accepts unconditionally with e < current_energy and # probabilistically with e > current_energy rs = check_random_state(123) count_accepted = 0 iterations = 1000 accept_param = -5 current_energy = 1 for _ in range(iterations): energy_state = EnergyState(lower=None, upper=None) # Set energy state with current_energy, any location. energy_state.update_current(current_energy, [0]) chain = StrategyChain(accept_param, None, None, None, rs, energy_state) # Normally this is set in run() chain.temperature_step = temp_step # Check if update is accepted. chain.accept_reject(j=1, e=new_e, x_visit=[2]) if energy_state.current_energy == new_e: count_accepted += 1 assert count_accepted == accepted # Check accept rate pqv = 1 - (1 - accept_param) * (new_e - current_energy) / temp_step rate = 0 if pqv <= 0 else np.exp(np.log(pqv) / (1 - accept_param)) assert_allclose(rate, accept_rate)
def test_reset(self): owf = ObjectiveFunWrapper(self.weirdfunc) lu = list(zip(*self.ld_bounds)) lower = np.array(lu[0]) upper = np.array(lu[1]) es = EnergyState(lower, upper) assert_raises(ValueError, es.reset, owf, check_random_state(None))