import numpy as np
import tensorflow as tf

EPS = 1e-8

def mlp(x, hidden_sizes=(32,), activation=tf.tanh, output_activation=None):
    for h in hidden_sizes[:-1]:
        x = tf.layers.dense(x, units=h, activation=activation)
    return tf.layers.dense(x, units=hidden_sizes[-1], activation=output_activation)

def gaussian_likelihood(x, mu, log_std):
    pre_sum = -0.5 * (((x-mu)/(tf.exp(log_std)+EPS))**2 + 2*log_std + np.log(2*np.pi))

    return tf.reduce_sum(pre_sum, axis=1)

def actor_critic( x, a, hidden_sizes=(64,64), activation=tf.tanh,
    output_activation=None, policy=None, action_space=None):

    act_dim = a.shape.as_list()[-1]

    with tf.variable_scope( 'pi'):
        mu = mlp( x, list(hidden_sizes) + [act_dim], activation, output_activation)
        log_std = tf.get_variable( name='log_std',
            initializer=-0.5*np.ones(act_dim, dtype=np.float32))
        std = tf.exp( log_std)  

        pi = mu + tf.random_normal( tf.shape(mu)) * std
        logp = gaussian_likelihood( a, mu, log_std)
        logp_pi = gaussian_likelihood(pi, mu, log_std)

    with tf.variable_scope( 'v'):
        v = tf.squeeze( mlp( x, list( hidden_sizes)+[1], activation, None), axis=1)

    return [pi, logp, logp_pi, v]
