n_episodes=1001# n games we want agent to play (default 1001)
```
%% Cell type:code id: tags:
``` python
output_dir='model_output/cartpole/'
```
%% Cell type:code id: tags:
``` python
ifnotos.path.exists(output_dir):
os.makedirs(output_dir)
```
%% Cell type:markdown id: tags:
#### Define agent
%% Cell type:code id: tags:
``` python
classDQNAgent:
def__init__(self,state_size,action_size):
self.state_size=state_size
self.action_size=action_size
self.memory=deque(maxlen=2000)# double-ended queue; acts like list, but elements can be added/removed from either end
self.gamma=0.95# decay or discount rate: enables agent to take into account future actions in addition to the immediate ones, but discounted at this rate
self.epsilon=1.0# exploration rate: how much to act randomly; more initially than later due to epsilon decay
self.epsilon_decay=0.995# decrease number of random explorations as the agent's performance (hopefully) improves over time
self.epsilon_min=0.01# minimum amount of random exploration permitted
self.learning_rate=0.001# rate at which NN adjusts models parameters via SGD to reduce cost
self.model=self._build_model()# private method
def_build_model(self):
# neural net to approximate Q-value function:
model=Sequential()
model.add(Dense(24,input_dim=self.state_size,activation='relu'))# 1st hidden layer; states as input