【发布时间】:2021-08-05 12:46:12
【问题描述】:
我正在尝试实现一个机器人环境,它是英特尔提供的免费课程的一部分。课程作业中有多个文件,MakeItLearn.py 是我们应该编辑的文件,并在那里添加我们自己的网络以训练机器人。
但是当我尝试使用英特尔也提供的ExploreAndCollect.py 文件收集数据并且我们没有被告知要更改该文件中的任何内容时,它会引发以下错误:
Traceback (most recent call last): File "ExploreAndCollect.py", line 268, in <module> env = BotEnv() File "ExploreAndCollect.py", line 35, in __init__ self.BuildBot(50.01, 450.01, 20) File "ExploreAndCollect.py", line 69, in BuildBot BoxPoints = list(map(Vec2d, [(-size, -size), (-size, size), (size,size), (size, -size)])) TypeError: __new__() missing 1 required positional argument: 'y'
这是定义类和函数的代码:
class BotEnv:
def __init__(self):
## Initialize Required Variables
self.crashed = False
self.DetectCrash = 0
self.space = pymunk.Space()
self.BuildBot(50.01, 450.01, 20) <---------------------------------------------
self.walls = []
self.WallShapes = []
self.WallRects = []
## Add Walls To The Map ###
WallBody, WallShape, WallRect = self.BuildWall(200, 50, 50)
self.WallRects.append(WallRect)
WallBody, WallShape, WallRect = self.BuildWall(200, 125, 50)
self.WallRects.append(WallRect)
WallBody, WallShape, WallRect = self.BuildWall(200, 550, 50)
self.WallRects.append(WallRect)
WallBody, WallShape, WallRect = self.BuildWall(200, 450, 50)
self.WallRects.append(WallRect)
WallBody, WallShape, WallRect = self.BuildWall(400, 350, 50)
self.WallRects.append(WallRect)
WallBody, WallShape, WallRect = self.BuildWall(400, 250, 50)
self.WallRects.append(WallRect)
WallBody, WallShape, WallRect = self.BuildWall(500, 250, 50)
self.WallRects.append(WallRect)
WallBody, WallShape, WallRect = self.BuildWall(600, 250, 50)
self.WallRects.append(WallRect)
WallBody, WallShape, WallRect = self.BuildWall(115, 1050, 500)
self.WallRects.append(WallRect)
self.PreviousAction = 0
def BuildBot(self, x, y, r):
### Build The Bot Object ###
size = r
BoxPoints = list(map(Vec2d, [(-size, -size), (-size, size), (size,size), (size, -size)])) <----------------
mass = 0.5
moment = pymunk.moment_for_poly(mass,BoxPoints, Vec2d(0,0))
self.Bot = pymunk.Body(mass, moment)
self.Bot.position = (x,y) # Declare Bot Position
self.Bot.angle = 1.54 # Set the Bot Angle
BotDirection = Vec2d(PointsFromAngle(self.Bot.angle)) # Get The Direction Vector From Angle
self.space.add(self.Bot)
self.BotRect = pygame.Rect(x-r,600-y-r, 2*r, 2*r)
return self.Bot
我查看了上面也提到的函数调用,其中包含y 组件,所以我似乎找不到导致错误的原因。 python 环境要求为 Python 2.7 和 Python 3 的所有版本
【问题讨论】:
-
看来
Vec2d有两个参数,x和y。它只得到一个,带有x和y坐标的元组(例如(size, size))。所以x的值有误,y没有设置。 -
您可以尝试将
map替换为itertools.starmap。 -
如果我使用
starmap,则会收到错误消息 -File "ExploreAndCollect.py", line 76, in BuildBot BotDirection = Vec2d(PointsFromAngle(self.Bot.angle)) # Get The Direction Vector From Angle TypeError: __new__() missing 1 required positional argument: 'y' -
这意味着使用
starmap修复了第 69 行的错误,现在您在第 76 行有相同的错误。您可以使用Vec2d(*PointsFromAngle(self.Bot.angle))修复它(注意 *)。但问题是为什么这个错误不断出现?您使用的Vec2d是否正确? -
正如我在上面的描述中所写,这个文件不是我创建的,它是由英特尔作为免费课程创建的,但是当我尝试使用上面的文件运行模拟时,它会抛出一个错误。这是您可以下载代码文件的课程链接:software.intel.com/content/www/us/en/develop/training/…