Search code examples
simulationnetlogomodeling

How to make Turtles attract with a specific color?


I'm playing around with Netlogo, and I've assigned at least two colors for two different groups. These two groups represent two different Stereotypes - represented by Blue and Red.

What my goal is : I need a 'RED' to flock with all the other reds, and the blue to flock with all the other blues.

Here's my current code.

breed [ firstgroups firstgroup ]
breed [ secondgroups secondgroup ]

turtles-own[
  groupmates 
  nearest-neighbor
]

to setup
  clear-all
  crt population-blue
  [ set size 1.5
    set shape "person"
    setxy random-xcor random-ycor 
    set color blue
   ]
  crt population-green
  [ set size 1.5
    set shape "person"
    setxy random-xcor random-ycor 
    set color green
   ]
  reset-ticks
end

to go
  ;;ask turtles [ group ]
  ask turtles [ fd 1 ]
  tick
end

;;to group
;;  ifelse (

Solution

  • turtles-own[
      groupmates ; unused
      nearest-neighbor
    ]
    
    to setup
      clear-all
      setup-globals
      create-firstgroups population-blue ;use your breeds
      [ set size 1.5
        ;set shape "person"
        setxy random-xcor random-ycor 
        set color blue
       ]
      create-secondgroups population-green
      [ set size 1.5
        ;set shape "person"
        setxy random-xcor random-ycor 
        set color green
       ]
      reset-ticks
    end
    
    to go
      ask turtles [ group ] ;use `group`
      ask turtles [ fd 1 ]
      tick
    end
    
    to group ;turtle proc
      ;let _partner min-one-of other breed [distance myself] ;form subgroups
      let _partner one-of other breed ;form big gropus
      set heading mean-heading (list heading towards _partner)
    end
    
    to-report mean-heading [ headings ] ;by Bryan Head
      let mean-x mean map sin headings
      let mean-y mean map cos headings
      report atan mean-x mean-y
    end