在netlogo中删除列表中链接的反面 [英] Deleting the opposite of a link in a list in netlogo

查看:39
本文介绍了在netlogo中删除列表中链接的反面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接列表,但有时在列表中,列表中会出现两个彼此相反的链接.所有链接都有值,列表按从最高值到最低值的顺序排列.我想要做的是使与较低价值的相反链接死亡.有没有人有任何想法?并不是说它会有很大帮助,但我从这行代码中得到了我的列表:

i have a list of links but sometimes within the list two links that are opposite of each other appear in the list. All links have values and the list is organized from highest value to lowest. what i want to do is to make the opposite link with the lower value die. does any one have any ideas? not that it would help a great deal but i got my list from this line of code:

set max-links sort-on [(- label)] link-set [max-one-of my-in-links [label]] of turtles

推荐答案

我假设这些是有向链接,因为在任何两只海龟之间不能有两个无向链接.下面的代码并不优雅,但我认为它会做你想做的.我已将其嵌入到一个工作模型中.

I'm assuming that these are directed links as there can not be two undirected links between any two turtles. The following code is not elegant, but I think it will do what you want. I've embedded it within a working model.

to test

  clear-all
  create-turtles 10 [fd random 10]
  ask turtles [ create-links-to n-of 8 other turtles ]
  ask links [set label random 100 ]
  let link-list sort links

  let to-die []
  let remaining link-set link-list
  foreach link-list [[m] ->
    let opposite (link [who] of [end2] of m [who] of [end1] of m)
    if opposite != nobody and member? opposite remaining [
      ifelse ([label] of opposite  < [label] of m) [
        set to-die lput opposite to-die
      ]
      [
        set to-die lput m to-die
      ] 
      set remaining remaining with [self != m]
    ]
  ]

  foreach to-die [m -> 
    set link-list remove m link-list
  ]
  ask link-set to-die [die]

end

对于列表中的每个链接,它会查看链接集remaining 中是否存在相反的链接,该链接集最初由列表中的链接组成.如果是这样,它会标记要删除的正确链接,然后将自己从 remaining 中取出,以便在测试相反的链接时不会找到它.找到所有要删除的链接后,将它们从列表中删除并要求去死.

For each link in the list, it looks to see if there is an opposite link in the linkset remaining, originally made up of the links in the list. If so, it marks the proper link for deletion and then takes itself out of remaining so that when the opposite link is tested it won't be found. Once all links to be deleted are found, they are removed from the list and asked to die.

希望这有帮助,查尔斯

这篇关于在netlogo中删除列表中链接的反面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆