@@ -0,0 +1,2110 @@
+import numpy as np%0Aimport keras%0Afrom keras.models import Sequential%0Afrom keras.layers import Dense%0Afrom collections import deque%0Aimport random%0Afrom keras.optimizers import Adam%0A%0A%0Aclass Learner:%0A    def __init__(self, state_space_size, action_space_size):%0A        self.state_size = state_space_size%0A        self.action_size = action_space_size%0A        self.learning_rate = 0.001%0A        self.firstHidden = 20%0A        self.secondHidden = 20%0A        self.regressor = self._build_model()%0A        self.exploration = 1.%0A        self.exploration_decay = 0.995%0A        self.min_exploration = 0.01%0A        self.memory = deque(maxlen=1000)%0A        self.batch_size = 32%0A        self.gamma = 0.95%0A%0A    def _build_model(self):%0A        regressor = Sequential()%0A        regressor.add(Dense(output_dim=self.firstHidden, input_dim=self.state_size, activation='relu'))%0A        regressor.add(Dense(output_dim=self.secondHidden, activation='relu'))%0A        regressor.add(Dense(output_dim=self.action_size, activation='linear'))%0A        regressor.compile(optimizer=Adam(lr=self.learning_rate), loss='mse')%0A        return regressor%0A%0A    def act(self, state):%0A        if np.random.rand() %3C= self.exploration:%0A            action = np.random.choice(%5B0, 2%5D)%0A            # action = np.random.choice(range(self.action_size))%0A        else:%0A            action = np.argmax(self.regressor.predict(state), axis=1)%5B0%5D%0A        return action%0A%0A    def remember(self, state, action, reward, new_state, done):%0A        self.memory.append((state, action, reward, new_state, done))%0A%0A    def replay(self):%0A        minibatch = random.sample(list(self.memory), self.batch_size)%0A        for state, action, reward, new_state, done in minibatch:%0A            target = reward%0A            if not done:%0A                target = reward + self.gamma*np.max(self.regressor.predict(new_state)%5B0%5D)%0A            target_f = self.regressor.predict(state)%0A            target_f%5B0%5D%5Baction%5D = target%0A            self.regressor.fit(state, target_f, epochs=1, verbose=0)%0A        if self.exploration %3E self.min_exploration:%0A            self.exploration *= self.exploration_decay

Add Deep Q Learner