def test_mountain_from_right(self): arr = range(0,10) + range(10,-1,-1) eval_funct = lambda x: arr[int(round(abs(x)))] move_funct = lambda X, i: maximum_number_move(X, i, step_size=1, bounds=(0,20)) pos, score = randomized_hill_climbing(eval_funct, move_funct, 0) self.assertEquals(score, 10) self.assertAlmostEqual(round(pos), 10)
def test_slope(self): arr = range(9, -1, -1) eval_funct = lambda x: arr[int(round(abs(x)))] move_funct = lambda X, i: maximum_number_move(X, i, step_size=1, bounds=(0,9)) pos, score = randomized_hill_climbing(eval_funct, move_funct, 9) self.assertEquals(score, 9) self.assertAlmostEqual(round(pos), 0)
def test_two_peaks_high_prob_jump(self): arr = range(0,10) + range(10,-1,-1) + range(1, 6) eval_funct = lambda x: arr[int(round(abs(x)))] move_funct = lambda X, i: maximum_number_move(X, i, step_size=1, bounds=(0,25)) pos, score = randomized_hill_climbing(eval_funct, move_funct, 20, prob_jump=1) self.assertTrue(score == 10 or score == 5) self.assertTrue(round(pos) == 10 or round(pos) == 25)
def test_hill_climbing_aapl_data(self): aapl = quandl_stocks('AAPL') close = aapl['WIKI/AAPL - Adj. Close'] arr = close.values eval_funct = lambda x: arr[int(round(abs(x)))] move_funct = lambda X, i: maximum_number_move(X, i, step_size=100, bounds = (0,len(arr))) pos, score = randomized_hill_climbing(eval_funct, move_funct, 6, prob_jump=0.25) plot_stock_data(aapl['WIKI/AAPL - Adj. Close'], close.index[pos], score)