Deleting the opposite of a link in a list in netlogo

荒凉一梦 提交于 2021-01-29 04:40:59

问题


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

回答1:


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

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.

Hope this helps, Charles



来源:https://stackoverflow.com/questions/62451182/deleting-the-opposite-of-a-link-in-a-list-in-netlogo

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