Help with "sort-by"

128 views
Skip to first unread message

vidus...@gmail.com

unread,
Apr 9, 2021, 1:55:18 AM4/9/21
to netlogo-users
Hi All 

I want my agents to choose target based on distance from the list of my-target variable. 
I want to do the same for richness variable. 
turtles-own [ my-target my-target-list closest-target rich-target ] ;; defines agent variables

patches-own [  potential-target?  ]
turtles-own [ my-target closest-target rich-target ]

to pick-target

 set my-target ( list patches with [ potential-target? = true ])
 
set closest-target sort-by [ (distance myself) ?1) > (distance myself) ?2)]  my-target
set rich-target sort-by [ richness ?1 > richness ?2 ] my-target

end 

It gives error saying expected closing bracket (yellow highlighted). I tried different bracket settings with' distance myself' but stil give me the same error.  What am I doing wrong here ?

I think I am not clear with how to use sort-by  and when to use it with anonymous procedure? 

Can anyone help me plase? 

Kind Regards

Vidushi


Aaron Andre Brandes

unread,
Apr 12, 2021, 11:02:58 AM4/12/21
to vidus...@gmail.com, netlogo-users

Hi Vidushi,

I am including below some code that I think will be helpful.

I think you need to think through some of what you are doing (unless you have implemented it code you didn’t show)

In particular as you have it set up potential-target? is a patch variable, so my-target will always be the same agent-set, and rich-target will always be one of the patches that has the highest value of richness.

 

Here are a few more suggestions:

You seem to be using an older syntax for anonymous functions. Maybe you copied a old example.  

 

I'd recommend downloading and installing NetLogo 6.2.0, our most recent version

and that you look commands up in the corresponding NetLogo Dictionary

 

I think you should pay attention to the differences between lists and agentsets, and which are acceptable as input or output for a given command.

Some commands operate on both, but some do not.  Note that when you sort an agent set you get a list.

 

The NetLogo Programming Guide is a good place to turn for concepts such as lists and agentsets.

 

Here is some code that does what I think you are trying to do.

 

patches-own [  potential-target?  richness ]

turtles-own [ my-target closest-target rich-target ]

 

to setup

  clear-all

  create-turtles 5 [fd random 10]

  ask patches [ set richness random 100 set potential-target? one-of [true false]]

end

 

to go

  ask turtles [ pick-target

    type "closest patch" show closest-target

    type "richness " show [richness] of rich-target

    type "richest patch" show rich-target

    rt ( random 60) - 30 fd 1 ]

end

 

to pick-target

  set my-target ( patches with [ potential-target? = true ])

 

  set closest-target max-one-of my-target [ distance myself]

  set rich-target max-one-of my-target [ richness]

end

 

Aaron

-- 

Aaron Brandes, Software Developer

Center for Connected Learning and Computer-Based Modeling

--
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/342601fe-dd28-4a2a-ae61-e466dde793c3n%40googlegroups.com.

Vidushi Patel

unread,
Apr 14, 2021, 10:40:14 PM4/14/21
to Aaron Andre Brandes, netlogo-users
Many thanks Aaron, this is very neat ! 

Actually I was writing three lines to randomly assign true false . Your way is a very neat one set potential-target? one-of [true false]. 
I have downloaded 6.2.0 and looking into the documentation as you suggested.  

Thank you for your help !

Kind Regards

Vidushi

Reply all
Reply to author
Forward
0 new messages