#Create a percept for each combination of signal and reward
percepts = np.array([[0,0],
[0,1],
[1,0],
[1,1]]
)
#Create observation function for 4-state environment, each selects producing one of the perceprs
observation_function = np.array([[1.,0.,0.,0.],
[0.,1.,0.,0.],
[0.,0.,1.,0.],
[0.,0.,0.,1.]]
)
#Create transition function for 4-state, 2-action environment, where transitions are uniform-random to states with the appropriate reward
transition_function = np.array([[[0.,0.5], #in state 0, action 0 transition either to state 1 or 3. Action 1 transitions either to state 0 or 2.
[0.5,0.],
[0.,0.5],
[0.5,0.]],
[[0.,0.5], #in state 1, action 0 transition either to state 1 or 3. Action 1 transitions either to state 0 or 2.
[0.5,0.],
[0.,0.5],
[0.5,0.]],
[[0.5,0.], #in state 2, action 0 transition either to state 0 or 2. Action 1 transitions either to state 1 or 3.
[0.,0.5],
[0.5,0.],
[0.,0.5]],
[[0.5,0.], #in state 3, action 0 transition either to state 0 or 2. Action 1 transitions either to state 1 or 3.
[0.,0.5],
[0.5,0.],
[0.,0.5]]]
)
#Initialize POMDP
example_env = POMDP(percepts = percepts, observation_function = observation_function, transition_function = transition_function)
#Initialize Agent
test_agent = Basic_2Layer_Agent(num_actions = np.shape(transition_function)[2], glow = 1., policy = 'softmax', policy_parameters = 1)
#run percept action loop for 50 steps
T = 100
observed_percepts: list[None|NDArray[np.int_]] = [None] * T #empty list for storing percepts
actions: list[None|int] = [None] * T #empty list for storing actions
for t in range(T):
percept = example_env.get_observation()
observed_percepts[t] = percept
reward = percept[1]
test_agent.update(reward)
action = test_agent.deliberate(str(percept))
actions[t] = action
example_env.transition(action)
#show final 10 percepts and actions. Agent should have learned to select actions that produce rewards
for t in range(T-10,T):
print(f'Step {t}: percept = {observed_percepts[t]}; action = {actions[t]}')