How to build code that accesses the patch variable and makes a decision

153 views
Skip to first unread message

Rafaela Lorena

unread,
May 19, 2021, 2:12:56 PM5/19/21
to netlogo-users
Hi all,

I have a question about building a code below. I want to create a code where the turtles are created and each turtle will have a ray of vision (each turtle will be able to see a certain number of patches). After that I want the turtles to ask which of these patches have pveg > 5 and walk over to them and collect the pveg. When you have no more pveg than fd 150

Can anyone help me progress in the code?

I'm not getting the command to:

  1. They check which patches within their vision range have pveg> 5 and then move in sequence to these patches and consume the pveg

  2. which code can be used for turtles to consume pveg (patch variable)


turtles-own
[
  vision
  vision-patches
]

patches-own [ pveg ]

to setup    
 ask turtles [
    set vision random-in-range 1 2 
    ;show vision
  set vision-patches []
  foreach ( range 1 ( vision + 1 ) ) [ n ->
    set vision-patches sentence vision-patches ( list ( list 0 n ) ( list n 0 ) ( list 0 ( - 
 n ) ) ( list ( - n ) 0 ) )
  ]
    move
 ]
end

to-report random-in-range [ low high ]  
  report low + random ( high - low + 1 )
end


to move
  let move-vision patch-set patch-here (patches at-points vision-patches) with [not 
  any? turtles-here])
  let possible-patches-pveg move-vision with-max [pveg]
  if any? possible-patches-pveg [
    ;; if there are any such patches move around
    fd 150
  ]
end

Every help is welcome! Thanks

vidus...@gmail.com

unread,
May 19, 2021, 10:15:33 PM5/19/21
to netlogo-users
Hi Rafael

1. Maybe creating an agentset something like potential-patches should work. 

let potential-patches patches with [pveg > 5]

2. You can have a turtle variable e.g. energy  or something similar and you can code it as with each tick pveg decrease the same amount as the increase in turtles energy variable. 

May be something like    ask turtles [ set energy energy + 1 set pveg pveg - 1 ]  should work. 

Hope this helps. 

Vidushi

Murat Yildizoglu

unread,
May 19, 2021, 10:42:44 PM5/19/21
to Rafaela Lorena, netlogo-users
Dear Rafaela,
Would the attached example file help you (if I have correctly understood your question?


patches-own [pveg]
turtles-own [pveg-eaten]

to setup
  clear-all
  reset-ticks
 
  create-turtles 1 [set pveg-eaten 0]
  ask patches [ 
    set pveg random max-pveg-dist
    if pveg >= pveg-limit [set pcolor green]
  ]
  
end

to go 

ask turtles [
  
    let pveg-patches patches with [pveg >= pveg-limit and distance myself < range-vision]
    while [count pveg-patches > 0] [
      move-to one-of pveg-patches
      set pveg-eaten pveg-eaten + pveg
      set pveg 0
      set pcolor red
      set pveg-patches pveg-patches with [pveg > 0]     
      
    ]
    
  ]
  
  tick
end
eating-turtle.nlogo
PastedGraphic-6.tiff

Rafaela Lorena

unread,
May 20, 2021, 10:12:47 AM5/20/21
to Murat Yildizoglu, netlogo-users, vidus...@gmail.com
Hi all,

Thank you very much Vidus and Murat Yildizoglu for the suggestions. They helped a lot.

But, I made some modifications following the Murat suggestions and this error appeared:

"The> = operator can only be used with two numbers, two strings, or two agents of the same type, but not with a number and TRUE / FALSE.
error while patch -16 16 running> =
  called by procedure GO-MURAT
  called by 'go-Murat once' button "

I attached the code in .nlogo.

Anyone who can help me resolve this error ... Just to remember that I am trying to code with some modifications the following:

1) They check which patches within their vision range have pveg-values > 0.4 and then move in sequence to these patches and consume the pveg-values

2) Which code can be used for turtles to consume pveg-values (patch variable)

Thanks a lot!

--
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/CAHs8kB_YBeCB4vtFC2iKABiGMVGg2QuZe%2BHh8J0wq_B0JuYwTw%40mail.gmail.com.



Prof. Murat Yildizoglu

Advisor to the Minister of Education, Youth and Sports of Cambodia
Expertise France





On temporary leave from 
University of Bordeaux


#80 Preah Norodom Boulevard
Phnom Penh
Royaume du Cambodge
Telegram: +855 95 732 088
http://yildizoglu.fr

Murat_modify_test.rar

Murat Yildizoglu

unread,
May 20, 2021, 10:31:20 AM5/20/21
to Rafaela Lorena, netlogo-users, vidus...@gmail.com
Hi Rafaela,
I put some comments in the code. I don’t understand the signification of vision-patches, but the error comes from the fact that you try co compare this list with a distance.

patch_ accesses _Murat_modify.nlogo
PastedGraphic-6.tiff

Rafaela Lorena

unread,
May 20, 2021, 10:50:15 AM5/20/21
to Murat Yildizoglu, netlogo-users
Hi Murat Yildizoglu and all,

I attached the code with some comments and explaining what the global variable vision-patches are.

Regarding the error > = I thought I was comparing 2 numbers. I think I'm wrong :(

Anyone who can help me resolve this error ... Just to remember that I am trying to code with some modifications the following:

1) They check which patches within their vision range have pveg-values > 0.4 and then move in sequence to these patches and consume the pveg-values

2) Which code can be used for turtles to consume pveg-values (patch variable)

If anyone can help me understand where I am going wrong, I am very grateful  

Thanks

Em qui., 20 de mai. de 2021 às 11:31, Murat Yildizoglu <myi...@gmail.com> escreveu:
Hi Rafaela,
I put some comments in the code. I don’t understand the signification of vision-patches, but the error comes from the fact that you try co compare this list with a distance.

<Murat_modify_test.rar>



Prof. Murat Yildizoglu

Advisor to the Minister of Education, Youth and Sports of Cambodia
Expertise France


Murat_modify_test2.rar

Murat Yildizoglu

unread,
May 20, 2021, 11:14:11 AM5/20/21
to Rafaela Lorena, netlogo-users
Hi Rafaela,

I am sorry, I have put in the first code what I have understood from your description and I do not understand what you want to accomplish when I read your code, with gis etc….

When I check, 

observer> show vision-patches
observer: [[0 1] [1 0] [0 -1] [-1 0] [0 2] [2 0] [0 -2] [-2 0]]

I obtain indeed a list as you can see (with negative numbers, for distance???). You cannot compare such a list to a distance. If any of the scalar elements makes any sens, you should access this element and compare it with the distance, like
 item 1 item 4 vision-patches -> 2

I think you should think a little bit more about what you want to accomplish. 


<Murat_modify_test2.rar>



Prof. Murat Yildizoglu

Advisor to the Minister of Education, Youth and Sports of Cambodia
Expertise France


Rafaela Lorena

unread,
May 20, 2021, 1:25:04 PM5/20/21
to Murat Yildizoglu, netlogo-users
Hi Murat Yildizoglu and all,

I certainly did not express myself well. I'm going to try to better express what I'm trying to code:

I am trying to code a part of the code which is:

1) Each turtle has a radius of vision (a turtles-own called "vision"). The variable "vision" is the distance that this turtle can see in the horizontal and vertical directions.

2) It is created for example 100 turtles

3) If have pveg-value > 0.4 in the patch that the turtle is found (the patch that is under the turtle)
      3.a) the turtles eaten the pveg-values and asking:
            - ate enough? each turtle can eat up to 3 pveg-values
                   - yes, die
                   - no, go to item 4

4) Turtles walk the world based on their radius of vision in search of pveg-values (pveg-values is a patches-own that is a .asc file)
      2.a) If within turtle's viewing range it has pveg-values > 0.4, the turtle randomly accepts / eats 1 of these patches and walks to this patch           
      2.b) If within radius of view the turtle has pveg-values <= 0.4, the turtle walks (fd 150)

I did a snippet of the general code, if I am confused I can try to rewrite it.

I understood what Murat said and made some changes. But still, I haven't been able to solve what I want.  The code is attached.  

If anyone can help with new suggestions or models that have already done this, I want to do it. I am very grateful.
Murat_modify_test3.rar

Murat Yildizoglu

unread,
May 20, 2021, 8:51:14 PM5/20/21
to Rafaela Lorena, netlogo-users
Dear Rafaela,
As far as I understand these given elements, my initial code was already doing all this for one turtle (as an exemple only, to allow you to see what is happening,  the code is agnostic to the number of turtles), except the points 2 (easy to change as I have just told), 3A (only the dying part, very easy to add) and 4b (easy to add).

But it seems that you want to do other things in the code (gis extension, a data file, etc.), I am not sure that the description you have just given completely describes all the elements you need. Hence my suggestion of writing first the objectives and  all the details of your model in words, before starting to code. If you have a clear vision :-) of the details, you should be able to find your way in the programming manual and the dictionary organised by topics. Also You can use the control center iat the bottom of  the Interface to check your code snippets and understand the kind of objects you are creating (like a list instead of a scalar).

I hope this helps a little bit.

Best,

Murat

Hi Rafaela,
<PastedGraphic-6.tiff>



On temporary leave from 
University of Bordeaux


#80 Preah Norodom Boulevard
Phnom Penh
Royaume du Cambodge
Telegram: +855 95 732 088
http://yildizoglu.fr

<Murat_modify_test3.rar>

Rafaela Lorena

unread,
May 21, 2021, 3:11:38 AM5/21/21
to Murat Yildizoglu, netlogo-users
Hi Murat Yildizoglu,

This is great, thank you so much! 

Cheers
Reply all
Reply to author
Forward
0 new messages