有人可以帮助我在每次迭代后计算距已创建点最远的点吗?
这是我使用Netlogo创建的基于代理的模型,现在尝试将其转换为Python
由于使用了预定义的函数来计算距离,因此我真的可以说出距离。
incompatible types: inference variable E has incompatible bounds
[ERROR] equality constraints: com.app.People
[ERROR] lower bounds: java.util.List<com.app.People>
netlogo上的原始模型是这样的:
import random
class Bug:
def __init__(self, number, xPos, yPos, worldXSize = 80, worldYSize = 80):
# the environment
self.number = number
self.worldXSize = worldXSize
self.worldYSize = worldYSize
# the bug
self.xPos = xPos
self.yPos = yPos
print ("Bug number ", self.number, \
" has been created at ", self.xPos, ", ", self.yPos)
# the action
def randomWalk(self):
self.xPos += randomMove()
self.yPos += randomMove()
self.xPos = (self.xPos + self.worldXSize) % self.worldXSize
self.yPos = (self.yPos + self.worldYSize) % self.worldYSize
# report
def reportPosition(self):
print ("Bug number ", self.number, " moved to X = ", \
self.xPos, " Y = ", self.yPos)
# returns -1, 0, 1 with equal probability
def randomMove():
return random.randint(-1, 1)
nBugs = input("How many bugs? ")
#bugList = [0] * nBugs
bugList=[]
worldXSize= input("X Size of the world? ")
worldYSize= input("Y Size of the world? ")
length = input("Length of the simulation in cycles? ")
for i in range(nBugs):
aBug = Bug(i, random.randint(0,worldXSize-1), \
random.randint(0,worldYSize-1),
worldXSize, worldYSize)
bugList.append(aBug)
for t in range(length):
for aBug in bugList:
aBug.randomWalk()
aBug.reportPosition()
答案 0 :(得分:1)
以下是我对所追求的目标的估计。我从turtle导入了Vec2D
来简化您的逻辑:
from random import randint, randrange
from turtle import Vec2D
class Bug:
def __init__(self, number, worldXSize=80, worldYSize=80):
# the environment
self.number = number
self.worldXSize = worldXSize
self.worldYSize = worldYSize
# the bug
self.position = Vec2D(randrange(worldYSize), randrange(worldYSize))
print("Bug number", self.number, "has been created at", self.position)
# the action
def randomWalk(self):
self.position += randomMove()
def distance(self, other):
return abs(other.position - self.position)
# report
def reportPosition(self):
print("Bug number", self.number, "is located at", self.position)
distance, bug = max((self.distance(bug), bug.number) for bug in bugList if bug != self)
print("the farthest bug from me is", bug, "at a distance of", distance)
# returns -1, 0, 1 with equal probability
def randomMove():
return Vec2D(randint(-1, 1), randint(-1, 1))
nBugs = int(input("How many bugs? "))
worldXSize = int(input("X Size of the world? "))
worldYSize = int(input("Y Size of the world? "))
length = int(input("Length of the simulation in cycles? "))
bugList = [Bug(i + 1, worldXSize, worldYSize) for i in range(nBugs)]
for _ in range(length):
for bug in bugList:
bug.randomWalk()
print()
for bug in bugList:
bug.reportPosition()
尽管我使用了worldXSize
和worldYSize
来约束错误的创建位置,但是我并没有约束它们的去向-可以根据需要进行修改。您的代码中的这种逻辑没有意义:
self.xPos = (self.xPos + self.worldXSize) % self.worldXSize
看起来很简单,以下就是您想要的(Y坐标为同上):
self.xPos = self.xPos % self.worldXSize
而且我完全看不到您的代码如何运行,因为您没有在数学情况下使用它们之前将任何输入从字符串转换为数字。
输出
% python3 test.py
How many bugs? 3
X Size of the world? 100
Y Size of the world? 100
Length of the simulation in cycles? 4
Bug number 1 has been created at (94.00,9.00)
Bug number 2 has been created at (91.00,40.00)
Bug number 3 has been created at (29.00,18.00)
Bug number 1 is located at (93.00,9.00)
the farthest bug from me is 3 at a distance of 64.77653896280658
Bug number 2 is located at (90.00,41.00)
the farthest bug from me is 3 at a distance of 64.8459713474939
Bug number 3 is located at (29.00,19.00)
the farthest bug from me is 2 at a distance of 64.8459713474939
Bug number 1 is located at (94.00,9.00)
the farthest bug from me is 3 at a distance of 66.91038783328041
Bug number 2 is located at (90.00,40.00)
the farthest bug from me is 3 at a distance of 65.14598989960932
Bug number 3 is located at (28.00,20.00)
the farthest bug from me is 1 at a distance of 66.91038783328041
Bug number 1 is located at (95.00,9.00)
the farthest bug from me is 3 at a distance of 66.75327707311455
Bug number 2 is located at (90.00,39.00)
the farthest bug from me is 3 at a distance of 64.19501538281614
Bug number 3 is located at (29.00,19.00)
the farthest bug from me is 1 at a distance of 66.75327707311455
Bug number 1 is located at (96.00,8.00)
the farthest bug from me is 3 at a distance of 68.06614430096654
Bug number 2 is located at (89.00,39.00)
the farthest bug from me is 3 at a distance of 62.93647591023825
Bug number 3 is located at (29.00,20.00)
the farthest bug from me is 1 at a distance of 68.06614430096654
%