0

My program is about :

A team of turtles walk around the world to find the best food source and go there.

I have a problem in storing a specific patch (which is the best food location) in my code. I have tried to use patch-here but I can't store the exact patch that I want

here is the code:

turtles-own
[ myteamset
  food-quality-found    ;the quality of food that the turtle found
  location              ;the location of food-quality-found
  best-food             ;the best food available
  location-best-food    ;the location of best food
]

patches-own
[ food-quality          ;the food source]

Here is the code

foreach teamNumbers [
    tn ->
    ask turtles with [ teamID = tn ] [
       if myteamset != nobody [
       ask myteamset [

          set food-quality-found food-quality
          set location patch-here
          ;face location
        ]
          if food-quality-found != 0           ;after all turtles found food, thus comparison can be done
          [ 
          let x1 max [food-quality-found] of myteamset   ; max for group
          set location-of-best-food [patch-here] of location  ;problem ???
            set best-food x1
          move-to location-of-best-food      ;turtles to go to the best food location
          face location-of-best-food      ;turtles stay there
        ]
        ]
      ]

This is how my world looks like, purple patch is for nest, others are food.

Each food has different food value.

I need help in storing the value of location-of-best-food as I can't use patch-here to store the location. The error is I keep on saving the location where turtles found their food, instead of the location best food.

Thank you for your time.

I have edited my question as I have understood what's @JenB was trying to say (thank you for explaining)

linda
  • 117
  • 9

1 Answers1

1

You have way too many questions in this, some of which are conceptual issues. You need to deal with one at a time, resolving that issue before moving to the next. If you get stuck on any one question, you can ask here (showing what you have tried to do and explaining what it is doing instead of what you want).

I think the best first step is to focus on storing the location of the best food. But first you need to sort out your global and turtle variable problems.

I think you are confused about what sorts of things are global variables. A global variable is one for which the value is identical for all model entities. It is clear that teammembers and nearest-teammates are different for each turtle. Thus, they must be stored at the turtle level, as turtles-own variables. This is also what you should be using for best-food.

You also appear to have far too many identifier type variables in your turtles-own variables list. All you need is a team-id then you can identify team members by simply finding turtles with the same team-id. If you do that a lot, then store the agentset of such turtles in the variable teammates so that you don't have to keep on creating the same agentset.

Your process should be something like:

  • each turtle moves around and, if it locates food, checks if the food it finds is better than the best food it knows about
  • if better, store the location (which is a patch not a pair of coordinates) and quality of that food
  • if teammates can communicate immediately, it also tells the other teammates the food quality and location
  • if teammates only communicate when in contact or similar, then when those conditions are met, the turtles check with their teammates whose food is better and change their food location and quality to their teammate's information if appropriate.

Note that this only finds the best food, it doesn't keep track of other food that turtles have found. The location of the other food will be forgotten as soon as better food is found. You will need to do lists to keep track of all food, but you should definitely not do this until you have the best food version working correctly.

As a general rule when starting out with NetLogo, if you are using identifiers (or who values) for individuals in your code, you should probably not be. If you are using foreach, you should probably not be. In both cases, you want agentsets instead.

You have almost got there with this code. You line set location patch-here is using the agentset patch-here instead of an identifier.

Having had a look at your previous question at How to make a turtle able to save other turtles ID in Netlogo?, it is clear that you have previously been advised to use agentsets for this model. I have added an answer to show you how to do this. You also simply copied the code provided there without understanding it, the answer to your previous question also points out the difference between global and turtle variables.

JenB
  • 17,620
  • 2
  • 17
  • 45
  • Ohhh~!, yes! yes! I understand what you are saying about teammates and nearest teammates, since every turtle stays at diff location, the value of nearest teammates should be diff from each other,thus they should be kept inside `turtles-own`; same goes to best-food since all turtle has diff value of the best food found. Thankss, I have put it in `turtles-own` before but my code didn't work, so I thought I must be doing something wrong (blame me, it was my weakness that I did not understand the concept of global and local variable). – linda Nov 07 '18 at 17:43
  • Yes, I agree that I have too many identifiers (as in variables in `turtles-own`, correct me if I'm mistaken this). You are right about all these turtles need is the same teamID (to make them into the same agentset) to be included in the same team. But how can I list down the agentset of the same teamID besides inspect each turtles? **If you do that a lot, then store the agentset of such turtles in the variable teammates so that you don't have to keep on creating the same agentset** -can you explain this sentence? Thank you. – linda Nov 07 '18 at 17:56
  • From the part,process should be:... I need to set best-food value first,then compare, if food-found > best-food, agent will save best-food and location, then communicate with others to tell this news (Is my understanding correct?) As you stated in the first paragraph, I need to know how to store the best-food location, but if the best-food location can be stored after all of the agents find food and compare the food value with each other, I'm stuck at comparing values through agentset. Is it possible to compare although they didn't meet each other? – linda Nov 07 '18 at 18:07
  • I'm sorry, this become a long comment section, but thank you for your time explaning this to me. (and my english is bad, I need time to understand things, thank you, thank you for your patience) – linda Nov 07 '18 at 18:08
  • The sentence that you want explained - every time you create an agentset, it takes computer time so you should store them in variables if you are going to use them a lot instead of creating each time. That's what I did in the answer to the other question, I created the team member agentset for each turtle and stored it in a turtle owned variable – JenB Nov 07 '18 at 21:01