How to make an agent-set move based on an if condition?

316 views
Skip to first unread message

Souzan Karsha

unread,
Sep 7, 2022, 12:14:52 PM9/7/22
to u...@northwestern.edu, netlog...@googlegroups.com
Hello,

I am very new to Netlogo and having hard time fixing my below code

If [patch- elevation] > 0 and <  730 [ ask turtles [ 
 fd. 1
Set [pcolor] of patch-here 1 red
Set [ patch safety level] of patch- here -1]
]
I am trying to make my agents move within a specific patch elevation range and when they move the patch they are on and the one they move to change Color and change safety variable level.

Please help me how to make this code work



Thanks 

Pradeesh Kumar K V

unread,
Sep 8, 2022, 10:32:48 AM9/8/22
to netlogo-users
You could try the following

ask turtles [
let x [patch-elevation] of patch-here
If x > 0 and x <  730 [
 fd 1
set [pcolor] of patch-here red
set [ patch-safety-level] of patch-here -1]
]

(please verify syntax)

--
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/CAL8q_10M%2BRcTTdRVdPStXDc-kteXipEBdMXTzB4-V_bGiz3r8w%40mail.gmail.com.

James Steiner

unread,
Sep 8, 2022, 1:19:26 PM9/8/22
to Pradeesh Kumar K V, netlogo-users
You can not use set on [ xxx] of

A turtle can directly use and set the variables of the patch it is standing on..


Ask turtles [ set pcolor white ]

What is patch-elevation? 

Please also explain what you intend to do, then we can tell you how to do that. 

Here we can only guess at some of it.

And we are very good at guessing, we can always use extra help.


Stephen Guerin

unread,
Sep 8, 2022, 4:10:13 PM9/8/22
to James Steiner, Pradeesh Kumar K V, netlogo-users
Souzan,

Building on James's response, note that a turtle has direct access to (inherits) the variables of the patch it occupies. It's why patch variables are prefixed with "p" so as not to conflict with the turtle's own variables.
  • pxcor
  • pycor
  • pcolor
  • plabel
  • and any user defined patch variables (presumably "patch-elevation" or "safety-level" that you defined in "patches-own")
For example, to paint the patches to the color of a turtles as it moves could be done with the statement:

ask turtles [set pcolor color]

or if you wanted a turtle to randomly move to patches within your elevation bounds you could do something like:

ask turtles [
  right-turn random 45   left-turn random 45
  forward 1
  if elevation < 0 or elevation > 760 [forward -1]
]


--- -. .   ..-. .. ... ....   - .-- ---   ..-. .. ... ....
Stephen...@Redfish.com
1600 Lena St #D1, Santa Fe, NM 87505
office: (505) 995-0206  mobile: (505) 577-5828   
tw: @redfishgroup  skype: redfishgroup
zoom.redfish.com


James Steiner

unread,
Sep 8, 2022, 11:56:08 PM9/8/22
to Souzan Karsha, Steve Guerin, Pradeesh Kumar K V, netlogo-users
Ok!

So one idea that may help is that most things happen one at a time, and that there's an idea of "what agent" is running any particular piece of code..

So if you're using a patch variable, then a patch or a turtle needs to be running the code.

Also, some folks seem to get lost around ask turtles, and forget that if code is running "inside" an ask turtles code-block, thats a turtle running the code, so there's no need to ask turtles again ... That would be one turtle asking all the turtles to do something... 

So, you might do something like


To move-turtles
;; Comments help you and your helpers understand your intent
;; Writing the steps out in human language helps you be sure you know what you are doing--that it makes sense 
;; It helps helpers to let you know if your intent was fulfilled by the code

;; Every turtle does this
;; If the current patch elevation is not within a certain range,
;; The turtle does not move
;; The turtle moves forward 1
;; The turtle turns the new current patch red
;;.the turtle subtracts 1 from the patch safety-level

Ask turtles 
[
  If elevation > 0 and elevation < 730
[
   Jump 1
   Set pcolor red
   Set safety-level safety-level - 1
]
End

This is probably not what you actually intend

Maybe you mean for the turtle to not just stop but turn away from the elevations that are out of bounds.

If that's the case you need to think hard and write out exactly what you want to happen (in human language terms, like you are teaching a child how a game is played) 

Once you have that figured out, you can try to code it.


On Thu, Sep 8, 2022, 9:25 PM Souzan Karsha <souzan...@gmail.com> wrote:
Hello, 

Thanks James and Stephane for your responses.

I am trying to make turtles move with a constrained elevation range (patches in my model have two variables patch-safety-level and patch elevation)
so what I am trying to do is:
to first make turtles move with a constraint patch elevation range
then make that turtle change its pcolor
then reduce safety-level of its patch

Thanks once again and really appreciate your help


Souzan😊



Michael Tamillow

unread,
Sep 9, 2022, 11:21:40 AM9/9/22
to James Steiner, Souzan Karsha, Steve Guerin, Pradeesh Kumar K V, netlogo-users
This is a very helpful thread, I always appreciate James comments!

Like James said, you may want to write it out in human terms. Often times this is the hardest part, the semantics of a program. It is where a lot of “lost in translation” happens.

Interdisciplinary science hinges on communication of esoteric topics from competent individuals.

Sent from my iPhone

On Sep 8, 2022, at 10:56 PM, James Steiner <grego...@gmail.com> wrote:



Souzan Karsha

unread,
Sep 9, 2022, 12:27:34 PM9/9/22
to stephen...@redfish.com, James Steiner, Pradeesh Kumar K V, netlogo-users
Hello, 

Thanks James and Stephane for your responses.

I am trying to make turtles move with a constrained elevation range (patches in my model have two variables patch-safety-level and patch elevation)
so what I am trying to do is:
to first make turtles move with a constraint patch elevation range
then make that turtle change its pcolor
then reduce safety-level of its patch

Thanks once again and really appreciate your help


Souzan😊



On Thu, 8 Sept 2022 at 14:10, Stephen Guerin <stephen...@redfish.com> wrote:
Reply all
Reply to author
Forward
0 new messages