Netlogo: “foreach” command with two lists

后端 未结 1 468
天涯浪人
天涯浪人 2021-01-27 19:10

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 a

相关标签:
1条回答
  • 2021-01-27 19:23

    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.

    0 讨论(0)
提交回复
热议问题