1

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.

linda
  • 117
  • 9
  • I am not clear what you are trying to do, but if what you want is the highest value of a variable over all the agents in an agentset, then that is found with `max [variable-name] of agentset-name`. And if you want to identify the turtle with the highest value so you can move to it, you need the primitive `max-one-of`. – JenB Jul 25 '19 at 11:44
  • what I'm trying to do: each turtles will roam around and find food, then they save the food's value in their best-food, which is one of turtles-own, after that, they will compare the value of best-food with the other agents, if the value of other agent is higher, than, they will go to the patch where has the highest food value. – linda Jul 25 '19 at 11:58
  • meaning that, `let best-food-turtle (turtles with-max [best-food])`, then from here, I, how can I compare the best-food? and find the location? Is `if max-one-of myteamset [count best-food] > best-food`, what should I do to get the location? – linda Jul 25 '19 at 11:59

1 Answers1

4

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.

JenB
  • 17,620
  • 2
  • 17
  • 45
  • I'm sorry, I totally forgot to mention, my lecturer ask me not to use link at all because I've tried it before, and he ask me use other beside link, (which is a big problem, I don't know how to do it without link...) – linda Jul 25 '19 at 14:11
  • This answer does not use `link` to identify the agent, it uses link simply for display purposes to show who each agent thinks the best team member is. Run the model and try to understand what each line is doing. – JenB Jul 25 '19 at 14:18