简体   繁体   中英

Netlogo - any one of the value in the list?

I have a basic question. I am making a conditional statement, saying that if any value in the list (list_N_COVID19_p) exceeds a certain value (let's say 1), then a variable that a turtle has would be increased.

The problem is that "one-of" option search for just a random one member of the list, but in my case, it should be indeed "any one of".

"any?" can be used but what I have is a list, not a agent set.

None of the below code works. Could anyone help?

ask turtles [
       ask patches with [centroid? = true and DONG_ID = m] [
       show list_N_COVID19_p
          if any? (patches with ([ list_N_COVID19_p ] > 1) )
       [
       ask myself [
       set SALE_AMNT (SALE_AMNT * 2)
       ]
       ]
       ]
    ]
    ask turtles [
       ask patches with [centroid? = true and DONG_ID = m] [
       show list_N_COVID19_p
          ifelse ( (one-of list_N_COVID19_p) > 1 )
       [
       ask myself [
       set SALE_AMNT (SALE_AMNT * 2)
       ]
       ]
       [
       ask myself [
       set SALE_AMNT SALE_AMNT
       ]
       ]

If you're sure the list will never be empty, you could use max for this:

max list_N_COVID19_p > 1

If any item in the list is greater than 1, then the max will be greater than 1, too.

If you need to account for the possibility of the empty list, then

max (fput 0 list_N_COVID19_p) > 1

would work but at that point the clarity of the code is kind of ruined.

Another solution approach:

not empty? filter [?n -> ?n > 1] list_N_COVID19_p

and another:

to-report any-over? [xs limit]
  ifelse empty? xs
    [ report false ]
    [ ifelse first xs > limit
        [ report true ]
        [ report any-over? (butfirst xs) limit ] ]
end

any-over? list_N_COVID19_p 1

And finally, the most general approach of all would be:

to-report exists? [pred xs]
  ifelse empty? xs
    [ report false ]
  [ ifelse (runresult pred first xs)
    [ report true ]
    [ report exists? pred butfirst xs ] ]
end

exists? [?n -> ?n > 1] list_N_COVID19_p

A language addition proposal in this area is https://github.com/NetLogo/NetLogo/issues/931

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM