# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
import random

from game_state import GameState
from game_ac_network import GameACNetwork
from a3c_training_thread import A3CTrainingThread

from constants import ACTION_SIZE
from constants import PARALLEL_SIZE
from constants import CHECKPOINT_DIR

def choose_action(pi_values):
  values = []
  sum = 0.0
  for rate in pi_values:
    sum = sum + rate
    value = sum
    values.append(value)
    
  r = random.random() * sum
  for i in range(len(values)):
    if values[i] >= r:
      return i;
  #fail safe
  return len(values)-1

global_network = GameACNetwork(ACTION_SIZE)

training_threads = []
for i in range(PARALLEL_SIZE):
  training_thread = A3CTrainingThread(i, global_network, 1.0, 8000000)
  training_threads.append(training_thread)

sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)

saver = tf.train.Saver()
checkpoint = tf.train.get_checkpoint_state(CHECKPOINT_DIR)
if checkpoint and checkpoint.model_checkpoint_path:
  saver.restore(sess, checkpoint.model_checkpoint_path)
  print "checkpoint loaded:", checkpoint.model_checkpoint_path
else:
  print "Could not find old checkpoint"

game_state = GameState(0, display=True)

while True:
  pi_values = global_network.run_policy(sess, game_state.s_t)

  action = choose_action(pi_values)
  game_state.process(action)

  game_state.update()


@@ -199,16 +199,59 @@
 ngThread
+%0Afrom rmsprop_applier import RMSPropApplier
 %0A%0Afrom c
@@ -350,16 +350,51 @@
 OINT_DIR
+%0Afrom constants import RMSP_EPSILON
 %0A%0Adef ch
@@ -719,122 +719,773 @@
 E)%0A%0A
-training_threads = %5B%5D%0Afor i in range(PARALLEL_SIZE):%0A  training_thread = A3CTrainingThread(i, global_network, 1.0,
+learning_rate_input = tf.placeholder(%22float%22)%0A%0Apolicy_applier = RMSPropApplier(learning_rate = learning_rate_input,%0A                                decay = 0.99,%0A                                momentum = 0.0,%0A                                epsilon = RMSP_EPSILON )%0A%0Avalue_applier = RMSPropApplier(learning_rate = learning_rate_input,%0A                               decay = 0.99,%0A                               momentum = 0.0,%0A                               epsilon = RMSP_EPSILON )%0A%0Atraining_threads = %5B%5D%0Afor i in range(PARALLEL_SIZE):%0A  training_thread = A3CTrainingThread(i, global_network, 1.0,%0A                                      learning_rate_input,%0A                                      policy_applier, value_applier,%0A                                     
  800

fix display tool bug