#library&const """ This part of code is the Deep Q Network (DQN) brain. Using: Tensorflow: r1.2 """ # #é®é¢ç¹ï¼ # -åæ°ï¼ # - Qâç½ç»åæ°æ´æ°iteration # - epsilonæ¯æ¬¡å¢å çé # -ä¸Prototypeä¸åä¹å¤ï¼s,r,a,s_å¨æ¯æ¬¡è·å¾s_æ¶ï¼è·å¾ï¼s,r,a,s_ï¼ ä½ä¸ºåæ°ä¼ å ¥store_transition(self, s, a, r, s_) import numpy as np import tensorflow as tf np.random.seed(1) tf.set_random_seed(1) class DeepQNetwork: def __init__( self, n_actions , n_features, learning_rate=0.003, reward_decay=0.99, #e_greedy=0.1, epsilon_max=0.99, replace_target_iter=50, #Qåæ°æ¿æ¢çè¿ä»£æ¬¡æ° memory_size=500, batch_size=48, e_greedy_increment=0.01, #89次å å°99 output_graph=False, ): #å¨ä½æ°é self.n_actions = n_actions #ç¶ææ°é self.n_features = n_features #learning_rateå¦ä¹ éç self.lr = learning_rate #Q-learningä¸rewardè¡°åå å self.gamma = reward_decay #e-greedyçéæ©æ¦çæå¤§å¼ self.epsilon_max = epsilon_max #æ´æ°Qç°å®ç½ç»åæ°çæ¥éª¤æ° self.replace_target_iter = replace_target_iter #åå¨è®°å¿çæ°é self.memory_size = memory_size #æ¯æ¬¡ä»è®°å¿åºä¸åçæ ·æ¬æ°é self.batch_size = batch_size #æ¯æ¬¡å¨ä½è¾åºepsilonå¢å¤§çé self.epsilon_increment = e_greedy_increment #epsilonçåå§å¼ self.epsilon = 0.1 #å¦ä¹ çæ¥éª¤åå§å¼ self.learn_step_counter = 0 #åå§åmemory为zero self.memory = np.zeros((self.memory_size, n_features * 2 + 2)) #æ建target_net&evaluate_net(BPç®æ³ï¼eç维度ï¼å¯¹æ¯ä¸ªè¾åºç¥ç»å æ±losså¹³æ¹å·®è¿è¡åé¦ä¼ æ) self._build_net() t_params = tf.get_collection('target_net_params') e_params = tf.get_collection('eval_net_params') self.replace_target_op = [tf.assign(t, e) for t, e in zip(t_params)] self.sess = tf.Session() #å建ä¼è¯ if output_graph: tf.summary.FileWriter("logs/", self.sess.graph) #å°graphçeventä¼ å ¥logç®å½ä¸? self.sess.run(tf.global_variables_initializer()) #对variableè¿è¡åå§åç代ç self.cost_his = [] #? def _build_net(self): #all inputs self.s = tf.placeholder(tf.float32,[None, self.n_features],name='s') self.s_ = tf.placeholder(tf.float32,[None, self.n_features], name = 's_') self.r = tf.placeholder(tf.float32, [None, ], name = 'r') self.a = tf.placeholder(tf.float32, [None, ], name = 'a') w_initializer, b_initializer = tf.random_normal_initializer(0.,0.3), tf.constant_initializer(0.1) #build_evaluate_net with tf.variable_scope('eval_net'): e1 = tf.layers.dense(self.s, 20, tf.nn.relu, kernel_initializer=w_initializer, bias_initializer=b_initializer,name='e1') self.q_eval = tf.layers.dense(e1, self.n_actions, kernel_initializer=w_initializer, bias_initializer=b_initializer,name='q') #build_target_net with tf.variable_scope('target_net'): t1 = tf.layers.dense(self.s_,20,tf.nn.relu, kernel_initializer=w_initializer, bias_initializer=b_initializer,name='t1') self.q_next = tf.layers.dense(t1,self.n_actions, kernel_initializer=w_initializer, bias_initializer=b_initializer, name='t2') with tf.variable_scope('q_target'): q_target = self.r + self.gamma * tf.reduce_max(self.q_next, axis=1, name='Qmax_s_') self.q_target = tf.stop_gradient(q_target) # with tf.variable_scope('q_eval'): a_indices = tf.stack([tf.range(tf.shape(self.a)[0], dtype=tf.int32), self.a], axis=1) self.q_eval_wrt_a = tf.gather_nd(params=self.q_eval, indices=a_inidices) with tf.variable_scope('loss'): self.loss = tf.reduce_mean(tf.squared_difference(self.q_target, self.q_eval_wrt_a, name = 'TD_error')) with tf.variable_scope('train'): self._train_op = tf.train.RMSPropOptimizer(self.lr).minimize(self.loss) def store_transition(self, s, a, r, s_): if not hasattr(self, 'memory_counter'): self.memory_counter = 0 transition = np.hstack((s, [a,r], s_)) index = self.memory_counter % self.memory_size #replace the old memory with new memory self.memory[index, :] = transition self.memory_counter += 1 def choose_action(self, observation): # è·åbatchç维度ï¼ç¨ä»¥feedå°placeholder if np.random.uniform() < self.epsilon: action_value = self.sess.run(self.q_eval, feed_dict={self.s: observation}) action = np.argmax(actions_value) else: action = np.random.randint(0,self.n_actions) return action def learn(self): #æ£æ¥å¹¶æ¿æ¢ç®æ åæ° if self.learn_step_counter % self.replace_target_iter == 0: self.sess.run(self.target_replace_op) print('\ntargrt_params_replaced\n') #sample batch memory from all memory if self.memory_counter > self.memory_size: sample_index = np.random.choice(self.memory_size, size=self.batch_size) else: sample_index = np.random.choice(self.memory_counter, size=self.batch_size) batch_memory = self.memory[sample_index, :] _, cost = self.sess.run( [self._train_op, self.loss], feed_dict={ self.s: batch_memory[:, :self.n_features], self.a: batch_memory[;, self.n_features], self.r: batch_memory[:, self.n_features+1], self.s_: batch_memory[:, -self.n_features:], }) self.cost_his.append(cost) #increase epsilon self.epsilon = self.epsilon + self.epsilon_increment if self.epsilon < self.epsilon_max else self.epsilon_max self.learn_step_counter += 1 def plot_cost(self): import matplotlib.pyplot as plt plt.plot(np.arange(len(self.cost_his)),self.cost_his) plt.ylabel('Cost') plt.xlabel('traing steps') plt.show() if __name__ == '__main__': DQN = DeepQNetwork(n_actions = ,n_features = 6, output_graph = True) ####if __name__ == '__main__'çæææ¯ï¼å½.pyæ件被ç´æ¥è¿è¡æ¶ï¼if __name__ == '__main__'ä¹ä¸ç代ç åå°è¢«è¿è¡ï¼å½.pyæ件以模åå½¢å¼è¢«å¯¼å ¥æ¶ï¼if __name__ == '__main__'ä¹ä¸ç代ç åä¸è¢«è¿è¡ã