我有一个问题,希望在我的YAML文件中防止别名的出现。有没有办法禁用所生成的YAML文件中的别名,以达到预期的输出结果?
我目前拥有的YAML文件如下:
agents:
- start: [0, 0]
goal: [2, 0]
name: agent0
- start: [2, 0]
goal: [0, 0]
name: agent1
map:
dimensions: [3, 3]
obstacles:
- !!python/tuple [0, 1]
- !!python/tuple [2, 1]
当我每次将代理达到目标点的值更新为起始点的值时,我会遇到别名成为起始点和目标点值而不是我想要的值的问题,即
[0,0]
是起始点和目标点的值。agents:
- start: &id001 [2, 0]
goal: *id001
name: agent0
- start: &id002 [0, 0]
goal: *id002
name: agent1
map:
dimensions: [3, 3]
obstacles:
- !!python/tuple [0, 1]
- !!python/tuple [2, 1]
预期的输出:
agents:
- start: [2, 0]
goal: [2, 0]
name: agent0
- start: [0, 0]
goal: [0, 0]
name: agent1
map:
dimensions: [3, 3]
obstacles:
- !!python/tuple [0, 1]
- !!python/tuple [2, 1]
我有以下代码,在Python中运行以更新文件:
def updateInput(self, agent):
yaml = ruamel.yaml.YAML()
with open('input.yaml') as f:
doc = yaml.load(f)
self.get_updated_dict(doc, agent)
with open('input.yaml', 'w') as y:
yaml.dump(doc, y)
return {}
def get_updated_dict(self, doc, agent):
obj = doc
if obj["agents"][0]["name"] == agent:
goal_state = obj["agents"][0]["goal"]
obj["agents"][0]["start"] = goal_state
return doc