测试主程序

import numpy as np
import tensorflow as tf

from environment.env import Env
from environment.carla.client import make_carla_client
from environment.carla.tcp import TCPConnectionError
from src.agent.ddpg_agent import DDPGAgent
from src.network.ddpg_network import CriticNetwork, ActorNetwork
import time

ACTOR_LEARNING_RATE = 0.0001
CRITIC_LEARNING_RATE = 0.001
TAU = 0.001
GAMMA = 0.99
RANDOM_SEED = 123
flags = tf.app.flags
flags.DEFINE_string('model_path', 'ckpt', '')
FLAGS = flags.FLAGS


def main(_):
    gpu_options = tf.GPUOptions(allow_growth=True)
    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        env = Env(MONITOR_DIR=None, SEED=RANDOM_SEED, FPS=10, sess=sess)
        tf.set_random_seed(RANDOM_SEED)

        state_dim = env.observation_space.shape
        action_dim = env.action_space.shape[0]
        action_bound = env.action_space.high
        assert(np.all(env.action_space.high == -env.action_space.low))
        action_type = 'Continuous'

        actor = ActorNetwork(sess, state_dim, action_dim, action_bound,
                             ACTOR_LEARNING_RATE, TAU, action_type)

        critic = CriticNetwork(sess, state_dim, action_dim, action_bound,
                               CRITIC_LEARNING_RATE, TAU, actor.get_num_trainable_vars(),
                               action_type)

        agent = DDPGAgent(sess, action_type, actor, critic, GAMMA, env, Inference=True, Inference_net_dir=FLAGS.model_path)

        while True:
            try:
                with make_carla_client('localhost', 2000) as client:
                    env.connected(client)
                    for i in range(2):
                        state = env.reset()
                        terminal = False
                        while not terminal:
                            action = actor.predict(np.expand_dims(state, 0))[0]
                            if action[1] >= 0:
                                step_ = {'steer': action[0], 'acc': action[1], 'brake': 0.0}
                            else:
                                step_ = {'steer': action[0], 'acc': 0.0, 'brake': action[1]}
                            state, reward, terminal, info = env.step(step_)

            except TCPConnectionError as error:
                print(error)
                time.sleep(1)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风尘23187

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值