nnetwork
nnetwork

Reputation: 11

NetLogo: Combine and form a new turtle

I am currently learning NetLogo and I need help. In my model I have same sized 10 turtles which moves randomly. When 2 or more turtles are on the same patch they will combine and form a new turtle with the double size. In this manner, the main rule is max. 5 turtles can combine to each other. And this formation will continue until the there will be 2 turtles (with each contain 5 turtles) remain.

I had created turtles and made them move randomly, but I could not managed to combine them. Can you show me a way to do this? Any help appreciated. Regards.

EDIT: I tried the "in-radius" command unsuccessfully. 5-5 distribution of the turtles (as you can can see from the code, they represent H2O molecules) is vital for the system definition and any other distributions are not allowed in the model.

In detail, when randomly moving 2 H2O molecules meet on the same patch, they will combine to form a new molecule (2H2O). The main rule is as previously mentioned, max. 5 molecules can combine which ends with forming 5H2O. Since, initially there are 10H2O molecules in the system, there will be 2 5H2O molecules at the end.

The code I tried to implement is as follows,

breed [h2o-molecules h2o]

to setup
  clear-all
  reset-ticks

  create-h2o-molecules h2o-num [
    set color 105
    set sIze .5
    set shape "circle"
    setxy random-xcor random-ycor
    set pen-mode "up"
  ]
end

to setup-patches
  ask patches [set pcolor 0]
  show count turtles
end

to set-label
  ask patches [
    ifelse count turtles-here > 0 
    [set plabel count turtles-here]
    [set plabel ""]
  ]
end

to move-h2o-molecules
  ask h2o-molecules [
    let dice random 1000
    let change (dice - 1)
    forward 2
    set HEADING (HEADING + change * 2)
  ]
end

to go 
  setup-patches
  move-h2o-molecules

  ask turtles [rt random 1
    fd 0.3]
  set-label

  tick
end

Thanks for your time and patience. Regards,

Upvotes: 1

Views: 592

Answers (1)

bergant
bergant

Reputation: 7232

Using turtles-here

You don't need to ask patches for turtles-here (as you did to set patches labels). The function runs as well if called by a turtle (and is more efficient when there are more patches than turtles). But take care to use other turtles-here if you don't want to include the calling turtle.

Combine procedure

If you declare a turtle variable after your breed declaration:

h2o-molecules-own [
  turtles-inside
]

(set the variable value inside your create-h2o-molecules)

and your combination limit max-inside as a global variable (use slider widget with 5 as default value) then the combine procedure can look like:

to combine  ;; turtle procedure
  ; take one turtle from the same patch as a target
  ; which has turtles-inside low enough to combine with
  let target one-of other h2o-molecules-here with 
    [turtles-inside <=  max-inside - [turtles-inside] of myself]     
  if target != nobody                        
  [ 
    set turtles-inside turtles-inside + 
      [turtles-inside] of target            ;; increase turtles-inside 
    ask target [ die ]                      ;; kill the target
    set size sqrt turtles-inside            ;; increase size
  ]  
end

Stop

You can stop the simulation by

if not any? h2o-molecules with [turtles-inside < max-inside] [ stop ]

Comment

The condition used to select the target turtle is using turtles-here, other and the maximum constraint which is compared to the sum of turtles inside the target and turtles inside the calling turtle (using myself function).

Upvotes: 1

Related Questions