Netlogo:“foreach"带有两个列表的命令 [英] Netlogo: "foreach" command with two lists
问题描述
我有一个完整的有向图,每个链接都有自己的权重.我设法选择了每只乌龟的最大输出链接.但是,有时两只海龟的最大输出链接彼此相反,导致选择彼此相反的两个链接.如果发生这种情况,我希望具有较低值的链接消失.我用这个创建了两个列表:
I have a complete directed graph with each link a weight of it's own. I've managed to select the max-out-link of every turtle. But, sometimes the max-out-link of two turtles are opposite of each other resulting in both links opposite of one another being selected. if this happens i want the link with the lower value to die. i have created the two lists with this:
set max-end1 [[end1] of max-one-of my-out-links [trust]] of turtles
set max-end2 [[end2] of max-one-of my-out-links [trust]] of turtles
并通过像这样设置 x 和 y 参数:
and by setting an x and y parameter like so:
ask turtles
[
set x max-one-of my-out-links [label]
set y my-in-links
]
我希望像这样比较两个列表中的每一项:
i was hoping to compare each item of the two lists like so:
if [x] of max-end2 = any? [y] of max-end1
[
ifelse x X y
[ask x [die]]
[ask y [die]]
]
但我不知道如何将 foreach 命令与 if 命令结合起来有人可以帮我吗?
but i have no idea how to combine the foreach command with the if command can someone help me?
推荐答案
我实际上无法弄清楚您的代码应该如何工作.列表似乎是处理这个问题的笨拙方式,而您使用 any?
的方式是行不通的.所以,我重新开始并编写了一个独立的模型(将它放在一个空的 NetLogo 会话中)来做我认为你正在尝试做的事情.
I can't actually figure out how your code is supposed to work. Lists seems like an awkward way to approach this and the way you are using any?
is not going to work. So, I have instead started again and written a standalone model (put it in an empty NetLogo session) to do what I think you are trying to do.
links-own [ weight ]
to testme
clear-all
create-turtles 15
ask turtles
[ create-links-to other turtles
[ set weight random 100
]
]
layout-circle turtles 10
kill-lowers
end
to kill-lowers
; first remove any that's not a max weight
ask turtles
[ let big-link max-one-of my-out-links [weight]
let dying my-out-links with [not member? self (link-set big-link)]
ask dying [die]
]
; find pairs and make lower turn red
ask links
[ let opp-links links with [end1 = [end2] of myself and end2 = [end1] of myself ]
if any? opp-links
[ ask opp-links [set color red]
]
]
end
testme 程序只是创建了一个完整的网络.然后它调用 kill-lowers 过程来执行链接删除.第一部分让每只海龟识别其最大权重的out-link
(如果两个同样高,则随机选择一个),然后创建所有其他海龟的link-set
链接并让他们死.我认为您的代码中已经发生了这种情况.
The testme procedure just creates a complete network. It then calls the kill-lowers procedure to do the link removal. The first section has each turtle identify its out-link
with the largest weight (or randomly selects one if two equally high) and then creates the link-set
of all the other links and gets them to die. I think you already have that happening in your code.
所以有趣的是以询问链接
开头的部分.它随机遍历所有链接(将其视为 foreach
,除了对一组链接而不是列表进行操作).反过来,对于特定链接,代码检查是否存在相反方向的链接,并将其设置为名为 opp-links 的变量.它检查的方式是简单地查看是否有一个链接将 end2
设为它自己的 end1
以及其他方式.end1
是定向链接的来源,end2
是目标.
So the interesting bit is the section that starts ask links
. It randomly runs through all the links (think of it as a foreach
except operating on a set of links rather than a list). In turn, for the particular link, the code checks if there is a link in the opposite direction and sets that to the variable named opp-links. The way it checks is to simply see if there is a link that has end2
to be its own end1
and also the other way. end1
is the source of a directed link and end2
is the target.
如果有这样的链接,就会变成红色.这样您就可以检查代码是否在执行您想要的操作.测试完之后,你就可以die
了.
If there is such a link, it becomes red. That's so you can check the code is doing what you want. After you have tested it, then you have it die
instead.
这篇关于Netlogo:“foreach"带有两个列表的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!