Netlogo: “foreach” command with two lists

和自甴很熟 提交于 2020-08-10 19:32:36

问题


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

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]]
]

but i have no idea how to combine the foreach command with the if command can someone help me?


回答1:


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

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.

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.

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.



来源:https://stackoverflow.com/questions/62678523/netlogo-foreach-command-with-two-lists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!