1

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

skivvy
  • 57
  • 4

1 Answers1

2

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.

Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37
  • Thanks so much for responding to so quickly @NicolasPayette! This works perfectly. I knew that the issue had something to do with the fact that the `chosen-bus-stop` didn't have access to the `job` variable but couldn't work out how to express it differently. I'm guessing here the `let my-job job` makes the `job` variable accessible (via `my-job`) to other breeds (e.g., the `chosen-bus-stop`). Is that right? – skivvy Sep 19 '19 at 12:46
  • 1
    Well, in practice, yes, it has that effect. But all it's really doing is creating a local variable that is then accessible anywhere in the procedure (independently of context). There is nothing more to it than that. – Nicolas Payette Sep 19 '19 at 14:03