In Netlogo, can a list of boolean variables be converted from true/false to 1/0? - boolean

I am creating a list of boolean values that represent a set of patches with the following code, where "obstacle" is a boolean property of the patch that says whether or not it is an obstacle and "map-area" is a list of specific patches.
set cell-walls map [ p -> [ obstacle ] of p] map-area
Then I print cell-walls to a text file, and it will print [ true true false true ... ].
I need it to print [ 1 1 0 1 ... ] instead.
Maybe I can replace elements in the list, or create a new list by iterating over this one and adding a 1 or 0 for each element. I'm not sure how to accomplish this in Netlogo. Help is appreciated! :)

If you only ever have true or false values, you could probably get away with something like:
to boolean-to-numeric
let bool [ true false true ]
let bool2 map [ i -> ifelse-value ( i = true ) [ 1 ] [ 0 ] ] bool
print bool2
end
But do note that the above will report 0 for any values other than true, not just for false.

Related

Netlogo, creating breed at the center of a particular polygon (i.e with id = 123456)

I have a netlogo gis model. the gis shape file consists of building footprint (in the form of polygons). I want to create a breed at the center of particular building with id = "66445345" (polygon id). There is large no of buildings/ polygons but I am only interested in creating the breed at this one polygon
Any ideas how to do this?
breed [blds bld]
set guo-building gis:load-dataset "guo-building.shp"
gis:drawing-color gray
gis:draw guo-buildings 1.0
foreach gis:vertex-list-of guo-buildings[
i ->
let bld-no gis:property-value i "id"
let center gis:centroid-of i
let center-location gis:location-of center
if bld-no = 66445345
[create-blds 1
[
set xcor (item 0 center-location)
set ycor (item 1 center-location)
set color red
set size 5
]
]
]
sorted the problem. Need insert blds-own variable and store id.
breeds [blds bld]
breeds-own [building-no]
to setup-pma-locations
foreach gis:feature-list-of guo-buildings[
i ->
let bld-no gis:property-value i "ID"
let center gis:centroid-of i
let center-coordinates gis:location-of center
if not empty? center-coordinates [
create-blds 1
[
set xcor (item 0 center-coordinates)
set ycor (item 1 center-coordinates)
set color red
set size 0
set building-no bld-no ;store in blds-own variable
]
]
]
ask blds[
let pma blds with [building-no = "66445345"]
ask pma [set color red
set size 5]
]

How to replace all items meeting a given criteria in a list

I've been using this post (how to do replacing-item in use nested list) as a guide to work out how to replace items in a list, that meet a given criteria.
Specifically, I want to replace all zeros in a list with the value = 0.5. However, the code I've come up with only seems to replace the first zero in the list and I can't seem to work out why.
This is my code:
to-report A-new-list-without-zeros [old new the-list]
let A-index-list n-values length the-list [?]
( foreach A-index-list the-list
[ if ?2 = old
[ report replace-item ?1 the-list new ]
])
report the-list
end
And this is what happens:
observer> show A-new-list-without-zeros 0 0.5 [0 1 0 5 5 0]
observer: [0.5 1 0 5 5 0]
Any help would be much appreciated! Thanks
This task is more easily accomplished with map than foreach.
NetLogo 6 syntax:
to-report A-new-list-without-zeros [old new the-list]
report map [[x] -> ifelse-value (x = old) [new] [x]] the-list
end
NetLogo 5 syntax:
to-report A-new-list-without-zeros [old new the-list]
report map [ifelse-value (? = old) [new] [?]] the-list
end
Anytime you use report, it exits the procedure and reports the output at that point. The quick fix using your code is to change the report line in your if statement so that it replaces the item at the current index:
to-report A-new-list-without-zeros [old new the-list]
let A-index-list n-values length the-list [?]
( foreach A-index-list the-list
[ if ?2 = old
[ set the-list replace-item ? the-list new ]
])
report the-list
end
observer> print A-new-list-without-zeros 0 0.5 [ 0 1 0 5 5 0 ]
[0.5 1 0.5 5 5 0.5]

Efficient CSV extraction and allocation

I've tried to find a way to do this more neat/more efficient cause I don't believe that I wouldn't be able to achieve the same result more efficient.
I have a csv file containing data with initial states for my turtles. Each row represents values for turtle-owned variables and each column represents an individual turtle. For each turtle, I would like to initialize his variables. At this moment I'm doing it the following way (which works, but to me looks not efficient):
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GENERAL PROCEDURES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
extensions [ csv ]
breed[households household]
globals ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[
HH-data
HHx-list
HHy-list
HHsize-list
HHcolor-list
]
households-own ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[
HHx
HHy
HHsize
Hhcolor
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SETUP PROCEDURES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to startup ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
set HH-data (csv:from-file "TestHH.csv" ";")
show HH-data
set HHx-list item 0 HH-data
set HHy-list item 1 HH-data
set HHsize-list item 2 HH-data
set HHcolor-list item 3 HH-data
show HHx-list
let numberOfHH length HHx-list
create-households numberOfHH
(
foreach (sort households) HHx-list
[ [?1 ?2] -> ask ?1
[
set xcor ?2
]
]
)
(
foreach (sort households) HHy-list
[ [?1 ?2] -> ask ?1
[
set ycor ?2
]
]
)
(
foreach (sort households) HHsize-list
[ [?1 ?2] -> ask ?1
[
set HHsize ?2
]
]
)
(
foreach (sort households) HHcolor-list
[ [?1 ?2] -> ask ?1
[
set color ?2
]
]
)
end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
However, in the global HH-data already all my data is present. How could I extract this and assign the turtle variables to each turtle without having to make all these individual for-each statements. I tried a few things like this:
set HH-data (csv:from-file "TestHH.csv" ";")
show HH-data
let numberOfHH length (item 0 HH-data)
create-households numberOfHH
(
foreach (sort households) HH-data
[[?1 ?2] -> ask ?1
[
set xcor ?2
set ycor ?2
set HHsize ?2
set HHcolor ?2
]
]
)
But then I keep getting errors.
Found out myself, while I was writing this question, how to solve it.
Thought it might be useful to others and thus therefore to still post it.
set HH-data (csv:from-file "TestHH.csv" ";")
;;show HH-data
let numberOfHH length (item 0 HH-data)
create-households numberOfHH
(
foreach (sort households) (item 0 HH-data) (item 1 HH-data) (item 2 HH-data) (item 3 HH-data)
[[?1 ?2 ?3 ?4 ?5] -> ask ?1
[
set xcor ?2
set ycor ?3
set HHsize ?4
set color ?5
]
]
)

How make a list of cumulative sum in netlogo

How can i make a list of cumulative sum of a other list?
i tried it that way:
;;all temperatrue-values around the turtle saved in list
set temperature_values (list [(output-heat + 1)^ Freedom] of neighbors)
;;build cumulative value of temperatures and put each value in list
let tempsum 0
set tempsum_list []
foreach temperature_values
[set tempsum (tempsum + ? )
set tempsum_list fput tempsum tempsum_list
]
but it doesn't work. can anyone fix this problem? it says that "+ excepted a input but gets a list instead".
your code for a cumulative sum works (except that I think you need lput rather than fput. You can see it with this:
to test
let ll [1 2 3 4]
let tempsum 0
let tempsum_list []
foreach ll
[ set tempsum (tempsum + ? )
set tempsum_list lput tempsum tempsum_list
]
print tempsum_list
end
Did the error highlight the line set temperature_values (list [(output-heat + 1)^ Freedom] of neighbors)? Try putting a space after between ) and ^. NetLogo is picky about space around mathematical operators.
As Jen suggested, you can use foreach. Another nice approach is reduce:
to-report partial-sums [#lst]
set #lst (fput [0] #lst) ;;prepare for reduce
report butfirst reduce [lput (?2 + last ?1) ?1] #lst
end
Similar to Alan's solution (Just an update for the recent version of NetLogo that replaces ? with -> for anonymous procedures.)
to-report partial-sums [lst]
report butfirst reduce [[result-so-far next-item] -> lput (next-item + last
result-so-far) result-so-far] fput [0] lst
end
This is like Alan's solution, just abstracted a bit further. (Perhaps too far, depending on your taste! I like JenB's solution as well.)
Let's first define a thing like reduce, but that keeps all the intermediate results:
to-report scan [fn xs]
report reduce [lput (runresult fn ?2 last ?1) ?1]
(fput (list first xs) butfirst xs)
end
Now we can use it to compute partial sums:
observer> show scan task + [1 2 3 4 5]
observer: [1 3 6 10 15]
but we are also free to swap in a different operation:
observer> show scan task * [1 2 3 4 5]
observer: [1 2 6 24 120]

Testing inequalities on lists

I have tried several different ways using different list primitives, but cannot find a way to test an inequality for each item of a list. For example, my list [1 2 -1 -2] could be tested for > 0, and give [1 2 0 0 ]. Can someone please help with this simple task.
There's a couple ways to do this, depending on the result you want. For your example, you can do:
map [ ifelse-value (? > 0) [ ? ] [ 0 ] ] my-list
map creates a new list by applying the given reporter task to each item of the given list. If you haven't used tasks in NetLogo before, ? represents the argument to the task. So, in this case doing:
map [ ifelse-value (? > 0) [ ? ] [ 0 ] ] [1 2 -1 -2]
basically does:
(list ifelse-value (1 > 0) [ 1 ] [ 0 ]
ifelse-value (2 > 0) [ 2 ] [ 0 ]
ifelse-value (-1 > 0) [ -1 ] [ 0 ]
ifelse-value (-2 > 0) [ -2 ] [ 0 ])
ifelse-value is like ifelse, except that it returns the value in the block that runs.
If, you just want to get rid of all items in the list that fail the inequality, you can use filter
filter [ ? < 0 ] my-list
map and filter are very powerful reporters, but they can take a little while to master. But, if you need to do something fancy with lists, chances are you can do it with map, filter, or (in more extreme cases) reduce.