习题:基于gym环境构建迷宫世界

效果演示

迷宫世界的最终演示效果如图。
黑色为墙不可通行,白色为道路可通行,黄色为出口。

在这里插入图片描述

环境测试代码

# 环境测试
import gym
import random
import time
env=gym.make('MazeWorld-v0')
env.reset()
reward=0
while True:
        action = env.actions[int(random.random()*len(env.actions))]
        next_state,r,is_terminal,info = env.step(action)
        env.render()
        reward += r
        if is_terminal == True:
            print("reward:",reward)
            break
        time.sleep(0.2)

测试代码的调用

方法请参考我的上一篇博客链接: 建立自己的gym环境并调用.

环境构建代码

import gym
from numpy import random
import time

class MazeEnv(gym.Env):
    def __init__(self):
        self.viewer = None
        # 状态空间
        self.states = [1, 2, 3, 4, 5,
                       6, 7, 8, 9, 10,
                       11, 12, 13, 14, 15,
                       16, 17, 18, 19, 20,
                       21, 22, 23, 24, 25]
        # 动作空间
        self.actions = ['n', 'e', 's', 'w']
        # 回报函数
        self.rewards = dict()
        self.rewards['10_s'] = 10
        self.rewards['14_e'] = 10
        self.rewards['20_n'] = 10

        # 状态转移概率,用状态_动作的模式存入字典
        self.t = dict()
        self.t['1_s'] = 6
        self.t['1_e'] = 2
        self.t['2_w'] = 1
        self.t['2_s'] = 7
        self.t['2_e'] = 3
        self.t['3_w'] = 2
        self.t['3_s'] = 8
        self.t['5_s'] = 10
        self.t['6_n'] = 1
        self.t['6_e'] = 7
        self.t['7_w'] = 6
        self.t['7_n'] = 2
        self.t['7_e'] = 8
        self.t['8_w'] = 7
        self.t['8_n'] = 3
        self.t['8_s'] = 13
        self.t['10_n'] = 5
        self.t['10_s'] = 15
        self.t['13_n'] = 8
        self.t['13_e'] = 14
        self.t['13_s'] = 18
        self.t['14_w'] = 13
        self.t['14_e'] = 15
        self.t['14_s'] = 19
        self.t['16_e'] = 17
        self.t['16_s'] = 21
        self.t['17_w'] = 16
        self.t['17_e'] = 18
        self.t['17_s'] = 22
        self.t['18_w'] = 17
        self.t['18_e'] = 19
        self.t['18_n'] = 13
        self.t['19_w'] = 18
        self.t['19_n'] = 14
        self.t['19_e'] = 20
        self.t['20_w'] = 19
        self.t['20_n'] = 15
        self.t['21_n'] = 16
        self.t['21_e'] = 22
        self.t['22_w'] = 21
        self.t['22_n'] = 17

    def step(self, action):
        #系统当前状态
        state = self.state
        #将状态和动作组成的字典的键值
        key = "%d_%s" % (state, action)
        #状态转移
        #self.t是状态转移表,如果键值在表中,则通过表选出下一状态
        if key in self.t:
            next_state = self.t[key]

        # 动作的下一刻是黑色墙壁的不在状态转移表中
        #如果键值不在状态转移表中,则维持当前状态
        else:
            next_state = state
        self.state = next_state

        #出口判断,初值为FALSE
        is_terminal = False
        #self.rewards为回报函数表,即下一状态为出口的的状态和动作
        #如果键值在回报函数表里,则出口判断函数is_terminal = True,奖励为10
        if key in self.rewards:
            r = 10
            is_terminal = True
        #如果键值不在回报函数里,则出口判断函数保持false值,奖励为-1
        else:
            r = -1
        return next_state, r, is_terminal, {}

    def reset(self):
        s = [4, 9, 11, 12, 23, 24, 25]
        self.state = self.states[int(random.random() * (len(self.states) ))]
        while self.state in s:
            self.state = self.states[int(random.random() * (len(self.states) - 1))]
        return self.state

    def close(self):
        if self.viewer:
            self.viewer.close()
            self.viewer = None

    def render(self, mode="human"):
        from gym.envs.classic_control import rendering
        width = 60
        height = 40
        edge_x = 0
        edge_y = 0
        if self.viewer is None:
            self.viewer = rendering.Viewer(300, 200)

        # 右下角                 用黑色表示墙
        self.viewer.draw_polygon([(0, 0), (0, height), (width, height), (width, 0)], filled=True,
                                 color=(0, 0, 0)).add_attr(
            rendering.Transform((edge_x + width * 2, edge_y + height * 1)))
        self.viewer.draw_polygon([(0, 0), (0, height), (width, height), (width, 0)], filled=True,
                                 color=(0, 0, 0)).add_attr(
            rendering.Transform((edge_x + width * 3, edge_y + height * 1)))
        self.viewer.draw_polygon([(0, 0), (0, height), (width, height), (width, 0)], filled=True,
                                 color=(0, 0, 0)).add_attr(
            rendering.Transform((edge_x + width * 4, edge_y + height * 1)))
        # 左边
        self.viewer.draw_polygon([(0, 0), (0, height), (width, height), (width, 0)], filled=True,
                                 color=(0, 0, 0)).add_attr(rendering.Transform((edge_x, edge_y + height * 3)))
        self.viewer.draw_polygon([(0, 0), (0, height), (width, height), (width, 0)], filled=True,
                                 color=(0, 0, 0)).add_attr(
            rendering.Transform((edge_x + width * 1, edge_y + height * 3)))
        # 上边
        self.viewer.draw_polygon([(0, 0), (0, height), (width, height), (width, 0)], filled=True,
                                 color=(0, 0, 0)).add_attr(
            rendering.Transform((edge_x + width * 3, edge_y + height * 4)))
        self.viewer.draw_polygon([(0, 0), (0, height), (width, height), (width, 0)], filled=True,
                                 color=(0, 0, 0)).add_attr(
            rendering.Transform((edge_x + width * 3, edge_y + height * 5)))
        # 出口,用黄色表示出口
        self.viewer.draw_polygon([(0, 0), (0, height), (width, height), (width, 0)], filled=True,
                                 color=(1, 0.9, 0)).add_attr(
            rendering.Transform((edge_x + width * 4, edge_y + height * 3)))
        # 画网格
        for i in range(1, 7):
            self.viewer.draw_line((edge_x, edge_y + height * i), (edge_x + 5 * width, edge_y + height * i))  # 横线
            self.viewer.draw_line((edge_x + width * (i - 1), edge_y + height),
                                  (edge_x + width * (i - 1), edge_y + height * 6))  # 竖线

        # 人的像素位置
        self.x = [edge_x + width * 0.5, edge_x + width * 1.5, edge_x + width * 2.5, 0, edge_x + width * 4.5,
                  edge_x + width * 0.5, edge_x + width * 1.5, edge_x + width * 2.5, 0, edge_x + width * 4.5,
                  0, 0, edge_x + width * 2.5, edge_x + width * 3.5, edge_x + width * 4.5,
                  edge_x + width * 0.5, edge_x + width * 1.5, edge_x + width * 2.5, edge_x + width * 3.5,
                  edge_x + width * 4.5,
                  edge_x + width * 0.5, edge_x + width * 1.5, 0, 0, 0]

        self.y = [edge_y + height * 5.5, edge_y + height * 5.5, edge_y + height * 5.5, 0, edge_y + height * 5.5,
                  edge_y + height * 4.5, edge_y + height * 4.5, edge_y + height * 4.5, 0, edge_y + height * 4.5,
                  0, 0, edge_y + height * 3.5, edge_y + height * 3.5, edge_y + height * 3.5,
                  edge_y + height * 2.5, edge_y + height * 2.5, edge_y + height * 2.5, edge_y + height * 2.5,
                  edge_y + height * 2.5,
                  edge_y + height * 1.5, edge_y + height * 1.5, 0, 0, 0]
        # 用圆表示人
        # self.viewer.draw_circle(18,color=(0.8,0.6,0.4)).add_attr(rendering.Transform(translation=(edge_x+width/2,edge_y+height*1.5)))
        self.viewer.draw_circle(18, color=(0.8, 0.6, 0.4)).add_attr(
            rendering.Transform(translation=(self.x[self.state - 1], self.y[self.state - 1])))

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')




  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python 是一种脚本语言,广泛应用于各类编程和数据分析任务。在机器学习和人工智能领域,Python 也是一门常用的编程语言。基于 Python 的 gym-carracing 是 OpenAI Gym 的一个环境,用于模拟汽车驾驶场景。 这个自动驾驶项目的目标是让计算机通过机器学习算法来自动驾驶模拟的汽车。首先,我们需要安装 gym-carracing 环境及其相关依赖库。然后,我们可以使用强化学习算法,比如深度强化学习算法(如深度 Q 网络),训练一个智能驾驶代理程序。 在训练过程中,代理程序会不断与环境进行交互,获取当前的观测状态,然后根据某种策略选择一个动作作为响应。之后,代理程序会根据环境给予的反馈来调整自己的策略和动作选择,以逐步提升驾驶技能。 在 gym-carracing 环境中,汽车会在一个虚拟的赛道上行驶,玩家需要使用方向键控制汽车的转向,以应对各种道路和弯道情况。在自动驾驶项目中,我们的目标是通过训练智能代理程序,使它能够自动控制汽车,适应不同的道路和驾驶情景,并以高效、安全的方式行驶。 该项目的难点在于设计和实现一个合适的驾驶策略,并通过强化学习算法不断优化代理程序的决策能力。为了取得好的效果,我们可能需要进行大量的训练和参数调整,并结合一些技巧和经验,优化代理程序的性能。 总而言之,Python 和 gym-carracing 提供了一个有趣且实用的平台,可用于开发自动驾驶项目。通过合理的算法和大量的训练,我们希望能够训练出一个高效、智能的驾驶代理程序,使其能够在模拟环境中完成自动驾驶任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值