simulationdistancenetlogomulti-agent

Distance between two different breeds of agents from the context of a particular agent


In my model there are three breeds of agents; people, bus-stops and workplaces. Each person is assigned a workplace (job) and chooses a bus-stop (chosen-bus-stop) from which to travel to work. I worked out how to find the distance between a particular person and both their job and their chosen-bus-stop using this thread for guidance: How can I compute the distance between two patches?. But now I'm struggling to find the distance between the person's chosen-bus-stop and their job. Any ideas would be much appreciated!

Here is my setup code:

breed [people person]
breed [workplaces workplace]
breed [transit-stops transit-stop]

people-own
[ ownHome
 job
 distance-to-job
 chosen-bus-stop
 distance-to-my-bus-stop
 distance-bus-stop-to-job ]

workplaces-own
[ location
 location-type ]

create-workplaces 1000
 [ set shape "triangle 2"
   set color 12
   set size 0.6 ]

create-people population
 [ set shape "circle"
   set color 4
   set size 0.4
   set job one-of workplaces
   set job-location [location] of job ]

create-transit-stops 800
 [ set shape "flag"
   set color blue 
   move-to one-of patches ]

;; I can work out the distance from a particular agent to their ```chosen-bus-stop``` and their ```job```:*

ask people
 [ set distance-to-job [ distance myself] of job
   set chosen-bus-stop one-of transit-stops with [color = blue] in-radius 9
   set distance-to-my-bus-stop [distance myself] of chosen-bus-stop ]

;; But when I try something similar to find the distance from the bus stop to their job I get this error: TRANSIT-STOPS breed does not own variable JOB*

   set distance-bus-stop-to-job [ distance job ] of chosen-bus-stop  

 ]
end


Solution

  • Try:

    set distance-bus-stop-to-job [ distance [ job ] of myself ] of chosen-bus-stop
    

    Or:

    let my-job job
    set distance-bus-stop-to-job [ distance my-job ] of chosen-bus-stop
    

    The important thing to remember is that (just like ask) the of primitive introduces a change of context. In this case, it means that the reporter block preceding of runs in the context of the chosen-bus-stop and (as the error message is telling us) the bus stop doesn't have direct access to the job variable, which is a people-own variable.