Complete the code to save the agent's current state to a checkpoint file.
agent_state = agent.get_state() with open('checkpoint.pkl', 'wb') as f: [1]
We use pickle.dump() to save the agent's state object to a file.
Complete the code to load the agent's state from a checkpoint file.
with open('checkpoint.pkl', 'rb') as f: agent_state = [1] agent.set_state(agent_state)
We use pickle.load() to read the saved agent state from the file.
Fix the error in the code to correctly checkpoint the agent's progress after each step.
for step in range(10): agent.act() with open('checkpoint.pkl', 'wb') as f: [1]
After each action, we save the current state using pickle.dump() to checkpoint progress.
Fill both blanks to create a function that saves and loads the agent's state.
def checkpoint_agent(agent, filename): with open(filename, 'wb') as f: [1] def load_agent(filename): with open(filename, 'rb') as f: return [2]
The function saves the agent's state with pickle.dump() and loads it with pickle.load().
Fill all three blanks to implement checkpointing with error handling and progress printout.
import pickle def save_checkpoint(agent, filename): try: with open(filename, 'wb') as f: [1] print(f"Checkpoint saved to [2]") except Exception as e: print(f"Failed to save checkpoint: [3]")
The code saves the agent's state, prints the filename on success, and prints the error message on failure.