Help with agent movement by their owner agents's decisions

83 views
Skip to first unread message

vidus...@gmail.com

unread,
Mar 8, 2021, 2:32:15 AM3/8/21
to netlogo-users
Hi All 

I am trying to make farmer agents move their loads (herds) to selected target locations. Each farmers may own random number of loads ranging between 1 -10. The farmers select number of targets same as their number of loads. Loads originate from farmer's home and move to target sites that farmers have chosen. Each load has a variable distance-travelled and each farmer has a variable  total-distance-travelled. My aim is to calculate sum of distance-travelled by each load of individual farmers under total-distance-travelled and patch coordinates for visited patches. 
 
I am using the following code. 

Breed [ loads load ]
breed [ farmers farmer]

farmers-own [visited-list my-loads num-loads my-targets my-home total-distance-travelled ]
loads-own [load-id origin current-destination distance-travelled]
patches-own [ my-targets? target-cell? resource my-agent ]


to setup
  ca
  ;;;;;;;;;;;;;;;;;;; setup farmers ;;;;;;;;;;;;;;;;;;;;;;;;;;;

  create-farmers num-farmer ; crt agents based on slider

[
    set shape "person"
    set color random 50
    set size 1.5
    set my-targets no-patches      ; no targets initially
    setxy random-xcor random-ycor
    set my-home patch-here         ; agent sets current-patch as my-home
    set num-loads random 10 + 1    ; number of loads starting from one instead of zero
    set total-distance-travelled 0       ; initial travel distance is zero
    set visited-list [ ]
    pen-down

    ;;;;;;;;;;;;;;;;;; setup loads  ;;;;;;;;;;;;;;;;;;;;;;;

    hatch-loads num-loads  ; farmers create loads same as their value of num-loads variable
    [ set load-id [ who ] of myself ] ; load variable that identify load-owner
   set my-loads loads with [ load-id = who ] ; farmer variable that identify farmer's loads
   ask loads [
      pen-down
      set shape "box" set size 1
      set origin [ my-home] of farmers with [ who = [load-id] of myself] ; setting origin patch as farmer's home patch
    ]
  ]
   ;;;;;;;;;;;;;;;;;; setup patches  ;;;;;;;;;;;;;;;;;;;;;;;

Ask patches [
    set resource random 9
    set my-agent nobody
    ifelse resource = 0
    [ set target-cell? false ]
    [ set target-cell? true ]
  ]

  reset-ticks

end

to go
  if ticks > 365 [ stop ]
  ask farmers [
    find-my-targets
    move-myloads
    calculate-total-distance-travelled
      ]
    tick
end

to find-my-targets ;; turtle procedure
  ask farmers [
    set my-targets up-to-n-of num-loads patches with [target-cell? ] 
    set visited-list fput my-targets visited-list 
     move-myloads
  ]
     
end

to move-myloads
  ask my-loads [
    let old-xcor xcor
    let old-ycor ycor
    move-to my-targets
    set current-destination my-targets
    set distance-travelled distance origin
  ]
end 
  
to calculate-total-distance-travelled 

end


It does work but it stores my-targets and visited-sites as number of patches e.g. 

show [my-targets] of farmers
[(agentset, 3 patches) (agentset, 5 patches)]

show [visited-list] of farmers
[[(agentset, 5 patches) (agentset, 5 patches)] [(agentset, 3 patches) (agentset, 3 patches)]]

How can I change it so that it stores patch coordinates rather than number of patches? 

It is super confusing please help 🙏!!

Kind Regards

Vidushi

wade.s...@gmail.com

unread,
Mar 8, 2021, 8:59:03 AM3/8/21
to netlogo-users
Vidushi, 

I believe you are combining "agent-sets" with "lists", and ending up with lists of lists of agent-sets which isn't really what you want.
What's also confusing is the way NetLogo displays and prints and shows an agent-set and a patch.

I attached a tutorial model
  Either make a "setup" button or just type "setup" in the command-center to run it.

Note: I added "mylist" to the globals,  and added two tiny utility procedures,
  (1) printcoords simply is shorter and clearer than  "print (word "(" pxcor "," pycor ")" )" or whatever it is you wanted to do with these coordinates when you get them, which you didn't specify.
  and
(2) "make-a-list-from" utility that converts an agentset into a list.
 
If you walk through it line by line and read the comments you can see what it's doing and how it's doing it.

I hope this helps!
Wade
lists if agentsets.nlogo

Vidushi Patel

unread,
Mar 9, 2021, 1:05:51 AM3/9/21
to wade.s...@gmail.com, netlogo-users
Thanks a lot Wade. This was helpful. I actually got answers for some of my other list related questions. 

I actually wanted to let the agent remember what patch coordinates in sequence e.g. in a year if an agent moves say 7 times its mylist records those 7 patch coordinates and also calculates travel distance between its current position and previous patch. 
I am still working out how to do that. 

Please share your idea if anyone has done this before.  

Thank you 

Kind Regards

Vidushi


--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/92d19f1f-1799-4d66-8546-1a586e498293n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages