mirror of
https://github.com/wassname/ray.git
synced 2026-07-14 11:17:54 +08:00
9f91eb8c91
* Direction substitution of @ray.remote -> @ray.task. * Changes to make '@ray.task' work. * Instantiate actors with Class.remote() instead of Class(). * Convert actor instantiation in tests and examples from Class() to Class.remote(). * Change actor method invocation from object.method() to object.method.remote(). * Update tests and examples to invoke actor methods with .remote(). * Fix bugs in jenkins tests. * Fix example applications. * Change @ray.task back to @ray.remote. * Changes to make @ray.actor -> @ray.remote work. * Direct substitution of @ray.actor -> @ray.remote. * Fixes. * Raise exception if @ray.actor decorator is used. * Simplify ActorMethod class.
44 lines
1.6 KiB
Python
44 lines
1.6 KiB
Python
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
|
|
import tensorflow as tf
|
|
import os
|
|
|
|
import ray
|
|
|
|
from reinforce.env import BatchedEnv
|
|
from reinforce.policy import ProximalPolicyLoss
|
|
from reinforce.filter import MeanStdFilter
|
|
from reinforce.rollout import rollouts, add_advantage_values
|
|
|
|
class Agent(object):
|
|
|
|
def __init__(self, name, batchsize, preprocessor, config, use_gpu):
|
|
if not use_gpu:
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
|
self.env = BatchedEnv(name, batchsize, preprocessor=preprocessor)
|
|
if preprocessor.shape is None:
|
|
preprocessor.shape = self.env.observation_space.shape
|
|
self.sess = tf.Session()
|
|
self.ppo = ProximalPolicyLoss(self.env.observation_space, self.env.action_space, preprocessor, config, self.sess)
|
|
self.optimizer = tf.train.AdamOptimizer(config["sgd_stepsize"])
|
|
self.train_op = self.optimizer.minimize(self.ppo.loss)
|
|
self.variables = ray.experimental.TensorFlowVariables(self.ppo.loss, self.sess)
|
|
self.observation_filter = MeanStdFilter(preprocessor.shape, clip=None)
|
|
self.reward_filter = MeanStdFilter((), clip=5.0)
|
|
self.sess.run(tf.global_variables_initializer())
|
|
|
|
def get_weights(self):
|
|
return self.variables.get_weights()
|
|
|
|
def load_weights(self, weights):
|
|
self.variables.set_weights(weights)
|
|
|
|
def compute_trajectory(self, gamma, lam, horizon):
|
|
trajectory = rollouts(self.ppo, self.env, horizon, self.observation_filter, self.reward_filter)
|
|
add_advantage_values(trajectory, gamma, lam, self.reward_filter)
|
|
return trajectory
|
|
|
|
RemoteAgent = ray.remote(Agent)
|