Net logo agents that are in a row and seperated by a empty patch must die

StackOverflow https://stackoverflow.com/questions/9445844

  •  12-11-2019
  •  | 
  •  

Вопрос

Imagine i have the same breed of turtle which is positioned like below and is separated by a empty patch.

1234 56 78 9 <br/>
AAAA AA AA A  

When a random position is generated. Let say position 2 is the target, i want all the agents that are positioned at 1 2 3 4 to die as they are all supposed to be linked together. Similarly 5 and 6 will die if position 5 or 6 is selected.

I tried using links and neighbors but they don't seem to work. When i tried it, the turtles at position 4 & 3 are the only wants that dies.

Below is the code that i used to link the turtles:

if (any? virus-on neighbors)
[set create-links-with virus-on neighbors [tie]] 

Below is the code that i used to kill the turle:

ask virus-on patch in_xcor in_ycor [
    ask link-neighbors [die]
    die
 ]
Это было полезно?

Решение

One thing for sure is that you don't need to go through the trouble of creating links between the turtles that you are about to kill. Your main problem is to identify which turtles need to die, and this can be accomplished with a recursive procedure: you have a target turtle that needs to die, but you want it to ask its neighbors do the same (and they, in turn, to ask theirs, etc.)

The process is made slightly more complex by the fact that killing a turtle in the middle of the recursion would mess things up, but you can get around this by using a turtle variable that serves as a flag for marking turtles that need to be killed, and then killing all of those at the end.

The following code should provide a fully working example:

turtles-own [ marked-for-death ]

to setup
  clear-all
  ask patch 1 0 [ sprout 1 ]
  ask patch 2 0 [ sprout 1 ]
  ask patch 3 0 [ sprout 1 ]
  ask patch 4 0 [ sprout 1 ]
  ask patch 6 0 [ sprout 1 ]
  ask patch 7 0 [ sprout 1 ]
  ask patch 9 0 [ sprout 1 ]
end

to go
  ask turtles [ set marked-for-death false ]
  ask turtles-on patch 2 0 [ mark-for-death ]
  ask turtles with [ marked-for-death = true ] [ die ]
end

to mark-for-death
  set marked-for-death true
  ask (turtles-on neighbors) with [ marked-for-death = false ] [ mark-for-death ]
end

This example kills the turtle on patch 2 0, and all of those that are linked to it. You will need to adjust it for own purposes but it should be enough to get you going...

Edit:

Slightly more elegant version, as it does not require a turtle variable (assuming same setup procedure):

to go
  let marked [ marked-for-death [] ] of turtles-on patch 2 0
  ask turtle-set marked [ die ]
end

to-report marked-for-death [ marked ]
  set marked fput self marked 
  ask (turtles-on neighbors) 
    with [ not (member? self marked) ] 
    [ set marked marked-for-death marked ]
  report marked
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top