def test_instantiation(): """ Testing common QLearner initial arguments and support functions. """ # Set-up: STATES = 10 ACTIONS = 5 rmatrix_sq = np.random.rand(STATES, STATES) rmatrix_rec = np.random.rand(STATES, ACTIONS) tmatrix = np.random.randint(0, STATES, size=(STATES, ACTIONS)) # making sure tmatrix points to goal states: tmatrix[:, ACTIONS - 1] = np.random.randint(0, 1, size=STATES) goal_l = (0, 1) goal_f = lambda x: x <= 1 np.savetxt('test.dat', rmatrix_sq) global QLEARNER # Test 1: list goal temp = QLearner(rmatrix_sq, goal_l) assert np.array_equal(temp.rmatrix, rmatrix_sq), "R matrix not equal to arg." assert temp.goal(0) and temp.goal(1) and not temp.goal(2) and not temp.goal(3), \ 'List goal not working.' QLEARNER = temp # Test 2: function goal temp = QLearner(rmatrix_sq, goal_f) assert temp.goal(0) and temp.goal( 1) and not temp.goal(2), 'Function goal not working.' QLEARNER = temp # Test 3: File I/O temp = QLearner('test.dat', goal_l) assert temp.qmatrix.shape == rmatrix_sq.shape, "Q & R matrix dimension mismatch." assert np.array_equal(temp.rmatrix, rmatrix_sq), "R matrix not equal to arg." QLEARNER = temp # Test 4: rectangular r matrix, no tmatrix try: QLearner(rmatrix_rec, goal_l) except ValueError: pass # Test 5: rectangular r matrix, t matrix of same dimension temp = QLearner(rmatrix_rec, goal_f, tmatrix) assert temp.next_state(1, 2) == tmatrix[1, 2], 'Next state prediction incorrect.' QLEARNER = temp # Test 6: episodes l = set(temp.episodes(coverage=1.0, mode='bfs')) assert l == set(range(temp.num_states)), 'Full episode coverage failed.' # Finalize os.remove('test.dat')