Search code examples
netlogoagent

How to specifically change a variable of an agent in the agentset?


I am trying to change a variable (score) of a particular agent in the agent set if it meets the specific condition of a patch. This is called by an another agent. To be more clear. My idea is for instance if there is a breed (horse) and it sees the patch (grass) and it is standing on another breed (vertices - since horse move along a path connected by nodes represented by vertices) - a score variable to added to vertices-own where if the grass quality <=3, it would add a score to the vertex on which it stands.

ask horses[
ask patches in-cone 50 60 [                                       
        if grass-quality <= 3 ask vertices with [min-one-of vertices in-radius 0 [distance myself] [set vertex-score vertex-score + 1 ]]]]

I know something is wrong with this code logic. I am trying to convert my mentioned thought into codes. Kindly suggest me.

Thank you all.

Regards,

Heng wah


Solution

  • NetLogo agent (turtle) positions are continuous numbers so it is generally wrong to try and say something like 'if another turtle is where I am'. While you may have got there using move-to, it's probably safer to have the horse identify a vertex that is very close to it rather than in the exact position. You have used radius 0 but I'm going to change that to 0.001 to allow for potential errors in position.

    ask horses
    [ if any? patches in-cone 50 60 with [ grass-quality <= 3 ]
      [ let my-vertex min-one-of vertices in-radius 0.001 [distance myself]
        ask my-vertex
        [ set vertex-score vertex-score + 1 ]
        ]
      ]
    ]
    

    This is not tested, but I have simply reorganised your code. You had some bracketing issues and you were also asking vertices to find the closest vertex (which would have been itself), rather than having the horse find the closest vertex.

    It's also not necessary to separate the let and the ask but I thought that would be easier for you to see how it works.