"""
"""
import argparse
import time

from rl_coach.base_parameters import TaskParameters
from rl_coach.coach import expand_preset
from rl_coach import core_types
from rl_coach.utils import short_dynamic_import
from rl_coach.memories.non_episodic.distributed_experience_replay import DistributedExperienceReplay
from rl_coach.memories.memory import MemoryGranularity

# Q: specify alternative distributed memory, or should this go in the preset?
# A: preset must define distributed memory to be used. we aren't going to take a non-distributed preset and automatically distribute it.


def training_worker(graph_manager, checkpoint_dir):
    """
    restore a checkpoint then perform rollouts using the restored model
    """
    # initialize graph
    task_parameters = TaskParameters()
    task_parameters.__dict__['save_checkpoint_dir'] = checkpoint_dir
    graph_manager.create_graph(task_parameters)

    # save randomly initialized graph
    graph_manager.save_checkpoint()

    memory = DistributedExperienceReplay(max_size=(MemoryGranularity.Transitions, 1000000),
                                         redis_ip=graph_manager.agent_params.memory.redis_ip,
                                         redis_port=graph_manager.agent_params.memory.redis_port)

    while(memory.num_transitions() < 100):
        time.sleep(10)
    # TODO: critical: wait for minimum number of rollouts in memory before training
    # TODO: Q: training steps passed into graph_manager.train ignored?
    # TODO: specify training steps between checkpoints (in preset?)
    # TODO: replace while true with what? number of steps, convergence, time, ...
    # TODO: low: move evaluate out of this process

    # training loop
    for _ in range(40):
        graph_manager.phase = core_types.RunPhase.TRAIN
        graph_manager.train(core_types.TrainingSteps(1))
        graph_manager.phase = core_types.RunPhase.UNDEFINED

        graph_manager.evaluate(graph_manager.evaluation_steps)

        graph_manager.save_checkpoint()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--preset',
                        help="(string) Name of a preset to run (class name from the 'presets' directory.)",
                        type=str,
                        required=True)
    parser.add_argument('--checkpoint_dir',
                        help='(string) Path to a folder containing a checkpoint to write the model to.',
                        type=str,
                        default='/checkpoint')
    parser.add_argument('-r', '--redis_ip',
                        help="(string) IP or host for the redis server",
                        default='localhost',
                        type=str)
    parser.add_argument('-rp', '--redis_port',
                        help="(int) Port of the redis server",
                        default=6379,
                        type=int)
    args = parser.parse_args()

    graph_manager = short_dynamic_import(expand_preset(args.preset), ignore_module_case=True)

    graph_manager.agent_params.memory.redis_ip = args.redis_ip
    graph_manager.agent_params.memory.redis_port = args.redis_port

    training_worker(
        graph_manager=graph_manager,
        checkpoint_dir=args.checkpoint_dir,
    )


if __name__ == '__main__':
    main()

@@ -580,403 +580,34 @@
 t.%0A%0A
-%0A
 def 
-training_worker(graph_manager, checkpoint_dir):%0A    %22%22%22%0A    restore a checkpoint then perform rollouts using the restored model%0A    %22%22%22%0A    # initialize graph%0A    task_parameters = TaskParameters()%0A    task_parameters.__dict__%5B'save_checkpoint_dir'%5D = checkpoint_dir%0A    graph_manager.create_graph(task_parameters)%0A%0A    # save randomly initialized graph%0A    graph_manager.save_checkpoint()%0A
+heatup(graph_manager):
 %0A   
@@ -888,16 +888,85 @@
 _port)%0A%0A
+    num_steps = graph_manager.schedule_params.heatup_steps.num_steps%0A
     whil
@@ -994,19 +994,25 @@
 ons() %3C 
-100
+num_steps
 ):%0A     
@@ -1033,363 +1033,521 @@
 10)%0A
-    # TODO: critical: wait for minimum number of rollouts in memory before training%0A    # TODO: Q: training steps passed into graph_manager.train ignored?%0A    # TODO: specify training steps between checkpoints (in preset?)%0A    # TODO: replace while true
+%0A%0Adef training_worker(graph_manager, checkpoint_dir):%0A    %22%22%22%0A    restore a checkpoint then perform rollouts using the restored model%0A    %22%22%22%0A    # initialize graph%0A    task_parameters = TaskParameters()%0A    task_parameters.__dict__%5B'save_checkpoint_dir'%5D = checkpoint_dir%0A    graph_manager.create_graph(task_parameters)%0A%0A    # save randomly initialized graph%0A    graph_manager.save_checkpoint()%0A%0A    # optionally
  w
+a
 it
-h what? number of steps, convergence, time, ...%0A    # TODO: low: move evaluate out of this process
+ for a specific number of transitions to be in memory before training%0A    heatup(graph_manager)
 %0A%0A  

use the number of heat up steps specified in schedule parameters