用python演示一个多agent的demo
时间: 2023-05-25 20:03:39 浏览: 52
以下是一个简单的多agent演示:
```python
import random
class Agent:
def __init__(self, x, y):
self.x = x
self.y = y
def move(self):
directions = ["up", "down", "left", "right"]
direction = random.choice(directions)
if direction == "up":
self.y += 1
elif direction == "down":
self.y -= 1
elif direction == "left":
self.x -= 1
else:
self.x += 1
agents = []
for i in range(10):
x = random.randint(0, 10)
y = random.randint(0, 10)
agent = Agent(x, y)
agents.append(agent)
for i in range(10):
for agent in agents:
agent.move()
```
该演示创建了10个代理,然后让每个代理随机移动10次。在每个步骤中,代理将在4个方向中随机选择一个方向进行移动。该演示可以通过在每次移动之后输出每个Agent的位置来扩展。