linda
linda

Reputation: 117

how to access an attribute inside an agent in agentset for netlogo?

Here is the problem:

I need to compare a value inside turtles-own, which is best-food, with the other agents in the same agentset but I don't know how to make the agent ask his teammates about this value and I need them to compare with each other' best-food value constantly throughout the simulation. (so that they can obtain the highest value of food)

if it is in Java, it is something like this=

if agent1.best-food > agent2.best-food then agent1 go to agent2

but in netlogo, I can't do it like this. can someone help me?

here is the code:

food_quality = food agent found on World

food_quality_found = the value of food_quality is kept in turtles-own

best-food = each agent save their food-quality-found in best-food; then all agents compare which one has the highest value and this comparison happen in an agentset.

ask myteamset [ set food-quality-found food-quality
set best-food food-quality-found set location patch-here set loca-best-food location]

ask myteamset[

            if best-food != 0 ; I need them to compare constantly so "!=0" need to be substitute with his teammates' best-food, but I don't know how to ask other teammates in same agentset about this value
            [

if (best-food < food-quality-found) ;to make sure best-food always have the highest value. [set best-food food-quality-found set location patch-here set loca-best-food location]

          if best-food > best-food ;

this is not correct logically because all of them compare their own best-food value, but I have no idea on how to ask other turtles their best-food and compare it with mine.. [

              set g random 100
              if (g >= probability_teammates_to_go) 
             [ move-to loca-best-food]]  

if (patch-here is loca-best-food) ;how to write if with patches? [set i random 100 if (i >= probability_to_ignore) ;after agent arrives at best-food, they still need to choose whether or not they want to stay there. [ fd 0.25 ]

I have tried let best-food-turtle (turtles with-max [food-quality-found]) but it doesn't compare the value with all his teammates, rather just to this one specific agent, which is wrong...

Thank you in advance.

Upvotes: 1

Views: 1842

Answers (1)

JenB
JenB

Reputation: 17678

Here is a solution to a simplified version of your problem. I have ignored all the different types of food variables and just assigned each turtle a random value for a variable called food. Each turtle is also a member of either the red or blue team (called R and B respectively). They find the member of their team with the highest value of food, create a link with that turtle and face that turtle.

The important primitive here is max-one-of, which identifies the agent within the specified agentset (in this case, all the turtles in the same team) that has the highest value of the specified variable (in this case, food).

turtles-own
[ food
  team
]

to testme
  clear-all
  create-turtles 10
  [ setxy random-xcor random-ycor
    set food random 100
    set team one-of [ "B" "R" ]
    ifelse team = "B"
    [ set color blue ]
    [ set color red]
  ]
  ask turtles
  [ let best-teammate max-one-of turtles with [team = [team] of myself] [food]
    if self != best-teammate
    [ create-link-to best-teammate
      face best-teammate
    ]
  ]
end

One you have identified the turtle with the highest value, you can ask ask that turtle for its patch (using [patch-here] of ...). If you just want the value, then you want max rather than max-one-of.

Upvotes: 4

Related Questions