|
| 1 | +import tensorflow as tf |
| 2 | +import numpy as np |
| 3 | +import random |
| 4 | +from collections import deque |
| 5 | +from Config import Categorical_DQN_Config |
| 6 | +from utils import conv, dense |
| 7 | +import math |
| 8 | + |
| 9 | + |
| 10 | +class Categorical_DQN(): |
| 11 | + def __init__(self,env,config): |
| 12 | + self.sess = tf.InteractiveSession() |
| 13 | + self.config = config |
| 14 | + self.v_max = self.config.v_max |
| 15 | + self.v_min = self.config.v_min |
| 16 | + self.atoms = self.config.atoms |
| 17 | + |
| 18 | + self.time_step = 0 |
| 19 | + self.epsilon = self.config.INITIAL_EPSILON |
| 20 | + self.state_shape = env.observation_space.shape |
| 21 | + self.action_dim = env.action_space.n |
| 22 | + |
| 23 | + target_state_shape = [1] |
| 24 | + target_state_shape.extend(self.state_shape) |
| 25 | + |
| 26 | + self.state_input = tf.placeholder(tf.float32,target_state_shape) |
| 27 | + self.action_input = tf.placeholder(tf.int32,[1,1]) |
| 28 | + |
| 29 | + self.m_input = tf.placeholder(tf.float32,[self.atoms]) |
| 30 | + |
| 31 | + self.delta_z = (self.v_max - self.v_min) / (self.atoms - 1) |
| 32 | + self.z = [self.v_min + i * self.delta_z for i in range(self.atoms)] |
| 33 | + |
| 34 | + self.build_cate_dqn_net() |
| 35 | + |
| 36 | + self.saver = tf.train.Saver() |
| 37 | + |
| 38 | + self.sess.run(tf.global_variables_initializer()) |
| 39 | + |
| 40 | + self.save_model() |
| 41 | + self.restore_model() |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + def build_layers(self, state, action, c_names, units_1, units_2, w_i, b_i, reg=None): |
| 47 | + with tf.variable_scope('conv1'): |
| 48 | + conv1 = conv(state, [5, 5, 3, 6], [6], [1, 2, 2, 1], w_i, b_i) |
| 49 | + with tf.variable_scope('conv2'): |
| 50 | + conv2 = conv(conv1, [3, 3, 6, 12], [12], [1, 2, 2, 1], w_i, b_i) |
| 51 | + with tf.variable_scope('flatten'): |
| 52 | + flatten = tf.contrib.layers.flatten(conv2) |
| 53 | + |
| 54 | + with tf.variable_scope('dense1'): |
| 55 | + dense1 = dense(flatten, units_1, [units_1], w_i, b_i) |
| 56 | + with tf.variable_scope('dense2'): |
| 57 | + dense2 = dense(dense1, units_2, [units_2], w_i, b_i) |
| 58 | + with tf.variable_scope('concat'): |
| 59 | + concatenated = tf.concat([dense2, tf.cast(action, tf.float32)], 1) |
| 60 | + with tf.variable_scope('dense3'): |
| 61 | + dense3 = dense(concatenated, self.atoms, [self.atoms], w_i, b_i) # 返回 |
| 62 | + return dense3 |
| 63 | + |
| 64 | + def build_cate_dqn_net(self): |
| 65 | + with tf.variable_scope('target_net'): |
| 66 | + c_names = ['target_net_arams',tf.GraphKeys.GLOBAL_VARIABLES] |
| 67 | + w_i = tf.random_uniform_initializer(-0.1,0.1) |
| 68 | + b_i = tf.constant_initializer(0.1) |
| 69 | + self.z_target = self.build_layers(self.state_input,self.action_input,c_names,24,24,w_i,b_i) |
| 70 | + |
| 71 | + with tf.variable_scope('eval_net'): |
| 72 | + c_names = ['eval_net_params',tf.GraphKeys.GLOBAL_VARIABLES] |
| 73 | + w_i = tf.random_uniform_initializer(-0.1,0.1) |
| 74 | + b_i = tf.constant_initializer(0.1) |
| 75 | + self.z_eval = self.build_layers(self.state_input,self.action_input,c_names,24,24,w_i,b_i) |
| 76 | + |
| 77 | + |
| 78 | + self.q_eval = tf.reduce_sum(self.z_eval * self.z) |
| 79 | + self.q_target = tf.reduce_sum(self.z_target * self.z) |
| 80 | + |
| 81 | + self.cross_entropy_loss = -tf.reduce_sum(self.m_input * tf.log(self.z_eval)) |
| 82 | + |
| 83 | + self.optimizer = tf.train.AdamOptimizer(self.config.LEARNING_RATE).minimize(self.cross_entropy_loss) |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + def train(self,s,r,action,s_,gamma): |
| 89 | + list_q_ = [self.sess.run(self.q_target,feed_dict={self.state_input:[s_],self.action_input:[[a]]}) for a in range(self.action_dim)] |
| 90 | + a_ = tf.argmax(list_q_).eval() |
| 91 | + m = np.zeros(self.atoms) |
| 92 | + p = self.sess.run(self.z_target,feed_dict = {self.state_input:[s_],self.action_input:[[a_]]})[0] |
| 93 | + for j in range(self.atoms): |
| 94 | + Tz = min(self.v_max,max(self.v_min,r+gamma * self.z[j])) |
| 95 | + bj = (Tz - self.v_min) / self.delta_z # 分在第几个块里 |
| 96 | + l,u = math.floor(bj),math.ceil(bj) # 上下界 |
| 97 | + |
| 98 | + pj = p[j] |
| 99 | + |
| 100 | + m[int(l)] += pj * (u - bj) |
| 101 | + m[int(u)] += pj * (bj - l) |
| 102 | + |
| 103 | + self.sess.run(self.optimizer,feed_dict={self.state_input:[s] , self.action_input:[action], self.m_input: m }) |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + def save_model(self): |
| 108 | + print("Model saved in : ", self.saver.save(self.sess, self.config.MODEL_PATH)) |
| 109 | + |
| 110 | + def restore_model(self): |
| 111 | + self.saver.restore(self.sess, self.config.MODEL_PATH) |
| 112 | + print("Model restored.") |
| 113 | + |
| 114 | + |
| 115 | + def greedy_action(self,s): |
| 116 | + self.epsilon = max(self.config.FINAL_EPSILON, self.epsilon * self.config.EPSILIN_DECAY) |
| 117 | + if random.random() <= self.epsilon: |
| 118 | + return random.randint(0, self.action_dim - 1) |
| 119 | + return np.argmax( |
| 120 | + [self.sess.run(self.q_target,feed_dict={self.state_input:[s],self.action_input:[[a]]}) for a in range(self.action_dim)]) |
0 commit comments