Search code examples
netlogoagents

How can I create territories for several groups of agents in netlogo?


I'm very new to Netlogo, and this is my very first post in a forum. I need to create animal agent groups which move in habitats or territories. The territories can overlap. More precisely I need male agents that move in different territories and several groups of female agents which move in other territories.

This is what I did so far. I created a territory just for one group of females and one group of males. Thats not exactly what I need.

    globals [fragments] 
breed [preys prey] 
breed [femaletigers femaletiger] 
breed [kids kid] 
breed [maletigers maletiger] 

turtles-own 
[ 
  energy 
  age 
  gender 
  territory 
] 

to setup 
  clear-all 
  setup-fragments 
  ;setup-habitats 
  setup-turtles 
  reset-ticks 
end 

to setup-fragments 

  ask patches[set pcolor 67] 
  repeat 50 
  [ 
    ask one-of patches 
    [ 
      set pcolor brown 
      repeat 30 
      [ 
        let a random 360 
        let b random 5 
        ask patch-at-heading-and-distance a b 
        [ 
          ask neighbors [ set pcolor brown] 
          set pcolor brown 
        ] 
      ] 
    ] 
  ] 
end 

to setup-turtles 

  set-default-shape femaletigers "default" ; default shape (dreieck) 
  create-femaletigers 10 
  [ 

  set color red 
  set size 1.5 
  set energy 100 
  set age random 20 
  set gender "female" 
  set territory patches-in-territory patch 10 6 15 
  move-to one-of territory with [pcolor = 67 ] ; tigers start in territory but not on fragmented areas 
  ] 

  set-default-shape maletigers "default" ; default shape (dreieck) 
  create-maletigers 10 
  [ 
  set color blue 
  set size 1.5 
  set energy random 100 
  set age random 20 
  set gender "male" 
  set territory patches-in-territory patch 40 15 10 
  move-to one-of territory with [pcolor = 67 ] ; tigers start in territory but not on fragmented areas 

  ] 


  set-default-shape preys "circle" 
  create-preys 100 
  [ 
  move-to one-of patches with [pcolor = 67] ;preys don't start in fragmented areas 
  set color 114 
  set size 0.75 
  ] 
end 

to-report patches-in-territory [Center rd] 
  let ptr [] 
  ask Center [set ptr patches in-radius 20] 
  report ptr 
end 

I'm thankful for any help. Maria


Solution

  • welcome to Stack Overflow. When posting on here, in general you will be better served by restricting your code to just the bare minimum needed to demonstrate your issue (see the MCVE guidelines here); for example you could remove your to-report, prey breeds, etc just to make it very obvious what you're trying to solve.

    I'm not following your code exactly as I'm not sure of some of your overall goal- instead I give an alternative example that will hopefully illustrate one way to accomplish what you're after. I'll show an example of territories that can overlap for females in this example and of territories that cannot overlap, for males in this example. Using these variables and setup:

    breed [ femaletigers femaletiger ]
    breed [ maletigers maletiger ]
    
    turtles-own [ territory ]
    patches-own [ maleclaimed? ]
    
    to setup
      ca  
      ask patches [ 
        set maleclaimed? false
      ]
    
      create-maletigers 3 [
        set shape "triangle"
        set size 1.5
        move-to one-of patches with [ maleclaimed? = false ]
        pd
        set territory patches in-radius 5 with [ maleclaimed? = false ]
        ask territory [ 
          set maleclaimed? true
        ]
      ]
    
      create-femaletigers 3 [
        set shape "square"
        setxy random-xcor random-ycor
        pd
        set territory patches in-radius 3 
        hatch 1 + random 3 [
          rt random 360
          fd 1
        ]
      ]
    
      reset-ticks
    end
    

    In the setup, the patches-own boolean indicates if a patch has been claimed by a male- set it to false to start so that a male can check, as it sets up its territory, whether a patch is already claimed or not. Tigers then set up their territories similar to how you did, except males will not select territory from another male. Females set up their territory then hatch a few more females that will share the territory of the "mother".

    To move within their territory, just restrict turtles such that they can only move to one of their territory patches- here is one way to do that:

    to go  
      ask turtles [
        let target one-of territory in-radius 1.5
        if target != nobody [
          face target
          fd 1
        ]
      ]
      tick
    end
    

    If you want to confirm that the turtles stay within a territory, run something like:

    to territory-check
    
      ask turtles [
        let col color + 2 + random 3
        ask territory [
          set pcolor col
        ]
      ]    
    
    end