# Copyright 2018 DeepMind Technologies Limited. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Example running TD3 on the control suite."""

from absl import flags
import acme
from acme import specs
from acme.agents.jax import td3
from absl import app
import helpers
from acme.utils import counting
from acme.utils import experiment_utils
import jax

FLAGS = flags.FLAGS
flags.DEFINE_integer('num_steps', 1000000,
                     'Number of env steps to run training for.')
flags.DEFINE_integer('eval_every', 10000, 'How often to run evaluation')
flags.DEFINE_string('env_name', 'MountainCarContinuous-v0',
                    'What environment to run')
flags.DEFINE_integer('num_sgd_steps_per_step', 1,
                     'Number of SGD steps per learner step().')
flags.DEFINE_integer('seed', 0, 'Random seed.')


def main(_):
  # Create an environment, grab the spec, and use it to create networks.
  environment = helpers.make_environment(task=FLAGS.env_name)
  environment_spec = specs.make_environment_spec(environment)
  agent_networks = td3.make_networks(environment_spec)

  # Construct the agent.
  config = td3.TD3Config(num_sgd_steps_per_step=FLAGS.num_sgd_steps_per_step)
  agent = td3.TD3(
      environment_spec, agent_networks, config=config, seed=FLAGS.seed)

  # Create the environment loop used for training.
  train_logger = experiment_utils.make_experiment_logger(
      label='train', steps_key='train_steps')
  train_loop = acme.EnvironmentLoop(
      environment,
      agent,
      counter=counting.Counter(prefix='train'),
      logger=train_logger)

  # Create the evaluation actor and loop.
  eval_logger = experiment_utils.make_experiment_logger(
      label='eval', steps_key='eval_steps')
  eval_actor = agent.builder.make_actor(
      random_key=jax.random.PRNGKey(FLAGS.seed),
      policy_network=td3.get_default_behavior_policy(
          agent_networks, environment_spec.actions, sigma=0.),
      variable_source=agent)
  eval_env = helpers.make_environment(task=FLAGS.env_name)
  eval_loop = acme.EnvironmentLoop(
      eval_env,
      eval_actor,
      counter=counting.Counter(prefix='eval'),
      logger=eval_logger)

  assert FLAGS.num_steps % FLAGS.eval_every == 0
  for _ in range(FLAGS.num_steps // FLAGS.eval_every):
    eval_loop.run(num_episodes=5)
    train_loop.run(num_steps=FLAGS.eval_every)
  eval_loop.run(num_episodes=5)


if __name__ == '__main__':
  app.run(main)

@@ -685,20 +685,8 @@
 ags%0A
-import acme%0A
 from
@@ -782,29 +782,27 @@
 om acme.
-utils
+jax
  import 
 counting
@@ -797,67 +797,15 @@
 ort 
-counting%0Afrom acme.utils import experiment_utils%0Aimport jax
+runners
 %0A%0AFL
@@ -1544,33 +1544,8 @@
 c)%0A%0A
-  # Construct the agent.%0A
   co
@@ -1624,637 +1624,206 @@
 )%0A  
-agent = td3.TD3(%0A      environment_spec, agent_networks, config=config, seed=FLAGS.seed)%0A%0A  # Create the environment loop used for training.%0A  train_logger = experiment_utils.make_experiment_logger(%0A      label='train', steps_key='train_steps')%0A  train_loop = acme.EnvironmentLoop(%0A      environment,%0A      agent,%0A      counter=counting.Counter(prefix='train'),%0A      logger=train_logger)%0A%0A  # Create the evaluation actor and loop.%0A  eval_logger = experiment_utils.make_experiment_logger(%0A      label='eval', steps_key='eval_steps')%0A  eval_actor = agent.builder.make_actor(%0A      random_key=jax.random.PRNGKey(FLAGS.seed),%0A      
+td3_builder = td3.TD3Builder(config)%0A  policy_network = td3.get_default_behavior_policy(%0A      networks=agent_networks,%0A      action_specs=environment_spec.actions,%0A      sigma=config.sigma)%0A  eval_
 poli
@@ -1832,17 +1832,19 @@
 _network
-=
+ = 
 td3.get_
@@ -1868,20 +1868,16 @@
 policy(%0A
-    
       ag
@@ -1929,457 +1929,292 @@
 =0.)
-,
 %0A  
-    variable_source=agent)%0A  eval_env = helpers.make_environment(task=FLAGS.env_name)%0A  eval_loop = acme.EnvironmentLoop(%0A      eval_env,%0A      eval_actor,%0A      counter=counting.Counter(prefix='eval'),%0A      logger=eval_logger)%0A%0A  assert FLAGS.num_steps %25 FLAGS.eval_every == 0%0A  for _ in range(FLAGS.num_steps // FLAGS.eval_every):%0A    eval_loop.run(num_episodes=5)%0A    train_loop.run(num_steps=FLAGS.eval_every)%0A  eval_loop.run(num_episodes=5
+runners.run_agent(%0A      builder=td3_builder,%0A      environment=environment,%0A      num_steps=FLAGS.num_steps,%0A      eval_every=FLAGS.eval_every,%0A      seed=FLAGS.seed,%0A      networks=agent_networks,%0A      policy_network=policy_network,%0A      eval_policy_network=eval_policy_network
 )%0A%0A%0A

Make td3 examples use run_agent method.