<jupyter_start><jupyter_text>Simple dp agent<jupyter_code>from src.agent.dp_agent import dp_agent
from src.environment.maze import maze
def run_sim(env, agent, steps = 100, disable_training = False):
    last_observation = None
    last_action      = None
    
    for s in range(steps):
        new_observation = env.observe()
        reward          = env.collect_reward()
        
        # store last transition
        if last_observation is not None:
            agent.store(last_observation, last_action, reward, new_observation)

        # act
        new_action = agent.action(np.array([new_observation]))
        env.perform_action(new_action)

        #train
        if not disable_training:
            agent.training_step()

        # update current state as last state.
        last_action = new_action
        last_observation = new_observation


array = np.array([[-1,-1,-1,-1,-1],[-1,1,4,2,-1],[-1,0,1,10,-1],[-1,5,1,10,-1], [-1,-1,-1,-1,-1]])
g = maze(array)
agent = dp_agent(g.observation_size, g.num_actions)
init_table = np.copy(agent.table)
run_sim(g, agent, 10000)
opt_actions = [["" for i in range(5)] for j in range(5)]
for i in range(agent.num_observations):
    action = np.argmax(agent.table[i,:])
    if action == 0:
        t = "U"
    elif action == 1:
        t = "D"
    elif action == 2:
        t = "L"
    else:
        t = "R"
    opt_actions[int(i/5)][i%5] = t
array
opt_actions
g.total_reward<jupyter_output><empty_output><jupyter_text>DQN agent<jupyter_code>import tensorflow as tf
from src.agent.dqn.dqn import exp_replay
from src.agent.dqn.models  import MLP
tf.reset_default_graph()
session = tf.InteractiveSession()

# Brain maps from observation to Q values for different actions.
# Here it is a done using a multi layer perceptron with 2 hidden
# layers
brain = MLP([1,], [20, 20, g.num_actions], # change size to larger observation arrays
            [tf.tanh, tf.tanh, tf.identity])

# The optimizer to use. Here we use RMSProp as recommended
# by the publication
optimizer = tf.train.RMSPropOptimizer(learning_rate= 0.001, decay=0.9)

# DiscreteDeepQ object
current_controller = exp_replay(g.observation_shape, g.num_actions, brain, optimizer, session,
                                   discount_rate=0.99, exploration_period=5000, max_experience=10000, 
                                   store_every_nth=4, train_every_nth=4)

session.run(tf.initialize_all_variables())
session.run(current_controller.target_network_update)
run_sim(g, current_controller, 10000)
g.total_reward<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11336982017200939, 'fraction_numerical': 0.028537920250195466, 'mean_word_length': 3.188216039279869, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '48780', 'license': 'MIT', 'n_tokens_mistral': 902, 'n_tokens_neox': 852, 'n_words': 200, 'path': 'notebooks/Maze_environment.ipynb', 'repo_name': 'mburq/Deep_rl_stochastic_opt', 'size': '2558'}